src/Controller/ClientController.php line 1179

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Billing;
  4. use App\Entity\Client;
  5. use App\Entity\Company;
  6. use App\Entity\Contract;
  7. use App\Entity\ContractProduct;
  8. use App\Entity\IntervenantSalon;
  9. use App\Entity\Product;
  10. use App\Entity\RDV;
  11. use App\Entity\RDVProduct;
  12. use App\Entity\Recurrence;
  13. use App\Entity\RecurrenceProduct;
  14. use App\Entity\Salon;
  15. use App\Entity\SessionSalon;
  16. use App\Entity\Site;
  17. use App\Form\Billing\FilterType;
  18. use App\Form\ClientType;
  19. use App\Form\Reglement\FilterType as ReglementFilterType;
  20. use App\Form\RelanceFactureType;
  21. use App\Manager\ClientManager;
  22. use App\Repository\BillingInfosRepository;
  23. use App\Repository\BillingRepository;
  24. use App\Repository\ClientRepository;
  25. use App\Repository\CompanyRepository;
  26. use App\Repository\FamilleRepository;
  27. use App\Repository\ProductRepository;
  28. use App\Repository\ProductTypeRepository;
  29. use App\Repository\RDVRepository;
  30. use App\Repository\RecurrenceProductRepository;
  31. use App\Repository\RecurrenceRepository;
  32. use App\Repository\ReglementRepository;
  33. use App\Repository\SessionSalonRepository;
  34. use App\Repository\SiteRepository;
  35. use App\Repository\SousFamilleRepository;
  36. use App\Service\FileUploader;
  37. use App\Service\MailService;
  38. use App\Service\PaytweakService;
  39. use App\Traits\Autowired\Vendor\EntityManagerTrait;
  40. use App\Traits\BillingManagerTrait;
  41. use App\Traits\MailServiceTrait;
  42. use App\Traits\PdfServiceTrait;
  43. use App\Traits\RDVManagerTrait;
  44. use Doctrine\Common\Collections\ArrayCollection;
  45. use Doctrine\Common\Collections\Criteria;
  46. use Doctrine\ORM\EntityManagerInterface;
  47. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  48. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  49. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  50. use Symfony\Component\Form\FormInterface;
  51. use Symfony\Component\HttpFoundation\JsonResponse;
  52. use Symfony\Component\HttpFoundation\Request;
  53. use Symfony\Component\HttpFoundation\Response;
  54. use Symfony\Component\HttpFoundation\StreamedResponse;
  55. use Symfony\Component\Routing\Annotation\Route;
  56. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  57. use Symfony\Component\Serializer\SerializerInterface;
  58. use Yectep\PhpSpreadsheetBundle\Factory;
  59. /**
  60.  * @Route("/client")
  61.  */
  62. class ClientController extends AbstractController
  63. {
  64.     use PdfServiceTrait;
  65.     use MailServiceTrait;
  66.     use EntityManagerTrait;
  67.     use BillingManagerTrait;
  68.     use RDVManagerTrait;
  69.     /** @var ProductRepository */
  70.     private $productRepository;
  71.     /**
  72.      * @Route("/", name="client_index", methods={"GET"})
  73.      */
  74.     public function index(Request $request,
  75.                           ClientRepository $clientRepository,
  76.                           SiteRepository $siteRepository
  77.     ): Response
  78.     {
  79.         $q =         $request->query->get('q');
  80.         $site =         $request->query->get('site');
  81.         $tel =         $request->query->get('tel');
  82.         $email =         $request->query->get('email');
  83.         $emailNotaire =         $request->query->get('email-notaire');
  84.         $emailContact =         $request->query->get('email-contact');
  85.         $nom $request->query->get('nom');
  86.         $sites $siteRepository->findBy(['status' => 'actif']);
  87.         $status =         $request->query->get('status');
  88.         return $this->render('client/index.html.twig', [
  89.             'nom' => $nom,
  90.             'emailNotaire' => $emailNotaire,
  91.             'status' => $status,
  92.             'emailContact' => $emailContact,
  93.             'email' => $email,
  94.             'q' => $q,
  95.             'tel' => $tel,
  96.             'sites' => $sites,
  97.             'clients' => $clientRepository->search($q$site$email$emailContact$emailNotaire$nom$tel$status),
  98.         ]);
  99.     }
  100.     /**
  101.      * @Route("/sendBilling/{client}", name="client_send_billing")
  102.      */
  103.     public function sendBilling(
  104.         Request $request,
  105.         Client  $client,
  106.         BillingRepository  $billingRepository,
  107.         PaytweakService $paytweakService
  108.     ) {
  109.         $status $request->request->get('status');
  110.         $billings $billingRepository->searchForLot(nullnullnullnull$statusnull$client);
  111.         foreach ($billings as $billing) {
  112.             /** @var Client $client */
  113.             $client $billing->getClient();
  114.             $company $client->getBillingInfos() ? $client->getBillingInfos()->getCompany() : null;
  115.             $email $billing->getClient()->getBillingInfos() ? $billing->getClient()->getBillingInfos()->getEmail() : null;
  116.             if ($email && $company) {
  117.                 $senderAdress = new \Symfony\Component\Mime\Address('comptabilite@silverbeaute.com''Comptabilité Silver Beauté');
  118.                 $html $this->renderView('billing/pdf.html.twig', [
  119.                     'billings' => [$billing],
  120.                     'company'  => $billing->getCompany()
  121.                 ]);
  122.                 $this->pdfService->setTimeout(120);
  123.                 $this->pdfService->setOption('enable-local-file-access'true);
  124.                 $pdf $this->pdfService->getOutputFromHtml($html);
  125.                 //$call  = $paytweakService->reSend($company, $billing->getCode(), $email);
  126.                 $res  $paytweakService->getLink($client->getBillingInfos()->getCompany(), $billing->getCode());
  127.                 if ($res['code'] === PaytweakService::CODE_OK) {
  128.                     $link reset($res);
  129.                     if ($link["paid"] == "1") {
  130.                         continue;
  131.                     }
  132.                     $this->mailService->sentRelance($senderAdress$email$company$pdf$billing$link["link_url"]);
  133.                 } elseif ($res['code'] === PaytweakService::CODE_NOT_FOUND) {
  134.                     $invoice $paytweakService->createInvoiceFromClient($client$billing);
  135.                     $call  $paytweakService->createBilling($invoice$company);
  136.                     if ($call['code'] == 'OK') {
  137.                         $billing->setStatus(Billing::STATUS_SENT);
  138.                         $billing->setSendingDate(new \DateTime());
  139.                         $this->em->persist($billing);
  140.                     } else {
  141.                         $this->mailService->sentErrorPaytweak($billing$call['code'], $call['message']);
  142.                     }
  143.                 } else {
  144.                     //$mailService->sentErrorPaytweak($billing, $call['code'], $call['message']);
  145.                 }
  146.             }
  147.         }
  148.         $this->em->flush();
  149.         $this->addFlash('success''Renvoi par payweak effectué');
  150.         return $this->redirectToRoute('client_edit', ['id' =>  $client->getId()]);
  151.     }
  152.     /**
  153.      * @Route("/new", name="client_new", methods={"GET","POST"})
  154.      */
  155.     public function new(Request $requestBillingInfosRepository $billingRepositoryClientManager $clientManager): Response
  156.     {
  157.         $entityManager $this->getDoctrine()->getManager();
  158.         $client = new Client();
  159.         $form $this->createForm(ClientType::class, $client, ['defaultCompany' => $entityManager->getRepository(Company::class)->find(2)]);
  160.         $form->handleRequest($request);
  161.         //dump($form->getErrors());die;
  162.         if ($form->isSubmitted() && $form->isValid()) {
  163.             $lastname $client->getLastname();
  164.             $mail $client->getEmailTuteur();
  165.             if ($mail) {
  166.                 $client->getBillingInfos()->setEmail($mail);
  167.             }
  168.             //$client->getBillingInfos()->setDiscount(0);
  169.             $client->setCreator($this->getUser());
  170.             $client->setMaj($this->getUser());
  171.             if (!$client->getBillingInfos()->getPhone()) {
  172.                 $client->getBillingInfos()->setPhone($client->getNumeroTuteur());
  173.             }
  174.             $client->setIdentifier(strtoupper(substr($client->getLastname(), 03) . $clientManager->getLastId()));
  175.             $entityManager->persist($client);
  176.             //$this->generateRecurrence($form);
  177.             $entityManager->flush();
  178.             if ($client->getBillingInfos() && !$client->getBillingInfos()->getAccount()) {
  179.                 if ($billingRepository->findOneBy(['account' => '411' substr($lastname0)])) {
  180.                     $client->getBillingInfos()->setAccount('411' substr($lastname0) . $client->getId());
  181.                 } else {
  182.                     $client->getBillingInfos()->setAccount('411' substr($lastname0));
  183.                 }
  184.             }
  185.             $entityManager->persist($client);
  186.             $entityManager->flush();
  187.             return $this->redirectToRoute('client_edit', ['id' =>  $client->getId()]);
  188.         }
  189.         return $this->render('client/new.html.twig', [
  190.             'client' => $client,
  191.             'form' => $form->createView(),
  192.         ]);
  193.     }
  194.     /**
  195.      * @Route("/import", name="client_import")
  196.      * @Template()
  197.      */
  198.     public function import(
  199.         Request $request,
  200.         SiteRepository $siteRepository,
  201.         CompanyRepository $companyRepository,
  202.         BillingInfosRepository  $billingInfosRepository
  203.     ) {
  204.         $sites $siteRepository->findBy(['status' => Site::STATUS_ACTIF]);
  205.         $companies $companyRepository->findBy([]);
  206.         $path $this->getParameter('client_import_directory');
  207.         $order = [
  208.             'A' => 'setIdentifier',
  209.             'B' => 'setFirstname',
  210.             'C' => 'setLastname',
  211.             'D' => 'setBirthday',
  212.             'E' => 'setDeathday',
  213.             'F' => 'setCivilite'// setGender is for contact infos
  214.             'G' => 'setEmail',
  215.             'H' => 'setPhone',
  216.             'I' => 'setComments',
  217.             'J' => 'setRoomNumber',
  218.             'K' => 'setBuildingRoom',
  219.             'L' => 'setStairs',
  220.             'M' => 'setGender',
  221.             'N' => 'setNomTuteur',
  222.             'O' => 'setPrenomTuteur',
  223.             'P' => 'setEmailTuteur',
  224.             'Q' => 'setEmailNotaire',
  225.             'R' => 'setNumeroTuteur',
  226.             'S' => 'setTypeTuteur',
  227.             'T' => 'setCommentTuteur',
  228.             'U' => 'setNumClient',
  229.             'V' => 'setAddressTuteur',
  230.             'W' => 'setAddressTuteur2',
  231.             'X' => 'setCity',
  232.             'Y' => 'setZipcode',
  233.             'AJ' => 'setProfilClient',
  234.             'AK' => 'setStatusProspect',
  235.             'AL' => 'setSuiviClient',
  236.         ];
  237.         if ($request->isMethod(Request::METHOD_POST)) {
  238.             $files $request->request->get('files');
  239.             $siteId $request->request->get('site');
  240.             $companyId $request->request->get('company');
  241.             $site $siteRepository->find($siteId);
  242.             $company $companyRepository->find($companyId);
  243.             $em $this->getDoctrine()->getManager();
  244.             if ($files) {
  245.                 foreach ($files as $file) {
  246.                     $path_file $path '/' $file;
  247.                     if (file_exists($path_file)) {
  248.                         try {
  249.                             /**  Identify the type of $inputFileName  **/
  250.                             $inputFileType \PhpOffice\PhpSpreadsheet\IOFactory::identify($path_file);
  251.                             /**  Create a new Reader of the type defined in $inputFileType  **/
  252.                             $reader \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
  253.                             /**  Advise the Reader that we only want to load cell data  **/
  254.                             $reader->setReadDataOnly(true);
  255.                             $spreadsheet $reader->load($path_file);
  256.                             $worksheet =  $spreadsheet->getActiveSheet();
  257.                             //$lines = $spreadsheet->getActiveSheet()->toArray();
  258.                             foreach ($worksheet->getRowIterator() as $key => $row) {
  259.                                 if ($key === 1) {
  260.                                     continue;
  261.                                 }
  262.                                 $cellIterator $row->getCellIterator();
  263.                                 $cellIterator->setIterateOnlyExistingCells(false); // This loops through all cells,
  264.                                 //    even if a cell value is not set.
  265.                                 // By default, only cells that have a value
  266.                                 //    set will be iterated.
  267.                                 $client = new Client();
  268.                                 $billingInfos = new Client\BillingInfos();
  269.                                 if ($company) {
  270.                                     $billingInfos->setCompany($company);
  271.                                 }
  272.                                 $em->persist($billingInfos);
  273.                                 $client->setBillingInfos($billingInfos);
  274.                                 if ($site) {
  275.                                     $client->setSite($site);
  276.                                 }
  277.                                 $clientExists false;
  278.                                 $lastname '';
  279.                                 foreach ($cellIterator as $key2 => $cell) {
  280.                                     if (array_key_exists($key2$order)) {
  281.                                         $setter $order[$key2];
  282.                                         if ($setter) {
  283.                                             $value $cell->getValue();
  284.                                             if (!isset($value)) {
  285.                                                 continue;
  286.                                             }
  287. //                                            if($setter === 'email'){
  288. //
  289. //                                                $existingClient = $em->getRepository(Client::class)->findOneBy(['email' => $value]);
  290. //                                                if ($existingClient) {
  291. //                                                    $clientExists = true;
  292. //                                                }
  293. //                                            }
  294.                                             call_user_func_array([$client$setter], [$value]);
  295. //                                            if(!$clientExists){
  296. //                                                $em->persist($client);
  297. //                                            }
  298.                                         }
  299.                                     }
  300.                                     else {
  301.                                         $cellValue $cell->getValue();
  302.                                         if (!isset($cellValue)) {
  303.                                             continue;
  304.                                         }
  305.                                         switch ($key2) {
  306.                                             case 'Z': {
  307.                                                     $billingInfos->setEmail($cellValue);
  308.                                                     break;
  309.                                                 }
  310.                                             case 'AA': {
  311.                                                     $billingInfos->setPhone($cellValue);
  312.                                                     break;
  313.                                                 }
  314.                                             case 'AB': {
  315.                                                     $billingInfos->setDiscount($cellValue);
  316.                                                     break;
  317.                                                 }
  318.                                             case 'AC': {
  319.                                                     $sendMode Client\BillingInfos::SEND_MODE_EMAIL;
  320.                                                     if ($cellValue == 'Courrier') {
  321.                                                         $sendMode Client\BillingInfos::SEND_MODE_COURRIER;
  322.                                                     }
  323.                                                     $billingInfos->setSendMode($sendMode);
  324.                                                     break;
  325.                                                 }
  326.                                             case 'AD': {
  327.                                                     $payMode Client\BillingInfos::PAY_MODE_CB;
  328.                                                     if ($cellValue == 'CB') {
  329.                                                         $payMode Client\BillingInfos::PAY_MODE_CB;
  330.                                                     } elseif ($cellValue == 'SEPA') {
  331.                                                         $payMode Client\BillingInfos::PAY_MODE_SEPA;
  332.                                                     } elseif ($cellValue == 'CHEQUES') {
  333.                                                         $payMode Client\BillingInfos::PAY_MODE_CHEQUES;
  334.                                                     } elseif ($cellValue == 'VIREMENT') {
  335.                                                         $payMode Client\BillingInfos::PAY_MODE_VIREMENT;
  336.                                                     }
  337.                                                     $billingInfos->setPayMode($payMode);
  338.                                                     break;
  339.                                                 }
  340.                                             case 'AE': {
  341.                                                     $billingInfos->setAccount($cellValue);
  342.                                                     break;
  343.                                                 }
  344.                                             case 'AF': {
  345.                                                     $billingInfos->setBankName($cellValue);
  346.                                                     break;
  347.                                                 }
  348.                                             case 'AG': {
  349.                                                     $billingInfos->setIban($cellValue);
  350.                                                     break;
  351.                                                 }
  352.                                             case 'AH': {
  353.                                                     $billingInfos->setBic($cellValue);
  354.                                                     break;
  355.                                                 }
  356.                                             case 'AI': {
  357.                                                     $billingInfos->setRum($cellValue);
  358.                                                     break;
  359.                                                 }
  360.                                                 //                                            case 'AJ': {
  361.                                                 //                                                $billingInfos->setDateSignatureMandat($cellValue);
  362.                                                 //                                                break;
  363.                                                 //                                            }
  364.                                             default:
  365.                                                 break;
  366.                                         }
  367.                                     }
  368.                                 }
  369.                                 // Check if the client with the same name and gender already exists for the same site
  370.                                 if ($client->getLastname() && $client->getCivilite() && $site) {
  371.                                     $existingClient $em->getRepository(Client::class)->findOneBy([
  372.                                         'lastname' => $client->getLastname(),
  373.                                         'civilite' => $client->getCivilite(),
  374.                                         'site' => $site
  375.                                     ]);
  376.                                     if ($existingClient) {
  377.                                         $clientExists true;
  378.                                     }
  379.                                 }
  380.                                 if (!$clientExists) {
  381.                                     if ($billingInfosRepository->findOneBy(['account' => '411' substr($client->getLastname(), 0)])) {
  382.                                         $client->getBillingInfos()->setAccount('411' substr($client->getLastname(), 0) . $client->getId());
  383.                                     } else {
  384.                                         $client->getBillingInfos()->setAccount('411' substr($client->getLastname(), 0));
  385.                                     }
  386.                                     if(!$client->getLastname()){
  387.                                         continue;
  388.                                     }
  389.                                     $em->persist($client);
  390.                                     $em->flush();
  391.                                     $client->setIdentifier($client->getId() . ' ' $client->getLastname());
  392.                                     $em->persist($client);
  393.                                 }
  394.                             }
  395.                             $em->flush();
  396.                         } catch (\Exception $exception) {
  397.                             dump($exception);
  398.                             die;
  399.                         }
  400.                     }
  401.                 }
  402.                 //
  403.                 $this->addFlash('success''Fichier importé');
  404.             }
  405.         }
  406.         return [
  407.             'compagnies' => $companies,
  408.             'sites' => $sites
  409.         ];
  410.     }
  411.     /**
  412.      * @Route("/import-ajax", name="client_importajax")
  413.      */
  414.     public function importajax(Request $requestFileUploader $fileUploader)
  415.     {
  416.         $path $this->getParameter('client_import_directory');
  417.         $file $request->files->get('file');
  418.         if ($file) {
  419.             try {
  420.                 $resp $fileUploader->upload($request->files->get('file'), $path);
  421.                 return new JsonResponse(['filename' => $resp]);
  422.             } catch (\Exception $e) {
  423.             }
  424.         }
  425.         return new JsonResponse(['error' => '']);
  426.     }
  427.     /**
  428.      * @Route("/import-ajax-contract", name="client_importajaxcontract")
  429.      */
  430.     public function importajaxcontract(Request $requestFileUploader $fileUploader)
  431.     {
  432.         $path $this->getParameter('client_contract_import_directory');
  433.         $file $request->files->get('file');
  434.         if ($file) {
  435.             try {
  436.                 $resp $fileUploader->upload($request->files->get('file'), $path);
  437.                 return new JsonResponse(['filename' => $resp]);
  438.             } catch (\Exception $e) {
  439.                 dd($e);
  440.             }
  441.         }
  442.         return new JsonResponse(['error' => '']);
  443.     }
  444.     /**
  445.      * @Route("/import-contract", name="client_importContract")
  446.      */
  447.     public function importContract(
  448.         Request $request,
  449.         ClientRepository $repository,
  450.         ProductTypeRepository $productTypeRepository,
  451.         ProductRepository $productRepository,
  452.         SousFamilleRepository $sousFamilleRepository,
  453.         FamilleRepository $familleRepository
  454.     ) {
  455.         $path $this->getParameter('client_contract_import_directory');
  456.         $em $this->getDoctrine()->getManager();
  457.         if ($request->isMethod(Request::METHOD_POST)) {
  458.             $files $request->request->get('files');
  459.             foreach ($files as $file) {
  460.                 $path_file $path '/' $file;
  461.                 if (file_exists($path_file)) {
  462.                     try {
  463.                         /**  Identify the type of $inputFileName  **/
  464.                         $inputFileType \PhpOffice\PhpSpreadsheet\IOFactory::identify($path_file);
  465.                         /**  Create a new Reader of the type defined in $inputFileType  **/
  466.                         $reader \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
  467.                         /**  Advise the Reader that we only want to load cell data  **/
  468.                         $reader->setReadDataOnly(true);
  469.                         $spreadsheet $reader->load($path_file);
  470.                         $worksheet $spreadsheet->getActiveSheet();
  471.                         //$lines = $spreadsheet->getActiveSheet()->toArray();
  472.                         foreach ($worksheet->getRowIterator() as $key => $row) {
  473.                             if ($key === 1) {
  474.                                 continue;
  475.                             }
  476.                             $cellIterator $row->getCellIterator();
  477.                             $cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
  478.                             //    even if a cell value is not set.
  479.                             // By default, only cells that have a value
  480.                             //    set will be iterated.
  481.                             /** @var Client|null $client */
  482.                             $client null;
  483.                             /** @var Contract|null $contract */
  484.                             $contract null;
  485.                             $skip true;
  486.                             $productsArrayEntities = [];
  487.                             foreach ($cellIterator as $key2 => $cell) {
  488.                                 $cellValue $cell->getValue();
  489.                                 if (!isset($cellValue)) {
  490.                                     continue;
  491.                                 }
  492.                                 //dump($key2);die;
  493.                                 switch ($key2) {
  494.                                     case 'A': {
  495.                                             $client $repository->findOneBy(['identifier' => $cellValue]);
  496.                                             dd($cellValue);
  497.                                             if ($client) {
  498.                                                 $contract = new Contract();
  499.                                                 $contract->setClient($client);
  500.                                                 $skip false;
  501.                                             }
  502.                                             break;
  503.                                         }
  504.                                     case 'B': {
  505.                                             if ($client) {
  506.                                                 $date \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cellValue);
  507.                                                 $contract->setDateDebutRelation($date->format('d/m/Y'));
  508.                                             }
  509.                                             break;
  510.                                         }
  511.                                     case 'C': {
  512.                                             if ($client) {
  513.                                                 $date \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cellValue);
  514.                                                 $contract->setDateDebutRelation($date->format('d/m/Y'));
  515.                                                 $contract->setDateFinRelation($cellValue);
  516.                                             }
  517.                                             break;
  518.                                         }
  519.                                     case 'D': {
  520.                                             if ($client) {
  521.                                                 $contract->setDateDebut($cellValue);
  522.                                             }
  523.                                             break;
  524.                                         }
  525.                                     case 'E': {
  526.                                             if ($client) {
  527.                                                 $contract->setDateFin($cellValue);
  528.                                             }
  529.                                             break;
  530.                                         }
  531.                                     case 'F': {
  532.                                             if ($client) {
  533.                                                 $contract->setTypePrestation($cellValue);
  534.                                             }
  535.                                             break;
  536.                                         }
  537.                                     case 'G': {
  538.                                             if ($client) {
  539.                                                 if ($cellValue === 'Périodiques') {
  540.                                                     $cellValue Contract::TYPE_RDV_PERIODIQUE;
  541.                                                 }
  542.                                                 if ($cellValue === 'Sur demande famille') {
  543.                                                     $cellValue Contract::TYPE_RDV_FAMILLE;
  544.                                                 }
  545.                                                 $contract->setTypeRDV($cellValue);
  546.                                             }
  547.                                             break;
  548.                                         }
  549.                                     case 'H': {
  550.                                             if ($client) {
  551.                                                 $contract->setLibellePrestation($cellValue);
  552.                                                 if ($client) {
  553.                                                     $product $productRepository->findOneBy(['name' => $cellValue]);
  554.                                                     if ($product) {
  555.                                                         $contractProduct = new ContractProduct();
  556.                                                         $contractProduct->setProduct($product);
  557.                                                         $contractProduct->setContract($contract);
  558.                                                         $em->persist($contractProduct);
  559.                                                         $productsArrayEntities[] = $product;
  560.                                                     }
  561.                                                 }
  562.                                             }
  563.                                             break;
  564.                                         }
  565.                                     case 'I': {
  566.                                             if ($client) {
  567.                                                 $contract->setRecurrence($cellValue);
  568.                                             }
  569.                                             break;
  570.                                         }
  571.                                     default:
  572.                                         break;
  573.                                 }
  574.                             }
  575.                             if (!$skip) {
  576.                                 $em->persist($contract);
  577.                                 $client->addContract($contract);
  578.                                 $em->persist($client);
  579.                                 $em->flush(); // flush contrqcct
  580.                                 if (!$contract->getRecurrenceGenerated() && $contract->getTypeRDV() === Contract::TYPE_RDV_PERIODIQUE) {
  581.                                     $intervenant null;
  582.                                     $salon null;
  583.                                     $start $contract->getDateDebutRelation();
  584.                                     $end $contract->getDateFinRelation();
  585.                                     if (!$start) {
  586.                                         continue;
  587.                                     }
  588.                                     //OK
  589.                                     $dateAdd strtotime($start); // date début contrat
  590.                                     $endTime strtotime($end); //date fin contrat
  591.                                     // si ehpad RDV généré sur 4 ans
  592.                                     if ($contract->getClient()->getSite()->getType() === Site::TYPE_EHPAD) {
  593.                                         $ending strtotime('+4 year'strtotime($start));
  594.                                     } else {
  595.                                         $ending strtotime('+6 month'strtotime($start));
  596.                                     }
  597.                                     // si pas défini par défault 4 ans aprés la date de début
  598.                                     if (!$endTime) {
  599.                                         $endTime strtotime('+4 year'strtotime($start));
  600.                                     }
  601.                                     $client $contract->getClient();
  602.                                     // get tag of the first product
  603.                                     $tag null;
  604.                                     if (isset($productsArrayEntities[0])) {
  605.                                         /** @var Product $product */
  606.                                         $product $productsArrayEntities[0];
  607.                                         $tag $product->getType();
  608.                                     }
  609.                                     $salons $client->getSite()->getFirstSalonsByTag($tag);
  610.                                     if (!$salons->isEmpty() && $salons->first()) {
  611.                                         /** @var Salon $first */
  612.                                         $salon $salons->first();
  613.                                         /** @var IntervenantSalon $intervenantSalon */
  614.                                         $intervenantSalon $salon->getIntervenantSalons()->first();
  615.                                         if ($intervenantSalon) {
  616.                                             $intervenant $intervenantSalon->getIntervenant();
  617.                                         }
  618.                                     }
  619.                                     // generate first one Le début et la fin sont le meme jour soit la date de début
  620.                                     $rdv = new RDV();
  621.                                     $rdv->setClient($client);
  622.                                     $datetime = new \DateTime();
  623.                                     //dump($dateAdd);
  624.                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  625.                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  626.                                     if ($salon) {
  627.                                         $rdv->setSalon($salon);
  628.                                     }
  629.                                     if ($intervenant) {
  630.                                         $rdv->setIntervenant($intervenant);
  631.                                     }
  632.                                     $rdv->setContract($contract);
  633.                                     $em->persist($rdv);
  634.                                     foreach ($productsArrayEntities as $product) {
  635.                                         $rdvProduct $this->createRDVProduct($product$rdv);
  636.                                         $em->persist($rdvProduct);
  637.                                     }
  638.                                     $i 0;
  639.                                     $max 1000;
  640.                                     // TODO : refactor
  641.                                     switch ($contract->getRecurrence()) {
  642.                                         case Contract::RECURRENCE_1_SUR_SEMAINE: {
  643.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  644.                                                     $dateAdd strtotime('+1 week'$dateAdd);
  645.                                                     //dump(date('d/m/y',$dateAdd));
  646.                                                     $rdv = new RDV();
  647.                                                     $rdv->setClient($client);
  648.                                                     $datetime = new \DateTime();
  649.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  650.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  651.                                                     $rdv->setContract($contract);
  652.                                                     $rdv->setPriorite(1);
  653.                                                     if ($intervenant) {
  654.                                                         $rdv->setIntervenant($intervenant);
  655.                                                     }
  656.                                                     if ($salon) {
  657.                                                         $rdv->setSalon($salon);
  658.                                                     }
  659.                                                     $em->persist($rdv);
  660.                                                     foreach ($productsArrayEntities as $product) {
  661.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  662.                                                         $em->persist($rdvProduct);
  663.                                                     }
  664.                                                     $i++;
  665.                                                 }
  666.                                                 break;
  667.                                             }
  668.                                         case Contract::RECURRENCE_1_SUR_2: {
  669.                                                 // compare timestamp
  670.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  671.                                                     $dateAdd strtotime('+2 week'$dateAdd);
  672.                                                     //dump(date('d/m/y',$dateAdd));
  673.                                                     $rdv = new RDV();
  674.                                                     $rdv->setClient($client);
  675.                                                     $datetime = new \DateTime();
  676.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  677.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  678.                                                     $rdv->setContract($contract);
  679.                                                     $rdv->setPriorite(1);
  680.                                                     if ($intervenant) {
  681.                                                         $rdv->setIntervenant($intervenant);
  682.                                                     }
  683.                                                     if ($salon) {
  684.                                                         $rdv->setSalon($salon);
  685.                                                     }
  686.                                                     $em->persist($rdv);
  687.                                                     foreach ($productsArrayEntities as $product) {
  688.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  689.                                                         $em->persist($rdvProduct);
  690.                                                     }
  691.                                                     $i++;
  692.                                                 }
  693.                                                 //dump($i); // max 13 6 month
  694.                                                 break;
  695.                                             }
  696.                                         case Contract::RECURRENCE_1_SUR_3: {
  697.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  698.                                                     $dateAdd strtotime('+3 week'$dateAdd);
  699.                                                     //dump(date('d/m/y',$dateAdd));
  700.                                                     $rdv = new RDV();
  701.                                                     $rdv->setClient($client);
  702.                                                     $datetime = new \DateTime();
  703.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  704.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  705.                                                     $rdv->setContract($contract);
  706.                                                     $rdv->setPriorite(2);
  707.                                                     if ($intervenant) {
  708.                                                         $rdv->setIntervenant($intervenant);
  709.                                                     }
  710.                                                     if ($salon) {
  711.                                                         $rdv->setSalon($salon);
  712.                                                     }
  713.                                                     $em->persist($rdv);
  714.                                                     foreach ($productsArrayEntities as $product) {
  715.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  716.                                                         $em->persist($rdvProduct);
  717.                                                     }
  718.                                                     $i++;
  719.                                                 }
  720.                                                 break;
  721.                                             }
  722.                                         case Contract::RECURRENCE_1_SUR_4: {
  723.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  724.                                                     $dateAdd strtotime('+4 week'$dateAdd);
  725.                                                     //dump(date('d/m/y',$dateAdd));
  726.                                                     $rdv = new RDV();
  727.                                                     $rdv->setClient($client);
  728.                                                     $datetime = new \DateTime();
  729.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  730.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  731.                                                     $rdv->setContract($contract);
  732.                                                     $rdv->setPriorite(2);
  733.                                                     if ($intervenant) {
  734.                                                         $rdv->setIntervenant($intervenant);
  735.                                                     }
  736.                                                     if ($salon) {
  737.                                                         $rdv->setSalon($salon);
  738.                                                     }
  739.                                                     $em->persist($rdv);
  740.                                                     foreach ($productsArrayEntities as $product) {
  741.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  742.                                                         $em->persist($rdvProduct);
  743.                                                     }
  744.                                                     $i++;
  745.                                                 }
  746.                                                 break;
  747.                                             }
  748.                                         case Contract::RECURRENCE_1_SUR_8: {
  749.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  750.                                                     $dateAdd strtotime('+5 week'$dateAdd);
  751.                                                     //dump(date('d/m/y',$dateAdd));
  752.                                                     $rdv = new RDV();
  753.                                                     $rdv->setClient($client);
  754.                                                     $datetime = new \DateTime();
  755.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  756.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  757.                                                     $rdv->setContract($contract);
  758.                                                     $rdv->setPriorite(2);
  759.                                                     if ($intervenant) {
  760.                                                         $rdv->setIntervenant($intervenant);
  761.                                                     }
  762.                                                     if ($salon) {
  763.                                                         $rdv->setSalon($salon);
  764.                                                     }
  765.                                                     $em->persist($rdv);
  766.                                                     foreach ($productsArrayEntities as $product) {
  767.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  768.                                                         $em->persist($rdvProduct);
  769.                                                     }
  770.                                                     $i++;
  771.                                                 }
  772.                                                 break;
  773.                                             }
  774.                                         case Contract::RECURRENCE_1_SUR_12: {
  775.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  776.                                                     $dateAdd strtotime('+6 week'$dateAdd);
  777.                                                     //dump(date('d/m/y',$dateAdd));
  778.                                                     $rdv = new RDV();
  779.                                                     $rdv->setClient($client);
  780.                                                     $datetime = new \DateTime();
  781.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  782.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  783.                                                     $rdv->setContract($contract);
  784.                                                     $rdv->setPriorite(2);
  785.                                                     if ($intervenant) {
  786.                                                         $rdv->setIntervenant($intervenant);
  787.                                                     }
  788.                                                     if ($salon) {
  789.                                                         $rdv->setSalon($salon);
  790.                                                     }
  791.                                                     $em->persist($rdv);
  792.                                                     foreach ($productsArrayEntities as $product) {
  793.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  794.                                                         $em->persist($rdvProduct);
  795.                                                     }
  796.                                                     $i++;
  797.                                                 }
  798.                                                 break;
  799.                                             }
  800.                                         case Contract::RECURRENCE_1_SUR_5: {
  801.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  802.                                                     $dateAdd strtotime('+5 week'$dateAdd);
  803.                                                     //dump(date('d/m/y',$dateAdd));
  804.                                                     $rdv = new RDV();
  805.                                                     $rdv->setClient($client);
  806.                                                     $datetime = new \DateTime();
  807.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  808.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  809.                                                     $rdv->setContract($contract);
  810.                                                     $rdv->setPriorite(2);
  811.                                                     if ($intervenant) {
  812.                                                         $rdv->setIntervenant($intervenant);
  813.                                                     }
  814.                                                     if ($salon) {
  815.                                                         $rdv->setSalon($salon);
  816.                                                     }
  817.                                                     $em->persist($rdv);
  818.                                                     foreach ($productsArrayEntities as $product) {
  819.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  820.                                                         $em->persist($rdvProduct);
  821.                                                     }
  822.                                                     $i++;
  823.                                                 }
  824.                                                 break;
  825.                                             }
  826.                                         case Contract::RECURRENCE_1_SUR_6: {
  827.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  828.                                                     $dateAdd strtotime('+6 week'$dateAdd);
  829.                                                     //dump(date('d/m/y',$dateAdd));
  830.                                                     $rdv = new RDV();
  831.                                                     $rdv->setClient($client);
  832.                                                     $datetime = new \DateTime();
  833.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  834.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  835.                                                     $rdv->setContract($contract);
  836.                                                     $rdv->setPriorite(2);
  837.                                                     if ($intervenant) {
  838.                                                         $rdv->setIntervenant($intervenant);
  839.                                                     }
  840.                                                     if ($salon) {
  841.                                                         $rdv->setSalon($salon);
  842.                                                     }
  843.                                                     $em->persist($rdv);
  844.                                                     foreach ($productsArrayEntities as $product) {
  845.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  846.                                                         $em->persist($rdvProduct);
  847.                                                     }
  848.                                                     $i++;
  849.                                                 }
  850.                                                 break;
  851.                                             }
  852.                                         case Contract::RECURRENCE_1_SUR_2_MONTH: {
  853.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  854.                                                     $dateAdd strtotime('+9 week'$dateAdd);
  855.                                                     //dump(date('d/m/y',$dateAdd));
  856.                                                     $rdv = new RDV();
  857.                                                     $rdv->setClient($client);
  858.                                                     $datetime = new \DateTime();
  859.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  860.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  861.                                                     $rdv->setContract($contract);
  862.                                                     $rdv->setPriorite(2);
  863.                                                     if ($intervenant) {
  864.                                                         $rdv->setIntervenant($intervenant);
  865.                                                     }
  866.                                                     if ($salon) {
  867.                                                         $rdv->setSalon($salon);
  868.                                                     }
  869.                                                     $em->persist($rdv);
  870.                                                     foreach ($productsArrayEntities as $product) {
  871.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  872.                                                         $em->persist($rdvProduct);
  873.                                                     }
  874.                                                     $i++;
  875.                                                 }
  876.                                                 break;
  877.                                             }
  878.                                         case Contract::RECURRENCE_1_SUR_TRIM: {
  879.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  880.                                                     $dateAdd strtotime('+13 week'$dateAdd);
  881.                                                     //dump(date('d/m/y',$dateAdd));
  882.                                                     $rdv = new RDV();
  883.                                                     $rdv->setClient($client);
  884.                                                     $datetime = new \DateTime();
  885.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  886.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  887.                                                     $rdv->setContract($contract);
  888.                                                     $rdv->setPriorite(2);
  889.                                                     if ($intervenant) {
  890.                                                         $rdv->setIntervenant($intervenant);
  891.                                                     }
  892.                                                     if ($salon) {
  893.                                                         $rdv->setSalon($salon);
  894.                                                     }
  895.                                                     $em->persist($rdv);
  896.                                                     foreach ($productsArrayEntities as $product) {
  897.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  898.                                                         $em->persist($rdvProduct);
  899.                                                     }
  900.                                                     $i++;
  901.                                                 }
  902.                                                 break;
  903.                                             }
  904.                                         case Contract::RECURRENCE_1_SUR_6_MONTH: {
  905.                                                 while ($i $max && $dateAdd $ending && $dateAdd $endTime) {
  906.                                                     $dateAdd strtotime('+26 week'$dateAdd);
  907.                                                     //dump(date('d/m/y',$dateAdd));
  908.                                                     $rdv = new RDV();
  909.                                                     $rdv->setClient($client);
  910.                                                     $datetime = new \DateTime();
  911.                                                     $rdv->setStart($datetime->setTimestamp($dateAdd));
  912.                                                     $rdv->setEnd($datetime->setTimestamp($dateAdd));
  913.                                                     $rdv->setContract($contract);
  914.                                                     $rdv->setPriorite(2);
  915.                                                     if ($intervenant) {
  916.                                                         $rdv->setIntervenant($intervenant);
  917.                                                     }
  918.                                                     if ($salon) {
  919.                                                         $rdv->setSalon($salon);
  920.                                                     }
  921.                                                     $em->persist($rdv);
  922.                                                     foreach ($productsArrayEntities as $product) {
  923.                                                         $rdvProduct $this->createRDVProduct($product$rdv);
  924.                                                         $em->persist($rdvProduct);
  925.                                                     }
  926.                                                     $i++;
  927.                                                 }
  928.                                                 break;
  929.                                             }
  930.                                     }
  931.                                     $contract->setRecurrenceGenerated(true);
  932.                                 }
  933.                                 $em->flush();
  934.                             }
  935.                         }
  936.                     } catch (\Exception $exception) {
  937.                         $this->addFlash('error''Erreur lors de limport'$exception->getMessage());
  938.                     }
  939.                 }
  940.             }
  941.         }
  942.         return $this->render('client/importContract.html.twig', []);
  943.     }
  944.     /**
  945.      * @Route("/downloadExcel")
  946.      */
  947.     public function downloadExcel(ClientRepository $repositoryRDVRepository  $RDVRepository)
  948.     {
  949.         $entities =  new ArrayCollection($repository->findAll());
  950.         $response = new StreamedResponse();
  951.         $columns $this->getColumnsForEntity(Client::class, $RDVRepository);
  952.         $response->setCallback(function () use ($entities$columns) {
  953.             $handle fopen('php://output''w+');
  954.             // Add header
  955.             fputcsv($handlearray_keys($columns));
  956.             while ($entity $entities->current()) {
  957.                 $values = [];
  958.                 foreach ($columns as $column => $callback) {
  959.                     $value $callback;
  960.                     if (is_callable($callback)) {
  961.                         $value $callback($entity);
  962.                     }
  963.                     $values[] = $value;
  964.                 }
  965.                 fputcsv($handle$values);
  966.                 $entities->next();
  967.             }
  968.             fclose($handle);
  969.         });
  970.         $filename 'client_export.csv';
  971.         $response->headers->set('Content-Type''text/csv; charset=utf-8');
  972.         $response->headers->set('Content-Disposition''attachment; filename="' $filename '"');
  973.         return $response;
  974.     }
  975.     /**
  976.      * @Route("/downloadClient")
  977.      */
  978.     public function downloadClient(Request $requestClientRepository $repository)
  979.     {
  980.         $statuses $request->request->get('statusProspect', []);
  981.         $site $request->request->get('site');
  982.         $filters = ['statusProspect' => $statuses];
  983.         if (!empty($site)) {
  984.             $filters['site'] = $site;
  985.         }
  986.         $entities =  new ArrayCollection($repository->downloadClient($statuses$site));
  987.         $response = new StreamedResponse();
  988.         $columns $this->getColumnsForEntity(Client::class);
  989.         $response->setCallback(function () use ($entities$columns) {
  990.             $handle fopen('php://output''w+');
  991.             // Add header
  992.             fputcsv($handlearray_keys($columns));
  993.             while ($entity $entities->current()) {
  994.                 $values = [];
  995.                 foreach ($columns as $column => $callback) {
  996.                     $value $callback;
  997.                     if (is_callable($callback)) {
  998.                         $value $callback($entity);
  999.                     }
  1000.                     $values[] = $value;
  1001.                 }
  1002.                 fputcsv($handle$values);
  1003.                 $entities->next();
  1004.             }
  1005.             fclose($handle);
  1006.         });
  1007.         $filename 'client_export.csv';
  1008.         $response->headers->set('Content-Type''text/csv; charset=utf-8');
  1009.         $response->headers->set('Content-Disposition''attachment; filename="' $filename '"');
  1010.         return $response;
  1011.     }
  1012.     /**
  1013.      * @Route("/{id}", name="client_show", methods={"GET"})
  1014.      */
  1015.     public function show(Client $client): Response
  1016.     {
  1017.         return $this->render('client/show.html.twig', [
  1018.             'client' => $client,
  1019.         ]);
  1020.     }
  1021.     /**
  1022.      * @Route("/{id}/edit", name="client_edit", methods={"GET","POST","PUT"})
  1023.      */
  1024.     public function edit(
  1025.         Request $request,
  1026.         Client $client,
  1027.         PaytweakService $paytweakService,
  1028.         BillingRepository $billingRepository,
  1029.         ReglementRepository $reglementRepository
  1030.     ): Response
  1031.     {
  1032.         $entityManager $this->getDoctrine()->getManager();
  1033.         $reglements $reglementRepository->findBy(['client' => $client], ['id' => 'DESC'], 12);
  1034.         $filterFormReglement $this->createForm(ReglementFilterType::class, [], ['client_form' => false]);
  1035.         $filterFormReglement->handleRequest($request);
  1036.         $isReg false;
  1037.         if ($filterFormReglement->isSubmitted() && $filterFormReglement->isValid()) {
  1038.             $status $filterFormReglement['statut']->getData();
  1039.             $mode $filterFormReglement['mode']->getData();
  1040.             $date $filterFormReglement['date']->getData();
  1041.             $dateEnd $filterFormReglement['dateEnd']->getData();
  1042.             //global list
  1043.             $reglements $reglementRepository->filter($mode$client$date$dateEnd, [], [], $statustrue);
  1044.             $isReg true;
  1045.         }
  1046.         $isBillingFilter false;
  1047.         $allBillings $billingRepository->filter(null$client,nullnull,null,nullnulltrue12);
  1048.         $filterForm $this->createForm(FilterType::class, null, ['client_form' => false]);
  1049.         $filterForm->handleRequest($request);
  1050.         if ($filterForm->isSubmitted() && $filterForm->isValid()) {
  1051.             $status $filterForm['status']->getData();
  1052.             $date $filterForm['dateDebut']->getData();
  1053.             $dateEnd $filterForm['dateFin']->getData();
  1054.             $allBillings $billingRepository->filter(null$client$date$dateEnd$statusnull,null,true);
  1055.             $isBillingFilter true;
  1056.         }
  1057.         $originalFamilyMembers = new ArrayCollection();
  1058.         $originalContracts = new ArrayCollection();
  1059.         // Create an ArrayCollection of the current Tag objects in the database
  1060.         foreach ($client->getFamilyMembers() as $familyMember) {
  1061.             $originalFamilyMembers->add($familyMember);
  1062.         }
  1063.         foreach ($client->getContract() as $contract) {
  1064.             $originalContracts->add($contract);
  1065.         }
  1066.         $form $this->createForm(ClientType::class, $client, [
  1067.             'show_sepa' => $client->getBillingInfos()->getPayMode() === Client\BillingInfos::PAY_MODE_SEPA,
  1068.         ]);
  1069.         $formRelance $this->createForm(RelanceFactureType::class);
  1070.         $form->handleRequest($request);
  1071.         $formRelance->handleRequest($request);
  1072.         if ($formRelance->isSubmitted() && $formRelance->isValid()) {
  1073.             $status $formRelance->get('status')->getData();
  1074.             $start $formRelance->get('start')->getData();
  1075.             $end $formRelance->get('end')->getData();
  1076.             /** @var BillingRepository  $billingRepo */
  1077.             $billingRepo $this->billingManager->getRepository();
  1078.             $startPass \DateTime::createFromFormat('d-m-Y'$start );
  1079.             $endPass \DateTime::createFromFormat('d-m-Y'$end );
  1080.             $billings $billingRepo->searchForRelanceByClient($client$status$startPass$endPass);
  1081.             /** @var Billing $billing */
  1082.             foreach($billings as $billing) {
  1083.                 /** @var Client $client */
  1084.                 $client $billing->getClient();
  1085.                 $payMode $client->getBillingInfos()->getPayMode();
  1086.                 $sendMode $client->getBillingInfos()->getSendMode();
  1087.                 if (
  1088.                     (
  1089.                         (
  1090.                             $sendMode === Client\BillingInfos::SEND_MODE_EMAIL &&
  1091.                             ($payMode == Client\BillingInfos::PAY_MODE_CB
  1092.                                 || $payMode == Client\BillingInfos::PAY_MODE_CHEQUES
  1093.                                 || $payMode == Client\BillingInfos::PAY_MODE_VIREMENT)
  1094.                         )
  1095.                         ||
  1096.                         ($sendMode === Client\BillingInfos::SEND_MODE_COURRIER && $payMode == Client\BillingInfos::PAY_MODE_CB  )
  1097.                         || ( $sendMode === Client\BillingInfos::SEND_MODE_EMAIL && $payMode === null)
  1098.                     )
  1099.                 )
  1100.                 {
  1101.                     $email $billing->getClient()->getBillingInfos() ? $billing->getClient()->getBillingInfos()->getEmail(): null;
  1102.                     if($email){
  1103.                         $senderAdress = new \Symfony\Component\Mime\Address'comptabilite@silverbeaute.com''Comptabilité Silver Beauté');
  1104.                         $html $this->renderView('billing/pdf.html.twig',[
  1105.                             'billings' => [$billing],
  1106.                             'company'  => $billing->getCompany()
  1107.                         ]);
  1108.                         $this->pdfService->setTimeout(120);
  1109.                         $this->pdfService->setOption('enable-local-file-access'true);
  1110.                         $pdf $this->pdfService->getOutputFromHtml($html);
  1111.                         //$call  = $paytweakService->reSend($company, $billing->getCode(), $email);
  1112.                         $res  $paytweakService->getLink($billing->getCompany(), $billing->getCode());
  1113.                         if($res['code'] === PaytweakService::CODE_OK) {
  1114.                             $link reset($res);
  1115.                             if($link["paid"] == "1"){
  1116.                                 continue;
  1117.                             }
  1118.                             $this->mailService->sentRelance($senderAdress,$email$billing->getCompany(), $pdf$billing$link["link_url"]);
  1119.                         }
  1120.                         elseif($res['code'] === PaytweakService::CODE_NOT_FOUND){
  1121.                             $invoice $paytweakService->createInvoiceFromClient($client$billing);
  1122.                             $call  $paytweakService->createBilling($invoice$billing->getCompany());
  1123.                             if($call['code'] == 'OK') {
  1124.                                 $billing->setStatus(Billing::STATUS_SENT);
  1125.                                 $billing->setSendingDate(new \DateTime());
  1126.                                 if(isset($call['link_id'] )){
  1127.                                     $billing->setLinkPaytweak($call['link_id']);
  1128.                                 }
  1129.                                 $entityManager->persist($billing);
  1130.                             }
  1131.                             else {
  1132.                                 $this->mailService->sentErrorPaytweak($billing$call['code'], $call['message']);
  1133.                             }
  1134.                         }
  1135.                         else {
  1136.                             //$mailService->sentErrorPaytweak($billing, $call['code'], $call['message']);
  1137.                         }
  1138.                     }
  1139.                 }
  1140.             }
  1141.             $entityManager->flush();
  1142.             return $this->redirectToRoute('client_edit', [
  1143.                 'id' => $client->getId(),
  1144.             ]);
  1145.         }
  1146.         $isContact false;
  1147.         if ($form->isSubmitted() && $form->isValid()) {
  1148.             /** @var Client\FamilyMember $familyMember */
  1149.             foreach ($originalFamilyMembers as $familyMember) {
  1150.                 if (false === $client->getFamilyMembers()->contains($familyMember)) {
  1151.                     // remove the Task from the Tag
  1152.                     $client->getFamilyMembers()->removeElement($client);
  1153.                     $entityManager->remove($familyMember);
  1154.                 }
  1155.             }
  1156.             /** @var Contract $contract */
  1157.             foreach ($originalContracts as $contract) {
  1158.                 if (false === $client->getContract()->contains($contract)) {
  1159.                     /** @var RDV $rdv */
  1160.                     foreach ($contract->getRdvs() as $rdv) {
  1161.                         if ($rdv->getStatus() === RDV::STATUS_PLANIFIER) {
  1162.                             $contract->getRdvs()->removeElement($rdv);
  1163.                             $entityManager->remove($rdv);
  1164.                         }
  1165.                     }
  1166.                     $client->getContract()->removeElement($client);
  1167.                     $entityManager->remove($contract);
  1168.                 }
  1169.             }
  1170.             /** @var Contract $contract */
  1171.             foreach ($client->getContract() as $contract) {
  1172.                 $contract->setClient($client);
  1173.                 $entityManager->persist($contract);
  1174.             }
  1175.             if (!$client->getBillingInfos()->getPhone()) {
  1176.                 $client->getBillingInfos()->setPhone($client->getNumeroTuteur());
  1177.             }
  1178.             $uow $entityManager->getUnitOfWork();
  1179.             $oldStatus $uow->getOriginalEntityData($client)['statusProspect'];
  1180.             if(
  1181.                 ($client->getStatusProspect() === Client::STATUS_INACTIF ||
  1182.                 $client->getStatusProspect() === Client::STATUS_PARTI) &&
  1183.                 ($oldStatus !=  Client::STATUS_INACTIF || $oldStatus !=  Client::STATUS_PARTI)
  1184.             ){
  1185.                 $rdvsTodelete $this->RDVManager->findFromTodayByStatusPlan($client);
  1186.                 foreach ($rdvsTodelete as $rdv) {
  1187.                     $entityManager->remove($rdv);
  1188.                 }
  1189.                 $entityManager->flush();
  1190.                 // Retrieve the recurrence record to delete
  1191.             }
  1192.             $client->setMaj($this->getUser());
  1193.             $entityManager->persist($client);
  1194.             //$this->generateRecurrence($form, true);
  1195.             $entityManager->flush();
  1196.             $isContact true;
  1197.             // return $this->redirectToRoute('client_edit', [
  1198.             //     'id' => $client->getId(),
  1199.             // ]);
  1200.         }
  1201.         $products $this->productRepository->findAll();
  1202.         return $this->render('client/edit.html.twig', [
  1203.             'products' => $products,
  1204.             'client' => $client,
  1205.             'formRelance' => $formRelance->createView(),
  1206.             'form' => $form->createView(),
  1207.             'formFilter' => $filterForm->createView(),
  1208.             'factures' => $allBillings,
  1209.             'isBilling' => $isBillingFilter,
  1210.             'reglements' => $reglements,
  1211.             'formFilterReglement' => $filterFormReglement->createView(),
  1212.             'isReg' => $isReg,
  1213.             'isContact' => $isContact,
  1214.         ]);
  1215.     }
  1216.     /**
  1217.      * @Route("/sendBillingMultiple", name="client_send_billing_multiple", methods={"GET","POST","PUT"})
  1218.      */
  1219.     public function sendBillingMultiple(Request  $requestBillingRepository  $billingRepositoryPaytweakService  $paytweakService) {
  1220.         $data json_decode($request->getContent(), true);
  1221.         $billings $data['factures'];
  1222.         foreach ($billings as $billingItem){
  1223.             $billing $billingRepository->find($billingItem);
  1224.             $client $billing->getClient();
  1225.             $payMode $client->getBillingInfos()->getPayMode();
  1226.             $sendMode $client->getBillingInfos()->getSendMode();
  1227.             if (
  1228.                 (
  1229.                     (
  1230.                         $sendMode === Client\BillingInfos::SEND_MODE_EMAIL &&
  1231.                         ($payMode == Client\BillingInfos::PAY_MODE_CB
  1232.                             || $payMode == Client\BillingInfos::PAY_MODE_CHEQUES
  1233.                             || $payMode == Client\BillingInfos::PAY_MODE_VIREMENT)
  1234.                     )
  1235.                     ||
  1236.                     ($sendMode === Client\BillingInfos::SEND_MODE_COURRIER && $payMode == Client\BillingInfos::PAY_MODE_CB  )
  1237.                     || ( $sendMode === Client\BillingInfos::SEND_MODE_EMAIL && $payMode === null)
  1238.                 )
  1239.             )
  1240.             {
  1241.                 $email $billing->getClient()->getBillingInfos() ? $billing->getClient()->getBillingInfos()->getEmail(): null;
  1242.                 if($email){
  1243.                     $senderAdress = new \Symfony\Component\Mime\Address'comptabilite@silverbeaute.com''Comptabilité Silver Beauté');
  1244.                     $html $this->renderView('billing/pdf.html.twig',[
  1245.                         'billings' => [$billing],
  1246.                         'company'  => $billing->getCompany()
  1247.                     ]);
  1248.                     $this->pdfService->setTimeout(120);
  1249.                     $this->pdfService->setOption('enable-local-file-access'true);
  1250.                     $pdf $this->pdfService->getOutputFromHtml($html);
  1251.                     //$call  = $paytweakService->reSend($company, $billing->getCode(), $email);
  1252.                     $res  $paytweakService->getLink($billing->getCompany(), $billing->getCode());
  1253.                     if($res['code'] === PaytweakService::CODE_OK) {
  1254.                         $link reset($res);
  1255.                         if($link["paid"] == "1"){
  1256.                             continue;
  1257.                         }
  1258.                         $this->mailService->sentRelance($senderAdress,$email$billing->getCompany(), $pdf$billing$link["link_url"]);
  1259.                     }
  1260.                     elseif($res['code'] === PaytweakService::CODE_NOT_FOUND){
  1261.                         $invoice $paytweakService->createInvoiceFromClient($client$billing);
  1262.                         $call  $paytweakService->createBilling($invoice$billing->getCompany());
  1263.                         if($call['code'] == 'OK') {
  1264.                             $billing->setStatus(Billing::STATUS_SENT);
  1265.                             $billing->setSendingDate(new \DateTime());
  1266.                             if(isset($call['link_id'] )){
  1267.                                 $billing->setLinkPaytweak($call['link_id']);
  1268.                             }
  1269.                             $this->em->persist($billing);
  1270.                         }
  1271.                         else {
  1272.                             $this->mailService->sentErrorPaytweak($billing$call['code'], $call['message']);
  1273.                         }
  1274.                     }
  1275.                     else {
  1276.                         //$mailService->sentErrorPaytweak($billing, $call['code'], $call['message']);
  1277.                     }
  1278.                 }
  1279.             }
  1280.         }
  1281.         $this->em->flush();
  1282.         return new JsonResponse('ok');
  1283.     }
  1284.     /**
  1285.      * @Route("/{id}/recurrence", methods={"GET","POST"})
  1286.      */
  1287.     public function recurrence(
  1288.         Client $clientRequest  $request,
  1289.         ProductRepository  $productRepository,
  1290.         RecurrenceRepository  $recurrenceRepository,
  1291.         RecurrenceProductRepository  $recurrenceProductRepository,
  1292.         SessionSalonRepository $sessionSalonRepository
  1293.     ) {
  1294.         $productsAll $this->productRepository->findBy([
  1295.             "indisponibleVente" =>false,
  1296.             "type" => Product::TYPE_COIFFURE
  1297.         ]);
  1298.         $type $request->request->get('type');
  1299.         $recurrence $recurrenceRepository->findOneBy(['client' => $client'type' => $type ?? Product::TYPE_COIFFURE]);
  1300.         $salonCoiffure $client->getSite()->getFirstSalonsByTag(Product::TYPE_COIFFURE)->first();
  1301.         $sessionSalonDates $sessionSalonRepository->findSessionsBySalon($salonCoiffure);
  1302. //dd($sessionSalonDates);
  1303.         $post $request->isMethod(Request::METHOD_POST);
  1304.         if($post){
  1305.             if(!$recurrence){
  1306.                 $recurrence = new Recurrence();
  1307.                 $recurrence->setClient($client);
  1308.             }
  1309.             else {
  1310.                 $currentTime = new \DateTime();
  1311.                 foreach ($recurrence->getRdvs() as $rdv) {
  1312.                     if ($rdv->getStart() > $currentTime && ($rdv->getStatus() === RDV::STATUS_PLANIFIER ||$rdv->getStatus() === RDV::STATUS_REPLANIFIER )) {
  1313.                         $this->em->remove($rdv);
  1314.                     }
  1315.                 }
  1316.             }
  1317.             $nbSemaine $request->request->get('nbSemaine');
  1318.             $JOS $request->request->get('date_debut');
  1319.             $products $request->request->get('products');
  1320.             $startDate \DateTime::createFromFormat('Y-m-d H:i'$JOS);
  1321.             $endDate \DateTime::createFromFormat('Y-m-d H:i'$JOS);
  1322.             $recurrence->setNbSemaine($nbSemaine);
  1323.             $recurrence->setType($type);
  1324.             $date = new \DateTime($JOS);
  1325.             $recurrence->setJos($date);
  1326.             $this->em->persist($recurrence);
  1327.             $endDate->add(new \DateInterval('P1Y'));
  1328.             if($products){
  1329.                 foreach ($products as $key => $product){
  1330.                     $productFind $productRepository->find($product['product']);
  1331.                     $recurrenceProduct $recurrenceProductRepository->findOneBy(['product' => $productFind'recurrence' => $recurrence]);
  1332.                     if(!$recurrenceProduct){
  1333.                         $recurrenceProduct = new RecurrenceProduct();
  1334.                     }
  1335.                     $recurrenceProduct->setFrequency($product['frequency']);
  1336.                     $this->em->persist($recurrenceProduct);
  1337.                     $productFind->addRecurrenceProduct($recurrenceProduct);
  1338.                     $this->em->persist($productFind);
  1339.                     $this->em->persist($recurrenceProduct);
  1340.                     $recurrence->addRecurrenceProduct($recurrenceProduct);
  1341.                     $this->em->persist($recurrence);
  1342.                 }
  1343.             }
  1344.             $i 0;
  1345.             while ($startDate <= $endDate) {
  1346.                 // Create an RDV object with the current date
  1347.                 $rdv = new RDV();
  1348.                 $rdv->setClient($client);
  1349.                 $newStartDate = clone $startDate;
  1350.                 $newEndDate = clone $startDate;
  1351.                 //$startDate->setTime(10, 0, 0);
  1352.                 $rdv->setStart($newStartDate);
  1353.                 $rdv->setEnd($newEndDate);
  1354.                 $salons $client->getSite()->getSalons();
  1355.                 /** @var Salon $salon */
  1356.                 $salonFind null;
  1357.                 foreach ($salons as $salon){
  1358.                     if($type === $salon->getService()) {
  1359.                         $salonFind $salon;
  1360.                         break;
  1361.                     }
  1362.                 }
  1363.                 if($salonFind){
  1364.                     $rdv->setSalon($salonFind);
  1365.                     /** @var IntervenantSalon $intervenant */
  1366.                     $intervenant $salonFind->getIntervenantSalons()->last();
  1367.                     if($intervenant){
  1368.                         $rdv->setIntervenant($intervenant->getIntervenant());
  1369.                     }
  1370.                 }
  1371.                 $rdv->setStatus(RDV::STATUS_PLANIFIER);
  1372.                 $rdv->setNewRecurrence(true);
  1373.                 if($products){
  1374.                     foreach ($products as $key => $product){
  1375.                         $productFind $productRepository->find($product['product']);
  1376.                         if($i === 0){
  1377.                             $rdvProduct = new RDVProduct();
  1378.                             $rdvProduct->setRdv($rdv);
  1379.                             $rdvProduct->setProduct($productFind);
  1380.                             $this->em->persist($rdvProduct);
  1381.                             $rdv->addRdvProduct($rdvProduct);
  1382.                         }
  1383.                         else {
  1384.                             $selectedFrequency $product['frequency'];
  1385.                             if ($selectedFrequency == "every") {
  1386.                                 // Handle the case where you want every RDV
  1387.                                 $rdvProduct = new RDVProduct();
  1388.                                 $rdvProduct->setRdv($rdv);
  1389.                                 $rdvProduct->setProduct($productFind);
  1390.                                 $this->em->persist($rdvProduct);
  1391.                                 $rdv->addRdvProduct($rdvProduct);
  1392.                             }
  1393.                             elseif ($selectedFrequency == "every_other") {
  1394.                                 // Handle the case where you want every 1 of 2 RDV
  1395.                                 if ($i == 0) { // Check if $i is even
  1396.                                     $rdvProduct = new RDVProduct();
  1397.                                     $rdvProduct->setRdv($rdv);
  1398.                                     $rdvProduct->setProduct($productFind);
  1399.                                     $this->em->persist($rdvProduct);
  1400.                                     $rdv->addRdvProduct($rdvProduct);
  1401.                                 }
  1402.                             }
  1403.                             elseif ($selectedFrequency == "every_third") {
  1404.                                 // Handle the case where you want every 1 of 3 RDV
  1405.                                 if ($i == 0) { // Check if $i is divisible by 3
  1406.                                     $rdvProduct = new RDVProduct();
  1407.                                     $rdvProduct->setRdv($rdv);
  1408.                                     $rdvProduct->setProduct($productFind);
  1409.                                     $this->em->persist($rdvProduct);
  1410.                                     $rdv->addRdvProduct($rdvProduct);
  1411.                                 }
  1412.                             }
  1413.                             elseif ($selectedFrequency == "every_fourth") {
  1414.                                 // Handle the case where you want every 1 of 4 RDV
  1415.                                 if ($i == 0) { // Check if $i is divisible by 4
  1416.                                     $rdvProduct = new RDVProduct();
  1417.                                     $rdvProduct->setRdv($rdv);
  1418.                                     $rdvProduct->setProduct($productFind);
  1419.                                     $this->em->persist($rdvProduct);
  1420.                                     $rdv->addRdvProduct($rdvProduct);
  1421.                                 }
  1422.                             }
  1423.                             elseif ($selectedFrequency == "every_fifth") {
  1424.                                 // Handle the case where you want every 1 of 5 RDV
  1425.                                 if ($i == 0) { // Check if $i is divisible by 5
  1426.                                     $rdvProduct = new RDVProduct();
  1427.                                     $rdvProduct->setRdv($rdv);
  1428.                                     $rdvProduct->setProduct($productFind);
  1429.                                     $this->em->persist($rdvProduct);
  1430.                                     $rdv->addRdvProduct($rdvProduct);
  1431.                                 }
  1432.                             }
  1433.                             elseif ($selectedFrequency == "every_sixth") {
  1434.                                 // Handle the case where you want every 1 of 6 RDV
  1435.                                 if ($i == 0) { // Check if $i is divisible by 6
  1436.                                     $rdvProduct = new RDVProduct();
  1437.                                     $rdvProduct->setRdv($rdv);
  1438.                                     $rdvProduct->setProduct($productFind);
  1439.                                     $this->em->persist($rdvProduct);
  1440.                                     $rdv->addRdvProduct($rdvProduct);
  1441.                                 }
  1442.                             }
  1443.                             elseif ($selectedFrequency == "every_seventh") {
  1444.                                 // Handle the case where you want every 1 of 7 RDV
  1445.                                 if ($i == 0) { // Check if $i is divisible by 7
  1446.                                     $rdvProduct = new RDVProduct();
  1447.                                     $rdvProduct->setRdv($rdv);
  1448.                                     $rdvProduct->setProduct($productFind);
  1449.                                     $this->em->persist($rdvProduct);
  1450.                                     $rdv->addRdvProduct($rdvProduct);
  1451.                                 }
  1452.                             }
  1453.                             elseif ($selectedFrequency == "every_eighth") {
  1454.                                 // Handle the case where you want every 1 of 8 RDV
  1455.                                 if ($i == 0) { // Check if $i is divisible by 8
  1456.                                     $rdvProduct = new RDVProduct();
  1457.                                     $rdvProduct->setRdv($rdv);
  1458.                                     $rdvProduct->setProduct($productFind);
  1459.                                     $this->em->persist($rdvProduct);
  1460.                                     $rdv->addRdvProduct($rdvProduct);
  1461.                                 }
  1462.                             }
  1463.                             elseif ($selectedFrequency == "every_ninth") {
  1464.                                 // Handle the case where you want every 1 of 9 RDV
  1465.                                 if ($i == 0) { // Check if $i is divisible by 9
  1466.                                     $rdvProduct = new RDVProduct();
  1467.                                     $rdvProduct->setRdv($rdv);
  1468.                                     $rdvProduct->setProduct($productFind);
  1469.                                     $this->em->persist($rdvProduct);
  1470.                                     $rdv->addRdvProduct($rdvProduct);
  1471.                                 }
  1472.                             } elseif ($selectedFrequency == "every_tenth") {
  1473.                                 // Handle the case where you want every 1 of 10 RDV
  1474.                                 if ($i 10 == 0) { // Check if $i is divisible by 10
  1475.                                     $rdvProduct = new RDVProduct();
  1476.                                     $rdvProduct->setRdv($rdv);
  1477.                                     $rdvProduct->setProduct($productFind);
  1478.                                     $this->em->persist($rdvProduct);
  1479.                                     $rdv->addRdvProduct($rdvProduct);
  1480.                                 }
  1481.                             }
  1482.                             elseif ($selectedFrequency == "every_eleventh") {
  1483.                                 // Handle the case where you want every 1 of 11 RDV
  1484.                                 if ($i 11 == 0) { // Check if $i is divisible by 11
  1485.                                     $rdvProduct = new RDVProduct();
  1486.                                     $rdvProduct->setRdv($rdv);
  1487.                                     $rdvProduct->setProduct($productFind);
  1488.                                     $this->em->persist($rdvProduct);
  1489.                                     $rdv->addRdvProduct($rdvProduct);
  1490.                                 }
  1491.                             }
  1492.                             elseif ($selectedFrequency == "every_twelfth") {
  1493.                                 // Handle the case where you want every 1 of 12 RDV
  1494.                                 if ($i 12 == 0) { // Check if $i is divisible by 12
  1495.                                     $rdvProduct = new RDVProduct();
  1496.                                     $rdvProduct->setRdv($rdv);
  1497.                                     $rdvProduct->setProduct($productFind);
  1498.                                     $this->em->persist($rdvProduct);
  1499.                                     $rdv->addRdvProduct($rdvProduct);
  1500.                                 }
  1501.                             }
  1502.                         }
  1503.                     }
  1504.                 }
  1505.                 $recurrence->addRdv($rdv);
  1506.                 $this->em->persist($recurrence);
  1507.                 $this->em->persist($rdv);
  1508.                 $i++;
  1509.                 // Increment the date by the number of weeks specified
  1510.                 $startDate->add(new \DateInterval('P' $nbSemaine 'W'));
  1511.             }
  1512.             $this->em->flush();
  1513.         }
  1514.         return $this->render('client/recurrence.html.twig', [
  1515.             'client' => $client,
  1516.             'products' => $productsAll,
  1517.             'recurrence' => $recurrence,
  1518.             'sessionSalonDates' => $sessionSalonDates
  1519.         ]);
  1520.     }
  1521.     /**
  1522.      * @Route("/{id}/recurrence/{type}/delete", methods={"POST"})
  1523.      */
  1524.     public function deleteRecurrence(Client $client$type)
  1525.     {
  1526.         $entityManager $this->getDoctrine()->getManager();
  1527.         $recurrenceRepository $this->getDoctrine()->getRepository(Recurrence::class);
  1528.         // Retrieve the recurrence record to delete
  1529.         /** @var Recurrence $recurrence */
  1530.         $recurrence $recurrenceRepository->findOneBy(['client' => $client'type' => $type]);
  1531.         $currentTime = new \DateTime();
  1532.         if ($recurrence) {
  1533.             foreach ($recurrence->getRdvs() as $rdv) {
  1534.                 if ($rdv->getStart() > $currentTime && ($rdv->getStatus() === RDV::STATUS_PLANIFIER ||$rdv->getStatus() === RDV::STATUS_REPLANIFIER )) {
  1535.                     $entityManager->remove($rdv);
  1536.                 }
  1537.                 else {
  1538.                     $rdv->setRecurrenceRdvs(null);
  1539.                     $entityManager->persist($rdv);
  1540.                 }
  1541.             }
  1542.             // Delete the recurrence record
  1543.             $entityManager->remove($recurrence);
  1544.             $entityManager->flush();
  1545.             // You can also delete associated RDV records here if needed
  1546.         }
  1547.         // Return a response indicating success or failure
  1548.         return new JsonResponse(['message' => 'Recurrence deleted successfully']);
  1549.     }
  1550.     /**
  1551.      * @Route("/{id}/recurrence-esthetique", methods={"GET","POST"})
  1552.      */
  1553.     public function recurrenceEsthetique(
  1554.         Client $clientRequest  $request,
  1555.         ProductRepository  $productRepository,
  1556.         RecurrenceRepository  $recurrenceRepository,
  1557.         RecurrenceProductRepository  $recurrenceProductRepository,
  1558.         EntityManagerInterface  $entityManager,
  1559.         SessionSalonRepository  $sessionSalonRepository
  1560.     ) {
  1561.         $productsAll $this->productRepository->findBy([
  1562.             "indisponibleVente" =>false,
  1563.             "type" => Product::TYPE_ESTHETHIQUE
  1564.         ]);
  1565.         $recurrence $recurrenceRepository->findOneBy(['client' => $client'type' => Product::TYPE_ESTHETHIQUE]);
  1566.         $salonCoiffure $client->getSite()->getFirstSalonsByTag(Product::TYPE_ESTHETHIQUE)->first();
  1567.         $sessionSalonDates $sessionSalonRepository->findSessionsBySalon($salonCoiffure);
  1568.         $post $request->isMethod(Request::METHOD_POST);
  1569.         if($post){
  1570.             if(!$recurrence){
  1571.                 $recurrence = new Recurrence();
  1572.                 $recurrence->setClient($client);
  1573.             }
  1574.             else {
  1575.                 $currentTime = new \DateTime();
  1576.                 foreach ($recurrence->getRdvs() as $rdv) {
  1577.                     if ($rdv->getStart() > $currentTime && ($rdv->getStatus() === RDV::STATUS_PLANIFIER ||$rdv->getStatus() === RDV::STATUS_REPLANIFIER )) {
  1578.                         $entityManager->remove($rdv);
  1579.                     }
  1580.                 }
  1581.             }
  1582.             $nbSemaine $request->request->get('nbSemaine');
  1583.             $JOS $request->request->get('date_debut');
  1584.             $products $request->request->get('products');
  1585.             $type $request->request->get('type');
  1586.             $startDate \DateTime::createFromFormat('Y-m-d H:i'$JOS);
  1587.             $endDate \DateTime::createFromFormat('Y-m-d H:i'$JOS);
  1588.             $recurrence->setNbSemaine($nbSemaine);
  1589.             $recurrence->setType($type);
  1590.             $date = new \DateTime($JOS);
  1591.             $recurrence->setJos($date);
  1592.             $this->em->persist($recurrence);
  1593.             $endDate->add(new \DateInterval('P1Y'));
  1594.             foreach ($products as $key => $product){
  1595.                 $productFind $productRepository->find($product['product']);
  1596.                 $recurrenceProduct $recurrenceProductRepository->findOneBy(['product' => $productFind'recurrence' => $recurrence]);
  1597.                 if(!$recurrenceProduct){
  1598.                     $recurrenceProduct = new RecurrenceProduct();
  1599.                 }
  1600.                 $recurrenceProduct->setFrequency($product['frequency']);
  1601.                 $this->em->persist($recurrenceProduct);
  1602.                 $productFind->addRecurrenceProduct($recurrenceProduct);
  1603.                 $this->em->persist($productFind);
  1604.                 $this->em->persist($recurrenceProduct);
  1605.                 $recurrence->addRecurrenceProduct($recurrenceProduct);
  1606.                 $this->em->persist($recurrence);
  1607.             }
  1608.             $i 0;
  1609.             while ($startDate <= $endDate) {
  1610.                 // Create an RDV object with the current date
  1611.                 $rdv = new RDV();
  1612.                 $rdv->setClient($client);
  1613.                 //$startDate->setTime(10, 0, 0);
  1614.                 $rdv->setStart(clone $startDate);
  1615.                 $rdv->setEnd(clone $startDate);
  1616.                 $salons $client->getSite()->getSalons();
  1617.                 /** @var Salon $salon */
  1618.                 $salonFind null;
  1619.                 foreach ($salons as $salon){
  1620.                     if($type === $salon->getService()) {
  1621.                         $salonFind $salon;
  1622.                         break;
  1623.                     }
  1624.                 }
  1625.                 if($salonFind){
  1626.                     $rdv->setSalon($salonFind);
  1627.                     /** @var IntervenantSalon $intervenant */
  1628.                     $intervenant $salonFind->getIntervenantSalons()->last();
  1629.                     if($intervenant){
  1630.                         $rdv->setIntervenant($intervenant->getIntervenant());
  1631.                     }
  1632.                 }
  1633.                 $rdv->setStatus(RDV::STATUS_PLANIFIER);
  1634.                 $rdv->setNewRecurrence(true);
  1635.                 foreach ($products as $key => $product){
  1636.                     $productFind $productRepository->find($product['product']);
  1637.                     if($i === 0){
  1638.                         $rdvProduct = new RDVProduct();
  1639.                         $rdvProduct->setRdv($rdv);
  1640.                         $rdvProduct->setProduct($productFind);
  1641.                         $this->em->persist($rdvProduct);
  1642.                         $rdv->addRdvProduct($rdvProduct);
  1643.                     }
  1644.                     else {
  1645.                         $selectedFrequency $product['frequency'];
  1646.                         if ($selectedFrequency == "every") {
  1647.                             // Handle the case where you want every RDV
  1648.                             $rdvProduct = new RDVProduct();
  1649.                             $rdvProduct->setRdv($rdv);
  1650.                             $rdvProduct->setProduct($productFind);
  1651.                             $this->em->persist($rdvProduct);
  1652.                             $rdv->addRdvProduct($rdvProduct);
  1653.                         }
  1654.                         elseif ($selectedFrequency == "every_other") {
  1655.                             // Handle the case where you want every 1 of 2 RDV
  1656.                             if ($i == 0) { // Check if $i is even
  1657.                                 $rdvProduct = new RDVProduct();
  1658.                                 $rdvProduct->setRdv($rdv);
  1659.                                 $rdvProduct->setProduct($productFind);
  1660.                                 $this->em->persist($rdvProduct);
  1661.                                 $rdv->addRdvProduct($rdvProduct);
  1662.                             }
  1663.                         }
  1664.                         elseif ($selectedFrequency == "every_third") {
  1665.                             // Handle the case where you want every 1 of 3 RDV
  1666.                             if ($i == 0) { // Check if $i is divisible by 3
  1667.                                 $rdvProduct = new RDVProduct();
  1668.                                 $rdvProduct->setRdv($rdv);
  1669.                                 $rdvProduct->setProduct($productFind);
  1670.                                 $this->em->persist($rdvProduct);
  1671.                                 $rdv->addRdvProduct($rdvProduct);
  1672.                             }
  1673.                         }
  1674.                         elseif ($selectedFrequency == "every_fourth") {
  1675.                             // Handle the case where you want every 1 of 4 RDV
  1676.                             if ($i == 0) { // Check if $i is divisible by 4
  1677.                                 $rdvProduct = new RDVProduct();
  1678.                                 $rdvProduct->setRdv($rdv);
  1679.                                 $rdvProduct->setProduct($productFind);
  1680.                                 $this->em->persist($rdvProduct);
  1681.                                 $rdv->addRdvProduct($rdvProduct);
  1682.                             }
  1683.                         }
  1684.                         elseif ($selectedFrequency == "every_fifth") {
  1685.                             // Handle the case where you want every 1 of 5 RDV
  1686.                             if ($i == 0) { // Check if $i is divisible by 5
  1687.                                 $rdvProduct = new RDVProduct();
  1688.                                 $rdvProduct->setRdv($rdv);
  1689.                                 $rdvProduct->setProduct($productFind);
  1690.                                 $this->em->persist($rdvProduct);
  1691.                                 $rdv->addRdvProduct($rdvProduct);
  1692.                             }
  1693.                         }
  1694.                         elseif ($selectedFrequency == "every_sixth") {
  1695.                             // Handle the case where you want every 1 of 6 RDV
  1696.                             if ($i == 0) { // Check if $i is divisible by 6
  1697.                                 $rdvProduct = new RDVProduct();
  1698.                                 $rdvProduct->setRdv($rdv);
  1699.                                 $rdvProduct->setProduct($productFind);
  1700.                                 $this->em->persist($rdvProduct);
  1701.                                 $rdv->addRdvProduct($rdvProduct);
  1702.                             }
  1703.                         }
  1704.                         elseif ($selectedFrequency == "every_seventh") {
  1705.                             // Handle the case where you want every 1 of 7 RDV
  1706.                             if ($i == 0) { // Check if $i is divisible by 7
  1707.                                 $rdvProduct = new RDVProduct();
  1708.                                 $rdvProduct->setRdv($rdv);
  1709.                                 $rdvProduct->setProduct($productFind);
  1710.                                 $this->em->persist($rdvProduct);
  1711.                                 $rdv->addRdvProduct($rdvProduct);
  1712.                             }
  1713.                         }
  1714.                         elseif ($selectedFrequency == "every_eighth") {
  1715.                             // Handle the case where you want every 1 of 8 RDV
  1716.                             if ($i == 0) { // Check if $i is divisible by 8
  1717.                                 $rdvProduct = new RDVProduct();
  1718.                                 $rdvProduct->setRdv($rdv);
  1719.                                 $rdvProduct->setProduct($productFind);
  1720.                                 $this->em->persist($rdvProduct);
  1721.                                 $rdv->addRdvProduct($rdvProduct);
  1722.                             }
  1723.                         }
  1724.                         elseif ($selectedFrequency == "every_ninth") {
  1725.                             // Handle the case where you want every 1 of 9 RDV
  1726.                             if ($i == 0) { // Check if $i is divisible by 9
  1727.                                 $rdvProduct = new RDVProduct();
  1728.                                 $rdvProduct->setRdv($rdv);
  1729.                                 $rdvProduct->setProduct($productFind);
  1730.                                 $this->em->persist($rdvProduct);
  1731.                                 $rdv->addRdvProduct($rdvProduct);
  1732.                             }
  1733.                         } elseif ($selectedFrequency == "every_tenth") {
  1734.                             // Handle the case where you want every 1 of 10 RDV
  1735.                             if ($i 10 == 0) { // Check if $i is divisible by 10
  1736.                                 $rdvProduct = new RDVProduct();
  1737.                                 $rdvProduct->setRdv($rdv);
  1738.                                 $rdvProduct->setProduct($productFind);
  1739.                                 $this->em->persist($rdvProduct);
  1740.                                 $rdv->addRdvProduct($rdvProduct);
  1741.                             }
  1742.                         }
  1743.                         elseif ($selectedFrequency == "every_eleventh") {
  1744.                             // Handle the case where you want every 1 of 11 RDV
  1745.                             if ($i 11 == 0) { // Check if $i is divisible by 11
  1746.                                 $rdvProduct = new RDVProduct();
  1747.                                 $rdvProduct->setRdv($rdv);
  1748.                                 $rdvProduct->setProduct($productFind);
  1749.                                 $this->em->persist($rdvProduct);
  1750.                                 $rdv->addRdvProduct($rdvProduct);
  1751.                             }
  1752.                         }
  1753.                         elseif ($selectedFrequency == "every_twelfth") {
  1754.                             // Handle the case where you want every 1 of 12 RDV
  1755.                             if ($i 12 == 0) { // Check if $i is divisible by 12
  1756.                                 $rdvProduct = new RDVProduct();
  1757.                                 $rdvProduct->setRdv($rdv);
  1758.                                 $rdvProduct->setProduct($productFind);
  1759.                                 $this->em->persist($rdvProduct);
  1760.                                 $rdv->addRdvProduct($rdvProduct);
  1761.                             }
  1762.                         }
  1763.                     }
  1764.                 }
  1765.                 $recurrence->addRdv($rdv);
  1766.                 $this->em->persist($recurrence);
  1767.                 $this->em->persist($rdv);
  1768.                 $i++;
  1769.                 // Increment the date by the number of weeks specified
  1770.                 $startDate->add(new \DateInterval('P' $nbSemaine 'W'));
  1771.             }
  1772.             $this->em->flush();
  1773.         }
  1774.         return $this->render('client/recurrence.html.twig', [
  1775.             'client' => $client,
  1776.             'products' => $productsAll,
  1777.             'recurrence' => $recurrence,
  1778.             'sessionSalonDates' => $sessionSalonDates
  1779.         ]);
  1780.     }
  1781.     /**
  1782.      * @Route("/{id}/generateRecurrence", name="client_generaterecurrence", methods={"GET","POST"})
  1783.      */
  1784.     public function generateRecurrenceNew(Request $requestClient $client): Response
  1785.     {
  1786.         return $this->redirectToRoute('client_edit', ['id' => $client->getId()]);
  1787.     }
  1788.     /**
  1789.      * @Route("/{id}", name="client_delete", methods={"DELETE"})
  1790.      */
  1791.     public function delete(Request $requestClient $client): Response
  1792.     {
  1793.         if ($this->isCsrfTokenValid('delete' $client->getId(), $request->request->get('_token'))) {
  1794.             $entityManager $this->getDoctrine()->getManager();
  1795.             $entityManager->remove($client);
  1796.             $entityManager->flush();
  1797.         }
  1798.         return $this->redirectToRoute('client_index');
  1799.     }
  1800.     /**
  1801.      * @param FormInterface $form
  1802.      * @throws \Exception
  1803.      */
  1804.     private function generateRecurrence(FormInterface $form$edit false)
  1805.     {
  1806.         $contracts $form->get('contract');
  1807.         $entityManager $this->getDoctrine()->getManager();
  1808.         foreach ($contracts as $contract) {
  1809.             $products $contract->get('products')->getViewData();
  1810.             /** @var Contract $contract */
  1811.             $contract $contract->getData();
  1812.             $dateDeFinNew date_create_from_format('d-m-Y'$contract->getDateFinRelation());
  1813.             $uow $entityManager->getUnitOfWork();
  1814.             $oldContract $uow->getOriginalEntityData($contract);
  1815.             $oldDate = isset($oldContract['dateFinRelation']) ? $oldContract['dateFinRelation'] : null;
  1816.             $dateDeFinOld date_create_from_format('d-m-Y'$oldDate);
  1817.             // createt all contract prroduct for this contract
  1818.             $productsArrayEntities = [];
  1819.             foreach ($products as $key => $product) {
  1820.                 $productE $this->productRepository->find($product);
  1821.                 $contractProduct = new ContractProduct();
  1822.                 $contractProduct->setProduct($productE);
  1823.                 $contractProduct->setContract($contract);
  1824.                 $entityManager->persist($contractProduct);
  1825.                 $productsArrayEntities[] = $productE;
  1826.             }
  1827.             //$condition =  $oldDate && $dateDeFinNew ? ($oldDate->format('d/m/Y') != $dateDeFinNew->format('d/m/Y') ): false;
  1828.             // suppresion des recurrences en cas de date de fin
  1829.             if ($edit && $contract->getRecurrenceGenerated() && $dateDeFinOld != $dateDeFinNew) {
  1830.                 foreach ($contract->getRdvs() as $rdv) {
  1831.                     if ($rdv->getStart() > $dateDeFinNew) {
  1832.                         $contract->removeRDV($rdv);
  1833.                         $entityManager->persist($contract);
  1834.                         $entityManager->remove($rdv);
  1835.                     }
  1836.                 }
  1837.                 $entityManager->flush();
  1838.             }
  1839.             if (!$contract->getRecurrenceGenerated() && $contract->getTypeRDV() === Contract::TYPE_RDV_PERIODIQUE) {
  1840.                 $intervenant null;
  1841.                 $salon null;
  1842.                 $start $contract->getDateDebutRelation();
  1843.                 $end $contract->getDateFinRelation();
  1844.                 $heure $contract->getHeure();
  1845.                 $remarque $contract->getRemarque();
  1846.                 if (!$start) {
  1847.                     continue;
  1848.                 }
  1849.                 //OK
  1850.                 $dateAdd strtotime($start ' ' $heure); // date début contrat
  1851.                 if ($end) {
  1852.                     $endTime strtotime($end ' ' $heure); //date fin contrat
  1853.                 } else {
  1854.                     // si ehpad RDV généré sur 4 ans
  1855.                     if ($contract->getClient()->getSite()->getType() === Site::TYPE_EHPAD) {
  1856.                         $endTime strtotime('+4 year'strtotime($start));
  1857.                     } else {
  1858.                         $endTime strtotime('+6 month'strtotime($start));
  1859.                     }
  1860.                 }
  1861.                 $client $contract->getClient();
  1862.                 // ON RECUPERER LETAG DU PREMIER PRODUIT
  1863.                 $tag null;
  1864.                 if (isset($productsArrayEntities[0])) {
  1865.                     /** @var Product $product */
  1866.                     $product $productsArrayEntities[0];
  1867.                     $tag $product->getType();
  1868.                 }
  1869.                 // get intervenant
  1870.                 $salons $client->getSite()->getFirstSalonsByTag($tag);
  1871.                 if (!$salons->isEmpty() && $salons->first()) {
  1872.                     /** @var Salon $first */
  1873.                     $salon $salons->first();
  1874.                     /** @var IntervenantSalon $intervenantSalon */
  1875.                     $intervenantSalon $salon->getIntervenantSalons()->last();
  1876.                     if ($intervenantSalon) {
  1877.                         $intervenant $intervenantSalon->getIntervenant();
  1878.                     }
  1879.                 }
  1880.                 // generate first one Le début et la fin sont le meme jour soit la date de début
  1881.                 $rdv = new RDV();
  1882.                 // recuperation du champ remarque
  1883.                 $rdv->setComment($remarque);
  1884.                 $rdv->setClient($client);
  1885.                 $datetime = new \DateTime();
  1886.                 //dump($dateAdd);
  1887.                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  1888.                 $end = (new \DateTime())->setTimestamp($dateAdd);
  1889.                 $end->add(new \DateInterval('PT1H'));
  1890.                 $rdv->setEnd($end);
  1891.                 if ($salon) {
  1892.                     $rdv->setSalon($salon);
  1893.                 }
  1894.                 if ($intervenant) {
  1895.                     $rdv->setIntervenant($intervenant);
  1896.                 }
  1897.                 $rdv->setContract($contract);
  1898.                 $entityManager->persist($rdv);
  1899.                 foreach ($productsArrayEntities as $product) {
  1900.                     $rdvProduct $this->createRDVProduct($product$rdv);
  1901.                     $entityManager->persist($rdvProduct);
  1902.                 }
  1903.                 $i 0;
  1904.                 $max 1000;
  1905.                 // TODO : refactor
  1906.                 switch ($contract->getRecurrence()) {
  1907.                     case Contract::RECURRENCE_1_SUR_SEMAINE: {
  1908.                             while ($i $max && $dateAdd $endTime) {
  1909.                                 $dateAdd strtotime('+1 week'$dateAdd);
  1910.                                 //dump(date('d/m/y',$dateAdd));
  1911.                                 $rdv = new RDV();
  1912.                                 $rdv->setClient($client);
  1913.                                 $rdv->setComment($remarque);
  1914.                                 $datetime = new \DateTime();
  1915.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  1916.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  1917.                                 $end->add(new \DateInterval('PT1H'));
  1918.                                 $rdv->setEnd($end);
  1919.                                 $rdv->setContract($contract);
  1920.                                 $rdv->setPriorite(1);
  1921.                                 if ($intervenant) {
  1922.                                     $rdv->setIntervenant($intervenant);
  1923.                                 }
  1924.                                 if ($salon) {
  1925.                                     $rdv->setSalon($salon);
  1926.                                 }
  1927.                                 $entityManager->persist($rdv);
  1928.                                 foreach ($productsArrayEntities as $product) {
  1929.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  1930.                                     $entityManager->persist($rdvProduct);
  1931.                                 }
  1932.                                 $i++;
  1933.                             }
  1934.                             break;
  1935.                         }
  1936.                     case Contract::RECURRENCE_1_SUR_2: {
  1937.                             // compare timestamp
  1938.                             while ($i $max && $dateAdd $endTime) {
  1939.                                 $dateAdd strtotime('+2 week'$dateAdd);
  1940.                                 //dump(date('d/m/y',$dateAdd));
  1941.                                 $rdv = new RDV();
  1942.                                 $rdv->setClient($client);
  1943.                                 $datetime = new \DateTime();
  1944.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  1945.                                 $rdv->setComment($remarque);
  1946.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  1947.                                 $end->add(new \DateInterval('PT1H'));
  1948.                                 $rdv->setEnd($end);
  1949.                                 $rdv->setContract($contract);
  1950.                                 $rdv->setPriorite(1);
  1951.                                 if ($intervenant) {
  1952.                                     $rdv->setIntervenant($intervenant);
  1953.                                 }
  1954.                                 if ($salon) {
  1955.                                     $rdv->setSalon($salon);
  1956.                                 }
  1957.                                 $entityManager->persist($rdv);
  1958.                                 foreach ($productsArrayEntities as $product) {
  1959.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  1960.                                     $entityManager->persist($rdvProduct);
  1961.                                 }
  1962.                                 $i++;
  1963.                             }
  1964.                             //dump($i); // max 13 6 month
  1965.                             break;
  1966.                         }
  1967.                     case Contract::RECURRENCE_1_SUR_3: {
  1968.                             while ($i $max && $dateAdd $endTime) {
  1969.                                 $dateAdd strtotime('+3 week'$dateAdd);
  1970.                                 //dump(date('d/m/y',$dateAdd));
  1971.                                 $rdv = new RDV();
  1972.                                 $rdv->setClient($client);
  1973.                                 $datetime = new \DateTime();
  1974.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  1975.                                 $rdv->setComment($remarque);
  1976.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  1977.                                 $end->add(new \DateInterval('PT1H'));
  1978.                                 $rdv->setEnd($end);
  1979.                                 $rdv->setContract($contract);
  1980.                                 $rdv->setPriorite(2);
  1981.                                 if ($intervenant) {
  1982.                                     $rdv->setIntervenant($intervenant);
  1983.                                 }
  1984.                                 if ($salon) {
  1985.                                     $rdv->setSalon($salon);
  1986.                                 }
  1987.                                 $entityManager->persist($rdv);
  1988.                                 foreach ($productsArrayEntities as $product) {
  1989.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  1990.                                     $entityManager->persist($rdvProduct);
  1991.                                 }
  1992.                                 $i++;
  1993.                             }
  1994.                             break;
  1995.                         }
  1996.                     case Contract::RECURRENCE_1_SUR_4: {
  1997.                             while ($i $max && $dateAdd $endTime) {
  1998.                                 $dateAdd strtotime('+4 week'$dateAdd);
  1999.                                 //dump(date('d/m/y',$dateAdd));
  2000.                                 $rdv = new RDV();
  2001.                                 $rdv->setClient($client);
  2002.                                 $datetime = new \DateTime();
  2003.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2004.                                 $rdv->setComment($remarque);
  2005.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2006.                                 $end->add(new \DateInterval('PT1H'));
  2007.                                 $rdv->setEnd($end);
  2008.                                 $rdv->setContract($contract);
  2009.                                 $rdv->setPriorite(2);
  2010.                                 if ($intervenant) {
  2011.                                     $rdv->setIntervenant($intervenant);
  2012.                                 }
  2013.                                 if ($salon) {
  2014.                                     $rdv->setSalon($salon);
  2015.                                 }
  2016.                                 $entityManager->persist($rdv);
  2017.                                 foreach ($productsArrayEntities as $product) {
  2018.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2019.                                     $entityManager->persist($rdvProduct);
  2020.                                 }
  2021.                                 $i++;
  2022.                             }
  2023.                             break;
  2024.                         }
  2025.                     case Contract::RECURRENCE_1_SUR_8: {
  2026.                             while ($i $max && $dateAdd $endTime) {
  2027.                                 $dateAdd strtotime('+8 week'$dateAdd);
  2028.                                 //dump(date('d/m/y',$dateAdd));
  2029.                                 $rdv = new RDV();
  2030.                                 $rdv->setClient($client);
  2031.                                 $datetime = new \DateTime();
  2032.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2033.                                 $rdv->setComment($remarque);
  2034.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2035.                                 $end->add(new \DateInterval('PT1H'));
  2036.                                 $rdv->setEnd($end);
  2037.                                 $rdv->setContract($contract);
  2038.                                 $rdv->setPriorite(2);
  2039.                                 if ($intervenant) {
  2040.                                     $rdv->setIntervenant($intervenant);
  2041.                                 }
  2042.                                 if ($salon) {
  2043.                                     $rdv->setSalon($salon);
  2044.                                 }
  2045.                                 $entityManager->persist($rdv);
  2046.                                 foreach ($productsArrayEntities as $product) {
  2047.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2048.                                     $entityManager->persist($rdvProduct);
  2049.                                 }
  2050.                                 $i++;
  2051.                             }
  2052.                             break;
  2053.                         }
  2054.                     case Contract::RECURRENCE_1_SUR_12: {
  2055.                             while ($i $max && $dateAdd $endTime) {
  2056.                                 $dateAdd strtotime('+12 week'$dateAdd);
  2057.                                 //dump(date('d/m/y',$dateAdd));
  2058.                                 $rdv = new RDV();
  2059.                                 $rdv->setClient($client);
  2060.                                 $datetime = new \DateTime();
  2061.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2062.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2063.                                 $end->add(new \DateInterval('PT1H'));
  2064.                                 $rdv->setEnd($end);
  2065.                                 $rdv->setComment($remarque);
  2066.                                 $rdv->setContract($contract);
  2067.                                 $rdv->setPriorite(2);
  2068.                                 if ($intervenant) {
  2069.                                     $rdv->setIntervenant($intervenant);
  2070.                                 }
  2071.                                 if ($salon) {
  2072.                                     $rdv->setSalon($salon);
  2073.                                 }
  2074.                                 $entityManager->persist($rdv);
  2075.                                 foreach ($productsArrayEntities as $product) {
  2076.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2077.                                     $entityManager->persist($rdvProduct);
  2078.                                 }
  2079.                                 $i++;
  2080.                             }
  2081.                             break;
  2082.                         }
  2083.                     case Contract::RECURRENCE_1_SUR_5: {
  2084.                             while ($i $max && $dateAdd $endTime) {
  2085.                                 $dateAdd strtotime('+5 week'$dateAdd);
  2086.                                 //dump(date('d/m/y',$dateAdd));
  2087.                                 $rdv = new RDV();
  2088.                                 $rdv->setClient($client);
  2089.                                 $datetime = new \DateTime();
  2090.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2091.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2092.                                 $end->add(new \DateInterval('PT1H'));
  2093.                                 $rdv->setEnd($end);
  2094.                                 $rdv->setComment($remarque);
  2095.                                 $rdv->setContract($contract);
  2096.                                 $rdv->setPriorite(2);
  2097.                                 if ($intervenant) {
  2098.                                     $rdv->setIntervenant($intervenant);
  2099.                                 }
  2100.                                 if ($salon) {
  2101.                                     $rdv->setSalon($salon);
  2102.                                 }
  2103.                                 $entityManager->persist($rdv);
  2104.                                 foreach ($productsArrayEntities as $product) {
  2105.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2106.                                     $entityManager->persist($rdvProduct);
  2107.                                 }
  2108.                                 $i++;
  2109.                             }
  2110.                             break;
  2111.                         }
  2112.                     case Contract::RECURRENCE_1_SUR_6: {
  2113.                             while ($i $max && $dateAdd $endTime) {
  2114.                                 $dateAdd strtotime('+6 week'$dateAdd);
  2115.                                 //dump(date('d/m/y',$dateAdd));
  2116.                                 $rdv = new RDV();
  2117.                                 $rdv->setClient($client);
  2118.                                 $datetime = new \DateTime();
  2119.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2120.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2121.                                 $end->add(new \DateInterval('PT1H'));
  2122.                                 $rdv->setEnd($end);
  2123.                                 $rdv->setComment($remarque);
  2124.                                 $rdv->setContract($contract);
  2125.                                 $rdv->setPriorite(2);
  2126.                                 if ($intervenant) {
  2127.                                     $rdv->setIntervenant($intervenant);
  2128.                                 }
  2129.                                 if ($salon) {
  2130.                                     $rdv->setSalon($salon);
  2131.                                 }
  2132.                                 $entityManager->persist($rdv);
  2133.                                 foreach ($productsArrayEntities as $product) {
  2134.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2135.                                     $entityManager->persist($rdvProduct);
  2136.                                 }
  2137.                                 $i++;
  2138.                             }
  2139.                             break;
  2140.                         }
  2141.                     case Contract::RECURRENCE_1_SUR_2_MONTH: {
  2142.                             while ($i $max && $dateAdd $endTime) {
  2143.                                 $dateAdd strtotime('+9 week'$dateAdd);
  2144.                                 //dump(date('d/m/y',$dateAdd));
  2145.                                 $rdv = new RDV();
  2146.                                 $rdv->setClient($client);
  2147.                                 $datetime = new \DateTime();
  2148.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2149.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2150.                                 $end->add(new \DateInterval('PT1H'));
  2151.                                 $rdv->setEnd($end);
  2152.                                 $rdv->setComment($remarque);
  2153.                                 $rdv->setContract($contract);
  2154.                                 $rdv->setPriorite(2);
  2155.                                 if ($intervenant) {
  2156.                                     $rdv->setIntervenant($intervenant);
  2157.                                 }
  2158.                                 if ($salon) {
  2159.                                     $rdv->setSalon($salon);
  2160.                                 }
  2161.                                 $entityManager->persist($rdv);
  2162.                                 foreach ($productsArrayEntities as $product) {
  2163.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2164.                                     $entityManager->persist($rdvProduct);
  2165.                                 }
  2166.                                 $i++;
  2167.                             }
  2168.                             break;
  2169.                         }
  2170.                     case Contract::RECURRENCE_1_SUR_TRIM: {
  2171.                             while ($i $max && $dateAdd $endTime) {
  2172.                                 $dateAdd strtotime('+13 week'$dateAdd);
  2173.                                 //dump(date('d/m/y',$dateAdd));
  2174.                                 $rdv = new RDV();
  2175.                                 $rdv->setClient($client);
  2176.                                 $datetime = new \DateTime();
  2177.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2178.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2179.                                 $end->add(new \DateInterval('PT1H'));
  2180.                                 $rdv->setEnd($end);
  2181.                                 $rdv->setComment($remarque);
  2182.                                 $rdv->setContract($contract);
  2183.                                 $rdv->setPriorite(2);
  2184.                                 if ($intervenant) {
  2185.                                     $rdv->setIntervenant($intervenant);
  2186.                                 }
  2187.                                 if ($salon) {
  2188.                                     $rdv->setSalon($salon);
  2189.                                 }
  2190.                                 $entityManager->persist($rdv);
  2191.                                 foreach ($productsArrayEntities as $product) {
  2192.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2193.                                     $entityManager->persist($rdvProduct);
  2194.                                 }
  2195.                                 $i++;
  2196.                             }
  2197.                             break;
  2198.                         }
  2199.                     case Contract::RECURRENCE_1_SUR_6_MONTH: {
  2200.                             while ($i $max && $dateAdd $endTime) {
  2201.                                 $dateAdd strtotime('+26 week'$dateAdd);
  2202.                                 //dump(date('d/m/y',$dateAdd));
  2203.                                 $rdv = new RDV();
  2204.                                 $rdv->setClient($client);
  2205.                                 $datetime = new \DateTime();
  2206.                                 $rdv->setStart($datetime->setTimestamp($dateAdd));
  2207.                                 $end = (new \DateTime())->setTimestamp($dateAdd);
  2208.                                 $end->add(new \DateInterval('PT1H'));
  2209.                                 $rdv->setEnd($end);
  2210.                                 $rdv->setComment($remarque);
  2211.                                 $rdv->setContract($contract);
  2212.                                 $rdv->setPriorite(2);
  2213.                                 if ($intervenant) {
  2214.                                     $rdv->setIntervenant($intervenant);
  2215.                                 }
  2216.                                 if ($salon) {
  2217.                                     $rdv->setSalon($salon);
  2218.                                 }
  2219.                                 $entityManager->persist($rdv);
  2220.                                 foreach ($productsArrayEntities as $product) {
  2221.                                     $rdvProduct $this->createRDVProduct($product$rdv);
  2222.                                     $entityManager->persist($rdvProduct);
  2223.                                 }
  2224.                                 $i++;
  2225.                             }
  2226.                             break;
  2227.                         }
  2228.                 }
  2229.                 $contract->setRecurrenceGenerated(true);
  2230.             }
  2231.         }
  2232.         $entityManager->flush();
  2233.     }
  2234.     /**
  2235.      * @Route("/getProducts/{id}", methods={"GET"}, condition="request.isXmlHttpRequest()")
  2236.      */
  2237.     public function ajaxGetProducts($idSerializerInterface $serializerProductRepository $productRepository)
  2238.     {
  2239.         // dump('d'. $client->getSite()->getId());die;
  2240.         $products $productRepository->findBy([
  2241.             'type' => strtolower($id)
  2242.         ]);
  2243.         $result $serializer->serialize($products'json', [
  2244.             'circular_reference_handler' => function ($object) {
  2245.                 return $object->getId();
  2246.             }
  2247.         ]);
  2248.         return new JsonResponse($resultResponse::HTTP_OK, [], true);
  2249.     }
  2250.     /**
  2251.      * @required
  2252.      * @param ProductRepository $productRepository
  2253.      */
  2254.     public function setProductRepository(ProductRepository $productRepository)
  2255.     {
  2256.         $this->productRepository $productRepository;
  2257.     }
  2258.     private function createRDVProduct(Product $productRDV $RDV)
  2259.     {
  2260.         $rdvProduct = new RDVProduct();
  2261.         $rdvProduct->setProduct($product);
  2262.         $rdvProduct->setRdv($RDV);
  2263.         return $rdvProduct;
  2264.     }
  2265.     private function getColumnsForEntity($class)
  2266.     {
  2267.         $columns[Client::class] = [
  2268.             'Id' => function (Client $client) {
  2269.                 return $client->getId();
  2270.             },
  2271.             'Prénom' => function (Client $client) {
  2272.                 return $client->getFirstname();
  2273.             },
  2274.             'Nom' => function (Client $client) {
  2275.                 return  $client->getLastname();
  2276.             },
  2277.             'Date de naissance' => function (Client $client) {
  2278.                 return $client->getBirthday();
  2279.             },
  2280.             'Date de Décès' => function (Client $client) {
  2281.                 return $client->getDeathday();
  2282.             },
  2283.             'Email' => function (Client $client) {
  2284.                 return $client->getEmail();
  2285.             },
  2286.             'Phone' => function (Client $client) {
  2287.                 return $client->getPhone();
  2288.             },
  2289.             'Commentaires' => function (Client $client) {
  2290.                 return $client->getComments();
  2291.             },
  2292.             'Numéro de chambre' => function (Client $client) {
  2293.                 return $client->getRoomNumber();
  2294.             },
  2295.             'Numéro de batiment' => function (Client $client) {
  2296.                 return $client->getBuildingRoom();
  2297.             },
  2298.             'Etages' => function (Client $client) {
  2299.                 return $client->getStairs();
  2300.             },
  2301.             'Civilite' => function (Client $client) {
  2302.                 return $client->getCivilite();
  2303.             },
  2304.             'Tuteur' => function (Client $client) {
  2305.                 return $client->getPrenomTuteur() . ' ' $client->getNomTuteur();
  2306.             },
  2307.             'Adresse Tuteur' => function (Client $client) {
  2308.                 return $client->getAddressTuteur() . ' ' $client->getAddressTuteur2() . ' ' $client->getZipcode() . ' ' $client->getCity();
  2309.             },
  2310.             // 'Mail notaire' => function (Client $client) {
  2311.             //     return $client->getEmailNotaire();
  2312.             // },
  2313.             'Email Tuteur' => function (Client $client) {
  2314.                 return $client->getEmailTuteur();
  2315.             },
  2316.             'Type Tuteur' => function (Client $client) {
  2317.                 return $client->getTypeTuteur();
  2318.             },
  2319.             'Numero tuteur' => function (Client $client) {
  2320.                 return $client->getNumeroTuteur();
  2321.             },
  2322.             'Compte bancaire' => function (Client $client) {
  2323.                 $bill $client->getBillingInfos();
  2324.                 return $bill $bill->getAccount() : '';
  2325.             },
  2326.             'Banque' => function (Client $client) {
  2327.                 $bill $client->getBillingInfos();
  2328.                 return $bill $bill->getBankName() : '';
  2329.             },
  2330.             'IBAN' => function (Client $client) {
  2331.                 $bill $client->getBillingInfos();
  2332.                 return $bill $bill->getIban() : '';
  2333.             },
  2334.             'Bic' => function (Client $client) {
  2335.                 $bill $client->getBillingInfos();
  2336.                 return $bill $bill->getBic() : '';
  2337.             },
  2338.             'Mode de paiement' => function (Client $client) {
  2339.                 $bill $client->getBillingInfos();
  2340.                 return $bill $bill->getBillingMode() : '';
  2341.             },
  2342.             'Société' => function (Client $client) {
  2343.                 $company null;
  2344.                 $bill $client->getBillingInfos();
  2345.                 if ($bill) {
  2346.                     $company $bill->getCompany();
  2347.                 }
  2348.                 return $company $client->getBillingInfos()->getCompany()->getName() : '';
  2349.             },
  2350.             'Status Prospect' => function (Client $client) {
  2351.                 return $client->getStatusProspect();
  2352.             },
  2353.             'Mail facturation' => function (Client $client) {
  2354.                 $bill $client->getBillingInfos();
  2355.                 return $bill $client->getBillingInfos()->getEmail() : '';
  2356.             },
  2357.             'Tel facturation' => function (Client $client) {
  2358.                 $bill $client->getBillingInfos();
  2359.                 return $bill ?  $client->getBillingInfos()->getPhone() : '';
  2360.             },
  2361.             'Mode envoi' => function (Client $client) {
  2362.                 $bill $client->getBillingInfos();
  2363.                 return $bill $bill->getSendMode() : '';
  2364.             },
  2365.             'Site' => function (Client $client) {
  2366.                 $siteName $client->getSite() ? $client->getSite()->getName() : "";
  2367.                 return $siteName;
  2368.             },
  2369.             'Creation' => function (Client $client) {
  2370.                 return $client->getCreated()->format('d/m/Y');
  2371.             },
  2372. //            'Contract' => function (Client $client) {
  2373. //
  2374. //                $contractString = '';
  2375. //                /** @var Contract $contrat */
  2376. //                foreach ($client->getContract() as $contrat) {
  2377. //                    $contractString .= (string) $contrat->getId() . ' ';
  2378. //                }
  2379. //
  2380. //                return $contractString;
  2381. //            },
  2382.             'Type de RDV' => function (Client $client) {
  2383.                 /** @var RDV $rdv */
  2384.                 $rdv $this->RDVManager->getRepository()->getLastRDVsByClient($client);
  2385.                 return $rdv &&  $rdv->getSalon() ? $rdv->getSalon()->getService(): "";
  2386.             },
  2387.             'Dernier RDV Date' => function (Client $client) {
  2388.                 $rdv $this->RDVManager->getRepository()->getLastRDVsByClient($client);
  2389.                 return $rdv $rdv->getStart()->format('d/m/y') : "";
  2390.             },
  2391.             'Dernier RDV Heure' => function (Client $client) {
  2392.                 $rdv $this->RDVManager->getRepository()->getLastRDVsByClient($client);
  2393.                 return $rdv $rdv->getStart()->format('H i') : "";
  2394.             },
  2395.             'Recurrence' => function (Client $client) {
  2396.                 $ids = [];
  2397.                 foreach ( $client->getRecurrences() as $recurrence){
  2398.                     $ids[]= $recurrence->getId();
  2399.                 }
  2400.                 /** @var RDV $rdv */
  2401.                 return implode(' ---- ',$ids);
  2402.             },
  2403.             'Recurrence type' => function (Client $client) {
  2404.                 $ids = [];
  2405.                 foreach ( $client->getRecurrences() as $recurrence){
  2406.                     $ids[]= $recurrence->getType();
  2407.                 }
  2408.                 /** @var RDV $rdv */
  2409.                 return implode(' ---- ',$ids);
  2410.             },
  2411.         ];
  2412.         if (array_key_exists($class$columns)) {
  2413.             return $columns[$class];
  2414.         }
  2415.         throw new \InvalidArgumentException(sprintf(
  2416.             'No columns set for "%s" entity',
  2417.             $class
  2418.         ));
  2419.     }
  2420. }