165 lines
6.0 KiB
PHP
165 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use traits\db_object_t;
|
|
|
|
class order_items_o extends db
|
|
{
|
|
use db_object_t;
|
|
public object_property $order_id;
|
|
public object_property $product_id;
|
|
public object_property $reference;
|
|
public object_property $notes;
|
|
public object_property $cashier_id;
|
|
public object_property $price;
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('order_items');
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->order_id = new object_property($this->table, $this->id, 'order_id', 'int', true);
|
|
$this->product_id = new object_property($this->table, $this->id, 'product_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->cashier_id = new object_property($this->table, $this->id, 'cashier_id', 'int', true);
|
|
$this->price = new object_property($this->table, $this->id, 'price', 'int', true);
|
|
}
|
|
|
|
public function getOrderItemById(int $id): order_items_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 $order_id, int $product_id, string $reference, string $notes, int $cashier_id, int $price): void
|
|
{
|
|
global $db, $response;
|
|
try {
|
|
// Avoid SQL injection
|
|
$reference = $db->escape_string($reference);
|
|
$notes = $db->escape_string($notes);
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO $this->table (order_id, product_id, reference, notes, cashier_id, price) VALUES ($order_id, $product_id, '$reference', '$notes', $cashier_id, $price)";
|
|
$db->query($sql);
|
|
|
|
// Get the id of the new record
|
|
$this->id = $db->insert_id();
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function edit(int $id, int $order_id, int $product_id, string $reference, string $notes, int $cashier_id, int $price): 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 order_id = $order_id, product_id = $product_id, reference = '$reference', notes = '$notes', cashier_id = $cashier_id, price = $price WHERE id = $this->id";
|
|
$db->query($sql);
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function addItemToOrder(int $order_id, int $product_id, int $cashier_id): void
|
|
{
|
|
global $db, $response;
|
|
try {
|
|
// Get the product price
|
|
$sql = "SELECT price FROM products WHERE id = $product_id";
|
|
$result = $db->query($sql);
|
|
$row = $db->fetch_assoc($result);
|
|
$price = $row['price'];
|
|
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO $this->table (order_id, product_id, price, cashier_id) VALUES ($order_id, $product_id, $price, $cashier_id)";
|
|
$db->query($sql);
|
|
// Get the id of the new record
|
|
$this->id = $db->insert_id();
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function removeOrderItem(int $id): void
|
|
{
|
|
global $db;
|
|
$this->id = $id;
|
|
$sql = "DELETE FROM $this->table WHERE id = $this->id";
|
|
$db->query($sql);
|
|
}
|
|
|
|
public function getItemAsArray(): array
|
|
{
|
|
return [
|
|
'id' => (int)$this->id,
|
|
'order_id' => (int)$this->order_id->value(),
|
|
'product_id' => (int)$this->product_id->value(),
|
|
'reference' => (string)$this->reference->value(),
|
|
'notes' => (string)$this->notes->value(),
|
|
'cashier_id' => (int)$this->cashier_id->value(),
|
|
'price' => (int)$this->price->value(),
|
|
'product' => (array)(new products_o())->getProductById($this->product_id->value())->asArray(),
|
|
'cashier' => (array)(new users_o())->getUserById($this->cashier_id->value())->asArray()
|
|
];
|
|
}
|
|
|
|
public function getAllItemsAsArray(int $orderId): array
|
|
{
|
|
global $db;
|
|
$sql = "SELECT * FROM $this->table WHERE order_id = $orderId";
|
|
$result = $db->query($sql);
|
|
// Circumvent the repeated instantiation of the object, by just selecting the fields
|
|
$items = [];
|
|
if ($result->num_rows > 0) {
|
|
while ($row = $result->fetch_assoc()) {
|
|
$items[] = $row;
|
|
}
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
public function updateOrderItem(int $id, int $price, string $notes, string $reference): void
|
|
{
|
|
global $db, $response;
|
|
$this->id = $id;
|
|
try {
|
|
// Avoid SQL injection
|
|
$price = $db->escape_string($price);
|
|
$notes = $db->escape_string($notes);
|
|
$reference = $db->escape_string($reference);
|
|
// Update the record in the database
|
|
$sql = "UPDATE $this->table SET price = $price, notes = '$notes', reference = '$reference' WHERE id = $this->id";
|
|
$db->query($sql);
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
} catch (\Exception $e) {
|
|
$response->error($e->getMessage());
|
|
}
|
|
}
|
|
} |