src/Form/Type/Admin/OrderItemType.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\Form\Type\Admin;
  12. use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
  13. use Sylius\Bundle\ResourceBundle\Form\Type\ResourceAutocompleteChoiceType;
  14. use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
  15. use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
  16. use Sylius\Component\Order\Repository\OrderItemRepositoryInterface;
  17. use Symfony\Component\Form\DataMapperInterface;
  18. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  19. use Symfony\Component\Form\FormBuilderInterface;
  20. use Symfony\Component\Form\FormError;
  21. use Symfony\Component\Form\FormEvent;
  22. use Symfony\Component\Form\FormEvents;
  23. use Symfony\Component\Validator\Constraints\NotBlank;
  24. final class OrderItemType extends AbstractResourceType
  25. {
  26.     /** @var DataMapperInterface */
  27.     private $dataMapper;
  28.     private $orderItemRepository;
  29.     private AvailabilityCheckerInterface $availabilityChecker;
  30.     private ProductVariantRepositoryInterface $productVariantRepository;
  31.     public function __construct(
  32.         string $dataClass,
  33.         array $validationGroups = [],
  34.         DataMapperInterface $dataMapper,
  35.         OrderItemRepositoryInterface $orderItemRepository,
  36.         AvailabilityCheckerInterface $availabilityChecker,
  37.         ProductVariantRepositoryInterface $productVariantRepository
  38.     ) {
  39.         parent::__construct($dataClass$validationGroups);
  40.         $this->dataMapper $dataMapper;
  41.         $this->orderItemRepository $orderItemRepository;
  42.         $this->availabilityChecker $availabilityChecker;
  43.         $this->productVariantRepository $productVariantRepository;
  44.     }
  45.     public function buildForm(FormBuilderInterface $builder, array $options): void
  46.     {
  47.         $builder
  48.             ->add('variant'ResourceAutocompleteChoiceType::class, [
  49.                 'label' => 'sylius.ui.variant',
  50.                 'choice_name' => 'descriptor',
  51.                 'choice_value' => 'code',
  52.                 'resource' => 'sylius.product_variant',
  53.                 'constraints' => [
  54.                     new NotBlank(['message' => 'Veuillez renseigner une variante''groups' => ['sylius']]),
  55.                 ],
  56.             ])
  57.             ->add('quantity'IntegerType::class, [
  58.                 'attr' => ['min' => 1],
  59.                 'label' => 'sylius.ui.quantity',
  60.                 'constraints' => [
  61.                     new NotBlank(['message' => 'Veuillez saisir une quantité''groups' => ['sylius']]),
  62.                 ],
  63.             ])
  64.             ->setDataMapper($this->dataMapper)
  65.             ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options): void {
  66.                 $orderItem $event->getForm()->getNormData();
  67.                 $order $event->getForm()->getParent()->getParent()->getNormData();
  68.                 $qte = (int) $event->getData()['quantity'];
  69.                 if (null !== $orderItem) {
  70.                     //Si on modifie un orderItem
  71.                     $variant $orderItem->getVariant();
  72.                     $order $orderItem->getOrder();
  73.                     $delta $qte $orderItem->getQuantity();
  74.                     if (!== $delta) {
  75.                         if ($delta and !$this->availabilityChecker->isStockSufficient($orderItem->getVariant(), $delta)) {
  76.                             $event->getForm()->addError(new FormError('STOCK ARTICLE ' $variant->getCode() . ' INSUFFISANT'));
  77.                         } else {
  78.                             if (in_array($order->getPaymentState(), ['paid''refunded''partially_refunded'])) {
  79.                                 $variant->setOnHand($variant->getOnHand() - $delta);
  80.                             } else {
  81.                                 $variant->setOnHold($variant->getOnHold() + $delta);
  82.                             }
  83.                         }
  84.                     }
  85.                 } else {
  86.                     //Si on ajoute un orderItem
  87.                     $variant $this->productVariantRepository->findOneBy(['code' => $event->getData('variant')]);
  88.                     if (!$this->availabilityChecker->isStockSufficient($variant$qte)) {
  89.                         $event->getForm()->addError(new FormError('STOCK ARTICLE ' $variant->getCode() . ' INSUFFISANT'));
  90.                     } else {
  91.                         if (in_array($order->getPaymentState(), ['paid''refunded''partially_refunded'])) {
  92.                             $variant->setOnHand($variant->getOnHand() - $qte);
  93.                         } else {
  94.                             $variant->setOnHold($variant->getOnHold() + $qte);
  95.                         }
  96.                     }
  97.                 }
  98.             })
  99.         ;
  100.     }
  101.     public function getBlockPrefix(): string
  102.     {
  103.         return 'app_order_update_item_block';
  104.     }
  105. }