![]() 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/Catalog/Model/Product/ |
<?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) * cnc_catalog_m2 * <[email protected]> */ declare(strict_types=1); namespace Cnc\Catalog\Model\Product; use Cnc\Catalog\Api\IsSalableInterface; use Cnc\Catalog\Helper\Data; use Cnc\Catalog\Helper\Data as CncCatalogHelper; use Cnc\Catalog\Model\Attribute\MsiAttributes; use Cnc\Catalog\Model\CachedGetProductSalableQty; use Cnc\Catalog\Model\Config; use Cnc\Checkout\Api\GetAssignedSourceCodesBySkuInterface; use Cnc\Checkout\Model\GetAssignedSourceCodes; use Ecombricks\InventoryInventorySales\Model\SourceItemManagement; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NotFoundException; use Magento\Framework\Phrase; use Magento\Framework\View\Element\Block\ArgumentInterface; use Magento\GroupedProduct\Model\Product\Type\Grouped; use Magento\GroupedProduct\Model\ResourceModel\Product\Link; use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite; use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface; use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface; use Magento\Store\Model\StoreManagerInterface; class Stock implements ArgumentInterface { const ESTIMATE_SHIPPING_AVAILABILITIES = [ Config::PRODUCT_AVAILABILITY_ICON_AVAILABLE_IN_5_TO_8_DAYS_VALUE, Config::PRODUCT_AVAILABILITY_ICON_SHIPMENT_WITHIN_3_DAYS_VALUE, Config::PRODUCT_AVAILABILITY_ICON_IMMEDIATE_SHIPMENT_VALUE ]; /** * @var GetStockItemConfigurationInterface */ private $getStockItemConfiguration; /** * @var GetStockIdForCurrentWebsite */ private $getStockIdForCurrentWebsite; /** * @var CncCatalogHelper */ private $cncCatalogHelper; /** * @var GetAssignedSourceCodes */ private $getAssignedSourceCodes; /** * @var StoreManagerInterface */ private $storeManager; /** * @var MsiAttributes */ private $msiAttributes; /** * @var Grouped */ private $groupedType; /** * @var CachedGetProductSalableQty */ private $getProductSalableQty; /** * @var CncCatalogHelper */ private $helper; /** * @var IsSalableInterface */ private $isSalable; /** * @var GetAssignedSourceCodesBySkuInterface */ private $getAssignedSourceCodesBySku; /** * Source item management * * @var SourceItemManagement */ protected $sourceItemManagement; /** * Stock constructor. * @param GetStockItemConfigurationInterface $getStockItemConfiguration * @param GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite * @param CncCatalogHelper $cncCatalogHelper * @param GetAssignedSourceCodes $getAssignedSourceCodes * @param StoreManagerInterface $storeManager * @param MsiAttributes $msiAttributes * @param Grouped $groupedType * @param CachedGetProductSalableQty $getProductSalableQty * @param CncCatalogHelper $helper * @param SourceItemManagement $sourceItemManagement */ public function __construct( GetStockItemConfigurationInterface $getStockItemConfiguration, GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite, CncCatalogHelper $cncCatalogHelper, GetAssignedSourceCodes $getAssignedSourceCodes, StoreManagerInterface $storeManager, MsiAttributes $msiAttributes, Grouped $groupedType, CachedGetProductSalableQty $getProductSalableQty, Data $helper, IsSalableInterface $isSalable, GetAssignedSourceCodesBySkuInterface $getAssignedSourceCodesBySku, SourceItemManagement $sourceItemManagement ) { $this->getStockItemConfiguration = $getStockItemConfiguration; $this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite; $this->cncCatalogHelper = $cncCatalogHelper; $this->getAssignedSourceCodes = $getAssignedSourceCodes; $this->storeManager = $storeManager; $this->msiAttributes = $msiAttributes; $this->groupedType = $groupedType; $this->getProductSalableQty = $getProductSalableQty; $this->helper = $helper; $this->isSalable = $isSalable; $this->getAssignedSourceCodesBySku = $getAssignedSourceCodesBySku; $this->sourceItemManagement = $sourceItemManagement; } public function isSalable(ProductInterface $product) { return $this->isSalable->execute($product->getSku()); } /** * @param ProductInterface $product * @param bool $withTranslate * * @return string * @todo add project logic */ public function getStatusLabel(ProductInterface $product, $withTranslate = true): string { if ($this->isSalable->execute($product->getSku())) { return $withTranslate ? __('In Stock')->__toString() : 'In Stock'; } else { return $withTranslate ? __('Out of stock')->__toString() : 'Out of stock'; } } /** * @param ProductInterface $product * * @return string * @todo add project logic */ public function getStatusCssClass(ProductInterface $product): string { if ($this->isSalable->execute($product->getSku())) { return 'stock available'; } else { return 'stock unavailable'; } } /** * @param ProductInterface $product * @param string|null $sourceCode * @return Phrase|string * @throws LocalizedException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getDeliveryDelayLabel(ProductInterface $product, string $sourceCode = null, bool $i18n = true) { $option = $this->getDeliveryDelayOption($product, $sourceCode); $dropdownValue = $this->msiAttributes->getDropdownValue( (string) (is_array($option) && isset($option['availability']) ? $option['availability'] : null) ); $txt = $dropdownValue->getId() ? $dropdownValue->getValue() : ''; return $i18n ? __($txt) : $txt; } /** * @param ProductInterface $product * @param string|null $sourceCode * @return array|null * @throws LocalizedException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function getDeliveryDelayOption(ProductInterface $product, string $sourceCode = null): ?array { $attributeId = $this->msiAttributes->getAttributeId('cnc_availability'); $vhsAttributeId = $this->msiAttributes->getAttributeId('cnc_vhs'); if (!$attributeId) { return null; } $products = []; if ($product->getTypeId() == 'grouped') { $children = $this->groupedType->getChildrenIds($product->getId()); foreach ($children[Link::LINK_TYPE_GROUPED] as $childId) { $sku = $this->helper->getSkuById($childId); if ($sku) { $products []= $sku; } } } elseif ($product->getTypeId() == 'simple') { $products []= $product; } $sources = $this->getAssignedSourceCodes->execute($this->storeManager->getWebsite()->getCode()); $minAvailability = null; $minSource = null; $firstAvailability = null; $outOfStockAvailability = null; $firstSource = null; foreach ($products as $product) { if (is_array($sources) && count($sources)) { $sku = ($product instanceof ProductInterface ? $product->getSku() : $product); $productSources = $this->getAssignedSourceCodesBySku->execute($sku); foreach ($sources as $source) { if ($sourceCode && $source !== $sourceCode || !in_array($source, $productSources)) { continue; } elseif ($sourceCode && $source == $sourceCode) { return [ 'availability' => (int) $this->msiAttributes->getAttributeValue( $attributeId, $sku, $source )->getValue(), 'source' => $source ]; } $attributeValue = $this->msiAttributes->getAttributeValue( $attributeId, $sku, $source ); if (!$attributeValue) { continue; } if ($this->helper->getCncAvailabilityValue($sku, $source) == 'Out of stock') { if ($firstAvailability == null) { $outOfStockAvailability = (int) $attributeValue->getValue(); } continue; } if ($firstAvailability == null) { $firstAvailability = (int) $attributeValue->getValue(); $firstSource = $source; } $isVhs = $this->msiAttributes->getAttributeValue( $vhsAttributeId, $sku, $source ); $isVhs = $isVhs && $isVhs->getValue(); $qty = $this->getProductSalableQty->execute($sku, $source); if ($qty > 0 || $isVhs) { if ($minAvailability == null || ($minAvailability > (int) $attributeValue->getValue())) { $minSource = $source; } $minAvailability = $minAvailability != null ? min($minAvailability, (int) $attributeValue->getValue()) : (int) $attributeValue->getValue(); if ($qty > 0 || $isVhs) { break; } } } } } return [ 'availability' => ($minAvailability ? $minAvailability : ($firstAvailability == null && $outOfStockAvailability != null ? $outOfStockAvailability : $firstAvailability)), 'source' => ($minSource ? $minSource : $firstSource) ]; } /** * @param ProductInterface $product * @param string|null $sourceCode * @return Phrase */ public function getShippedFromLabel(ProductInterface $product, string $sourceCode = null): Phrase { return __( 'Ships from: %1', ($sourceCode ? ($sourceCode == 'cnc_fr' ? 'FR' : 'USA') : __($this->getDeliveryOriginLabel($product))) ); } /** * @param ProductInterface $product * @return string * @todo add project logic */ public function getDeliveryOriginLabel(ProductInterface $product): string { $option = $this->getDeliveryDelayOption($product); if (is_array($option) && isset($option['source'])) { return 'Stock ' . ($option['source'] == 'cnc_fr' ? 'FR' : 'USA'); } return 'Stock FR'; } /** * @param $sku * @return StockItemConfigurationInterface * @throws NotFoundException */ public function getStockItemConfigurationBySku($sku): StockItemConfigurationInterface { try { $stockId = $this->getStockIdForCurrentWebsite->execute(); return $this->getStockItemConfiguration->execute($sku, $stockId); } catch (LocalizedException $e) { throw new NotFoundException(__($e->getMessage())); } } /** * @param \Magento\Catalog\Model\Product $product * @return bool * @throws LocalizedException * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function isEstimationAvailable(\Magento\Catalog\Model\Product $product) { $isAvailable = false; if (!$this->hasProductSource($product)) { return false; } $option = $this->getDeliveryDelayOption($product); $availability = (is_array($option) && isset($option['availability']) ? $option['availability'] : null); $dropdownValue = $this->msiAttributes->getDropdownValue((string) $availability); if ($dropdownValue->getValue() && in_array($dropdownValue->getValue(), self::ESTIMATE_SHIPPING_AVAILABILITIES)) { $isAvailable = true; } return $isAvailable; } /** * @param \Magento\Catalog\Model\Product $product * @return bool */ public function hasProductSource(\Magento\Catalog\Model\Product $product) { return (bool)(count($this->sourceItemManagement->getProductCurrentStockSalableSourceCodes($product))); } }