src/Flexy/FrontBundle/Themes/Lasalle/Controller/Api/ApiController.php line 202

  1. <?php
  2. namespace App\Flexy\FrontBundle\Themes\Lasalle\Controller\Api;
  3. use App\Flexy\ProductBundle\Repository\ProductRepository;
  4. use App\Flexy\ShopBundle\Entity\Customer\Customer;
  5. use App\Flexy\ShopBundle\Entity\Customer\Visit;
  6. use App\Flexy\ShopBundle\Entity\Order\Order;
  7. use App\Flexy\ShopBundle\Repository\Order\OrderRepository;
  8. use App\Flexy\ShopBundle\Entity\Product\ProductShop;
  9. use App\Flexy\ShopBundle\Entity\Store\Store;
  10. use App\Flexy\ShopBundle\Repository\Customer\CustomerRepository;
  11. use App\Flexy\ShopBundle\Repository\Shipping\ShippingMethodRepository;
  12. use App\Service\FlexySettingsProvider;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Exception;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use Symfony\Component\Mercure\HubInterface;
  21. use Symfony\Component\Mercure\Update;
  22. #[Route('/api')]
  23. class ApiController extends AbstractController
  24. {
  25.     
  26.     #[Route('/check-customer-account'name'api_check_customer_account',methods: ['POST'])]
  27.     public function api_check_customer_account(Request $request ,ManagerRegistry $doctrine,   
  28.     CustomerRepository $customerRepository,HubInterface $hub,FlexySettingsProvider $flexySettingsProvider,
  29.     OrderRepository $orderRepository)
  30.     {
  31.         //dd($customer);
  32.         if ($request->getContentType() == 'json' and $request->getContent()) {
  33.             $contentJsonRequest json_decode($request->getContent(),true512JSON_THROW_ON_ERROR);
  34.             if($contentJsonRequest){
  35.                 $request->request->replace($contentJsonRequest);
  36.             }else{
  37.                 return $this->json([
  38.                     "status"=>"error","message"=>"Content is not well formated"
  39.                 ]);
  40.             }
  41.             
  42.         }
  43.         $reference =  $request->request->get("reference");
  44.         $em $doctrine->getManager();
  45.         $currentDateTime = new \DateTime();
  46.         $query $em->createQuery(
  47.             'SELECT customer
  48.             FROM App\Flexy\ShopBundle\Entity\Customer\Customer customer
  49.             
  50.             LEFT JOIN customer.productSubscriptions  productSubscriptions
  51.             WHERE customer.reference = :reference AND (customer.is_actif  IS NULL OR customer.is_actif = 0)
  52.             AND :currentDateTime BETWEEN productSubscriptions.startAt AND productSubscriptions.endAt
  53.             '
  54.         )
  55.         
  56.         ->setParameter('reference'$reference)
  57.         ->setParameter('currentDateTime'$currentDateTime);
  58.     
  59.         $customer $query->getOneOrNullResult();
  60.        
  61.         
  62.          $content $request->getContent();
  63.         
  64.         
  65.          $url $request->getUri();
  66.          
  67.           
  68.           $status = (boolean)$customer;
  69.           if( $status == 1) {
  70.             $value  "true";
  71.             
  72.             $customer $customerRepository->findOneBy(["reference"=> $reference]);
  73.              
  74.             $customer_id $customer->getId();
  75.        
  76.              
  77.             $order $orderRepository->findOneBy(["customer"=> $customer_id]);
  78.             
  79.           
  80.              
  81.             // Vérifiez si l'ordre est "checked"
  82.             if ($order && $order->getIsChecked()) {
  83.                 // Récupérez l'heure actuelle
  84.                 $currentHour = (new \DateTime())->format('H:i');
  85.                  
  86.                 // Vérifiez si l'heure est entre 07:00 et 13:30
  87.                 if ($currentHour >= '07:00' && $currentHour <= '13:30') {
  88.                     $value "true";
  89.                 
  90.                     
  91.                 } else {
  92.                     $value "false";
  93.                      
  94.                 }
  95.             }
  96.            
  97.             //dd($value);
  98.             
  99.               // Créer l'entité Visit seulement si $value est "true"
  100.             if ($value === "true") {
  101.             $visit = new Visit();
  102.             $entityManager =  $doctrine->getManager();
  103.             $visit->setCustomer($customer);
  104.             $visit->setCreatedAt(\DateTime::createFromFormat('Y-m-d H:i:s',date('Y-m-d H:i:s')));
  105.             if($customer->getStore()){
  106.                $visit->setStore($customer->getStore()); 
  107.             }
  108.             
  109.             $visit->setCardReference($reference);
  110.             $entityManager->persist($visit);
  111.             $entityManager->flush();
  112.         }
  113.             // Publication Mercure selon la valeur de $value
  114.     if ($flexySettingsProvider->get()->isIsMercureEnabled()) {
  115.         $status = ($value === "true") ? "success" "failed";
  116.         $update = new Update(
  117.             'https://hello.com/books/1',
  118.             json_encode([
  119.                 'status' => $status,
  120.                 'entity' => 'VisitCustomer',
  121.                 "customerId" => $customer_id,
  122.                 'data' => $value === "true" $customer null// uniquement ajouter data si success
  123.                 
  124.             ])
  125.         );
  126.         $hub->publish($update);
  127.     }
  128.             /*if($flexySettingsProvider->get()->isIsMercureEnabled()){
  129.                 $update = new Update(
  130.                     'https://hello.com/books/1',
  131.                     json_encode(
  132.                         [
  133.                             'status' => 'success',
  134.                             'entity'=>'VisitCustomer',
  135.                             "customerId"=>$customer_id,
  136.                             'data'=>$customer,
  137.                         ]
  138.                         )
  139.                 );
  140.                 $hub->publish($update);
  141.                
  142.             }*/
  143.     
  144.           
  145.           }else{
  146.             if($flexySettingsProvider->get()->isIsMercureEnabled()){
  147.                 $update = new Update(
  148.                     'https://hello.com/books/1',
  149.                     json_encode(
  150.                         [
  151.                             'status' => 'failed',
  152.                             'entity'=>'VisitCustomer',
  153.                         ]
  154.                         )
  155.                 );
  156.                 $hub->publish($update);
  157.                
  158.             }
  159.              $value  "false"
  160.           }
  161.         
  162.           //dd($customer[0]->getStore()->getName()); 
  163.          return new Response($value);
  164.      
  165.         
  166.         // Logic
  167.         /*return $this->json([
  168.             "status"=>(boolean)$customer
  169.         ]);*/
  170.         
  171.     }
  172.     #[Route('/visit-customer'name'visit-customer')]
  173.     public function visitCustomer(
  174.         CustomerRepository $customerRepository,
  175.         OrderRepository $orderRepository,
  176.         ManagerRegistry $doctrine
  177.         Request $request 
  178.         ): Response
  179.     {
  180.         
  181.         $timeStatus='';
  182.         
  183.         $customer $customerRepository->find((int)$request->query->get("customerId"));
  184.         $order $orderRepository->findOneBy(["customer"=> $customer]);
  185.     
  186.          if($order){
  187.             $timeStatus $order->getisChecked();
  188.          }
  189.          else{
  190.             $timeStatus false;
  191.         }
  192.           
  193.        // $customer = $doctrine->getManager()->getRepository(Customer::class)->findOneBy(["reference"=>7890222]);
  194.         //dd($customer[0]->getStore());
  195.         
  196.        
  197.         
  198.         return $this->render('@Flexy/FrontBundle/Themes/Lasalle/templates/front/customer/visit.html.twig', [
  199.             'customer' => $customer,
  200.             'timeStatus'=>$timeStatus,
  201.             
  202.         ]);
  203.     }
  204. }