vendor/bluue/stocks-bundle/src/EventSubscriber/SuppliersOrdersBundle/ReceiptNoteSubscriber.php line 79

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\SuppliersOrdersBundle;
  9. use App\Services\CheckBundleInstall;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Bluue\StocksBundle\Entity\StockProduct;
  12. use Bluue\StocksBundle\Entity\StockLocation;
  13. use Bluue\StocksBundle\Entity\StockQuantity;
  14. use Bluue\StocksBundle\Services\StockService;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Bluue\StocksBundle\Repository\StockProductRepository;
  17. use Bluue\StocksBundle\Repository\StockLocationRepository;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class ReceiptNoteSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var RequestStack $requestStack
  23.      */
  24.     protected RequestStack $requestStack;
  25.     /**
  26.      * @var EntityManagerInterface $em
  27.      */
  28.     private EntityManagerInterface $em;
  29.     /**
  30.      * @var StockService $stockService
  31.      */
  32.     private StockService $stockService;
  33.     /**
  34.      * @var StockProductRepository $stockProductRepo
  35.      */
  36.     private StockProductRepository $stockProductRepo;
  37.     /**
  38.      * @var StockLocationRepository $stockLocationRepo
  39.      */
  40.     private StockLocationRepository $stockLocationRepo;
  41.     public function __construct(
  42.         RequestStack $requestStack,
  43.         EntityManagerInterface $em,
  44.         StockService $stockService,
  45.         StockProductRepository $stockProductRepo,
  46.         StockLocationRepository $stockLocationRepo
  47.     ) {
  48.         $this->requestStack $requestStack;
  49.         $this->em $em;
  50.         $this->stockService $stockService;
  51.         $this->stockProductRepo $stockProductRepo;
  52.         $this->stockLocationRepo $stockLocationRepo;
  53.     }
  54.     /**
  55.      * @return array
  56.      */
  57.     public static function getSubscribedEvents(): array
  58.     {
  59.         if (CheckBundleInstall::exist('suppliers-orders-bundle')) {
  60.             $reflectionClass = new \ReflectionClass('Bluue\SuppliersOrdersBundle\Event\ReceiptNoteEvent');
  61.             return [
  62.                 $reflectionClass->getConstant('POST_VALIDATE') => 'validateReceipt'
  63.             ];
  64.         }
  65.         return [];
  66.     }
  67.     public function validateReceipt(object $event)
  68.     {
  69.         $receiptNote $event->getReceiptNote();
  70.         $eventReceiptNoteLine $event->getReceiptNoteLine();
  71.         $user $eventReceiptNoteLine $eventReceiptNoteLine->getUpdatedBy() : $receiptNote->getValidatedBy();
  72.         if ($receiptNote->getLines()->count()) {
  73.             $receiptNoteLineClassName 'Bluue\SuppliersOrdersBundle\Entity\ReceiptNoteLine';
  74.             foreach ($receiptNote->getLines() as $receiptNoteLine) {
  75.                 if ($eventReceiptNoteLine && $eventReceiptNoteLine != $receiptNoteLine) {
  76.                     continue;
  77.                 }
  78.                 if (!$eventReceiptNoteLine && $receiptNoteLine->getQuantityRemaining() == 0) {
  79.                     continue;
  80.                 }
  81.                 $quantity = (int) $receiptNoteLine->getQuantityReceived();
  82.                 if (
  83.                     $quantity === ||
  84.                     (
  85.                         !$receiptNoteLine->getStockLocation() &&
  86.                         !$receiptNoteLine->getLocation() &&
  87.                         !$receiptNoteLine->getSubLocation()
  88.                     )
  89.                 ) {
  90.                     continue;
  91.                 }
  92.                 if ($stockLoc $receiptNoteLine->getStockLocation()) {
  93.                     $stockQuantity = (new StockQuantity())
  94.                         ->setQuantityEntry($quantity)
  95.                         ->setWholesalePrice($receiptNoteLine->getWholesalePrice())
  96.                         ->addOptions([$receiptNoteLineClassName => $receiptNoteLine->getId()])
  97.                         ->setCreatedBy($user)
  98.                         ->setUpdatedBy($user)
  99.                     ;
  100.                     $stockLoc->addStockQuantity($stockQuantity);
  101.                     $this->em->persist($stockQuantity);
  102.                 } else {
  103.                     // Get product and declination.
  104.                     $supplierOrderLine $receiptNoteLine->getSupplierOrderLine();
  105.                     $product $supplierOrderLine->getProduct() ?? $supplierOrderLine->getDeclination()->getProduct();
  106.                     $declination $supplierOrderLine->getDeclination();
  107.                     if ($receiptNoteLine->getSubLocation()) {
  108.                         $location $receiptNoteLine->getSubLocation()->getLocation();
  109.                         $subLoc $receiptNoteLine->getSubLocation();
  110.                         // Assign automatic-location by width comparaison
  111.                         if ($subLoc->getLocation()->getIsDefaultForEmptySubLocation()) {
  112.                             $autoAssignLocation $this->stockService->foundLocationByEmptyVolumeForSubLocation(
  113.                                 $subLoc
  114.                             );
  115.                             if ($autoAssignLocation) {
  116.                                 $subLoc->setLocation($autoAssignLocation);
  117.                                 $location $subLoc->getLocation();
  118.                             }
  119.                         }
  120.                     } else {
  121.                         $location $receiptNoteLine->getLocation();
  122.                     }
  123.                     $warehouse $location->getWarehouse();
  124.                     // Check stock product
  125.                     $reqStockProduct $this->stockProductRepo->createQueryBuilder('sp')
  126.                         ->andWhere('sp.warehouse = :warehouse')
  127.                         ->andWhere('sp.product = :product')
  128.                         ->setParameter('warehouse'$warehouse->getId()->toBinary())
  129.                         ->setParameter('product'$product->getId()->toBinary());
  130.                     if ($declination) {
  131.                         $reqStockProduct $reqStockProduct
  132.                             ->andWhere('sp.declination = :declination')
  133.                             ->setParameter('declination'$declination->getId()->toBinary());
  134.                     } else {
  135.                         $reqStockProduct $reqStockProduct
  136.                             ->andWhere('sp.declination IS NULL');
  137.                     }
  138.                     $stockProduct $reqStockProduct->setMaxResults(1)->getQuery()->getOneOrNullResult();
  139.                     if (!$stockProduct) {
  140.                         $stockProduct = (new StockProduct())
  141.                             ->setProduct($product)
  142.                             ->setDeclination($declination)
  143.                             ->setWarehouse($warehouse)
  144.                             ->setCreatedBy($user)
  145.                             ->setUpdatedBy($user)
  146.                         ;
  147.                         $this->em->persist($stockProduct);
  148.                     }
  149.                     // Check stock location
  150.                     $stockLocsQueryBuilder $this->stockLocationRepo->createQueryBuilder('sl')
  151.                         ->join('sl.stockProduct''sp')
  152.                         ->andWhere('sp.warehouse = :warehouse')
  153.                         ->andWhere('sp.product = :product')
  154.                         ->setParameter('product'$product->getId()->toBinary())
  155.                         ->setParameter('warehouse'$warehouse->getId()->toBinary())
  156.                     ;
  157.                     if ($supplierOrderLine->getDeclination()) {
  158.                         $stockLocsQueryBuilder->andWhere('sp.declination = :declination')
  159.                             ->setParameter('declination'$supplierOrderLine->getDeclination()->getId()->toBinary());
  160.                     } else {
  161.                         $stockLocsQueryBuilder->andWhere('sp.declination IS NULL');
  162.                     }
  163.                     if ($receiptNoteLine->getLocation()) {
  164.                         $stockLocsQueryBuilder->andWhere('sl.location = :location')
  165.                             ->setParameter('location'$receiptNoteLine->getLocation()->getId()->toBinary());
  166.                     } else {
  167.                         $stockLocsQueryBuilder->andWhere('sl.location IS NULL');
  168.                     }
  169.                     if ($receiptNoteLine->getSubLocation()) {
  170.                         $stockLocsQueryBuilder->andWhere('sl.subLocation = :sub_location')
  171.                             ->setParameter('sub_location'$receiptNoteLine->getSubLocation()->getId()->toBinary());
  172.                     } else {
  173.                         $stockLocsQueryBuilder->andWhere('sl.subLocation IS NULL');
  174.                     }
  175.                     if ($receiptNoteLine->getBatchNumber()) {
  176.                         $stockLocsQueryBuilder->andWhere('sl.batch_number = :batchNumber')
  177.                             ->setParameter('batchNumber'$receiptNoteLine->getBatchNumber());
  178.                     } else {
  179.                         $stockLocsQueryBuilder->andWhere('sl.batch_number IS NULL');
  180.                     }
  181.                     if ($receiptNoteLine->getDateType()) {
  182.                         $stockLocsQueryBuilder->andWhere('sl.dateType = :dateType')
  183.                             ->setParameter('dateType'$receiptNoteLine->getDateType()->getId()->toBinary());
  184.                     } else {
  185.                         $stockLocsQueryBuilder->andWhere('sl.dateType IS NULL');
  186.                     }
  187.                     if ($receiptNoteLine->getDate()) {
  188.                         $stockLocsQueryBuilder->andWhere('sl.date = :date')
  189.                             ->setParameter('date'$receiptNoteLine->getDate());
  190.                     } else {
  191.                         $stockLocsQueryBuilder->andWhere('sl.date IS NULL');
  192.                     }
  193.                     $stockLoc $stockLocsQueryBuilder->setMaxResults(1)->getQuery()->getOneOrNullResult();
  194.                     if (!$stockLoc) {
  195.                         $stockLoc = (new StockLocation())
  196.                             ->setLocation($receiptNoteLine->getLocation())
  197.                             ->setSubLocation($receiptNoteLine->getSubLocation())
  198.                             ->setDateType($receiptNoteLine->getDateType())
  199.                             ->setDate($receiptNoteLine->getDate())
  200.                             ->setBatchNumber($receiptNoteLine->getBatchNumber())
  201.                             ->setStockProduct($stockProduct)
  202.                             ->setCreatedBy($user)
  203.                             ->setUpdatedBy($user)
  204.                         ;
  205.                         $this->em->persist($stockLoc);
  206.                     }
  207.                     $stockQuantity = (new StockQuantity())
  208.                         ->setQuantityEntry($quantity)
  209.                         ->setStockLocation($stockLoc)
  210.                         ->setWholesalePrice($receiptNoteLine->getWholesalePrice())
  211.                         ->addOptions([$receiptNoteLineClassName => $receiptNoteLine->getId()])
  212.                         ->setCreatedBy($user)
  213.                         ->setUpdatedBy($user)
  214.                     ;
  215.                     $this->em->persist($stockQuantity);
  216.                     $stockLoc->addStockQuantity($stockQuantity);
  217.                     $stockProduct->addStockLocation($stockLoc);
  218.                     // Search existing stock with same parameters
  219.                 }
  220.                 $this->em->flush();
  221.             }
  222.         }
  223.     }
  224. }