49 lines
1.7 KiB
PHP
49 lines
1.7 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'
|
|
*/
|
|
require_once __DIR__ . '/../../../session.php';
|
|
require_once '../../../assets/utils/db_auth.php';
|
|
require_once '../../../assets/utils/classes/StockManager.php';
|
|
require_once '../../../assets/utils/classes/WarehouseManager.php';
|
|
require_once '../../../assets/utils/notify_node.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);
|
|
});
|
|
|
|
notify_node('stock_updated', ['warehouse_id' => $warehouse_id, 'type' => $type], $company_id);
|
|
|
|
$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));
|
|
} |