stock dash
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
|
||||
|
||||
$sql = "SELECT SUM(total_in) as total_in, SUM(total_out) as total_out, b.min_stock
|
||||
FROM warehouse_balance 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
|
||||
GROUP BY a.product_sku
|
||||
";
|
||||
$sth = $pdo2->prepare($sql);
|
||||
$sth->execute([
|
||||
":company_id" => $company_id
|
||||
]);
|
||||
db_check($sth, $answer);
|
||||
|
||||
$output = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
// calculate stock balance
|
||||
$low_stock = 0;
|
||||
foreach($output as $key => $value) {
|
||||
$output[$key]["low_stock"] = ($value["total_in"] - $value["total_out"]) < $value["min_stock"]?true:false;
|
||||
if ($output[$key]["low_stock"]) {
|
||||
$low_stock++;
|
||||
}
|
||||
}
|
||||
$answer["output"] = $output;
|
||||
$answer["low_stock"] = $low_stock;
|
||||
$answer["success"] = 1;
|
||||
exit(json_encode($answer));
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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));
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,69 @@
|
||||
<?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'];
|
||||
|
||||
|
||||
// ── Always last 12 months ────────────────────────────────────────────
|
||||
$now = new DateTime();
|
||||
$start = (clone $now)->modify('-12 months')->format('Y-m-d 00:00:00');
|
||||
$end = $now->format('Y-m-d 23:59:59');
|
||||
|
||||
// ── Query: group by month ────────────────────────────────────────────
|
||||
$sql = "SELECT
|
||||
DATE_FORMAT(a.date, '%b') AS `label`,
|
||||
DATE_FORMAT(a.date, '%Y-%m') AS `sort_key`,
|
||||
ROUND(SUM(a.`in`), 2) AS `stock_in`,
|
||||
ROUND(SUM(a.`out`), 2) AS `stock_out`
|
||||
FROM `{$table}` a
|
||||
WHERE
|
||||
a.company_id = {$company_id} and
|
||||
a.date >= '{$start}' and
|
||||
a.date <= '{$end}'
|
||||
GROUP BY `sort_key`, `label`
|
||||
ORDER BY `sort_key` ASC";
|
||||
|
||||
$sth = $pdo2->prepare($sql);
|
||||
$sth->execute();
|
||||
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// ── Index DB results by sort_key (YYYY-MM) ──────────────────────────
|
||||
$dataByMonth = [];
|
||||
foreach ($rows as $row) {
|
||||
$dataByMonth[$row['sort_key']] = $row;
|
||||
}
|
||||
|
||||
// ── Always build 12 months, fill zeros where no data ────────────────
|
||||
$labels = [];
|
||||
$stock_in = [];
|
||||
$stock_out = [];
|
||||
|
||||
for ($i = 11; $i >= 0; $i--) {
|
||||
$dt = (clone $now)->modify("-{$i} months");
|
||||
$key = $dt->format('Y-m'); // e.g. "2025-04"
|
||||
$label = $dt->format('M'); // e.g. "Apr"
|
||||
|
||||
$labels[] = $label;
|
||||
$stock_in[] = isset($dataByMonth[$key]) ? round((float)$dataByMonth[$key]['stock_in'], 2) : 0;
|
||||
$stock_out[] = isset($dataByMonth[$key]) ? round((float)$dataByMonth[$key]['stock_out'], 2) : 0;
|
||||
}
|
||||
|
||||
// ── Response ─────────────────────────────────────────────────────────
|
||||
$output = [
|
||||
'labels' => $labels,
|
||||
'stock_in' => $stock_in,
|
||||
'stock_out' => $stock_out,
|
||||
];
|
||||
|
||||
$answer["output"] = $output;
|
||||
$answer["success"] = 1;
|
||||
exit(json_encode($answer));
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,83 @@
|
||||
<?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'];
|
||||
|
||||
|
||||
// ── Always last 12 months ────────────────────────────────────────────
|
||||
$now = new DateTime();
|
||||
$start = (clone $now)->modify('-12 months')->format('Y-m-d 00:00:00');
|
||||
$end = $now->format('Y-m-d 23:59:59');
|
||||
|
||||
// ── Get cumulative balance BEFORE the 12-month window ────────────────
|
||||
$sql_prior = "SELECT
|
||||
ROUND(COALESCE(SUM(a.`in`), 0) - COALESCE(SUM(a.`out`), 0), 2) AS `balance`
|
||||
FROM `{$table}` a
|
||||
WHERE
|
||||
a.company_id = {$company_id} and
|
||||
a.date < '{$start}'";
|
||||
|
||||
$sth = $pdo2->prepare($sql_prior);
|
||||
$sth->execute();
|
||||
$prior_balance = (float)$sth->fetchColumn();
|
||||
|
||||
// ── Get monthly in/out for the 12-month window ──────────────────────
|
||||
$sql = "SELECT
|
||||
DATE_FORMAT(a.date, '%Y-%m') AS `sort_key`,
|
||||
ROUND(SUM(a.`in`), 2) AS `stock_in`,
|
||||
ROUND(SUM(a.`out`), 2) AS `stock_out`
|
||||
FROM `{$table}` a
|
||||
WHERE
|
||||
a.company_id = {$company_id} and
|
||||
a.date >= '{$start}' and
|
||||
a.date <= '{$end}'
|
||||
GROUP BY `sort_key`
|
||||
ORDER BY `sort_key` ASC";
|
||||
|
||||
$sth = $pdo2->prepare($sql);
|
||||
$sth->execute();
|
||||
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// ── Index DB results by sort_key (YYYY-MM) ──────────────────────────
|
||||
$dataByMonth = [];
|
||||
foreach ($rows as $row) {
|
||||
$dataByMonth[$row['sort_key']] = $row;
|
||||
}
|
||||
|
||||
// ── Always build 12 months, accumulate running balance ──────────────
|
||||
$labels = [];
|
||||
$balance = [];
|
||||
$running = $prior_balance;
|
||||
|
||||
for ($i = 11; $i >= 0; $i--) {
|
||||
$dt = (clone $now)->modify("-{$i} months");
|
||||
$key = $dt->format('Y-m');
|
||||
$label = $dt->format('M');
|
||||
|
||||
$month_in = isset($dataByMonth[$key]) ? (float)$dataByMonth[$key]['stock_in'] : 0;
|
||||
$month_out = isset($dataByMonth[$key]) ? (float)$dataByMonth[$key]['stock_out'] : 0;
|
||||
|
||||
$running += ($month_in - $month_out);
|
||||
|
||||
$labels[] = $label;
|
||||
$balance[] = round($running, 2);
|
||||
}
|
||||
|
||||
// ── Response ─────────────────────────────────────────────────────────
|
||||
$output = [
|
||||
'labels' => $labels,
|
||||
'balance' => $balance,
|
||||
];
|
||||
|
||||
$answer["output"] = $output;
|
||||
$answer["success"] = 1;
|
||||
exit(json_encode($answer));
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
|
||||
|
||||
$sql = "SELECT SUM(total_in) as total_in, SUM(total_out) as total_out, COUNT(DISTINCT warehouse_id) as total_warehouse
|
||||
FROM warehouse_balance a
|
||||
WHERE
|
||||
a.company_id = :company_id
|
||||
";
|
||||
$sth = $pdo2->prepare($sql);
|
||||
$sth->execute([
|
||||
":company_id" => $company_id
|
||||
]);
|
||||
db_check($sth, $answer);
|
||||
|
||||
$output = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$answer["output"] = $output;
|
||||
$answer["success"] = 1;
|
||||
exit(json_encode($answer));
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user