src/Controller/CartController.php line 40

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