src/App/Form/Type/EventListener/AddOrderDataListener.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type\EventListener;
  3. use App\Entity\Core\Customer;
  4. use App\Service\EstimationCartService;
  5. use DateTime;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Sylius\Component\Addressing\Model\AddressInterface;
  8. use Sylius\Component\Core\Model\CustomerInterface;
  9. use Sylius\Component\Core\Model\OrderInterface;
  10. use Sylius\Component\Core\OrderCheckoutTransitions;
  11. use Sylius\Component\Core\OrderPaymentStates;
  12. use Sylius\Component\Core\OrderShippingStates;
  13. use Sylius\Component\Core\Repository\CustomerRepositoryInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Form\FormEvent;
  16. use Symfony\Component\Form\FormEvents;
  17. class AddOrderDataListener implements EventSubscriberInterface
  18. {
  19.     private $customerRepository;
  20.     private $entityManager;
  21.     public function __construct(
  22.         CustomerRepositoryInterface $customerRepository,
  23.         EntityManagerInterface $entityManager
  24.     ) {
  25.         $this->customerRepository $customerRepository;
  26.         $this->entityManager $entityManager;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             FormEvents::POST_SUBMIT => 'onPostSubmit',
  32.         ];
  33.     }
  34.     public function onPostSubmit(FormEvent $event)
  35.     {
  36.         $order $event->getData();
  37.         if (!$order) {
  38.             return;
  39.         }
  40.         
  41.         $this->removeShippingAddress($event);
  42.     }
  43.     private function removeShippingAddress(FormEvent $event): void
  44.     {
  45.         $order $event->getData();
  46.         $customerType $event->getForm()->get('type')->getData();
  47.         $order->getCustomer()->setType($customerType);
  48.         if ($customerType == Customer::TYPE_PERSON) {
  49.             $order->getCustomer()->setLine($order->getCustomer()->getCurrentTypeValue());
  50.         }
  51.         $shippingMethod count($order->getShipments()) > $order->getShipments()->first()->getMethod() : null;
  52.         $customer $this->getCustomer($order->getCustomer());
  53.         $billingAddress $order->getBillingAddress();
  54.         $shippingAddress $order->getShippingAddress();
  55.         $order->setCustomer($customer);
  56.         $this->processAddress($billingAddress$customer$order);
  57.         $this->processAddress($shippingAddress$customer$order);
  58.         if (
  59.             $shippingMethod &&
  60.             $shippingMethod->getCode() == 'retiro'
  61.         ) {
  62.             $order->setShippingAddress(null);
  63.         }
  64.         
  65.         $this->setOrderData($order);
  66.     }
  67.     private function setAddressData(AddressInterface $addressCustomerInterface $customer): void
  68.     {
  69.         if (!$address->getId()) {
  70.             $address->setPostCode('0');
  71.             $address->setCountryCode('CL');
  72.             $address->setFirstName($customer->getFirstName());
  73.             $address->setLastName($customer->getFirstName());
  74.         }
  75.     }
  76.     private function setOrderData(OrderInterface $order): void
  77.     {
  78.         $order->setCheckoutState(OrderCheckoutTransitions::TRANSITION_COMPLETE);
  79.         if ($order->getState() == EstimationCartService::SHOPPING_CART) {
  80.             $order->setState(OrderInterface::STATE_NEW);
  81.         } else {
  82.             $order->setState('estimation_' OrderInterface::STATE_NEW);
  83.         }
  84.         $order->setPaymentState(OrderPaymentStates::STATE_AWAITING_PAYMENT);
  85.         $order->setShippingState(OrderShippingStates::STATE_READY);
  86.         $order->setCheckoutCompletedAt(new DateTime());
  87.     }
  88.     private function copyCustomerFieldsFromCustomer(CustomerInterface $oldCustomerCustomerInterface $toCopyCustomer)
  89.     {
  90.         $oldCustomer->setFirstName($toCopyCustomer->getFirstName());
  91.         $oldCustomer->setLastName($toCopyCustomer->getLastName());
  92.         $oldCustomer->setPhoneNumber($toCopyCustomer->getPhoneNumber());
  93.         $oldCustomer->setEmail($toCopyCustomer->getEmail());
  94.         $oldCustomer->setLine($toCopyCustomer->getLine());
  95.         $oldCustomer->setCompany($toCopyCustomer->getCompany());
  96.     }
  97.     private function getCustomer(CustomerInterface $customer): CustomerInterface
  98.     {
  99.         $storedCustomer $this->customerRepository->findOneByRut($customer->getRut());
  100.         if ($storedCustomer) {
  101.             $this->copyCustomerFieldsFromCustomer($storedCustomer$customer);
  102.             $this->entityManager->persist($storedCustomer);
  103.         }
  104.         return $storedCustomer ?? $customer;
  105.     }
  106.     private function processAddress(
  107.         ?AddressInterface $address,
  108.         CustomerInterface $customer,
  109.         OrderInterface $order
  110.     ): void {
  111.         if ($address && $address->getStreet()) {
  112.             $this->setAddressData($address$customer);
  113.         } else {
  114.             $order->setShippingAddress(null);
  115.         }
  116.     }
  117. }