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
@@ -0,0 +1,154 @@
<?php
declare(strict_types=1);
namespace Lts\Catalog\ViewModel;
use Lts\Catalog\Helper\ShowProductOrNot;
use Lts\Inventory\Api\SourceCustomerRepositoryInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\InventoryApi\Api\GetSourceItemsBySkuInterface;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
class ProductAvailability implements ArgumentInterface
{
private ?SourceInterface $pickupSource = null;
private bool $pickupSourceResolved = false;
private array $sourceQty = [];
private array $deliveryQty = [];
public function __construct(
private readonly CustomerSession $customerSession,
private readonly ShowProductOrNot $showProductOrNot,
private readonly SourceCustomerRepositoryInterface $sourceCustomerRepository,
private readonly GetSourceItemsBySkuInterface $getSourceItemsBySku,
private readonly GetProductSalableQtyInterface $getProductSalableQty,
private readonly StockResolverInterface $stockResolver,
private readonly StoreManagerInterface $storeManager
) {
}
public function isLoggedIn(): bool
{
return $this->customerSession->isLoggedIn();
}
public function canPurchase(): bool
{
return $this->showProductOrNot->checkByNetsuiteStatus();
}
public function getPickupSource(): ?SourceInterface
{
if ($this->pickupSourceResolved) {
return $this->pickupSource;
}
$this->pickupSourceResolved = true;
if (!$this->isLoggedIn()) {
return null;
}
try {
$items = $this->sourceCustomerRepository
->getList((string) $this->customerSession->getCustomerId())
->getItems();
} catch (\Throwable) {
return null;
}
$address = $this->customerSession->getCustomer()->getDefaultShippingAddress()
?: $this->customerSession->getCustomer()->getDefaultBillingAddress();
$customerCity = $address ? trim((string) $address->getCity()) : '';
$fallback = null;
foreach ($items as $item) {
$source = $item->getSource();
if (!$source) {
continue;
}
$fallback ??= $source;
if ($item->getCustomerId()) {
$this->pickupSource = $source;
return $this->pickupSource;
}
if ($customerCity !== '' && strcasecmp($customerCity, (string) $source->getCity()) === 0) {
$this->pickupSource = $source;
return $this->pickupSource;
}
}
$this->pickupSource = $fallback;
return $this->pickupSource;
}
public function getPickupCity(): string
{
$source = $this->getPickupSource();
return $source && trim((string) $source->getCity()) !== ''
? trim((string) $source->getCity())
: (string) __('Local Branch');
}
public function getPickupQuantity(string $sku): float
{
if (array_key_exists($sku, $this->sourceQty)) {
return $this->sourceQty[$sku];
}
$source = $this->getPickupSource();
if (!$source) {
return $this->sourceQty[$sku] = 0.0;
}
try {
foreach ($this->getSourceItemsBySku->execute($sku) as $sourceItem) {
if ($sourceItem->getSourceCode() === $source->getSourceCode()
&& (int) $sourceItem->getStatus() === 1) {
return $this->sourceQty[$sku] = max(0.0, (float) $sourceItem->getQuantity());
}
}
} catch (\Throwable) {
return $this->sourceQty[$sku] = 0.0;
}
return $this->sourceQty[$sku] = 0.0;
}
public function getDeliveryQuantity(string $sku): float
{
if (array_key_exists($sku, $this->deliveryQty)) {
return $this->deliveryQty[$sku];
}
try {
$websiteCode = $this->storeManager->getWebsite()->getCode();
$stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
$qty = $this->getProductSalableQty->execute($sku, (int) $stock->getStockId());
return $this->deliveryQty[$sku] = max(0.0, (float) $qty);
} catch (\Throwable) {
return $this->deliveryQty[$sku] = 0.0;
}
}
public function getDeliveryPostcode(): string
{
if (!$this->isLoggedIn()) {
return '';
}
$address = $this->customerSession->getCustomer()->getDefaultShippingAddress()
?: $this->customerSession->getCustomer()->getDefaultBillingAddress();
return $address ? trim((string) $address->getPostcode()) : '';
}
public function formatQuantity(float $quantity): string
{
return abs($quantity - round($quantity)) < 0.001
? (string) (int) round($quantity)
: rtrim(rtrim(number_format($quantity, 2, '.', ''), '0'), '.');
}
}