src/Controller/ClientController.php line 178

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