accounting modules
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require '../../../assets/utils/classes/OrderManager.php';
|
||||
|
||||
$quotation_id = (int)($data['quotation_id'] ?? 0);
|
||||
if (!$quotation_id) {
|
||||
exit(json_encode(['success' => 0, 'message' => 'Quotation ID required']));
|
||||
}
|
||||
|
||||
// Load quotation — must be Accepted (status=2)
|
||||
$sth = $pdo2->prepare(
|
||||
"SELECT q.*, c.contact_name
|
||||
FROM ac_quotation q
|
||||
LEFT JOIN md_contact c ON c.id = q.contact_id
|
||||
WHERE q.id = :id AND q.company_id = :cid AND q.status = 2
|
||||
LIMIT 1"
|
||||
);
|
||||
$sth->execute([':id' => $quotation_id, ':cid' => $company_id]);
|
||||
$q = $sth->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$q) {
|
||||
exit(json_encode(['success' => 0, 'message' => 'Quotation not found or not in Accepted status']));
|
||||
}
|
||||
|
||||
// Check not already converted
|
||||
if ((int)$q['order_id'] > 0) {
|
||||
exit(json_encode(['success' => 0, 'message' => 'Quotation has already been converted to an order']));
|
||||
}
|
||||
|
||||
// Map quotation items → order items (warehouse_id left 0 = to be filled by WMS)
|
||||
$qt_items = json_decode($q['items'] ?? '[]', true) ?: [];
|
||||
$ord_items = [];
|
||||
foreach ($qt_items as $i => $qi) {
|
||||
$ord_items[] = [
|
||||
'item_id' => $i,
|
||||
'product_sku' => $qi['product_sku'] ?? '',
|
||||
'product_name' => $qi['description'] ?? '',
|
||||
'warehouse_id' => 0,
|
||||
'zone' => '',
|
||||
'aisle' => '',
|
||||
'rack' => '',
|
||||
'lot_number' => '',
|
||||
'serial_number' => '',
|
||||
'quantity' => (float)($qi['quantity'] ?? 0),
|
||||
'price' => (float)($qi['unit_price'] ?? 0),
|
||||
'total_price' => (float)($qi['total_price'] ?? 0),
|
||||
'stock_out_id' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
$order_data = [
|
||||
'quotation_id' => $quotation_id,
|
||||
'contact_id' => (int)$q['contact_id'],
|
||||
'order_date' => date('Y-m-d'),
|
||||
'items' => $ord_items,
|
||||
'discount' => (float)$q['discount'],
|
||||
'tax' => (float)$q['tax'],
|
||||
'shipping_fee' => 0,
|
||||
'shipping_tracking_number' => '',
|
||||
'notes' => $q['notes'] ?? '',
|
||||
];
|
||||
|
||||
try {
|
||||
$new_order_id = null;
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($order_data, $company_id, $logging, $quotation_id, &$new_order_id) {
|
||||
$order = new OrderManager($pdo, $company_id);
|
||||
$new_order_id = $order->saveOrder($order_data, $logging);
|
||||
|
||||
// Mark quotation as Converted and link order_id
|
||||
$pdo->prepare(
|
||||
"UPDATE ac_quotation SET status = 5, order_id = :oid
|
||||
WHERE id = :id AND company_id = :cid"
|
||||
)->execute([':oid' => $new_order_id, ':id' => $quotation_id, ':cid' => $company_id]);
|
||||
});
|
||||
|
||||
$answer['success'] = 1;
|
||||
$answer['message'] = 'Order created successfully';
|
||||
$answer['order_id'] = $new_order_id;
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer['message'] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
Reference in New Issue
Block a user