![]() 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/Checkout/Helper/ |
<?php /** * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Krzysztof Majkowski <[email protected]> <[email protected]> */ namespace Cnc\Checkout\Helper; use Cnc\Catalog\Helper\Data as CncCatalogHelper; use Cnc\Catalog\Model\Attribute\MsiAttributes; use Cnc\Checkout\Api\GetAssignedSourceCodesBySkuInterface; use Cnc\Checkout\Api\GetAssignedSourceCodesInterface; use Ecombricks\InventoryInventorySales\Api\GetProductSalableQtyInterface; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Quote\Model\Quote\Item; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; class Cart { /** * @var GetProductSalableQtyInterface */ private $getProductSalableQty; /** * @var GetAssignedSourceCodesInterface */ private $getAssignedSourceCodes; /** * @var MsiAttributes */ private $msiAttributes; /** * @var ScopeConfigInterface */ private $scopeConfig; /** * @var StoreManagerInterface */ private $storeManager; /** * @var CncCatalogHelper */ private $helper; /** * @var array */ private $sourcesByWebsite = []; /** * @var GetAssignedSourceCodesBySkuInterface */ private $getAssignedSourceCodesBySku; /** * Cart constructor. * @param ScopeConfigInterface $scopeConfig * @param StoreManagerInterface $storeManager * @param GetProductSalableQtyInterface $getProductSalableQty * @param GetAssignedSourceCodesInterface $getAssignedSourceCodes * @param MsiAttributes $msiAttributes * @param CncCatalogHelper $helper * @param GetAssignedSourceCodesBySkuInterface $getAssignedSourceCodesBySku */ public function __construct( ScopeConfigInterface $scopeConfig, StoreManagerInterface $storeManager, GetProductSalableQtyInterface $getProductSalableQty, GetAssignedSourceCodesInterface $getAssignedSourceCodes, MsiAttributes $msiAttributes, CncCatalogHelper $helper, GetAssignedSourceCodesBySkuInterface $getAssignedSourceCodesBySku ) { $this->getProductSalableQty = $getProductSalableQty; $this->getAssignedSourceCodes = $getAssignedSourceCodes; $this->msiAttributes = $msiAttributes; $this->scopeConfig = $scopeConfig; $this->storeManager = $storeManager; $this->helper = $helper; $this->getAssignedSourceCodesBySku = $getAssignedSourceCodesBySku; } /** * @return bool */ public function isBackorderEnabled() { return $this->scopeConfig->isSetFlag( 'cataloginventory/item_options/backorders', ScopeInterface::SCOPE_STORE ); } /** * @param string $sku * @param string $sourceCode * @return bool */ public function isBackorderEnabledForProductAndSource(string $sku, string $sourceCode) { $vhsAttributeId = $this->msiAttributes->getAttributeId('cnc_vhs'); return (bool) $this->msiAttributes->getAttributeValue( $vhsAttributeId, $sku, $sourceCode )->getValue(); } /** * Get source data per product. * If items ($cart->getItems()) is added then availability will be decreased by qty in cart. * * @param $sku * @param null $items * @return array|array[] * @throws LocalizedException * @throws \Magento\Framework\Exception\InputException */ public function getSourceDataForProduct($sku, $items = null): array { if ($sku instanceof ProductInterface) { $sku = $sku->getSku(); } $productSources = $this->getAssignedSourceCodesBySku->execute($sku); $data = ['sources' => []]; $max = 0; foreach ($this->getSourcePriority() as $sourceCode) { if (!in_array($sourceCode, $productSources)) { continue; } if ($this->helper->getCncAvailabilityValue($sku, $sourceCode) == 'Out of stock') { $available = 0; $max += $available; $data['sources'][$sourceCode] = $available; continue; } $available = $this->getProductSalableQty->execute($sku, $sourceCode); if ($items && count($items)) { /** @var Item $item */ foreach ($items as $item) { if ($item->getSku() == $sku) { $option = $item->getOptionByCode('source'); if ($option && $option->getValue() == $sourceCode) { $available -= $item->getQty(); } } } } $available = max(0, $available); $max += $available; $data['sources'][$sourceCode] = $available; if ($this->isBackorderEnabled() && $this->isBackorderEnabledForProductAndSource($sku, $sourceCode)) { $max = 999; $data['sources'][$sourceCode] = 999; } } $data['max'] = $max; return $data; } /** * Get source codes by priority. * * @return string[] * @throws LocalizedException */ public function getSourcePriority(): array { $code = $this->storeManager->getWebsite()->getCode(); if (!isset($this->sourcesByWebsite[$code])) { $sources = $this->getAssignedSourceCodes->execute($code); $this->sourcesByWebsite[$code] = $sources; } return $this->sourcesByWebsite[$code]; } }