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

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