Add tests to ensure order PO defaults from booking when missing and enhance existing routing logic.
This commit is contained in:
@@ -16,6 +16,7 @@ use objects\collected_order_invoices_o;
|
||||
use objects\departments_o;
|
||||
use objects\economic_module_orders;
|
||||
use objects\logs_o;
|
||||
use objects\order_bookings_o;
|
||||
use objects\orders_o;
|
||||
use objects\stripe_module_orders_o;
|
||||
use objects\stripe_payment_intents_o;
|
||||
@@ -193,18 +194,25 @@ class ordersRoute
|
||||
$response->error($e->getMessage(), 400);
|
||||
}
|
||||
|
||||
$bookingId = !empty($data['booking_id']) ? (int)$data['booking_id'] : null;
|
||||
$po = $this->resolveOrderPoForBookingDefault(
|
||||
array_key_exists('po', $data) ? $data['po'] : null,
|
||||
array_key_exists('po', $data),
|
||||
$bookingId
|
||||
);
|
||||
$new_data = [
|
||||
'customer_id' => (int)$data['customer_id'],
|
||||
'department_id' => (int)$data['department_id'],
|
||||
'reference' => (string)$data['reference'] ?? '',
|
||||
'cashier_id' => (int)$user->id, // The user who created the order
|
||||
'notes' => (string)$data['notes'] ?? '',
|
||||
...($po !== null ? ['po' => $po] : []),
|
||||
'reg_1' => (string)$reg_1,
|
||||
'reg_2' => (string)$reg_2,
|
||||
'reg_3' => (string)$reg_3,
|
||||
...(!empty($data['lane']) ? ['lane' => (int)$data['lane']] : []), // Optional lane
|
||||
...(!empty($data['wash_id']) ? ['wash_id' => (string)$data['wash_id']] : []), // Optional wash ID
|
||||
...(!empty($data['booking_id']) ? ['booking_id' => (int)$data['booking_id']] : []), // Optional booking ID
|
||||
...($bookingId !== null ? ['booking_id' => $bookingId] : []), // Optional booking ID
|
||||
'created_at' => $createdAt, // Default to current time if not set
|
||||
...(array_key_exists('include_in_invoice', $data) ? ['include_in_invoice' => $includeInInvoice] : []),
|
||||
...(array_key_exists('safety_seal', $data) ? ['safety_seal' => orders_o::normalizeSafetySealValue($data['safety_seal'])] : []),
|
||||
@@ -1177,7 +1185,9 @@ class ordersRoute
|
||||
}
|
||||
// If the booking ID is set, validate it
|
||||
if (isset($data['booking_id'])) {
|
||||
$order->booking_id->set((int)$data['booking_id']);
|
||||
$bookingId = (int)$data['booking_id'];
|
||||
$order->booking_id->set($bookingId);
|
||||
$this->applyBookingPoDefaultToOrder($order, $bookingId);
|
||||
}
|
||||
// Check if the invoice collection is set
|
||||
if (isset($data['invoice_collection_id']) && !$shouldAutoReassignInvoiceCollection) {
|
||||
@@ -1225,6 +1235,55 @@ class ordersRoute
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveOrderPoForBookingDefault(mixed $po, bool $poProvided, ?int $bookingId): ?string
|
||||
{
|
||||
$currentPo = is_scalar($po) || $po === null ? trim((string)$po) : '';
|
||||
if ($currentPo !== '') {
|
||||
return $currentPo;
|
||||
}
|
||||
|
||||
$bookingPo = $this->getBookingPoDefault($bookingId);
|
||||
if ($bookingPo !== null) {
|
||||
return $bookingPo;
|
||||
}
|
||||
|
||||
return $poProvided ? '' : null;
|
||||
}
|
||||
|
||||
private function applyBookingPoDefaultToOrder(orders_o $order, ?int $bookingId = null): void
|
||||
{
|
||||
$currentPo = trim((string)($order->po->value() ?? ''));
|
||||
if ($currentPo !== '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$bookingPo = $this->getBookingPoDefault($bookingId ?? (int)($order->booking_id->value() ?? 0));
|
||||
if ($bookingPo === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$order->po->set($bookingPo);
|
||||
}
|
||||
|
||||
private function getBookingPoDefault(?int $bookingId): ?string
|
||||
{
|
||||
if ($bookingId === null || $bookingId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$booking = (new order_bookings_o())->select($bookingId);
|
||||
if (!$booking->exists()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$bookingPo = trim((string)($booking->po->value() ?? ''));
|
||||
return $bookingPo !== '' ? $bookingPo : null;
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeLegacyEditableFieldPayload(array $data, response $response): array
|
||||
{
|
||||
if (!array_key_exists('field', $data) && !array_key_exists('value', $data)) {
|
||||
|
||||
Reference in New Issue
Block a user