[OOP] contact,inventory,ics,dashboard

This commit is contained in:
Thanakorn S
2026-04-28 15:44:57 +07:00
parent 3d3b497c08
commit 2061624641
49 changed files with 1946 additions and 2146 deletions
+5 -61
View File
@@ -1,66 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
require '../../../assets/utils/classes/ReportManager.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;
$report = new ReportManager($pdo2, $company_id);
$answer['output'] = $report->getWarehouseActivity((int)$data['warehouse']);
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -65
View File
@@ -1,70 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
require '../../../assets/utils/classes/ReportManager.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;
$report = new ReportManager($pdo2, $company_id);
$answer['output'] = $report->getWarehouseStockMovement((int)$data['warehouse']);
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -79
View File
@@ -1,84 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
require '../../../assets/utils/classes/ReportManager.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;
$report = new ReportManager($pdo2, $company_id);
$answer['output'] = $report->getWarehouseStockTrend((int)$data['warehouse']);
$answer['success'] = 1;
exit(json_encode($answer));
?>
@@ -1,24 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/ReportManager.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;
$report = new ReportManager($pdo2, $company_id);
$answer['output'] = $report->getWarehouseBalanceSummary();
$answer['success'] = 1;
exit(json_encode($answer));
?>