Classe methods: remove reducdancy

This commit is contained in:
Thanakorn S
2026-05-29 08:56:58 +07:00
parent ed3dd2f215
commit a0677d6d8d
9 changed files with 112 additions and 93 deletions
+24 -19
View File
@@ -86,20 +86,6 @@ class OrderManager {
return 'td_stock_' . $warehouse_id;
}
private function getStockTables(): array
{
$sth = $this->pdo->prepare(
"SELECT t.table_name
FROM md_warehouse w
JOIN information_schema.tables t
ON t.table_schema = DATABASE()
AND t.table_name = CONCAT('td_stock_', w.id)
WHERE w.company_id = :company_id"
);
$sth->execute([':company_id' => $this->company_id]);
return $sth->fetchAll(PDO::FETCH_COLUMN);
}
/**
* FIFO bin pick — find the oldest approved stock-in row for a SKU
* in a given warehouse that is still bin-occupied.
@@ -198,7 +184,7 @@ class OrderManager {
$stock_out_id = (int)($item['stock_out_id'] ?? 0);
if ($warehouse_id <= 0 || $stock_out_id <= 0) continue;
$table = $this->stockTableNameFromWarehouseId($warehouse_id);
$table = 'td_stock_' . $warehouse_id;
$sth = $this->pdo->prepare(
"SELECT status FROM `{$table}`
WHERE company_id = :company_id
@@ -379,7 +365,7 @@ class OrderManager {
$stock_out_id = (int)($item['stock_out_id'] ?? 0);
if ($stock_out_warehouse_id > 0 && $stock_out_id > 0) {
$table = $this->stockTableNameFromWarehouseId($stock_out_warehouse_id);
$table = 'td_stock_' . $stock_out_warehouse_id;
$sth = $this->pdo->prepare(
"SELECT id, status, zone, aisle, bin, lot_number, serial_number
FROM `{$table}`
@@ -716,6 +702,22 @@ class OrderManager {
throw new Exception(implode("\n", $preflight_errors));
}
// Validate all warehouse IDs in one query before entering the item loop.
$itemWarehouseIds = array_values(array_unique(array_filter(
array_map(fn($item) => (int)($item['warehouse_id'] ?? 0), $items),
fn($id) => $id > 0
)));
if (!empty($itemWarehouseIds)) {
$placeholders = implode(',', array_fill(0, count($itemWarehouseIds), '?'));
$sth = $this->pdo->prepare(
"SELECT id FROM md_warehouse WHERE company_id = ? AND id IN ($placeholders)"
);
$sth->execute([$this->company_id, ...$itemWarehouseIds]);
$validWarehouseIds = array_flip($sth->fetchAll(PDO::FETCH_COLUMN));
} else {
$validWarehouseIds = [];
}
foreach ($items as $i => &$item) {
$warehouse_id = (int)($item['warehouse_id'] ?? 0);
@@ -725,7 +727,10 @@ class OrderManager {
$bin_stock = $this->pickFifoRack($warehouse_id, $product_sku);
$available_qty = (float)($bin_stock['available_qty'] ?? $bin_stock['in'] ?? 0);
$table = $this->stockTableNameFromWarehouseId($warehouse_id);
if (!isset($validWarehouseIds[$warehouse_id])) {
throw new Exception("Warehouse ID {$warehouse_id} not found.");
}
$table = 'td_stock_' . $warehouse_id;
$item_uuid = $uuid . '_' . $i;
$item_log = [array_merge($logging, ['action' => 'confirm_item'])];
@@ -888,8 +893,8 @@ class OrderManager {
}
// ── Reverse approved stock-out side effects, then soft-delete rows ─
$tables = $this->getStockTables();
$whMgmt = new WarehouseManager($this->pdo, $this->company_id);
$tables = $whMgmt->getStockTables();
foreach ($tables as $table) {
if (!preg_match('/^td_stock_(\d+)$/', (string)$table, $matches)) {
@@ -1034,7 +1039,7 @@ class OrderManager {
}
// Block if any approved stock-out rows exist; user must reverse via ICS first
$tables = $this->getStockTables();
$tables = (new WarehouseManager($this->pdo, $this->company_id))->getStockTables();
foreach ($tables as $table) {
$chk = $this->pdo->prepare(