src/Form/Extension/BitbagCheckoutExtension.php line 283

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\Extension;
  4. use BitBag\SyliusOnePageCheckoutPlugin\Form\Type\CheckoutType;
  5. use Sylius\Component\Resource\Repository\RepositoryInterface;
  6. use Symfony\Component\Form\AbstractTypeExtension;
  7. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Form\FormError;
  12. use App\Entity\Order\Order;
  13. use Sylius\Component\Shipping\Repository\ShippingMethodRepositoryInterface;
  14. use App\Services\PPMCErpSynchroService;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\Process\Process;
  17. use Symfony\Component\Process\Exception\ProcessTimedOutException;
  18. use Sylius\Component\Core\Model\AdjustmentInterface;
  19. final class BitbagCheckoutExtension extends AbstractTypeExtension
  20. {
  21.     private $shippingMethodRepository;
  22.     private $ppmcErpSynchroService;
  23.     private $container;
  24.     public function __construct(ShippingMethodRepositoryInterface $shippingMethodRepositoryPPMCErpSynchroService $ppmcErpSynchroServiceContainerInterface $container)
  25.     {
  26.         $this->shippingMethodRepository $shippingMethodRepository;
  27.         $this->ppmcErpSynchroService $ppmcErpSynchroService;
  28.         $this->container $container;
  29.     }
  30.     public function buildForm(FormBuilderInterface $builder, array $options): void
  31.     {        
  32.         $builder
  33.             ->add('orderPickupSoColissimo'HiddenType::class, [
  34.                 'required' => false,
  35.             ])
  36.             ->add('orderPickupShop'HiddenType::class, [
  37.                 'required' => false
  38.             ])
  39.             // ->addEventListener(FormEvents::, function (FormEvent $event) use ($options) {
  40.             //     $data = $event->getData();
  41.             //     dump($data);z
  42.             //     // dump($data->getCustomer());
  43.             //     // dump($data->getCustomer()->getDefaultAddress());
  44.             //     // dump($data->getCustomer()->getDefaultAddress()->getCountryCode());
  45.             //     // dump($data->getShippingAddress());
  46.             //     // dump($data->getShippingAddress()->getCountryCode());
  47.             //     // foreach($data->getCustomer()->getAddresses() as $ad){
  48.             //     //     dump($ad);
  49.             //     // }
  50.             //     // dump($data->getCustomer()->getShippingAddress());
  51.             //     // dump($data->getCustomer());
  52.             
  53.             
  54.             // })
  55.             ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
  56.                 $data $event->getData();
  57.                 $form $event->getForm();
  58.                 // dump($form);
  59.                 dump($data);
  60.                 // exit();
  61.                 // echo 'icic';
  62.                 //Soumission complète ?
  63.                 if (isset($data['select_shipping'], 
  64.                 $data['select_shipping']['shipments'],
  65.                 $data['address'])) {
  66.                     //on envoie le formulaire complet, on check donc
  67.                     
  68.                     if(isset($data['select_payment'],
  69.                     $data['select_payment']['payments'])) {
  70.                         //on ne peut pas payer par ici avec la carte cadeau
  71.                         if($data['select_payment']['payments'][0]['method'] == 'KDO'){
  72.                             $form->addError(new FormError(
  73.                                 "Vous ne pouvez pas payer en complément avec une carte cadeau"
  74.                             ));
  75.                         }
  76.                     }
  77.                     $cart $form->getNormData();
  78.                     
  79.                     //On fait une valo de check final
  80.                     $previousTotal $cart->getTotal();
  81.                     $process = new Process(['/usr/bin/php'$this->container->get('kernel')->getProjectDir().'/bin/console',  'ppmc:valorize-cart''--cartId='.$cart->getId()]);
  82.                     $process->setTimeout(3);
  83.                     try {
  84.         
  85.                         $process->start();
  86.                         $process->wait();
  87.                         // echo $process->getOutput();
  88.                         $em $this->container->get('doctrine.orm.entity_manager');
  89.                         $em->refresh($cart);
  90.             
  91.                         $cartItems $cart->getItems();
  92.                         foreach ($cartItems as $cartRow) {
  93.                             $em->refresh($cartRow);
  94.             
  95.                         }
  96.                     } catch (ProcessTimedOutException $exception) {
  97.                     }
  98.                     $currentTotal $cart->getTotal();
  99.                     if($previousTotal != $currentTotal) {
  100.                         //La valorisation a évoluée, donc on a une erreur
  101.                         //Promo plus valable par exemple etc...
  102.                         $form->addError(new FormError(
  103.                             "Votre panier a évolué, veuillez le vérifier avant de continuer. "
  104.                         ));
  105.                     }
  106.                 
  107.                     //On vérifie la carte cadeau si on essai d'en utiliser une
  108.                     //On doit forcer la recharge des paiements car pas loadé dans l'objet
  109.                     $payments $cart->getPayments();
  110.                     $adjustments $cart->getAdjustments();
  111.                     $this->container->get('doctrine')->getManager()->initializeObject($payments);
  112.                     $this->container->get('doctrine')->getManager()->initializeObject($adjustments);
  113.                     foreach($payments as $payment) {
  114.                         $details $payment->getDetails();
  115.                         if(array_key_exists('giftCardCode'$details)) {
  116.                             //On vérifie sa validité
  117.                             $giftCard $this->ppmcErpSynchroService->checkGiftCard(
  118.                                 $details['giftCardCode'],
  119.                                 $details['giftCardKey']
  120.                             );
  121.                             if($giftCard->getAmount()) {
  122.                                 $giftAmout = (int)($giftCard->getAmount()*100);
  123.                                 if($giftAmout $payment->getAmount()) {
  124.                                     $form->addError(new FormError(
  125.                                         "Le montant de votre carte cadeau ne correspond pas au montant à payer, veuillez rééssayer.. "
  126.                                     ));
  127.                                     $cart->removePayment($payment);
  128.                                     foreach($adjustments as $adjustement) {
  129.                                         $details $adjustement->getDetails();
  130.                                         if($adjustement->getType() == AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT && array_key_exists('giftCardCode'$details)) {
  131.                                             $adjustement->unlock();
  132.                                             $cart->removeAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT);
  133.                                         }
  134.                                     }
  135.                                 }
  136.                             }
  137.                             else {
  138.                                 $form->addError(new FormError(
  139.                                     "Votre carte cadeau n'est plus valide. "
  140.                                 ));
  141.                                 $cart->removePayment($payment);
  142.                                 foreach($adjustments as $adjustement) {
  143.                                     $details $adjustement->getDetails();
  144.                                     if($adjustement->getType() == AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT && array_key_exists('giftCardCode'$details)) {
  145.                                         $adjustement->unlock();
  146.                                         $cart->removeAdjustments(AdjustmentInterface::ORDER_PROMOTION_ADJUSTMENT);
  147.                                     }
  148.                                 }
  149.                             }
  150.                         }
  151.                     }
  152.                     //On vérifie les ajustements au cas où (multi ajustements livraison notamment)
  153.                     $hasShippingAdjustment false;
  154.                     foreach($adjustments as $adjustment) {
  155.                         if($adjustment->getType() == AdjustmentInterface::SHIPPING_ADJUSTMENT) {
  156.                             if($hasShippingAdjustment) {
  157.                                 $adjustement->unlock();
  158.                                 $cart->removeAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT);
  159.                             }
  160.                             $hasShippingAdjustment true;
  161.                         }
  162.                     }
  163.                     $cartManager $this->container->get('sylius.manager.order');
  164.                     $cartManager->persist($cart);
  165.                     $cartManager->flush();
  166.                 }
  167.                 if (isset($data['select_shipping'], $data['select_shipping']['shipments'])) {
  168.                     if (count($data['select_shipping']['shipments']) > 0) {
  169.                         // dump($data);
  170.                         // dump($event);
  171.                         if(isset( $data['select_shipping']['shipments'][1])){
  172.                             $shippingMethod $this->shippingMethodRepository->findOneBy([
  173.                                 'code' => $data['select_shipping']['shipments'][1]['method']
  174.                             ]);
  175.                         }else{
  176.                             $shippingMethod $this->shippingMethodRepository->findOneBy([
  177.                                 'code' => $data['select_shipping']['shipments'][0]['method']
  178.                             ]);
  179.                         }
  180.                         $cart $form->getNormData();
  181.                         dump($cart);
  182.                         
  183.                         if ($shippingMethod->getPickup() == 'so_colissimo') {
  184.                             // Si c'est un point retrait type "so colissimo"
  185.                             // Alors on vérifie que "orderPickupSoColissimo" n'est pas vide
  186.                             if (isset($data['orderPickupSoColissimo']) && empty($data['orderPickupSoColissimo'])) {
  187.                                 $form->addError(new FormError(
  188.                                     "Vous devez selectionner un point retrait So Colissimo. "
  189.                                     "Cliquez sur le mode de livraison pour selectionner un point retrait."
  190.                                 ));                                
  191.                             }
  192.                             if(isset($data['address']['shippingAddress'])){
  193.                                 $firstName $cart->getShippingAddress()->getFirstName();
  194.                                 $lastName $cart->getShippingAddress()->getLastName();
  195.                                 $phoneNumber $cart->getShippingAddress()->getPhoneNumber();
  196.                                 if(empty($firstName) || empty($lastName) || empty($phoneNumber) ){
  197.                                     $form->addError(new FormError(
  198.                                         "Vous devez renseigner les coordonnées de livraison. "                                   
  199.                                     ));
  200.                                 }
  201.                             }
  202.                             // dump($data);
  203.                         } else if ($shippingMethod->getPickup() == 'shop') {
  204.                             // Si c'est un point retrait type "retrait boutique"
  205.                             // Alors on vérifie que "orderPickupShop" n'est pas vide
  206.                             if (isset($data['orderPickupShop']) && empty($data['orderPickupShop'])) {
  207.                                 $form->addError(new FormError(
  208.                                     "Vous devez selectionner une boutique pour retirer vos articles. "
  209.                                     "Cliquez sur le mode de livraison pour selectionner une boutique."
  210.                                 ));
  211.                             }
  212.                             if(isset($data['address']['shippingAddress'])){
  213.                                 $firstName $cart->getShippingAddress()->getFirstName();
  214.                                 $lastName $cart->getShippingAddress()->getLastName();
  215.                                 $phoneNumber $cart->getShippingAddress()->getPhoneNumber();
  216.                                 if(empty($firstName) || empty($lastName) || empty($phoneNumber) ){
  217.                                     $form->addError(new FormError(
  218.                                         "Vous devez renseigner les coordonnées de livraison. "                                   
  219.                                     ));
  220.                                 }
  221.                             }
  222.                             // dump($data);
  223.                             
  224.                         } else if ($shippingMethod->getPickup() == 'apel') {
  225.                             $cart $form->getNormData();
  226.                             // Si c'est un point retrait type "APEL"
  227.                             // Alors on vérifie que "orderPickupShop" n'est pas vide
  228.                             if (empty($cart->getApel())) {
  229.                                 $form->addError(new FormError(
  230.                                     "Vous devez selectionner une école pour retirer vos articles. "
  231.                                     "Cliquez sur le mode de livraison pour selectionner une école."
  232.                                 ));
  233.                             }
  234.                             if(isset($data['address']['shippingAddress'])){
  235.                                 $firstName $cart->getShippingAddress()->getFirstName();
  236.                                 $lastName $cart->getShippingAddress()->getLastName();
  237.                                 $phoneNumber $cart->getShippingAddress()->getPhoneNumber();
  238.                                 if(empty($firstName) || empty($lastName) || empty($phoneNumber) ){
  239.                                     $form->addError(new FormError(
  240.                                         "Vous devez renseigner les coordonnées de livraison. "                                   
  241.                                     ));
  242.                                 }
  243.                             }
  244.                             // dump($data);
  245.                             
  246.                         } else {
  247.                             // Sinon on vide les champs
  248.                             $data['orderPickupSoColissimo'] = '';
  249.                             $data['orderPickupShop'] = '';
  250.                             $event->setData($data);
  251.                         }
  252.                     }
  253.                 }
  254.                 else {
  255.                     $data['orderPickupSoColissimo'] = $form->getNormData()->getOrderPickupSoColissimo();
  256.                     $data['orderPickupShop'] = $form->getNormData()->getOrderPickupShop();
  257.                     $event->setData($data);
  258.                 }
  259.                 dump($form);
  260.                 // exit();
  261.             })
  262.             ->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) use ($options) {
  263.                 dump($event);
  264.             })
  265.             // ->addEventListener(FormEvents::PRE_SET_DATA , function (FormEvent $event) use ($options) {
  266.             //     $data = $event->getData();
  267.             //     // $form = $event->getForm();
  268.                 
  269.             //     dump($data);
  270.             //     if(empty($data->getShippingAddress())){
  271.                     
  272.             //         $addresses = $data->getCustomer()->getAddresses();
  273.                     
  274.             //         if(count($addresses) > 0){
  275.             //             foreach($addresses as $address){
  276.                             
  277.             //                 // $newAddress = $this->container->get('sylius.factory.address')->createNew();
  278.             //                 // $newAddress->setFirstName($address->getFirstName());
  279.             //                 // $newAddress->setLastName($address->getLastName());
  280.             //                 // $newAddress->setCountryCode($address->getCountryCode());
  281.             //                 // $newAddress->setCity($address->getCity());
  282.             //                 // $newAddress->setStreet($address->getStreet());
  283.             //                 // $newAddress->setPostcode($address->getPostcode());
  284.             //                 // $newAddress->setPhoneNumber($address->getPhoneNumber());
  285.             //                 // $data->getCustomer()->addAddress($newAddress);
  286.             //                 $newAddress = clone $address;
  287.             //                 $newAddress->setCustomer($data->getCustomer());
  288.             //                 dump($newAddress);
  289.             //                 $this->container->get('doctrine')->getManager()->initializeObject($newAddress);
  290.             //                 $data->setBillingAddress($newAddress);
  291.             //                 $data->setShippingAddress($newAddress);
  292.             //                 $cartManager = $this->container->get('sylius.manager.order');
  293.             //                 $cartManager->persist($data);
  294.             //                 $cartManager->flush();
  295.             //                 $event->setData($data);
  296.                             
  297.             //                 dump($data);
  298.             //                 break;
  299.         
  300.             //             }
  301.             //         }
  302.             //     }else{
  303.             //         // $address = $data->getShippingAddress();
  304.             //         // dump($address);
  305.             //         // foreach($data->getCustomer()->getAddresses() as $customerAddress){
  306.             //         //     if($customerAddress->getId() == $address->getId()){
  307.                             
  308.             //         //         $this->container->get('doctrine')->getManager()->initializeObject($customerAddress);
  309.             //         //         $data->setBillingAddress($customerAddress);
  310.             //         //         $data->setShippingAddress($customerAddress);
  311.             //         //         $cartManager = $this->container->get('sylius.manager.order');
  312.             //         //         $cartManager->persist($data);
  313.             //         //         $cartManager->flush();
  314.             //         //         $event->setData($data);
  315.             //         //         dump($data);
  316.             //         //     }
  317.             //         // }
  318.                     
  319.             //     }
  320.                 
  321.                 
  322.             // })
  323.         ;
  324.     }
  325.     public static function getExtendedTypes(): iterable
  326.     {
  327.         return [CheckoutType::class];
  328.     }
  329. }