Files
wms-app/app/ics/api/engine/approve_stock.php
T

46 lines
1.5 KiB
PHP

<?php
/**
* approve_stock.php — Approve a draft td_stock row (status 0 → 1).
*
* Flips status to 1 and calls adjustBalance() so etl_stock_summary
* reflects the transaction. This is the ONLY place adjustBalance() is
* called — both auto-approve (on save) and manual approve go through here.
*
* Expected $data keys:
* id int td_stock row id
* warehouse int warehouse_id (to resolve the dynamic table)
* type string 'in' | 'out' | 'transfer'
*/
session_start();
require_once '../../../assets/utils/db_auth.php';
require_once '../../../assets/utils/classes/StockManager.php';
require_once '../../../assets/utils/classes/WarehouseManager.php';
require_role($user_role, ['owner', 'admin']);
$id = (int)($data['id'] ?? 0);
$warehouse_id = (int)($data['warehouse'] ?? 0);
$type = $data['type'] ?? '';
if (!$id || !$warehouse_id || !in_array($type, ['in', 'out', 'transfer'])) {
http_response_code(400);
$answer['message'] = 'Invalid parameters.';
exit(json_encode($answer));
}
try {
dbTransaction($pdo2, function($pdo) use ($id, $warehouse_id, $type, $company_id) {
$stock = new StockManager($pdo, $company_id);
$whMgmt = new WarehouseManager($pdo, $company_id);
$stock->approveStock($id, $warehouse_id, $type, $whMgmt);
});
$answer['success'] = 1;
$answer['message'] = 'Stock approved.';
exit(json_encode($answer));
} catch (Exception $e) {
http_response_code(400);
$answer['message'] = $e->getMessage();
exit(json_encode($answer));
}