formating order number
This commit is contained in:
parent
2807096c56
commit
3f1e18552b
@ -46,10 +46,26 @@ try {
|
||||
$stmt_prefix->execute([$branch_id]);
|
||||
$prefix = $stmt_prefix->fetchColumn() ?: 'ORD';
|
||||
if (strlen($prefix) > 3) $prefix = substr($prefix, 0, 3);
|
||||
$prefix = str_pad($prefix, 3, 'X'); // Just in case it's shorter than 3
|
||||
$prefix = strtoupper(str_pad($prefix, 3, 'X'));
|
||||
|
||||
// Format order_number as XXX#####1 (5 digits for #####)
|
||||
$order_number = strtoupper($prefix) . str_pad($order_id, 5, '0', STR_PAD_LEFT) . '1';
|
||||
// Determine the next serial number for this branch
|
||||
// We look at existing order numbers for this branch that follow the XXX-###### format
|
||||
$stmt_max = $pdo->prepare("SELECT order_number FROM orders WHERE branch_id = ? AND order_number LIKE ? ORDER BY id DESC LIMIT 100");
|
||||
$stmt_max->execute([$branch_id, "$prefix-%"]);
|
||||
$existing_orders = $stmt_max->fetchAll(PDO::FETCH_COLUMN);
|
||||
|
||||
$max_serial = 0;
|
||||
foreach ($existing_orders as $onum) {
|
||||
// Format is XXX-######.
|
||||
if (preg_match('/' . preg_quote($prefix) . '-(\d{6})/', $onum, $matches)) {
|
||||
$serial = (int)$matches[1];
|
||||
if ($serial > $max_serial) $max_serial = $serial;
|
||||
}
|
||||
}
|
||||
$next_serial = $max_serial + 1;
|
||||
|
||||
// Format order_number as XXX-###### (6 digits)
|
||||
$order_number = $prefix . '-' . str_pad($next_serial, 6, '0', STR_PAD_LEFT);
|
||||
|
||||
// Update the order with the generated order_number
|
||||
$stmt_update = $pdo->prepare("UPDATE orders SET order_number = ? WHERE id = ?");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user