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

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.                     $mvStockQuantity $sq;
  142.                     $stockLocation $sq->getStockLocation();
  143.                     $stockProduct $stockLocation->getStockProduct();
  144.                     if ($stockProduct->isDeleted()) {
  145.                         $stockProduct->setDeletedAt();
  146.                         $stockProduct->setDeletedBy();
  147.                         $stockLocation->setStockProduct($stockProduct);
  148.                     }
  149.                     if ($stockLocation->isDeleted()) {
  150.                         $stockLocation->setDeletedAt();
  151.                         $stockLocation->setDeletedBy();
  152.                         $mvStockQuantity->setStockLocation($stockLocation);
  153.                     }
  154.                     $location $stockLocation->getLocation();
  155.                     $subLoc $stockLocation->getSubLocation();
  156.                     if ($location && $location->isDeleted()) {
  157.                         $location->setDeletedAt();
  158.                         $location->setDeletedBy();
  159.                         $stockLocation->setLocation($location);
  160.                     }
  161.                     if ($subLoc && $subLoc->isDeleted()) {
  162.                         $subLoc->setDeletedAt();
  163.                         $subLoc->setDeletedBy();
  164.                         $stockLocation->setSubLocation($subLoc);
  165.                     }
  166.                     $stockMovement->setStockQuantity($mvStockQuantity);
  167.                     $stockQuantity->setStockLocation($sq->getStockLocation());
  168.                     $stockQuantity->setWholesalePrice($sq->getWholesalePrice());
  169.                     $stockQuantity->setDateEntry($sq->getDateEntry());
  170.                     $stockQuantity->setCurrencyChangeRate($sq->getCurrencyChangeRate());
  171.                     $stockQuantity->setCurrency($sq->getCurrency());
  172.                     $this->em->flush();
  173.                     $this->em->persist($stockQuantity);
  174.                 } else {
  175.                     $this->stockService->updateStockQuantity(
  176.                         $sq,
  177.                         intval($stockMovement->getQuantity()),
  178.                         $stockMovementReason,
  179.                         [
  180.                             DeliveryNote::class => $event->getDeliveryNote()->getId(),
  181.                             DeliveryNoteLine::class => $stockMovement->getEntityId()
  182.                         ]
  183.                     );
  184.                 }
  185.             }
  186.             $this->em->flush();
  187.             $this->stockService->dispatchMessageRemainingQuantityOrdered($event->getDeliveryNote()->getOrder());
  188.         }
  189.     }
  190. }