Spamworldpro Mini Shell
Spamworldpro


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/old/app/code/Cnc/SerialNumber/Plugin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/SerialNumber/Plugin/SourceItemsProcessorPlugin.php
<?php
/**
 * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Krzysztof Majkowski <[email protected]>
 */

declare(strict_types=1);

namespace Cnc\SerialNumber\Plugin;

use Cnc\SerialNumber\Api\IsSerialNumberEnabledForProductInterface;
use Cnc\SerialNumber\Helper\SerialNumber;
use Cnc\SerialNumber\Model\Logger;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryCatalog\Model\SourceItemsProcessor;

class SourceItemsProcessorPlugin
{
    /**
     * @var SearchCriteriaBuilderFactory
     */
    private $searchCriteriaBuilderFactory;

    /**
     * @var SourceItemRepositoryInterface
     */
    private $sourceItemRepository;

    /**
     * @var SourceRepositoryInterface
     */
    private $sourceRepository;

    /**
     * @var Product
     */
    private $resourceProduct;

    /**
     * @var SerialNumber
     */
    private $helper;

    /**
     * @var Logger
     */
    private $logger;

    /**
     * @var array
     */
    private $cacheInstance = [];
    /**
     * @var IsSerialNumberEnabledForProductInterface
     */
    private $isSerialNumberEnabledForProduct;

    /**
     * SourceItemsProcessorPlugin constructor.
     * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
     * @param SourceItemRepositoryInterface $sourceItemRepository
     * @param SourceRepositoryInterface $sourceRepository
     * @param Product $resourceProduct
     * @param SerialNumber $helper
     * @param Logger $logger
     * @param IsSerialNumberEnabledForProductInterface $isSerialNumberEnabledForProduct
     */
    public function __construct(
        SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
        SourceItemRepositoryInterface $sourceItemRepository,
        SourceRepositoryInterface $sourceRepository,
        Product $resourceProduct,
        SerialNumber $helper,
        Logger $logger,
        IsSerialNumberEnabledForProductInterface $isSerialNumberEnabledForProduct
    ) {
        $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
        $this->sourceItemRepository = $sourceItemRepository;
        $this->sourceRepository = $sourceRepository;
        $this->resourceProduct = $resourceProduct;
        $this->logger = $logger;
        $this->helper = $helper;
        $this->isSerialNumberEnabledForProduct = $isSerialNumberEnabledForProduct;
    }

    /**
     * @param SourceItemsProcessor $subject
     * @param string $sku
     * @param array $sourceItemsData
     * @return array
     * @throws AlreadyExistsException
     */
    public function beforeExecute(
        SourceItemsProcessor $subject,
        string $sku,
        array $sourceItemsData
    ): array {
        if (!$this->isSerialNumberEnabledForProduct->execute($sku)) {
            return [$sku, $sourceItemsData];
        }

        $productId = $this->getIdBySku($sku);
        $currentItems = $this->getCurrentSourceItemsMap($sku);

        $sources = $this->sourceRepository->getList()->getItems();
        foreach ($sources as $source) {
            $currentQty = 0;
            $newQty = 0;
            if (isset($currentItems[$source->getSourceCode()])) {
                $currentItem = $currentItems[$source->getSourceCode()];
                $currentQty = (int) $currentItem[SourceItemInterface::QUANTITY];
            }

            foreach ($sourceItemsData as $sourceItemData) {
                if ($source->getSourceCode() == $sourceItemData[SourceItemInterface::SOURCE_CODE]) {
                    $newQty = empty($sourceItemData[SourceItemInterface::QUANTITY]) ?
                        0 : $sourceItemData[SourceItemInterface::QUANTITY];
                }
            }

            $diffQty = $newQty - $currentQty;
            if ($diffQty > 0) {
                $this->helper->createSerialNumbers(
                    (int) $productId,
                    (string) $sku,
                    (string) $source->getSourceCode(),
                    (int) $diffQty
                );
            } elseif ($diffQty < 0) {
                $this->helper->blockSerialNumbers((string) $sku, $source->getSourceCode(), (int) abs($diffQty));
            }
        }

        return [$sku, $sourceItemsData];
    }

    /**
     * Get source source items.
     *
     * @param string $sku
     * @return array
     */
    private function getCurrentSourceItemsMap(string $sku): array
    {
        $searchCriteriaBuilder = $this->searchCriteriaBuilderFactory->create();
        $searchCriteria = $searchCriteriaBuilder->addFilter(ProductInterface::SKU, $sku)->create();
        $sourceItems = $this->sourceItemRepository->getList($searchCriteria)->getItems();

        $sourceItemMap = [];
        if ($sourceItems) {
            foreach ($sourceItems as $sourceItem) {
                $sourceItemMap[$sourceItem->getSourceCode()] = $sourceItem;
            }
        }
        return $sourceItemMap;
    }

    /**
     * Get id by sku.
     *
     * @param $sku
     * @return false|int
     */
    private function getIdBySku($sku)
    {
        $key = 'get_id_by_sku_' . $sku;
        if (!isset($this->cacheInstance[$key])) {
            $this->cacheInstance[$key] = $this->resourceProduct->getIdBySku($sku);
        }
        return $this->cacheInstance[$key];
    }
}

Spamworldpro Mini