Files
api/objects/economic_module_orders.php
T
2024-12-16 08:54:03 +01:00

68 lines
1.9 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use classes\response;
use traits\db_object_t;
class economic_module_orders extends db
{
use db_object_t;
public object_property $economic_invoice_draft_id;
public object_property $economic_invoice_id;
public function structure(): void
{
$this->setTable('economic_module_orders');
}
public function getObjectProperties(): void
{
$this->economic_invoice_draft_id = new object_property($this->table, $this->id, 'invoice_draft_id', 'int', true);
$this->economic_invoice_id = new object_property($this->table, $this->id, 'invoice_id', 'int', true);
}
public function getByOrderId(int $orderId): economic_module_orders
{
global $db;
// Create a new record in the database, if it does not exist
$sql = "SELECT * FROM $this->table WHERE id = $orderId";
$result = $db->query($sql);
if ($result->num_rows > 0) {
$this->id = $orderId;
$this->getObjectProperties();
} else {
$this->add($orderId);
}
return $this;
}
public function add(int $orderId): void
{
global $db, $response;
try {
// Create a new record in the database
$sql = "INSERT INTO $this->table (id) VALUES ($orderId)";
$db->query($sql);
// Get the id of the new record
$this->id = $orderId;
// Set the values of the object properties
$this->getObjectProperties();
} catch (\Exception $e) {
$response->error($e->getMessage());
}
}
public function asArray(): array
{
return [
'id' => $this->id,
'invoice_draft_id' => $this->economic_invoice_draft_id->value(),
'invoice_id' => $this->economic_invoice_id->value(),
];
}
}