Fix and enhance order processing with improved data handling
Set proper timestamps for collected invoices and added `getLastOrderId` for vehicles. Streamlined order fetching logic by fixing parameter usage, refining customer and order item retrieval, and improving type casting in multiple methods.
This commit is contained in:
@@ -44,10 +44,12 @@ class customer_vehicles_o extends db
|
||||
$available_addons = self::transformObjectsToArray(
|
||||
self::getAvailableAddons()
|
||||
);
|
||||
$last_order_id = self::getLastOrderId();
|
||||
return [
|
||||
'id' => (int)$this->id,
|
||||
'user_id' => (int)(new users_o())->getUserIdFromEconomic((int)$this->customer_id->value()),
|
||||
'customer_id' => (int)$this->customer_id->value(),
|
||||
'customer_name' => (string)(new users_o())->getCustomerName((int)$this->customer_id->value()),
|
||||
'type' => (int)$this->type->value(),
|
||||
'reg' => (string)$this->reg->value(),
|
||||
'wash_subscription' => $wash_subscription,
|
||||
@@ -55,7 +57,8 @@ class customer_vehicles_o extends db
|
||||
'enabled' => count($addons),
|
||||
'available' => count($available_addons),
|
||||
'list' => $addons,
|
||||
]
|
||||
],
|
||||
'last_order_id' => ($last_order_id ? (int)$last_order_id : null),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -95,6 +98,29 @@ class customer_vehicles_o extends db
|
||||
}, $available_addons);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last order id for the vehicle
|
||||
* @return int|null
|
||||
* @throws Exception If the object is not selected
|
||||
* @throws Exception If the there is an error selecting the order
|
||||
*/
|
||||
private function getLastOrderId(): ?int
|
||||
{
|
||||
self::requireSelected();
|
||||
$orders_o = (new orders_o());
|
||||
$orders = $orders_o->getFieldsWhere([
|
||||
'reg_1' => (string)$this->reg->value(),
|
||||
'deleted_at' => null,
|
||||
], [
|
||||
'id',
|
||||
]);
|
||||
if (count($orders) > 0) {
|
||||
$last_order = array_pop($orders);
|
||||
return (int)$last_order['id'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the default addons to the vehicle
|
||||
* @return void
|
||||
|
||||
@@ -456,7 +456,7 @@ class orders_o extends db
|
||||
'id' => 'DESC',
|
||||
],
|
||||
function ($object) {
|
||||
return (new orders_o())->select($object['id'])->includeIncludes()->asArray();
|
||||
return (new orders_o())->select($object['id'])->asArray();
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -512,13 +512,15 @@ class orders_o extends db
|
||||
$includeEverything = $response->getRequestParameter('include_all') === 'true';
|
||||
/** orderItems */
|
||||
if ($response->getRequestParameter('includeOrderItems') || $includeEverything) {
|
||||
$response->add_include('orderItems', $this->applyDepartmentPrices($this->getOrderItems($this->id), $this->department_id->value()));
|
||||
$response->add_include('orderItems', $this->applyDepartmentPrices($this->getOrderItems($this->id), (int)$this->department_id->value()));
|
||||
}
|
||||
|
||||
/** customer */
|
||||
if ($response->getRequestParameter('includeCustomer') || $includeEverything) {
|
||||
$customer = new users_o();
|
||||
$response->add_include('customer', $customer->getOrImportCustomerByCustomerNumber($this->customer_id->value())->includeIncludes()->asArray());
|
||||
$response->add_include('customer', $customer->getUserByCustomerNumber((int)$this->customer_id->value())->includeIncludes()->asArray());
|
||||
}
|
||||
|
||||
/** cashier */
|
||||
if ($response->getRequestParameter('includeCashier') || $includeEverything) {
|
||||
$cashier = new users_o();
|
||||
@@ -537,6 +539,7 @@ class orders_o extends db
|
||||
$response->add_include('stripeModuleOrders', $this->stripe_module_orders->exists() ? $this->stripe_module_orders->asArray() : []);
|
||||
}
|
||||
return $this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -551,7 +551,7 @@ class users_o extends db
|
||||
{
|
||||
self::requireSelected();
|
||||
$orders_o = new orders_o();
|
||||
$wash_subscription_transactions = $orders_o->getWashSubscriptionTransactions($this->customer_number->value());
|
||||
$wash_subscription_transactions = $orders_o->getWashSubscriptionTransactions((int)$this->customer_number->value());
|
||||
$this->wash_subscription_transactions = $wash_subscription_transactions;
|
||||
}
|
||||
|
||||
|
||||
@@ -348,6 +348,10 @@ class orderInvoicesRoute
|
||||
$collected_order_invoices->addVehicleSubscriptionsTransaction();
|
||||
// Close the collected order invoice
|
||||
$collected_order_invoices->closeCollection();
|
||||
// Set the correct date for the collected order invoice
|
||||
$collected_order_invoices->created_at->set($timestamp_selected);
|
||||
// Update the collected order invoice
|
||||
$collected_order_invoices->objectChanged();
|
||||
// Return the collected order invoice
|
||||
$response->success($collected_order_invoices->asArray());
|
||||
} else {
|
||||
|
||||
@@ -20,26 +20,26 @@ class orderRoute
|
||||
$response;
|
||||
$this->requirePermission('fetch_order');
|
||||
// Get the user object
|
||||
$user = $response->get_user();
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Make sure the order id is set
|
||||
if (!(int)$this->fromRequest('id')) {
|
||||
if (!(int)$this->getParameter('id')) {
|
||||
$response->error('Order id is required', 400);
|
||||
}
|
||||
$orders_o = new orders_o();
|
||||
|
||||
$orders_o->select((int)$this->getParameter('id'));
|
||||
// Make sure the order exists
|
||||
if (!$orders_o->getOrderById($this->fromRequest('id'))->exists()) {
|
||||
if (!$orders_o->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, $user->id, 'FETCH_ORDER', 'Successfully fetched order');
|
||||
// Check if the user has access to the department the order is in
|
||||
$this->requirePermission('department_access_' . $orders_o->department_id->value());
|
||||
self::requireDepartmentAccess($orders_o->department_id->value());
|
||||
// Return the list of departments
|
||||
$response->success(
|
||||
$orders_o->getOrderById($this->fromRequest('id'))->includeIncludes()->asArray()
|
||||
$orders_o->includeIncludes()->asArray()
|
||||
);
|
||||
} else {
|
||||
// Log the incident
|
||||
|
||||
Reference in New Issue
Block a user