Validate document lines on the server and recompute their totals, store notes with quotes/markup/emoji (utf8mb4, idempotent escaping, decode in form fields), exclude transfers from company-wide stock in/out, count revenue from confirmed orders only, one low-stock rule everywhere, list unapproved lots, natural bin sort, stable order/PO sort, status tiles that add up.
37 lines
1.2 KiB
PHP
37 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* alert_low_stock.php — Called by Node.js scheduler daily at 8am.
|
|
* Returns companies that have one or more low-stock products.
|
|
* Response: { success: 1, alerts: [{ company_id, count }] }
|
|
*/
|
|
require __DIR__ . '/../assets/utils/cron_auth.php';
|
|
|
|
// Same rule as ReportManager::getLowStockItems(): counted per warehouse, at or
|
|
// below the higher of reorder point and minimum stock.
|
|
$sth = $pdo2->query(
|
|
"SELECT e.company_id, COUNT(*) AS count
|
|
FROM (
|
|
SELECT company_id, warehouse_id, product_sku, SUM(total_in - total_out) AS balance
|
|
FROM etl_stock_summary
|
|
WHERE company_id > 0
|
|
GROUP BY company_id, warehouse_id, product_sku
|
|
) e
|
|
JOIN md_product p
|
|
ON p.sku = e.product_sku
|
|
AND p.company_id = e.company_id
|
|
AND p.status > 0
|
|
JOIN md_warehouse w
|
|
ON w.id = e.warehouse_id
|
|
AND w.company_id = e.company_id
|
|
AND w.status = 1
|
|
WHERE GREATEST(p.reorder_point, p.min_stock) > 0
|
|
AND e.balance <= GREATEST(p.reorder_point, p.min_stock)
|
|
GROUP BY e.company_id
|
|
HAVING count > 0"
|
|
);
|
|
|
|
$answer['success'] = 1;
|
|
$answer['alerts'] = $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
exit(json_encode($answer));
|