Files
2026-09-15 10:35:56 +08:00

160 lines
6.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\LingxingErp\Model\Warehouse;
use Magento\Framework\App\ResourceConnection;
class WarehouseRepository
{
public function __construct(private readonly ResourceConnection $resource)
{
}
/** @param array<string, mixed> $warehouse */
public function saveMetadata(array $warehouse): void
{
$now = gmdate('Y-m-d H:i:s');
$data = [
'wid' => (int)$warehouse['wid'],
'warehouse_name' => trim((string)($warehouse['name'] ?? '')),
'warehouse_type' => (int)($warehouse['type'] ?? 0),
'country_code' => trim((string)($warehouse['country_code'] ?? '')) ?: null,
'third_party_code' => trim((string)($warehouse['t_warehouse_code'] ?? '')) ?: null,
'third_party_name' => trim((string)($warehouse['t_warehouse_name'] ?? '')) ?: null,
'lingxing_status' => trim((string)($warehouse['t_status'] ?? '')) === ''
? 1
: (int)$warehouse['t_status'],
'is_deleted' => (int)($warehouse['is_delete'] ?? 0),
'last_synced_at' => $now,
'updated_at' => $now,
];
$this->resource->getConnection()->insertOnDuplicate(
$this->resource->getTableName('esun_lingxing_warehouse'),
$data,
array_keys(array_diff_key($data, ['wid' => true]))
);
}
public function assignSource(string $sourceCode, ?int $wid): void
{
$sourceCode = trim($sourceCode);
if ($sourceCode === '') {
throw new \InvalidArgumentException('Magento Source code is required.');
}
$connection = $this->resource->getConnection();
$table = $this->resource->getTableName('esun_lingxing_warehouse');
$connection->beginTransaction();
try {
$connection->update($table, ['source_code' => null], ['source_code = ?' => $sourceCode]);
if ($wid !== null) {
if ($wid <= 0) {
throw new \InvalidArgumentException('Lingxing warehouse ID must be positive.');
}
$updated = $connection->update(
$table,
['source_code' => $sourceCode],
['wid = ?' => $wid, 'warehouse_type = ?' => 3, 'is_deleted = ?' => 0]
);
if ($updated !== 1) {
throw new \RuntimeException(sprintf(
'Lingxing type 3 warehouse WID %d is not available. Synchronize warehouses first.',
$wid
));
}
}
$connection->commit();
} catch (\Throwable $exception) {
$connection->rollBack();
throw $exception;
}
}
public function getWidForSource(string $sourceCode): ?int
{
$connection = $this->resource->getConnection();
$value = $connection->fetchOne($connection->select()
->from($this->resource->getTableName('esun_lingxing_warehouse'), ['wid'])
->where('source_code = ?', $sourceCode)
->where('warehouse_type = ?', 3)
->limit(1));
return $value === false ? null : (int)$value;
}
/** @return array<int, array<string, mixed>> */
public function getInventoryTargets(): array
{
$connection = $this->resource->getConnection();
$warehouse = $this->resource->getTableName('esun_lingxing_warehouse');
$links = $this->resource->getTableName('inventory_source_stock_link');
return $connection->fetchAll($connection->select()
->distinct()
->from(['warehouse' => $warehouse])
->joinInner(['source_stock' => $links], 'source_stock.source_code = warehouse.source_code', [])
->where('warehouse.warehouse_type = ?', 3)
->where('warehouse.is_deleted = ?', 0)
->where('warehouse.lingxing_status = ?', 1)
->order('warehouse.wid ASC'));
}
/** @return int[] */
public function getWebsiteIdsForSource(string $sourceCode): array
{
$connection = $this->resource->getConnection();
$channelCondition = $connection->quoteInto(
'channel.stock_id = source_stock.stock_id AND channel.type = ?',
'website'
);
$select = $connection->select()
->distinct()
->from(['source_stock' => $this->resource->getTableName('inventory_source_stock_link')], [])
->joinInner(
['channel' => $this->resource->getTableName('inventory_stock_sales_channel')],
$channelCondition,
[]
)
->joinInner(
['website' => $this->resource->getTableName('store_website')],
'website.code = channel.code',
['website_id']
)
->where('source_stock.source_code = ?', $sourceCode)
->where('website.website_id > ?', 0);
return array_map('intval', $connection->fetchCol($select));
}
public function findSourceCodeByWid(int $wid): ?string
{
if ($wid <= 0) {
return null;
}
$connection = $this->resource->getConnection();
$value = $connection->fetchOne($connection->select()
->from($this->resource->getTableName('esun_lingxing_warehouse'), ['source_code'])
->where('wid = ?', $wid)
->where('is_deleted = ?', 0)
->limit(1));
return $value === false || trim((string)$value) === '' ? null : (string)$value;
}
/** @return string[] */
public function getExistingSourceItemSkus(string $sourceCode): array
{
$connection = $this->resource->getConnection();
return array_map('strval', $connection->fetchCol($connection->select()
->from($this->resource->getTableName('inventory_source_item'), ['sku'])
->where('source_code = ?', $sourceCode)));
}
public function markInventorySynchronized(int $wid): void
{
$now = gmdate('Y-m-d H:i:s');
$this->resource->getConnection()->update(
$this->resource->getTableName('esun_lingxing_warehouse'),
['last_inventory_synced_at' => $now, 'updated_at' => $now],
['wid = ?' => $wid]
);
}
}