vendor/bluue/stocks-bundle/src/EventSubscriber/SalesBundle/DeliveryNoteSubscriber.php line 81

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\StocksBundle\EventSubscriber\SalesBundle;
  9. use ReflectionClass;
  10. use App\Services\Configuration;
  11. use App\Services\SoftdeleteFilter;
  12. use App\Services\CheckBundleInstall;
  13. use Bluue\SalesBundle\Entity\DeliveryNote;
  14. use Bluue\SalesBundle\Entity\DeliveryNoteLine;
  15. use Bluue\StocksBundle\Entity\Location;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Bluue\StocksBundle\Entity\SubLocation;
  18. use Bluue\StocksBundle\Entity\StockProduct;
  19. use Bluue\StocksBundle\Entity\StockLocation;
  20. use Bluue\StocksBundle\Entity\StockQuantity;
  21. use Bluue\StocksBundle\Services\StockService;
  22. use Bluue\StocksBundle\Entity\StockProductRoot;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. use Bluue\StocksBundle\Repository\StockMovementRepository;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Bluue\StocksBundle\Repository\StockMovementReasonRepository;
  27. use Doctrine\Persistence\ManagerRegistry;
  28. use Symfony\Component\Uid\Uuid;
  29. class DeliveryNoteSubscriber implements EventSubscriberInterface
  30. {
  31.     protected RequestStack $requestStack;
  32.     private StockMovementRepository $stockMovementRepo;
  33.     private Configuration $configuration;
  34.     private StockMovementReasonRepository $stockMovementReasonRepo;
  35.     private StockService $stockService;
  36.     private EntityManagerInterface $em;
  37.     private ManagerRegistry $registry;
  38.     public function __construct(
  39.         RequestStack $requestStack,
  40.         StockMovementRepository $stockMovementRepo,
  41.         StockMovementReasonRepository $stockMovementReasonRepo,
  42.         Configuration $configuration,
  43.         StockService $stockService,
  44.         EntityManagerInterface $em,
  45.         ManagerRegistry $registry
  46.     ) {
  47.         $this->requestStack $requestStack;
  48.         $this->stockMovementRepo $stockMovementRepo;
  49.         $this->stockMovementReasonRepo $stockMovementReasonRepo;
  50.         $this->configuration $configuration;
  51.         $this->stockService $stockService;
  52.         $this->em $em;
  53.         $this->registry $registry;
  54.     }
  55.     /**
  56.      * @return array
  57.      */
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         if (CheckBundleInstall::exist('sales-bundle')) {
  61.             $reflectionClass = new ReflectionClass('Bluue\SalesBundle\Event\DeliveryNoteMovementsEvent');
  62.             return [
  63.                 $reflectionClass->getConstant('VALIDATE_DELIVERY') => 'validateDelivery',
  64.                 $reflectionClass->getConstant('CANCEL_DELIVERY') => 'cancelDelivery'
  65.             ];
  66.         }
  67.         return [];
  68.     }
  69.     /**
  70.      * @param object $event
  71.      * @return void
  72.      */
  73.     public function validateDelivery(object $event): void
  74.     {
  75.         if (!$this->em->isOpen()) {
  76.             $this->registry->resetManager();
  77.         }
  78.         /** @var DeliveryNote $deliveryNote */
  79.         $deliveryNote $event->getDeliveryNote();
  80.         if ($deliveryNote->getProductLines()->count()) {
  81.             $stockMovementReason $this->configuration
  82.                 ->get('stocks_bundle__default_stock_movement_reason__delivery_note_validate');
  83.             if ($stockMovementReason) {
  84.                 $stockMovementReason $this->stockMovementReasonRepo->find($stockMovementReason);
  85.             }
  86.             foreach ($deliveryNote->getProductLines() as $productLine) {
  87.                 $options $productLine->getOptions();
  88.                 if (
  89.                     (array_key_exists('no_remove_stock'$options) && $options['no_remove_stock'])
  90.                     || $productLine->getIsPack()
  91.                 ) {
  92.                     continue;
  93.                 }
  94.                 $quantity intval($productLine->getQuantity());
  95.                 if (!empty($options['product']['virtual_quantity'])) {
  96.                     $quantity *= intval($options['product']['virtual_quantity']);
  97.                 }
  98.                 $this->stockService->processStockOut(
  99.                     new Uuid($options['product']['id']),
  100.                     ($productLine->isDeclination() ? new Uuid($options['product']['declination_id']) : null),
  101.                     $quantity,
  102.                     $stockMovementReason,
  103.                     $deliveryNote->getContext()->getId(),
  104.                     [
  105.                         'deliveryNoteLine' => $productLine
  106.                     ],
  107.                     null,
  108.                     $deliveryNote->getEstablishment() ? $deliveryNote->getEstablishment()->getId()->toRfc4122() : null
  109.                 );
  110.             }
  111.             $this->stockService->dispatchMessageRemainingQuantityOrdered($deliveryNote->getOrder());
  112.         }
  113.     }
  114.     /**
  115.      * @param object $event
  116.      * @return void
  117.      */
  118.     public function cancelDelivery(object $event): void
  119.     {
  120.         $deliveryNote $event->getDeliveryNote();
  121.         $productLines $deliveryNote->getProductLines();
  122.         if ($productLines->count()) {
  123.             SoftdeleteFilter::disable($this->em, [
  124.                 StockQuantity::class,
  125.                 StockLocation::class,
  126.                 StockProduct::class,
  127.                 StockProductRoot::class,
  128.                 Location::class,
  129.                 SubLocation::class
  130.             ]);
  131.             $stockMovementReason $this->configuration
  132.                 ->get('stocks_bundle__default_stock_movement_reason__delivery_note_cancel');
  133.             if ($stockMovementReason) {
  134.                 $stockMovementReason $this->stockMovementReasonRepo->find($stockMovementReason);
  135.             }
  136.             $stockMovements = [];
  137.             foreach ($productLines as $productLine) {
  138.                 $stockMovements array_merge(
  139.                     $stockMovements,
  140.                     $this->stockMovementRepo->findBy(['entityId' => $productLine->getId()])
  141.                 );
  142.             }
  143.             foreach ($stockMovements as $stockMovement) {
  144.                 $sq $stockMovement->getStockQuantity();
  145.                 if ($sq->isDeleted()) {
  146.                     $stockQuantity = new StockQuantity();
  147.                     $stockQuantity->setQuantityEntry(intval($stockMovement->getQuantity()));
  148.                     $stockQuantity->setReason($stockMovementReason);
  149.                     $stockQuantity->setOptions([
  150.                         DeliveryNoteLine::class => $stockMovement->getEntityId()
  151.                     ]);
  152.                     $mvStockQuantity $sq;
  153.                     $stockLocation $sq->getStockLocation();
  154.                     $stockProduct $stockLocation->getStockProduct();
  155.                     if ($stockProduct->isDeleted()) {
  156.                         $stockProduct->setDeletedAt();
  157.                         $stockProduct->setDeletedBy();
  158.                         $stockLocation->setStockProduct($stockProduct);
  159.                     }
  160.                     if ($stockLocation->isDeleted()) {
  161.                         $stockLocation->setDeletedAt();
  162.                         $stockLocation->setDeletedBy();
  163.                         $mvStockQuantity->setStockLocation($stockLocation);
  164.                     }
  165.                     $location $stockLocation->getLocation();
  166.                     $subLoc $stockLocation->getSubLocation();
  167.                     if ($location && $location->isDeleted()) {
  168.                         $location->setDeletedAt();
  169.                         $location->setDeletedBy();
  170.                         $stockLocation->setLocation($location);
  171.                     }
  172.                     if ($subLoc && $subLoc->isDeleted()) {
  173.                         $subLoc->setDeletedAt();
  174.                         $subLoc->setDeletedBy();
  175.                         $stockLocation->setSubLocation($subLoc);
  176.                     }
  177.                     $stockMovement->setStockQuantity($mvStockQuantity);
  178.                     $stockQuantity->setStockLocation($sq->getStockLocation());
  179.                     $stockQuantity->setWholesalePrice($sq->getWholesalePrice());
  180.                     $stockQuantity->setDateEntry($sq->getDateEntry());
  181.                     $stockQuantity->setCurrencyChangeRate($sq->getCurrencyChangeRate());
  182.                     $stockQuantity->setCurrency($sq->getCurrency());
  183.                     $this->em->flush();
  184.                     $this->em->persist($stockQuantity);
  185.                 } else {
  186.                     $this->stockService->updateStockQuantity(
  187.                         $sq,
  188.                         intval($stockMovement->getQuantity()),
  189.                         $stockMovementReason,
  190.                         [
  191.                             DeliveryNote::class => $deliveryNote->getId(),
  192.                             DeliveryNoteLine::class => $stockMovement->getEntityId()
  193.                         ]
  194.                     );
  195.                 }
  196.             }
  197.             $this->em->flush();
  198.             $this->stockService->dispatchMessageRemainingQuantityOrdered($deliveryNote->getOrder());
  199.         }
  200.     }
  201. }