66 lines
2.1 KiB
PHP
66 lines
2.1 KiB
PHP
<?php
|
|
session_start();
|
|
require_once '../../../assets/utils/db_auth.php';
|
|
require_role($user_role, ['owner', 'admin']);
|
|
require_once '../../../assets/utils/classes/EtlStockManager.php';
|
|
|
|
$etl = new EtlStockManager($pdo2, $pdo1, $company_id);
|
|
$action = $data['action'] ?? '';
|
|
|
|
try {
|
|
if ($action === 'read') {
|
|
$answer['output'] = $etl->getStatus();
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
if ($action === 'get_current_gaps') {
|
|
$gaps = $etl->getGaps();
|
|
$answer['output'] = ['current_gaps' => count($gaps)];
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
if ($action === 'get_warehouses') {
|
|
$sth = $pdo2->prepare(
|
|
"SELECT id, warehouse_name FROM md_warehouse
|
|
WHERE company_id = :cid AND status = 1
|
|
ORDER BY warehouse_name"
|
|
);
|
|
$sth->execute([':cid' => $company_id]);
|
|
$answer['output'] = $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
if ($action === 'get_periods') {
|
|
$warehouse_id = (int)($data['warehouse_id'] ?? 0);
|
|
if (!$warehouse_id) {
|
|
$answer['message'] = 'warehouse_id is required.';
|
|
exit(json_encode($answer));
|
|
}
|
|
$answer['output'] = $etl->getDistinctPeriods($warehouse_id);
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
if ($action === 'rebuild_period') {
|
|
$warehouse_id = (int)($data['warehouse_id'] ?? 0);
|
|
$period = $data['period'] ?? '';
|
|
if (!$warehouse_id || !preg_match('/^\d{4}-\d{2}$/', $period)) {
|
|
$answer['message'] = 'Valid warehouse_id and period (YYYY-MM) are required.';
|
|
exit(json_encode($answer));
|
|
}
|
|
$etl->rebuildPeriod($warehouse_id, $period);
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
$answer['message'] = 'Invalid action.';
|
|
} catch (Exception $e) {
|
|
$answer['message'] = $e->getMessage();
|
|
http_response_code(400);
|
|
}
|
|
|
|
exit(json_encode($answer));
|