60 lines
1.8 KiB
PHP
60 lines
1.8 KiB
PHP
<?php
|
|
session_start();
|
|
require '../../../assets/utils/db_auth.php';
|
|
|
|
$product_sku = $data['product_sku'] ?? '';
|
|
$lot_number = $data['lot_number'] ?? '';
|
|
|
|
if (!$product_sku || !$lot_number) {
|
|
$answer['output'] = [];
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
// Discover all warehouses
|
|
$sth = $pdo2->prepare(
|
|
"SELECT id, warehouse_name FROM md_warehouse
|
|
WHERE company_id = :company_id AND status = 1"
|
|
);
|
|
$sth->execute([':company_id' => $company_id]);
|
|
$warehouses = $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$rows = [];
|
|
foreach ($warehouses as $wh) {
|
|
$safe = preg_replace('/[^a-zA-Z0-9_]/', '', $wh['warehouse_name']);
|
|
$table = "td_stock_{$safe}";
|
|
|
|
$sth = $pdo2->prepare(
|
|
"SELECT
|
|
s.id,
|
|
s.date,
|
|
s.type,
|
|
s.zone,
|
|
s.aisle,
|
|
s.rack,
|
|
ROUND(COALESCE(s.`in`, 0), 2) AS stock_in,
|
|
ROUND(COALESCE(s.`out`, 0), 2) AS stock_out,
|
|
s.serial_number,
|
|
s.description,
|
|
'{$wh['warehouse_name']}' AS warehouse_name
|
|
FROM `{$table}` s
|
|
WHERE s.company_id = :company_id
|
|
AND s.product_sku = :product_sku
|
|
AND s.lot_number = :lot_number
|
|
ORDER BY s.date DESC"
|
|
);
|
|
$sth->execute([
|
|
':company_id' => $company_id,
|
|
':product_sku' => $product_sku,
|
|
':lot_number' => $lot_number,
|
|
]);
|
|
$rows = array_merge($rows, $sth->fetchAll(PDO::FETCH_ASSOC));
|
|
}
|
|
|
|
// Sort all rows by date DESC
|
|
usort($rows, fn($a, $b) => strcmp($b['date'], $a['date']));
|
|
|
|
$answer['output'] = $rows;
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
?>
|