src/Controller/Api/ApiOpcController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Adherent\Annuaire\MagasinContact;
  4. use App\Entity\Adherent\Magasin;
  5. use App\Entity\Adherent\MagasinCoordonnees;
  6. use App\Entity\Adherent\MagasinUrl;
  7. use App\Entity\Image\Image;
  8. use App\Entity\TraficMagasin\NotifReport;
  9. use App\Entity\TraficMagasin\RdvMotifCustom;
  10. use App\Entity\TraficMagasin\RdvMotifDefault;
  11. use App\Entity\TraficMagasin\RdvOpc;
  12. use App\Entity\Webfactory\Opc\OpcImage;
  13. use App\Entity\Webfactory\Opc\OpcSpecialite;
  14. use App\Manager\NotificationManager;
  15. use App\Manager\OpcManager;
  16. use App\Manager\TraficMagasin\SmsApiManager;
  17. use App\Repository\TraficMagasin\RdvMotifCustomRepository;
  18. use App\Repository\TraficMagasin\RdvMotifDefaultRepository;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\Finder\Exception\AccessDeniedException;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. #[Route(path'/api')]
  26. class ApiOpcController extends AbstractController
  27. {
  28.     public function __construct(
  29.         private readonly EntityManagerInterface $entityManager,
  30.         private readonly NotificationManager    $notificationManager,
  31.     ) {
  32.     }
  33.     #[Route(path'/no-permission'name'no_permission')]
  34.     public function noPermission(): never
  35.     {
  36.         throw new AccessDeniedException();
  37.     }
  38.     #[Route(path'/get-url-fiche/{code}'name'get_url_fiche')]
  39.     public function getUrlFiche(Request $requestEntityManagerInterface $entityManager$code)
  40.     {
  41.         $url $entityManager->getRepository(MagasinUrl::class)->findOneBy(["type" => "OPC/OZC""magasin" => $code]);
  42.         if (!empty($url)) {
  43.             return $this->json(['code' => $url->getMagasin()->getKAdherent(), 'url' => $url->getUrl()]);
  44.         } else {
  45.             return $this->json([]);
  46.         }
  47.     }
  48.     #[Route(path'/get-url-rdv/{code}'name'get_url_rdv')]
  49.     public function getUrlRDV(Request $requestEntityManagerInterface $entityManagerOpcManager $manager$code)
  50.     {
  51.         $urlRdv $this->entityManager->getRepository(MagasinUrl::class)->findOneBy(['magasin' => $code'type' => 'rdv']);
  52.         if (!is_null($urlRdv)) {
  53.             return $this->json(['code' => $code'url' => $urlRdv]);
  54.         } else {
  55.             $magasinCoordonnees $entityManager->getRepository(MagasinCoordonnees::class)->findOneBy(['K_ADHERENT' => $code]);
  56.             if (!empty($magasinCoordonnees)) {
  57.                 $url $manager->getAdhOpcUrl($magasinCoordonneespriseDeRdvtrue);
  58.                 return $this->json(['code' => $code'url' => $url]);
  59.             }
  60.         }
  61.     }
  62.     #[Route(path'/image/vignettes'name'get_vignettes')]
  63.     public function getVignettes(Request $request)
  64.     {
  65.         $tab        = [];
  66.         $parameters json_decode($request->getContent(), true);
  67.         if (isset($parameters['adherents']) && !empty($parameters['adherents'])) {
  68.             $vignettes $this->entityManager->getRepository(Image::class)->getVignettes($parameters['adherents']);
  69.             foreach ($vignettes as $vignette) {
  70.                 $tab[$vignette->getAdherent()] = [
  71.                     'img'      => $vignette->getName(),
  72.                     'ext'      => $vignette->getExtension(),
  73.                     'type'     => $vignette->getType()->getName(),
  74.                     'logo'     => $vignette->getLogoDefault(),
  75.                     'vignette' => $vignette->getProfileDefault(),
  76.                 ];
  77.             }
  78.             return new JsonResponse(['Status' => "200"'datas' => $tab]);
  79.         } else {
  80.             return new JsonResponse(['Status' => "400"'errors' => "Parametre adherents manquant"]);
  81.         }
  82.     }
  83.     #[Route(path'/image/photos/{magasin}'name'get_photos_by_adherents')]
  84.     public function getPhotos(Request $requestMagasin $magasin)
  85.     {
  86.         $tab    = [];
  87.         $photos $this->entityManager->getRepository(OpcImage::class)->findBy(['kAdh' => $magasin->getKAdherent()], ['sort' => 'ASC']);
  88.         foreach ($photos as $photo) {
  89.             if ($photo->getIdImage()->getActive()) {
  90.                 $tab[strtoupper((string)$photo->getIdImage()->getAdherent())][] = [
  91.                     'img'      => $photo->getIdImage()->getName(),
  92.                     'ext'      => $photo->getIdImage()->getExtension(),
  93.                     'type'     => $photo->getIdImage()->getType()->getName(),
  94.                     'logo'     => $photo->getIdImage()->getLogoDefault(),
  95.                     'vignette' => $photo->getIdImage()->getProfileDefault(),
  96.                 ];
  97.             }
  98.         }
  99.         return new JsonResponse(['Status' => "200"'datas' => $tab]);
  100.     }
  101.     #[Route(path'/sendRdvOpc'name'sendrdvopc')]
  102.     public function sendRdvOpc(Request $request): JsonResponse
  103.     {
  104.         $datas json_decode($request->getContent(), true);
  105.         if ($datas) {
  106.             $new_rdv = new RdvOpc();
  107.             $new_rdv->setKAdherent($datas[0]['codeAdh']);
  108.             $new_rdv->setFirstName($datas[0]['prenom']);
  109.             $new_rdv->setLastName($datas[0]['nom']);
  110.             $new_rdv->setPhone($datas[0]['tel']);
  111.             $new_rdv->setIndicatif($datas[0]['indicatif']);
  112.             if ($datas[0]['email'] != null) {
  113.                 $new_rdv->setEmail($datas[0]['email']);
  114.             }
  115.             $new_rdv->setObject($datas[0]['objet']);
  116.             $new_rdv->setDate(new \DateTime($datas[0]['date']['date']));
  117.             $new_rdv->setDateNaissance(new \DateTime($datas[0]['date_naissance']['date']));
  118.             $new_rdv->setMoment($datas[0]['moment']);
  119.             $new_rdv->setLang($datas[0]['lang']);
  120.             $new_rdv->setAlreadyCustomer($datas[0]['client'] == "Non" 1);
  121.             $new_rdv->setComment($datas[0]['comment'] ?: '');
  122.             $new_rdv->setNumDemande($datas[0]['numDemande'] ?: '');
  123.             $new_rdv->setPrenomProche($datas[0]['prenom_proche']);
  124.             $new_rdv->setNomProche($datas[0]['nom_proche']);
  125.             $new_rdv->setDateNaissanceProche(($datas[0]['date_naissance_proche']) ? new \DateTime($datas[0]['date_naissance_proche']['date']) : null);
  126.             $new_rdv->setIsForMe((int)$datas[0]['rdvPourMoi']);
  127.             $new_rdv->setRepresentantLegals($datas[0]['representant_legals']);
  128.             $this->entityManager->persist($new_rdv);
  129.             $this->entityManager->flush();
  130.             return new JsonResponse(['status' => 200'datas' => $datas]);
  131.         } else {
  132.             return new JsonResponse(['status' => 400'error' => "Aucune donnée envoyée"]);
  133.         }
  134.     }
  135.     #[Route(path'/pushNotif'name'pushnotif')]
  136.     public function pushNotificationAction(Request $request): JsonResponse
  137.     {
  138.         $datas json_decode($request->getContent(), true);
  139.         if ($datas) {
  140.             $this->notificationManager->popNotification(
  141.                 $datas[0]['codeAdh'],
  142.                 strtolower($datas[0]['langue']),
  143.                 'trafic_magasin',
  144.                 'trafic_magasin_new_rdv',
  145.                 ((strtolower($datas[0]['langue']) == "fr") ? 'Mon trafic magasin' 'Mijn winkelverkeer'),
  146.                 $this->generateUrl('trafic_index'),
  147.                 $datas[0]['objet'],
  148.                 true
  149.             );
  150.             return new JsonResponse(['status' => 200'datas' => $datas]);
  151.         } else {
  152.             return new JsonResponse(['status' => 400'error' => "Aucune donnée envoyée"]);
  153.         }
  154.     }
  155.     #[Route(path'/send-sms'name'send_sms')]
  156.     public function sendSms(Request $requestSmsApiManager $smsApiManager)
  157.     {
  158.         if ($request->isMethod("POST")) {
  159.             $errors = [];
  160.             if (!$request->request->has('codeAdherent'))     $errors["errors"][] = "codeAdherent not found";
  161.             if ($request->request->has('deliveryTime'))       $request->request->set('deliveryTime', new \DateTime($request->request->get('deliveryTime')));
  162.             if (!$request->request->has('destinationAddress')) $errors["errors"][] = "Field destinationAddress not found !";
  163.             if (!$request->request->has('messageText'))       $errors["errors"][] = "Field messageText not found !";
  164.             if (!$request->request->has('campaignName'))      $errors["errors"][] = "Field campaignName not found !";
  165.             if (!$request->request->has('originatingAddress')) $errors["errors"][] = "Field originatingAddress not found !";
  166.             if (empty($errors)) {
  167.                 if ($request->request->get('destinationAddress') == "false" && $request->request->get('searchPhone') == "true") {
  168.                     $phoneNumber $this->entityManager->getRepository(MagasinContact::class)->getSpecificContact($request->request->get('codeAdherent'), 'rdv_sms');
  169.                     $phoneNumber $smsApiManager->getFormatSmsNumber($phoneNumber);
  170.                     if ($phoneNumber) {
  171.                         $request->request->set('destinationAddress'$phoneNumber);
  172.                     } else {
  173.                         $errors["errors"][] = "Invalid phone number or not found";
  174.                         return new JsonResponse($errors);
  175.                     }
  176.                 }
  177.                 if ($smsApiManager->canISendSms($request->request->get('codeAdherent'))) {
  178.                     $notifReport = new NotifReport();
  179.                     $notifReport->setKAdherent($request->request->get('codeAdherent'));
  180.                     $notifReport->setKPays((substr(strtoupper($request->request->get('codeAdherent')), 01) == "B") ? "BE" 'FR');
  181.                     $notifReport->setType("SMS");
  182.                     $notifReport->setDeliveryDate(new \DateTime());
  183.                     $notifReport->setDestination($request->request->get('destinationAddress'));
  184.                     $notifReport->setMessageText($request->request->get('messageText'));
  185.                     $notifReport->setCampaign($request->request->get('campaignName'));
  186.                     $this->entityManager->persist($notifReport);
  187.                     $this->entityManager->flush();
  188.                     $rep $smsApiManager->sendSms(
  189.                         $request->request->get('destinationAddress'),
  190.                         $request->request->get('messageText'),
  191.                         $request->request->get('campaignName'),
  192.                         $request->request->get('originatingAddress'),
  193.                         ($request->request->has('deliveryTime')) ? $request->request->get('deliveryTime') : null
  194.                     );
  195.                     return new JsonResponse(json_decode($rep));
  196.                 } else {
  197.                     $errors["errors"][] = "Impossible d'envoyer le sms ce jour.";
  198.                     return new JsonResponse($errors);
  199.                 }
  200.             } else {
  201.                 return new JsonResponse($errors);
  202.             }
  203.         }
  204.     }
  205.     #[Route(path'/send-sms-client'name'send_sms_client')]
  206.     public function sendSmsClient(Request $requestSmsApiManager $smsApiManager)
  207.     {
  208.         if ($request->isMethod("POST")) {
  209.             $errors = [];
  210.             if ($request->request->has('deliveryTime'))        $request->request->set('deliveryTime', new \DateTime($request->request->get('deliveryTime')));
  211.             if (!$request->request->has('destinationAddress')) $errors["errors"][] = "Field destinationAddress not found !";
  212.             if (!$request->request->has('messageText'))        $errors["errors"][] = "Field messageText not found !";
  213.             if (!$request->request->has('campaignName'))       $errors["errors"][] = "Field campaignName not found !";
  214.             if (!$request->request->has('originatingAddress')) $errors["errors"][] = "Field originatingAddress not found !";
  215.             if (!$request->request->has('phone'))              $errors["errors"][] = "Phone number not found !";
  216.             if (empty($errors)) {
  217.                 if ($request->request->get('destinationAddress') == "false" && $request->request->get('searchPhone') == "true") {
  218.                     $phoneNumber          $request->request->get('phone');
  219.                     $formattedPhoneNumber $smsApiManager->getFormatSmsNumberClient($phoneNumber$request->request->get('lang'));
  220.                     if ($formattedPhoneNumber) {
  221.                         $request->request->set('destinationAddress'$formattedPhoneNumber);
  222.                     } else {
  223.                         $errors["errors"][] = "Invalid phone number or not found";
  224.                         return new JsonResponse($errors);
  225.                     }
  226.                 }
  227.                 $rep $smsApiManager->sendSms(
  228.                     $request->request->get('destinationAddress'),
  229.                     $request->request->get('messageText'),
  230.                     $request->request->get('campaignName'),
  231.                     $request->request->get('originatingAddress'),
  232.                 );
  233.                 return new JsonResponse(json_decode($rep));
  234.             } else {
  235.                 return new JsonResponse($errors);
  236.             }
  237.         }
  238.     }
  239.     /**
  240.      * Appelé par le Portail Conviction pour afficher la liste des motifs RDV.
  241.      * En cas d'échec, OPC utilise sa propre rdv_motif_list en backup.
  242.      */
  243.     #[Route(path'/get-motifs-ordered/{code}'name'get_motifs_ordered')]
  244.     public function getMotifsOrdered(
  245.         string $code,
  246.         Request $request,
  247.         RdvMotifCustomRepository  $customRepo,
  248.         RdvMotifDefaultRepository $defaultRepo,
  249.     ): JsonResponse {
  250.         $langue strtoupper($request->getLocale());
  251.         $audition $this->entityManager
  252.                 ->getRepository(OpcSpecialite::class)
  253.                 ->findOneBy(['kAdh' => $code'idSpecialite' => 3]) !== null;
  254.         $row $customRepo->findByAdherent($code);
  255.         // L'adhérent a des données personnalisées -> on les retourne directement
  256.         if ($row && $row->getData() !== null) {
  257.             $motifs = [];
  258.             foreach ($row->getSortedData() as $entry) {
  259.                 if (!($entry['active'] ?? 1)) continue;
  260.                 $name RdvMotifCustom::resolveName($entry$langue);
  261.                 if (str_contains(strtolower($name), 'audition') && !$audition) continue;
  262.                 $motifs[] = ['type' => $entry['type'], 'id' => $entry['id'], 'name' => $name];
  263.             }
  264.             return new JsonResponse(['status' => 200'motifs' => $motifs]);
  265.         }
  266.         $defaults $defaultRepo->findAllActive();
  267.         $motifs   = [];
  268.         foreach ($defaults as $m) {
  269.             $name $m->getName($langue);
  270.             if (str_contains(strtolower($name), 'audition') && !$audition) continue;
  271.             $motifs[] = ['type' => 'default''id' => $m->getId(), 'name' => $name];
  272.         }
  273.         return new JsonResponse(['status' => 200'motifs' => $motifs]);
  274.     }
  275. }