119 lines
3.7 KiB
PHP
Executable File
119 lines
3.7 KiB
PHP
Executable File
<?php
|
|
namespace JR\Inventory\ViewModel;
|
|
|
|
use JR\NetSuite\Logger\Logger;
|
|
use Lts\Catalog\Helper\ShowProductOrNot;
|
|
use Lts\Inventory\Model\SourceCustomerRepository;
|
|
use Magento\Catalog\Model\Product;
|
|
use Magento\Framework\Exception\InputException;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
|
use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface;
|
|
use Magento\InventoryApi\Api\Data\SourceItemInterface;
|
|
use Magento\Inventory\Model\SourceRepository;
|
|
use Magento\Customer\Model\Session as CustomerSession;
|
|
use Magento\Inventory\Model\Stock;
|
|
use Magento\InventorySales\Model\GetProductSalableQty;
|
|
|
|
class StockDetailsViewModel implements ArgumentInterface
|
|
{
|
|
private $getSourceItemsBySku;
|
|
private $sourceRepository;
|
|
private $customerSession;
|
|
private SourceCustomerRepository $sourceCustomerRepository;
|
|
private ShowProductOrNot $showProductOrNot;
|
|
private Logger $logger;
|
|
|
|
const MAIN_STOCK = 'Main';
|
|
private Stock $stock;
|
|
private GetProductSalableQty $getProductSalableQty;
|
|
|
|
public function __construct(
|
|
GetSourceItemsBySkuInterface $getSourceItemsBySku,
|
|
SourceRepository $sourceRepository,
|
|
CustomerSession $customerSession,
|
|
SourceCustomerRepository $sourceCustomerRepository,
|
|
ShowProductOrNot $showProductOrNot,
|
|
Stock $stock,
|
|
GetProductSalableQty $getProductSalableQty,
|
|
Logger $logger
|
|
) {
|
|
$this->getSourceItemsBySku = $getSourceItemsBySku;
|
|
$this->sourceRepository = $sourceRepository;
|
|
$this->customerSession = $customerSession;
|
|
$this->sourceCustomerRepository = $sourceCustomerRepository;
|
|
$this->showProductOrNot = $showProductOrNot;
|
|
$this->stock = $stock;
|
|
$this->getProductSalableQty = $getProductSalableQty;
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
protected function getStockBySku(string $sku): float|int
|
|
{
|
|
try {
|
|
$stock = $this->stock->load(self::MAIN_STOCK, 'name');
|
|
return $this->getProductSalableQty->execute($sku, $stock->getId());
|
|
} catch (InputException | LocalizedException $e) {
|
|
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function getStockDetails(Product $product)
|
|
{
|
|
$sourceItems = $this->getSourceItemsBySku->execute($product->getSku());
|
|
$stockData = [];
|
|
$stockStatus = 'Out of Stock';
|
|
$total = 0;
|
|
|
|
if (!$product->isSalable()) {
|
|
return [
|
|
'show' => false,
|
|
'status' => $stockStatus,
|
|
'data' => [],
|
|
'total' => $total,
|
|
];
|
|
}
|
|
|
|
$preferSourceCode = null;
|
|
if ($this->customerSession->isLoggedIn()) {
|
|
try {
|
|
$preferSource = $this->sourceCustomerRepository->getFromCustomerId($this->customerSession->getCustomerId());
|
|
$preferSourceCode = $preferSource->getSourceCode();
|
|
} catch (LocalizedException $e) {
|
|
|
|
}
|
|
}
|
|
|
|
// $stockQty = $this->getStockBySku($sku);
|
|
foreach ($sourceItems as $sourceItem) {
|
|
$sourceCode = $sourceItem->getSourceCode();
|
|
$source = $this->sourceRepository->get($sourceCode);
|
|
if ($sourceCode == 'default') {
|
|
continue;
|
|
}
|
|
|
|
$status = $sourceItem->getStatus() == SourceItemInterface::STATUS_IN_STOCK ? 'In Stock' : 'Out of Stock';
|
|
if ($status == 'In Stock' && $sourceCode != 'default') {
|
|
$stockStatus = 'In Stock';
|
|
}
|
|
$quantity = $sourceItem->getQuantity();
|
|
$total += $quantity;
|
|
if ($sourceCode == $preferSourceCode && $quantity) {
|
|
$stockData[$source->getName()] = [
|
|
'status' => $status,
|
|
'quantity' => $quantity,
|
|
];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'show' => $this->showProductOrNot->checkByNetsuiteStatus(),
|
|
'status' => $stockStatus,
|
|
'data' => $stockData,
|
|
'total' => $total,
|
|
];
|
|
}
|
|
}
|
|
|