Added the /orders DELETE route.
This commit is contained in:
+74
-46
@@ -10,6 +10,7 @@ use traits\db_object_t;
|
||||
class orders_o extends db
|
||||
{
|
||||
use db_object_t;
|
||||
|
||||
public object_property $customer_id;
|
||||
public object_property $cashier_id;
|
||||
public object_property $reference;
|
||||
@@ -20,12 +21,26 @@ class orders_o extends db
|
||||
public object_property $reg_3;
|
||||
public economic_module_orders $economic_module_orders;
|
||||
public object_property $created_at;
|
||||
public object_property $deleted_at;
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
$this->setTable('orders');
|
||||
}
|
||||
|
||||
public function getOrderById(int $id): orders_o
|
||||
{
|
||||
global $db;
|
||||
// Get the record from the database
|
||||
$sql = "SELECT * FROM $this->table WHERE id = $id";
|
||||
$result = $db->query($sql);
|
||||
if ($result->num_rows > 0) {
|
||||
$this->id = $id;
|
||||
$this->getObjectProperties();
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getObjectProperties(): void
|
||||
{
|
||||
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
|
||||
@@ -38,19 +53,7 @@ class orders_o extends db
|
||||
$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->economic_module_orders = (new economic_module_orders())->getByOrderId($this->id);
|
||||
}
|
||||
|
||||
public function getOrderById(int $id): orders_o
|
||||
{
|
||||
global $db;
|
||||
// Get the record from the database
|
||||
$sql = "SELECT * FROM $this->table WHERE id = $id";
|
||||
$result = $db->query($sql);
|
||||
if ($result->num_rows > 0) {
|
||||
$this->id = $id;
|
||||
$this->getObjectProperties();
|
||||
}
|
||||
return $this;
|
||||
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
|
||||
}
|
||||
|
||||
public function add(int $customer_id, int $cashier_id, string $reference, string $notes, int $department_id, string $reg_1 = '', string $reg_2 = '', string $reg_3 = ''): orders_o
|
||||
@@ -98,37 +101,6 @@ class orders_o extends db
|
||||
}
|
||||
}
|
||||
|
||||
public function getOrderItems(int $order_id): array
|
||||
{
|
||||
global $db;
|
||||
$sql = "SELECT * FROM order_items WHERE order_id = $order_id";
|
||||
$result = $db->query($sql);
|
||||
$order_items = [];
|
||||
if ($result->num_rows > 0 && $result) {
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$order_item = new order_items_o();
|
||||
$order_items[] = $order_item->getOrderItemById($row['id'])->getItemAsArray();
|
||||
}
|
||||
}
|
||||
return $order_items;
|
||||
}
|
||||
|
||||
public function asArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'customer_id' => $this->customer_id->value(),
|
||||
'cashier_id' => $this->cashier_id->value(),
|
||||
'reference' => $this->reference->value(),
|
||||
'notes' => $this->notes->value(),
|
||||
'department_id' => $this->department_id->value(),
|
||||
'reg_1' => $this->reg_1->value(),
|
||||
'reg_2' => $this->reg_2->value(),
|
||||
'reg_3' => $this->reg_3->value(),
|
||||
'created_at' => $this->created_at->value(),
|
||||
];
|
||||
}
|
||||
|
||||
public function includeIncludes(): orders_o
|
||||
{
|
||||
global /** @var response $response */
|
||||
@@ -157,6 +129,38 @@ class orders_o extends db
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getOrderItems(int $order_id): array
|
||||
{
|
||||
global $db;
|
||||
$sql = "SELECT * FROM order_items WHERE order_id = $order_id";
|
||||
$result = $db->query($sql);
|
||||
$order_items = [];
|
||||
if ($result->num_rows > 0 && $result) {
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$order_item = new order_items_o();
|
||||
$order_items[] = $order_item->getOrderItemById($row['id'])->getItemAsArray();
|
||||
}
|
||||
}
|
||||
return $order_items;
|
||||
}
|
||||
|
||||
public function asArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'customer_id' => $this->customer_id->value(),
|
||||
'cashier_id' => $this->cashier_id->value(),
|
||||
'reference' => $this->reference->value(),
|
||||
'notes' => $this->notes->value(),
|
||||
'department_id' => $this->department_id->value(),
|
||||
'reg_1' => $this->reg_1->value(),
|
||||
'reg_2' => $this->reg_2->value(),
|
||||
'reg_3' => $this->reg_3->value(),
|
||||
'created_at' => $this->created_at->value(),
|
||||
'deleted_at' => $this->deleted_at->value(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getCustomerByOrderId(?string $order_id): users_o
|
||||
{
|
||||
global $db;
|
||||
@@ -173,11 +177,11 @@ class orders_o extends db
|
||||
{
|
||||
global /** @var response $response */
|
||||
$db, $response;
|
||||
$sql = "SELECT * FROM $this->table WHERE customer_id = $customer_number ORDER BY id $order LIMIT $limit OFFSET " . ($page - 1) * $limit;
|
||||
$sql = "SELECT * FROM $this->table WHERE customer_id = $customer_number AND deleted_at IS NULL ORDER BY id $order LIMIT $limit OFFSET " . ($page - 1) * $limit;
|
||||
$result = $db->query($sql);
|
||||
$array = $db->fetch_all($result);
|
||||
// Add the metadata
|
||||
$sql = "SELECT COUNT(*) as count FROM $this->table WHERE customer_id = $customer_number";
|
||||
$sql = "SELECT COUNT(*) as count FROM $this->table WHERE customer_id = $customer_number AND deleted_at IS NULL";
|
||||
$result = $db->query($sql);
|
||||
$row = $db->fetch_assoc($result);
|
||||
$total = $row['count'];
|
||||
@@ -189,4 +193,28 @@ class orders_o extends db
|
||||
{
|
||||
return (new departments_o())->getDepartmentById($this->department_id->value());
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
// Set the deleted_at property to the current timestamp
|
||||
$this->deleted_at->set(date('Y-m-d H:i:s'));
|
||||
// Save the object
|
||||
}
|
||||
|
||||
public function restore(): void
|
||||
{
|
||||
// Set the deleted_at property to null
|
||||
$this->deleted_at->set(null);
|
||||
// Save the object
|
||||
}
|
||||
|
||||
public function exists(): bool
|
||||
{
|
||||
// Check if the id is greater than 0, and that the deleted_at property is null
|
||||
if ($this->id > 0) {
|
||||
$this->getObjectProperties();
|
||||
return $this->deleted_at->value() === null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+37
-1
@@ -92,7 +92,9 @@ class ordersRoute
|
||||
// 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); }
|
||||
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
|
||||
@@ -130,6 +132,40 @@ class ordersRoute
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
});
|
||||
|
||||
$this->delete('/orders', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
$this->requirePermission('delete_order');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the payload
|
||||
$id = $this->fromRequest('id');
|
||||
// Check if the ID is set
|
||||
if (!$id) {
|
||||
$response->error('ID is required', 400);
|
||||
}
|
||||
// Get the current order
|
||||
$order = (new orders_o())->getOrderById((int)$id);
|
||||
// Check if the order exists
|
||||
if (!$order->exists()) {
|
||||
$response->error('Order not found', 400);
|
||||
}
|
||||
// Delete the order
|
||||
$order->delete();
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', $id, 1, $user->id, 'DELETE_ORDER', 'Successfully deleted an order (ID: ' . $id . ')');
|
||||
// Return a success message
|
||||
$response->success(['message' => 'Order deleted successfully']);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('orders', 'global', 1, 0, 'DELETE_ORDER', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user