192 lines
7.6 KiB
PHP
192 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use classes\response;
|
|
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;
|
|
public object_property $notes;
|
|
public object_property $department_id;
|
|
public object_property $reg_1;
|
|
public object_property $reg_2;
|
|
public object_property $reg_3;
|
|
public economic_module_orders $economic_module_orders;
|
|
public object_property $created_at;
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('orders');
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
|
|
$this->cashier_id = new object_property($this->table, $this->id, 'cashier_id', 'int', true);
|
|
$this->reference = new object_property($this->table, $this->id, 'reference', 'string', true);
|
|
$this->notes = new object_property($this->table, $this->id, 'notes', 'string', false);
|
|
$this->department_id = new object_property($this->table, $this->id, 'department_id', 'int', true);
|
|
$this->reg_1 = new object_property($this->table, $this->id, 'reg_1', 'string', false);
|
|
$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->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;
|
|
}
|
|
|
|
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
|
|
{
|
|
global $db, $response;
|
|
try {
|
|
// Avoid SQL injection
|
|
$reference = $db->escape_string($reference);
|
|
$notes = $db->escape_string($notes);
|
|
$reg_1 = $db->escape_string($reg_1);
|
|
$reg_2 = $db->escape_string($reg_2);
|
|
$reg_3 = $db->escape_string($reg_3);
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO $this->table (customer_id, cashier_id, reference, notes, department_id, reg_1, reg_2, reg_3) VALUES ($customer_id, $cashier_id, '$reference', '$notes', $department_id, '$reg_1', '$reg_2', '$reg_3')";
|
|
$db->query($sql);
|
|
|
|
// Get the id of the new record
|
|
$this->id = $db->insert_id();
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
return $this;
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage());
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
public function edit(int $id, int $customer_id, int $cashier_id, string $reference, string $notes, int $department_id): void
|
|
{
|
|
global $db, $response;
|
|
$this->id = $id;
|
|
try {
|
|
// Avoid SQL injection
|
|
$reference = $db->escape_string($reference);
|
|
$notes = $db->escape_string($notes);
|
|
// Update the record in the database
|
|
$sql = "UPDATE $this->table SET customer_id = $customer_id, cashier_id = $cashier_id, reference = '$reference', notes = '$notes', department_id = $department_id WHERE id = $id";
|
|
$db->query($sql);
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
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 */
|
|
$response;
|
|
$includeEverything = $response->getRequestParameter('include_all') === 'true';
|
|
/** orderItems */
|
|
if ($response->getRequestParameter('includeOrderItems') || $includeEverything) {
|
|
$response->add_include('orderItems', $this->getOrderItems($this->id));
|
|
}
|
|
/** customer */
|
|
if ($response->getRequestParameter('includeCustomer') || $includeEverything) {
|
|
$customer = new users_o();
|
|
$response->add_include('customer', $customer->getCustomerByIdOrCustomerNumber($this->customer_id->value())->includeIncludes()->asArray());
|
|
}
|
|
/** cashier */
|
|
if ($response->getRequestParameter('includeCashier') || $includeEverything) {
|
|
$cashier = new users_o();
|
|
$response->add_include('cashier', $cashier->getUserById($this->cashier_id->value())->asArray());
|
|
}
|
|
/**
|
|
* economicModuleOrders
|
|
*/
|
|
if ($response->getRequestParameter('includeEconomicModuleOrders') || $includeEverything) {
|
|
$response->add_include('economicModuleOrders', $this->economic_module_orders->getByOrderId($this->id)->asArray());
|
|
}
|
|
return $this;
|
|
}
|
|
|
|
public function getCustomerByOrderId(?string $order_id): users_o
|
|
{
|
|
global $db;
|
|
$sql = "SELECT customer_id FROM orders WHERE id = $order_id";
|
|
$result = $db->query($sql);
|
|
if ($result->num_rows > 0) {
|
|
$row = $result->fetch_assoc();
|
|
return (new users_o())->getCustomerByIdOrCustomerNumber($row['customer_id']);
|
|
}
|
|
return new users_o();
|
|
}
|
|
|
|
public function getCustomerOrdersPaginated(int $customer_number, int $page = 1, int $limit = 10, string $order = 'DESC'): array
|
|
{
|
|
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;
|
|
$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";
|
|
$result = $db->query($sql);
|
|
$row = $db->fetch_assoc($result);
|
|
$total = $row['count'];
|
|
$response->paginate($page, $limit, $total);
|
|
return $array;
|
|
}
|
|
|
|
public function getDepartmentByOrderId($order_id): array
|
|
{
|
|
return (new departments_o())->getDepartmentById($this->department_id->value());
|
|
}
|
|
} |