![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryInventorySales/Plugin/Model/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ declare(strict_types=1); namespace Ecombricks\InventoryInventorySales\Plugin\Model; /** * Place reservations for sales event plugin */ class PlaceReservationsForSalesEvent { /** * Product management * * @var \Ecombricks\InventoryInventoryCatalog\Model\ProductManagement */ protected $productManagement; /** * Reservation builder * * @var \Ecombricks\InventoryInventoryReservations\Model\ReservationBuilderInterface */ protected $reservationBuilder; /** * Append reservations * * @var \Magento\InventoryReservationsApi\Model\AppendReservationsInterface */ protected $appendReservations; /** * Get product types by SKUs * * @var \Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface */ protected $getProductTypesBySkus; /** * Serializer * * @var \Magento\Framework\Serialize\SerializerInterface */ protected $serializer; /** * Sales event to array converter * * @var \Magento\InventorySales\Model\SalesEventToArrayConverter */ protected $salesEventToArrayConverter; /** * Constructor * * @param \Ecombricks\InventoryInventoryCatalog\Model\ProductManagement $productManagement * @param \Ecombricks\InventoryInventoryReservations\Model\ReservationBuilderInterface $reservationBuilder * @param \Magento\InventoryReservationsApi\Model\AppendReservationsInterface $appendReservations * @param \Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface $getProductTypesBySkus * @param \Magento\Framework\Serialize\SerializerInterface $serializer * @param \Magento\InventorySales\Model\SalesEventToArrayConverter $salesEventToArrayConverter * @return void */ public function __construct( \Ecombricks\InventoryInventoryCatalog\Model\ProductManagement $productManagement, \Ecombricks\InventoryInventoryReservations\Model\ReservationBuilderInterface $reservationBuilder, \Magento\InventoryReservationsApi\Model\AppendReservationsInterface $appendReservations, \Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface $getProductTypesBySkus, \Magento\Framework\Serialize\SerializerInterface $serializer, \Magento\InventorySales\Model\SalesEventToArrayConverter $salesEventToArrayConverter ) { $this->productManagement = $productManagement; $this->reservationBuilder = $reservationBuilder; $this->appendReservations = $appendReservations; $this->getProductTypesBySkus = $getProductTypesBySkus; $this->serializer = $serializer; $this->salesEventToArrayConverter = $salesEventToArrayConverter; } /** * Execute * * @param array $items * @param \Magento\InventorySalesApi\Api\Data\SalesChannelInterface $salesChannel * @param \Magento\InventorySalesApi\Api\Data\SalesEventInterface $salesEvent * @return void * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\CouldNotSaveException */ public function execute( array $items, \Magento\InventorySalesApi\Api\Data\SalesChannelInterface $salesChannel, \Magento\InventorySalesApi\Api\Data\SalesEventInterface $salesEvent ): void { if (empty($items)) { return; } $skus = []; foreach ($items as $item) { $skus[] = $item->getSku(); } $productTypes = $this->getProductTypesBySkus->execute($skus); $reservations = []; foreach ($items as $item) { $sku = $item->getSku(); $skuNotExistInCatalog = !isset($productTypes[$sku]); if (!$skuNotExistInCatalog && !$this->productManagement->isSourceItemManagementAllowedByProductType($productTypes[$sku])) { continue; } $sourceCode = $item->getExtensionAttributes()->getSourceCode(); $quantity = (float) $item->getQuantity(); $reservations[] = $this->reservationBuilder ->setSku($sku) ->setSourceCode($sourceCode) ->setQuantity($quantity) ->setMetadata($this->serializer->serialize($this->salesEventToArrayConverter->execute($salesEvent))) ->build(); } $this->appendReservations->execute($reservations); } /** * Around execute * * @param \Magento\InventorySales\Model\PlaceReservationsForSalesEvent $subject * @param callable $proceed * @param array $items * @param \Magento\InventorySalesApi\Api\Data\SalesChannelInterface $salesChannel * @param \Magento\InventorySalesApi\Api\Data\SalesEventInterface $salesEvent * @return void */ public function aroundExecute( \Magento\InventorySales\Model\PlaceReservationsForSalesEvent $subject, callable $proceed, array $items, \Magento\InventorySalesApi\Api\Data\SalesChannelInterface $salesChannel, \Magento\InventorySalesApi\Api\Data\SalesEventInterface $salesEvent ): void { $this->execute($items, $salesChannel, $salesEvent); } }