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/Ecombricks/InventoryInventorySales/Model/ResourceModel/SourceItem/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/corals/Ecombricks/InventoryInventorySales/Model/ResourceModel/SourceItem/Get.php
<?php
/**
 * Copyright © eComBricks. All rights reserved.
 * See LICENSE.txt for license details.
 */
declare(strict_types=1);

namespace Ecombricks\InventoryInventorySales\Model\ResourceModel\SourceItem;

/**
 * Get source items resource
 */
class Get
{
    
    /**
     * Connection provider
     * 
     * @var \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider 
     */
    protected $connectionProvider;
    
    /**
     * Get is stock item salable condition
     * 
     * @var \Magento\Framework\App\ResourceConnection
     */
    protected $getIsStockItemSalableCondition;
    
    /**
     * Constructor
     * 
     * @param \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider $connectionProvider
     * @param \Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\GetIsStockItemSalableConditionInterface $getIsStockItemSalableCondition
     * @return void
     */
    public function __construct(
        \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider $connectionProvider,
        \Magento\InventorySales\Model\ResourceModel\IsStockItemSalableCondition\GetIsStockItemSalableConditionInterface $getIsStockItemSalableCondition
    )
    {
        $this->connectionProvider = $connectionProvider;
        $this->getIsStockItemSalableCondition = $getIsStockItemSalableCondition;
    }
    
    /**
     * Get quantity expression
     * 
     * @param string $sourceItemTableAlias
     * @return string
     */
    protected function getQuantityExpression(string $sourceItemTableAlias): string
    {
        return 'SUM('.$this->connectionProvider->getConnection()->getCheckSql(
            $sourceItemTableAlias.'.'.\Magento\InventoryApi\Api\Data\SourceItemInterface::STATUS.' = '.\Magento\InventoryApi\Api\Data\SourceItemInterface::STATUS_OUT_OF_STOCK,
            0,
            \Magento\InventoryApi\Api\Data\SourceItemInterface::QUANTITY
        ).')';
    }
    
    /**
     * Get is salable condition select
     * 
     * @param string $sourceItemTableAlias
     * @return \Magento\Framework\DB\Select
     */
    protected function getIsSalableConditionSelect(string $sourceItemTableAlias): \Magento\Framework\DB\Select
    {
        return $this->connectionProvider->getSelect()
            ->joinLeft(
                ['product' => $this->connectionProvider->getTable('catalog_product_entity')],
                'product.sku = '.$sourceItemTableAlias.'.'.\Magento\InventoryApi\Api\Data\SourceItemInterface::SKU,
                []
            )
            ->joinLeft(
                ['legacy_stock_item' => $this->connectionProvider->getTable('cataloginventory_stock_item')],
                'product.entity_id = legacy_stock_item.product_id',
                []
            );
    }
    
    /**
     * Get is salable expression
     * 
     * @param \Magento\Framework\DB\Select $select
     * @return string
     */
    protected function getIsSalableExpression(\Magento\Framework\DB\Select $select): string
    {
        return $this->getIsStockItemSalableCondition->execute($select);
    }
    
    /**
     * Execute
     * 
     * @param array $skus
     * @param int $stockId
     * @return array
     */
    public function execute(array $skus, int $stockId): array
    {
        $sourceItemTableAlias = 'source_item';
        $stockSourceLinkTableAlias = 'stock_source_link';
        $sourceItemSku = $sourceItemTableAlias.'.'.\Magento\InventoryApi\Api\Data\SourceItemInterface::SKU;
        $sourceItemSourceCode = $sourceItemTableAlias.'.'.\Magento\InventoryApi\Api\Data\SourceItemInterface::SOURCE_CODE;
        $stockSourceLinkStockId = $stockSourceLinkTableAlias.'.'.\Magento\InventoryApi\Api\Data\StockSourceLinkInterface::STOCK_ID;
        $stockSourceLinkSourceCode = $stockSourceLinkTableAlias.'.'.\Magento\InventoryApi\Api\Data\StockSourceLinkInterface::SOURCE_CODE;
        $stockSourceLinkPriority = $stockSourceLinkTableAlias.'.'.\Magento\InventoryApi\Api\Data\StockSourceLinkInterface::PRIORITY;
        $select = $this->getIsSalableConditionSelect($sourceItemTableAlias);
        $select
            ->from(
                [$sourceItemTableAlias => $this->connectionProvider->getTable(\Magento\Inventory\Model\ResourceModel\SourceItem::TABLE_NAME_SOURCE_ITEM)],
                []
            )
            ->from(
                [$stockSourceLinkTableAlias => $this->connectionProvider->getTable(\Magento\Inventory\Model\ResourceModel\StockSourceLink::TABLE_NAME_STOCK_SOURCE_LINK)],
                []
            )
            ->columns([
                'sku' => $sourceItemSku,
                'source_code' => $sourceItemSourceCode,
                'quantity' => $this->getQuantityExpression($sourceItemTableAlias),
                'is_salable' => $this->getIsSalableExpression($select),
            ])
            ->where($sourceItemSku.' IN (?)', $skus)
            ->where($stockSourceLinkStockId.' = ?', $stockId)
            ->where($stockSourceLinkSourceCode.' = '.$sourceItemSourceCode)
            ->order([$stockSourceLinkPriority.' '.\Magento\Framework\DB\Select::SQL_ASC])
            ->group([$sourceItemSku, $sourceItemSourceCode]);
        return $this->connectionProvider->getConnection()->fetchAll($select);
    }
    
}

Spamworldpro Mini