ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
+118
View File
@@ -0,0 +1,118 @@
<?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,
];
}
}
+17
View File
@@ -0,0 +1,17 @@
{
"name": "jr/inventory",
"description": "",
"type": "magento2-module",
"version": "1.0.0",
"license": [
"MIT"
],
"autoload": {
"psr-4": {
"JR\\Inventory\\": ""
},
"files": [
"registration.php"
]
}
}
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="JR_Inventory" setup_version="1.0.0" />
</config>
+6
View File
@@ -0,0 +1,6 @@
<?php
Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'JR_Inventory',
__DIR__
);
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="product.info.stock.sku">
<block class="Magento\Catalog\Block\Product\View" name="product.info.stock.details" as="product_info_stock_details" template="JR_Inventory::product/view/stock_details.phtml">
<arguments>
<argument name="view_model" xsi:type="object">JR\Inventory\ViewModel\StockDetailsViewModel</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
@@ -0,0 +1,25 @@
<?php
/** @var JR\Inventory\ViewModel\StockDetailsViewModel $viewModel */
$viewModel = $block->getData('view_model');
if ($viewModel) {
// $sku = $block->getProduct()->getSku();
$stockDetails = $viewModel->getStockDetails($block->getProduct());
$stockStatus = $stockDetails['status'];
if ($stockDetails['show']) {
echo "<div>{$stockStatus}: {$stockDetails['total']}</div>";
} else {
echo "<div>{$stockStatus}</div>";
}
// Output the stock data as needed
if ($stockDetails['show']) {
foreach ($stockDetails['data'] as $source => $details) {
if ($details['quantity'] == 0) {
continue;
}
echo "<div>{$source}: {$details['quantity']}</div>";
}
}
}