Add feature to mark orders as completed
Implemented a new endpoint to mark orders as completed and updated the `orders_o` object with the necessary logic and database integration. Added a `completed_at` property to track when an order is marked as completed. Includes validation, error handling, and logging to ensure proper functionality.
This commit is contained in:
@@ -24,6 +24,8 @@ class orders_o extends db
|
||||
public economic_module_orders $economic_module_orders;
|
||||
public object_property $created_at;
|
||||
public object_property $deleted_at;
|
||||
|
||||
public object_property $completed_at;
|
||||
public stripe_module_orders_o $stripe_module_orders;
|
||||
public object_property $invoice_collection_id;
|
||||
|
||||
@@ -62,6 +64,7 @@ class orders_o extends db
|
||||
$this->reg_2 = new object_property($this->table, $this->id, 'reg_2', 'string', false);
|
||||
$this->reg_3 = new object_property($this->table, $this->id, 'reg_3', 'string', false);
|
||||
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
|
||||
$this->completed_at = new object_property($this->table, $this->id, 'completed_at', 'timestamp', false);
|
||||
$this->economic_module_orders = (new economic_module_orders())->getByOrderId($this->id);
|
||||
$this->stripe_module_orders = (new stripe_module_orders_o())->select($this->id);
|
||||
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
|
||||
@@ -150,6 +153,7 @@ class orders_o extends db
|
||||
'reg_1' => $this->reg_1->value(),
|
||||
'reg_2' => $this->reg_2->value(),
|
||||
'reg_3' => $this->reg_3->value(),
|
||||
'completed_at' => $this->completed_at->value(),
|
||||
'created_at' => $this->created_at->value(),
|
||||
'deleted_at' => $this->deleted_at->value(),
|
||||
'total_net_amount' => $this->getNetAmount(),
|
||||
@@ -299,6 +303,38 @@ class orders_o extends db
|
||||
$this->{$data['field']}->set($data['value']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark the order as completed
|
||||
* @throws Exception If the order is not selected
|
||||
* @throws Exception If the order is already completed
|
||||
*/
|
||||
public function markAsCompleted(): void
|
||||
{
|
||||
global /** @var db $db */
|
||||
$db;
|
||||
self::requireSelected();
|
||||
// Check if the order is already completed
|
||||
if ($this->completed_at->value() !== null) {
|
||||
throw new Exception('Order is already completed');
|
||||
}
|
||||
// Set the completed_at property to the current timestamp
|
||||
$this->completed_at->set(date('Y-m-d H:i:s'));
|
||||
$sql = "UPDATE $this->table SET completed_at = '" . $this->completed_at->value() . "' WHERE id = " . $this->id;
|
||||
$db->query($sql);
|
||||
$this->objectChanged();
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
// Since the orders object is not cached, there is no need to invalidate the cache
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the order by invoice id
|
||||
* @param int $invoiceId
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getOrderByInvoiceId(int $invoiceId): orders_o
|
||||
{
|
||||
global $db;
|
||||
@@ -369,11 +405,6 @@ class orders_o extends db
|
||||
self::objectChanged();
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
// Since the orders object is not cached, there is no need to invalidate the cache
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the recommended order items, based on the order history, vehicle plate and department
|
||||
* @return array The recommended order items
|
||||
|
||||
@@ -222,6 +222,44 @@ class ordersRoute
|
||||
'delete_order' => 'Delete an order'
|
||||
]
|
||||
);
|
||||
|
||||
$this->post('/orders/mark_as_completed', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('mark_order_as_completed');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the post data
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
// Check if the required fields are set
|
||||
if (!isset($data['id'])) {
|
||||
$response->error('ID is required', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$data['id']);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Mark the order as completed
|
||||
$order->markAsCompleted();
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $order->department_id->value(), 1, $user->id, 'MARK_ORDER_AS_COMPLETED', 'Successfully marked an order as completed (ID: ' . $data['id'] . ')');
|
||||
// Return a success message
|
||||
$response->success(['message' => 'Order marked as completed successfully']);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'MARK_ORDER_AS_COMPLETED', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'mark_order_as_completed' => 'Mark an order as completed'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user