src/Controller/CartController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\SessionService;
  4. use App\Service\CartService;
  5. use App\Service\VoucherService;
  6. use App\Entity\Main\Forfait;
  7. use App\Entity\Main\Price;
  8. use App\Entity\Main\CartoBon;
  9. use App\Entity\Main\PromosPanier;
  10. use App\Entity\Main\Reservation;
  11. use App\Entity\Main\ReservationDetails;
  12. use App\Entity\Main\Calendar;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class CartController extends AbstractController
  19. {
  20.     private $ss;
  21.     private $cart_service;
  22.     private $vs;
  23.     public function __constructSessionService $ssCartService $cart_serviceVoucherService $vs )
  24.     {
  25.         $this->ss $ss;
  26.         $this->cart_service $cart_service;
  27.         $this->vs $vs;
  28.     }
  29.     /**
  30.      * @Route("/panier", name="cart")
  31.      */
  32.     public function showPanier(Request $request): Response
  33.     {
  34.         $em $this->getDoctrine()->getManager();
  35.             // $this->ss->empty(); // force vidage panier
  36.         // dd($request->request->get('reset_cart'));
  37.         if( $request->request->get('reset_cart') == "true" ) {
  38.             $cart = array();
  39.             // $this->ss->update($cart);
  40.             $this->ss->empty();
  41.         } else {
  42.             $cart $this->ss->get();
  43.         }
  44.         // dd($cart);
  45.         $code_promo_response null;
  46.         if( null !== $request->request->get('codepromo') && null !== $request->request->get('totalPanier')  ) {
  47.             $codepromo $request->request->get('codepromo');
  48.             $totalPanier $request->request->get('totalPanier');
  49.             $code_promo_response $this->checkCodePromo($codepromo$totalPanier);
  50.         }
  51.         if($code_promo_response && !$code_promo_response['error']) {
  52.             $cart['code_promo'] = $code_promo_response['codeToCheck'][0];
  53.             $this->ss->update($cart);
  54.         } 
  55.         // dd($cart);
  56.         if( null !== $request->request->get('remove_formule')  ) {
  57.             $remove_formule $request->request->get('remove_formule');
  58.             if (($key array_search($remove_formule$cart['formules'])) !== false) {
  59.                 unset($cart['formules'][$key]);
  60.                 unset($cart['forfaits'][$remove_formule]);
  61.             }
  62.             if(count($cart['formules']) == 0$cart = array();
  63.             $this->ss->update($cart);
  64.         }
  65.         if( null !== $request->request->get('remove_bon_numerique') && $cart['bon_numerique'] ) {
  66.             foreach ($cart['bon_numerique'] as $key => $bon_data) {
  67.                 if($bon_data['bon'] == $request->request->get('remove_bon_numerique')) {
  68.                     if(isset($cart['nb_bon_id_product_shop_'.$cart['bon_numerique'][$key]['_product_id']])) {
  69.                         $cart['nb_bon_id_product_shop_'.$cart['bon_numerique'][$key]['_product_id']] = $cart['nb_bon_id_product_shop_'.$cart['bon_numerique'][$key]['_product_id']] - $cart['bon_numerique'][$key]['qty'];
  70.                     }
  71.                     unset($cart['bon_numerique'][$key]);
  72.                 }
  73.             }
  74.             $this->ss->update($cart);
  75.         }
  76.         // dd($request->request);
  77.         if( $request->request->get('tarifs') ) {
  78.             if(!empty($cart) && isset($cart['forfaits'])) {
  79.                 $forfaits $cart['forfaits'];
  80.             } else {
  81.                 $forfaits = array();
  82.             }
  83.             foreach ($request->request->get('tarifs') as $id_forfait => $array_tarif) {
  84.                 if(!isset($forfait))  {
  85.                     $forfait $em->getRepository(Forfait::class)->findOneBy(['id' => $id_forfait]);
  86.                     $formule $forfait->getFormule();
  87.                 }
  88.                 $tarifs = array();
  89.                 foreach ($array_tarif as $id_tarif => $quantity) {
  90.                     if($quantity != "" && $quantity != "0") {
  91.                         $tarif $em->getRepository(Price::class)->findOneBy(['id' => $id_tarif]);
  92.                         $tarifs[$id_tarif] = $quantity $tarif->getMultiGame();
  93.                     }
  94.                 }
  95.                 if(!isset($forfaits[$formule->getCode()])) $forfaits[$formule->getCode()] = array();
  96.                 if(!empty($tarifs)) $forfaits[$formule->getCode()][$id_forfait] = array('tarifs' => $tarifs);
  97.             }
  98.             if( $request->request->get('options') ) {
  99.                 foreach ($request->request->get('options') as $id_forfait => $array_option) {
  100.                     $options = array();
  101.                     foreach ($array_option as $id_option => $quantity) {
  102.                         if($quantity != "" && $quantity != "0") {
  103.                             $options[$id_option] = $quantity;
  104.                         }
  105.                     }
  106.                     if(!isset($forfaits[$formule->getCode()])) $forfaits[$formule->getCode()] = array();
  107.                     if(!empty($options) && isset($forfaits[$formule->getCode()][$id_forfait]['tarifs'])) $forfaits[$formule->getCode()][$id_forfait]['options'] = $options;
  108.                 }
  109.             }
  110.             
  111.             if( $request->request->get('creneaux') ) {
  112.                 $creneaux $request->request->get('creneaux');
  113.                 // dd($creneaux,$forfaits[$formule->getCode()][$id_forfait]);
  114.                 foreach ($creneaux as $id_forfait => $array_creneau) {
  115.                     foreach ($array_creneau as $id_activity => $creneau) {
  116.                         if($creneau != "" && $creneau != "0") {
  117.                             if(isset($forfaits[$formule->getCode()][$id_forfait]) 
  118.                                 && !empty($forfaits[$formule->getCode()][$id_forfait])
  119.                                 && isset($forfaits[$formule->getCode()][$id_forfait]['tarifs'])) 
  120.                             {
  121.                                 if(isset($forfaits[$formule->getCode()][$id_forfait]['activity']) 
  122.                                     && !empty($forfaits[$formule->getCode()][$id_forfait]['activity'])) 
  123.                                 {
  124.                                     $forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity] = array('creneau' => $creneau);
  125.                                 } else {
  126.                                     $forfaits[$formule->getCode()][$id_forfait]['activity'] = array($id_activity => array('creneau' => $creneau));
  127.                                 }
  128.                             }
  129.                         }
  130.                     }
  131.                 }
  132.                 // dd($creneaux,$forfaits[$formule->getCode()][$id_forfait]['activity']);
  133.             }
  134.             
  135.             if( $request->request->get('participants') ) {
  136.                 $participants $request->request->get('participants');
  137.                 // dd($participants);
  138.                 foreach ($participants as $id_forfait => $array_participant) {
  139.                     foreach ($array_participant as $id_activity => $participant) {
  140.                         if($participant != "" && $participant != "0") {
  141.                             if(isset($forfaits[$formule->getCode()][$id_forfait]) 
  142.                                 && !empty($forfaits[$formule->getCode()][$id_forfait])
  143.                                 && isset($forfaits[$formule->getCode()][$id_forfait]['tarifs'])
  144.                             ) {
  145.                                 if(is_array($participant)) {
  146.                                     $forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity]['participants'] = 0;
  147.                                     foreach ($participant as $key => $value) {
  148.                                         $forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity]['participants'] += $value;
  149.                                     }
  150.                                 } else {
  151.                                     $forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity]['participants'] = $participant;
  152.                                 }
  153.                             }
  154.                         }
  155.                     }
  156.                 }
  157.             }
  158.             
  159.             if( $request->request->get('parties') ) {
  160.                 $parties $request->request->get('parties');
  161.                 // dd($parties);
  162.                 foreach ($parties as $id_forfait => $array_partie) {
  163.                     foreach ($array_partie as $id_activity => $partie) {
  164.                         if($partie != "" && $partie != "0") {
  165.                             if(isset($forfaits[$formule->getCode()][$id_forfait]) && !empty($forfaits[$formule->getCode()][$id_forfait])) {
  166.                                 if(is_array($partie)) {
  167.                                     $forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity]['parties'] = 0;
  168.                                     foreach ($partie as $key => $value) {
  169.                                         if($forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity]['parties'] < $value) {
  170.                                             $forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity]['parties'] = $value;
  171.                                         }
  172.                                     }
  173.                                 } else {
  174.                                     $forfaits[$formule->getCode()][$id_forfait]['activity'][$id_activity]['parties'] = $partie;
  175.                                 }
  176.                             }
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.             $cart['forfaits'] = $forfaits;
  182.             // dd($request->request,$cart);
  183.             $this->ss->update($cart);
  184.         }
  185.         $cart_details $this->cart_service->getCartDetails($cart);
  186.         $alert "";
  187.         if(null !== $request->request->get('birthday_customer_comment')){
  188.             $cart['birthday_customer_comment'] = $request->request->get('birthday_customer_comment');
  189.             $this->ss->update($cart);
  190.         }
  191.         if(null !== $request->request->get('bon_numerique')){
  192.             $bon_numerique $request->request->get('bon_numerique');
  193.             
  194.             if(strpos($bon_numerique,' ') !== false){
  195.                 $alert 'Vérifiez la conformité du code saisi, un espace a été trouvé.';
  196.                 $data = array();
  197.             } else {
  198.                 $data $this->vs->getVoucher($bon_numerique);
  199.             }
  200.             
  201.             if(!empty($data)) {
  202.                 $date_resa = \DateTime::createFromFormat('d-m-Y H:i',$cart['date'].' 00:00');
  203.                 if(isset($data['unlimited_voucher'])) {
  204.                     $unlimited_voucher_start = \DateTime::createFromFormat('d/m/Y H:i'$data['unlimited_voucher']['unlimited_voucher_start'].' 00:00');
  205.                     $unlimited_voucher_start->add(new \DateInterval('P1D'));
  206.                     $unlimited_voucher_end = \DateTime::createFromFormat('d/m/Y H:i'$data['unlimited_voucher']['unlimited_voucher_end'].' 00:00');
  207.                     $unlimited_voucher_end->add(new \DateInterval('P1D'));
  208.                 }
  209.                 if(isset($data['limited_voucher'])) {
  210.                     $limited_voucher_start = \DateTime::createFromFormat('d/m/Y H:i'$data['limited_voucher']['unlimited_voucher_start'].' 00:00');
  211.                     $limited_voucher_start->add(new \DateInterval('P1D'));
  212.                     $limited_voucher_end = \DateTime::createFromFormat('d/m/Y H:i'$data['limited_voucher']['unlimited_voucher_end'].' 00:00');
  213.                     $limited_voucher_end->add(new \DateInterval('P1D'));
  214.                 }
  215.                 if ($data['status'] == "wcpdf-redeemed" || $data['_remaining_value'] == 0) {
  216.                     $alert 'Ce bon a été consommé.';
  217.                 } elseif($data['status'] != "wcpdf-active") {
  218.                     $alert 'Ce bon est inactif, contactez la boutique.';
  219.                 } elseif(
  220.                     isset($data['unlimited_voucher']) 
  221.                     && (
  222.                         ($date_resa $unlimited_voucher_start || $date_resa $unlimited_voucher_end
  223.                           || !$this->checkVoucherValid($date_resa,$data['unlimited_voucher'])
  224.                     )
  225.                 ) {
  226.                     if($date_resa $unlimited_voucher_start) {
  227.                         $alert "Ce bon à usage illimité ne sera utilisable qu'à partir du ".$unlimited_voucher_start->format('d/m/Y')." !";
  228.                     }
  229.                     if($date_resa $unlimited_voucher_end) {
  230.                         $alert "Ce bon à usage illimité n'est utilisable que jusqu'au ".$unlimited_voucher_end->format('d/m/Y')." !";
  231.                     }
  232.                     
  233.                     if(!$this->checkVoucherValid($date_resa,$data['unlimited_voucher'])) {
  234.                         $alert "Ce bon à usage illimité n'est pas valable pour ce jour ! Vérifiez les conditions d'utilisation";
  235.                     }
  236.                 } elseif(
  237.                     isset($data['limited_voucher']) 
  238.                     && (
  239.                         ($date_resa $limited_voucher_start || $date_resa $limited_voucher_end
  240.                           || !$this->checkVoucherValid($date_resa,$data['limited_voucher'])
  241.                     )
  242.                 ) {
  243.                     if($date_resa $limited_voucher_start) {
  244.                         $alert "Ce E-ticket ne sera utilisable qu'à partir du ".$limited_voucher_start->format('d/m/Y')." !";
  245.                     }
  246.                     if($date_resa $limited_voucher_end) {
  247.                         $alert "Ce E-ticket n'est utilisable que jusqu'au ".$limited_voucher_end->format('d/m/Y')." !";
  248.                     }
  249.                     
  250.                     if(!$this->checkVoucherValid($date_resa,$data['limited_voucher'])) {
  251.                         $alert "Ce E-ticket n'est pas valable pour ce jour ! Vérifiez les conditions d'utilisation";
  252.                     }
  253.                 } else {
  254.                     $map $em->getRepository(CartoBon::class)->findOneBy(array("idBon" => $data['_product_id']));
  255.                     
  256.             // dd($cart_details,$data,$map);
  257.                     if($map == NULL || $map->getType() == 3) {
  258.                         $alert 'Ce bon ne peut pas être utilisé en ligne.';
  259.                     } else {
  260.                         if($map->getType() == || $map->getType() == 4){
  261.                             // Vérifions si le bon a déja été utilisé pour aujourd'hui
  262.                             $exist_resa_today $em->getRepository(ReservationDetails::class)->findOneByBonNumerique($bon_numerique);
  263.                             $bon_numerique_is_used_today false;
  264.                             if(!empty($exist_resa_today)) {
  265.                                 foreach ($exist_resa_today as $key => $resaDetails) {
  266.                                     $status_resa_today $em->getRepository(Reservation::class)->find$resaDetails->getReservation() );
  267.                                     if($date_resa == $resaDetails->getDate() && $status_resa_today->getActive()) {
  268.                                         $bon_numerique_is_used_today true;
  269.                                     }
  270.                                 }
  271.                             }
  272.                             if($map->getType() == && $bon_numerique_is_used_today) {
  273.                                 $alert "Ce bon n'est utilisable qu'une fois par jour !";
  274.                             } else {
  275.                                 $price = array();
  276.                                 $exact_prices_founded = array();
  277.                                 foreach ($map->getForfait() as $forfait) {
  278.                                     $code_formule $forfait->getFormule()->getCode();
  279.                                     if( isset($cart_details[$code_formule]) ) {
  280.                                         foreach($cart_details[$code_formule]['forfaits'] as $detail){
  281.                                             if($detail['obj_forfait']->getId() == $forfait->getId()){
  282.                                                 foreach($detail['tarifs'] as $tarif){
  283.                                                     for ($i=0$i $tarif['qty']; $i++) { 
  284.                                                         // FIX Multi-tarifs ############
  285.                                                         $prices[] = array(
  286.                                                             'price' => $tarif['obj_tarif']->getPriceTtc(),
  287.                                                             'tax_rate' => $tarif['obj_tarif']->getTauxTva(),
  288.                                                             'id_forfait' => $forfait->getId(),
  289.                                                         );
  290.                                                         // if(($data['_product_price'] + $data['_product_tax']) == $tarif['obj_tarif']->getPriceTtc()) {
  291.                                                         //     $exact_prices_founded[] = array(
  292.                                                         //         'price' => $tarif['obj_tarif']->getPriceTtc(),
  293.                                                         //         'tax_rate' => $tarif['obj_tarif']->getTauxTva(),
  294.                                                         //         'id_forfait' => $forfait->getId(),
  295.                                                         //     );
  296.                                                         // }
  297.                                                         // ############
  298.                                                     }
  299.                                                 }
  300.                                             }
  301.                                         }
  302.                                     }
  303.                                 }
  304.                                 
  305.                                 // dd($prices,$cart,$data['id']);
  306.                                 if(!empty($prices)) {
  307.                                     // if(!empty($exact_prices_founded)) $prices = $exact_prices_founded;
  308.                                     rsort($prices);
  309.                                     if(!isset($data['_product_quantity'])){
  310.                                         $data['_product_quantity'] = round($data['_remaining_value'] / $data['_product_price']);
  311.                                     }
  312.                                     $remises = array();
  313.                                     $total 0;
  314.                                     if($data['_product_quantity']>count($prices)){
  315.                                         $data['_product_quantity'] = count($prices);
  316.                                     }
  317.                                     // dd($cart,$prices,$data['_product_id'],$cart['nb_bon_id_product_shop_'.$data['_product_id']],count($prices));
  318.                                     if(isset($cart['nb_bon_id_product_shop_'.$data['_product_id']]) && $cart['nb_bon_id_product_shop_'.$data['_product_id']] > ) {
  319.                                         $new_prices_list = array();
  320.                                         for ($i=$cart['nb_bon_id_product_shop_'.$data['_product_id']]; $i count($prices); $i++) {
  321.                                             $new_prices_list[] = $prices[$i];
  322.                                         }
  323.                                         $prices $new_prices_list;
  324.                                     }
  325.                                     for ($i=0$i $data['_product_quantity']; $i++) { 
  326.                                         if (isset($remises[(string)$prices[$i]['tax_rate']])) {
  327.                                             $remises[(string)$prices[$i]['tax_rate']] += $prices[$i]['price'];
  328.                                         }else{
  329.                                             $remises[(string)$prices[$i]['tax_rate']] = $prices[$i]['price'];
  330.                                         }
  331.                                         if(!isset($cart['nb_bon_id_product_shop_'.$data['_product_id']])) $cart['nb_bon_id_product_shop_'.$data['_product_id']] = 1;
  332.                                         else $cart['nb_bon_id_product_shop_'.$data['_product_id']]++;
  333.                                         $total += $prices[$i]['price'];
  334.                                     }
  335.                                     $tax 0;
  336.                                     foreach($remises as $i => $remise){
  337.                                         $tax += (float)$i*(float)$remise;
  338.                                     }
  339.                                     if($total == 0) {
  340.                                         $alert 'Une erreur est survenu ! Veuillez réessayer.';
  341.                                     } else {
  342.                                         $cart['bon_numerique'][$data['id']] = array(
  343.                                             'total' => $total,
  344.                                             'qty' => $data['_product_quantity'],
  345.                                             'tax' => $tax,
  346.                                             'bon' => $bon_numerique,
  347.                                             '_product_id' => $data['_product_id'],
  348.                                         );
  349.                                     }
  350.                                     // dd($cart,$prices,$data['_product_id']);
  351.                                 } else {
  352.                                     $alert 'La valeur du E-ticket ne correspond à aucun tarif.';
  353.                                 }
  354.                             }
  355.                         } elseif($map->getType() == 1) {
  356.                             $cart['bon_numerique'][$data['id']] = array(
  357.                                 'total' => $data['ttc'] * $data['_product_quantity'],
  358.                                 'qty' => $data['_product_quantity'],
  359.                                 'bon_total' => $data['_product_price'],
  360.                                 'bon' => $bon_numerique
  361.                             );
  362.                         }
  363.                         $this->ss->update($cart);
  364.                     }
  365.                 }
  366.             } elseif(strpos($bon_numerique,' ') === false) {
  367.                 $alert 'Vérifiez la conformité du code saisi, auccun bon valide n\'a été trouvé.';
  368.             }
  369.             // dd($data);
  370.         }
  371.         // dd($cart,$cart_details);
  372.         return $this->render('etapes/panier.html.twig', [
  373.             'code_promo_response' => $code_promo_response,
  374.             'cart' => $cart,
  375.             'alert_voucher' => $alert,
  376.             'cart_details' => $cart_details,
  377.             'controller_name' => 'CartController',
  378.             'page_name' => 'page_panier',
  379.         ]);
  380.     }
  381.     public function checkCodePromo($codepromo$montantTTC)
  382.     {
  383.         /* POST Method */
  384.         // $data = json_decode($request->request->get('data'));
  385.         $error false;
  386.         $data null;
  387.         
  388.         // if(md5($this->pk) != $request->request->get('key')) exit();
  389.         $dateTimeZone = new \DateTimeZone('Europe/Paris'); 
  390.         $dateAchat = new \DateTime('now'$dateTimeZone);
  391.         $formatter = new \IntlDateFormatter('fr_FR'NULLNULL$dateTimeZoneNULL'EEEE');
  392.         $dayName $formatter->format($dateAchat);
  393.         // On récupère les infos du client
  394.         $em $this->getDoctrine()->getManager();
  395.         $codeToCheck $em->getRepository(PromosPanier::class)->checkCode($codepromo$dateAchat$dayName);
  396.         if (!$codeToCheck) {
  397.             $error 'E.1: Le code utilisé n\'est pas valide';
  398.             // return new JsonResponse(['error' => $error], JsonResponse::HTTP_CREATED);
  399.         } else if($codeToCheck[0][0] instanceof PromosPanier) {
  400.             // dd($codeToCheck);
  401.             if($montantTTC >= $codeToCheck[0][0]->getMontantMinimun()) {
  402.                 $data $codeToCheck[0];
  403.             } else {
  404.                 $error 'E.2: Le code utilisé n\'est pas valide';
  405.             }
  406.         } else $error 'E.3: Le code utilisé n\'est pas valide';
  407.         
  408.         return array('error' => $error'codeToCheck' => $data);
  409.     }
  410.     private function checkVoucherValid$dateResa$unlimitedVoucherDatas ) {
  411.         $isValid false;
  412.         $em $this->getDoctrine()->getManager();
  413.         $dateTypes $em->getRepository(Calendar::class)->findTypeByDate($dateResa);
  414.         $formatter = new \IntlDateFormatter('fr_FR'NULLNULLNULLNULL'EEEE');
  415.         $dayName $formatter->format($dateResa);
  416.         if(in_array($dayName$unlimitedVoucherDatas['day_enabled'])) $isValid true;
  417.         if($dateTypes) {
  418.             foreach ($dateTypes as $key => $dateType) {
  419.                 if(!in_array($dateType['type'], $unlimitedVoucherDatas['day_enabled'])
  420.                     && $dateType['type'] != 'indisponible' && $dateType['activityClosed'] == null
  421.                 {
  422.                     $isValid false;
  423.                 } 
  424.             }
  425.         }
  426.         // dd($dateTypes,$dayName,$unlimitedVoucherDatas,$isValid);
  427.         return $isValid;
  428.     }
  429.     /**
  430.      * @Route("/vider_panier", name="resetcart")
  431.      */
  432.     public function resetCart(Request $request): Response
  433.     {
  434.         return $this->redirectToRoute('cart');
  435.     }
  436. }