Files
wms-app/app/ics/api/engine_report/stock_activity.php
T
2026-04-09 09:48:47 +07:00

65 lines
2.2 KiB
PHP

<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = 0;
$warehouse_id = (int)$data['warehouse'];
$whMgmt = new WarehouseManager($pdo2, $company_id);
$warehouse = $whMgmt->getStockContext($warehouse_id, $id);
$table = $warehouse['table'];
// ── Last 10 stock in/out transactions ────────────────────────────────
$sql = "SELECT
a.id,
a.date,
a.product_sku,
a.`in` AS stock_in,
a.`out` AS stock_out,
a.type,
a.description,
b.product_name
FROM `{$table}` a
LEFT JOIN md_product b ON
a.company_id = b.company_id and
a.product_sku = b.sku
WHERE
a.company_id = {$company_id} and
(a.`in` > 0 OR a.`out` > 0)
ORDER BY a.date DESC, a.id DESC
LIMIT 10";
$sth = $pdo2->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
// ── Build output ─────────────────────────────────────────────────────
$activities = [];
foreach ($rows as $row) {
$is_in = (float)$row['stock_in'] > 0;
$qty = $is_in ? (float)$row['stock_in'] : (float)$row['stock_out'];
$activities[] = [
'product_name' => $row['product_name'] ?: $row['product_sku'],
'sku' => $row['product_sku'],
'direction' => $is_in ? 'in' : 'out',
'qty' => round($qty, 2),
'type' => $row['type'],
'description' => $row['description'],
'date' => $row['date'],
];
}
// ── Response ─────────────────────────────────────────────────────────
$output = [
'activities' => $activities,
];
$answer["output"] = $output;
$answer["success"] = 1;
exit(json_encode($answer));
?>