Files
wms-app/app/cron/etl_stock_maintenance.php
2026-05-25 13:30:10 +07:00

42 lines
1.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* etl_stock_maintenance.php — Called by Node.js scheduler every hour.
* Checks and repairs etl_stock_summary for companies in the current slot.
*
* Body: { slot: 14 } (0–23, = HOUR(NOW()) from Node.js)
*/
require __DIR__ . '/../assets/utils/cron_auth.php';
require_once __DIR__ . '/../assets/utils/classes/EtlStockManager.php';
$slot = (int)($data['slot'] ?? -1);
if ($slot < 0 || $slot > 23) {
http_response_code(400);
exit(json_encode(['success' => 0, 'message' => 'Invalid slot.']));
}
$sth = $pdo1->prepare(
"SELECT company_id FROM company_list WHERE company_id % 24 = :slot"
);
$sth->execute([':slot' => $slot]);
$companies = $sth->fetchAll(PDO::FETCH_COLUMN);
$results = [];
foreach ($companies as $company_id) {
try {
$etl = new EtlStockManager($pdo2, $pdo1, (int)$company_id);
$result = $etl->checkAndRepair();
$results[] = [
'company_id' => $company_id,
'gaps_found' => $result['gaps_found'],
'periods_rebuilt' => $result['periods_rebuilt'],
];
} catch (Exception $e) {
$results[] = ['company_id' => $company_id, 'error' => $e->getMessage()];
}
}
$answer['success'] = 1;
$answer['slot'] = $slot;
$answer['results'] = $results;
exit(json_encode($answer));