Files
wms-app/app/order/api/engine/manage_order.php
T
2026-05-13 15:11:24 +07:00

42 lines
1.4 KiB
PHP

<?php
session_start();
require '../../../assets/utils/db_auth.php';
require_role($user_role, ['owner', 'admin', 'staff']);
require '../../../assets/utils/classes/OrderManager.php';
// Decode items JSON string back to array
$data['items'] = json_decode($data['items'] ?? '[]', true) ?: [];
try {
$new_id = null;
$quotation_id = (int)($data['quotation_id'] ?? 0);
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $quotation_id, &$new_id) {
$order = new OrderManager($pdo, $company_id);
$new_id = $order->saveOrder($data, $logging);
// On new order created from a quotation: link both records
if ($new_id && $quotation_id > 0) {
$pdo->prepare(
"UPDATE ac_quotation SET order_id = :order_id, status = 5
WHERE id = :id AND company_id = :cid AND status = 2"
)->execute([':order_id' => $new_id, ':id' => $quotation_id, ':cid' => $company_id]);
}
});
$answer['success'] = 1;
if ($new_id) {
$answer['new_id'] = $new_id;
}
} catch (PDOException $e) {
$answer['message'] = 'Database error, please try again.';
http_response_code(500);
} catch (Exception $e) {
$answer['message'] = $e->getMessage();
http_response_code(400);
}
exit(json_encode($answer));
?>