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

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