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

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