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

93 lines
2.8 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use traits\db_object_t;
class customer_notes_o extends db
{
use db_object_t;
public object_property $customer_id;
public object_property $note;
public object_property $cashier_id;
public object_property $deleted_at;
public function structure(): void
{
$this->setTable('customer_notes');
}
public function getObjectProperties(): void
{
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
$this->note = new object_property($this->table, $this->id, 'note', 'string', true);
$this->cashier_id = new object_property($this->table, $this->id, 'cashier_id', 'int', true);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
}
public function add(int $customer_id, string $note, int $cashier_id): customer_notes_o
{
global $db, $response;
try {
// Avoid SQL injection
$note = $db->escape_string($note);
// Create a new record in the database
$sql = "INSERT INTO $this->table (customer_id, note, cashier_id) VALUES ($customer_id, '$note', $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());
}
return $this;
}
public function getCustomerNotesAsArray(int $customer_id): array
{
global $db;
$sql = "SELECT * FROM $this->table WHERE customer_id = $customer_id AND deleted_at IS NULL";
$result = $db->query($sql);
$customer_notes = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$customer_notes[] = $row;
}
}
return $customer_notes;
}
public function getCustomerNoteById(int $id): customer_notes_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 delete(int $id): void
{
global $db;
$this->id = $id;
$sql = "UPDATE $this->table SET deleted_at = NOW() WHERE id = $this->id";
$db->query($sql);
}
public function restore(int $id): void
{
global $db;
$this->id = $id;
$sql = "UPDATE $this->table SET deleted_at = NULL WHERE id = $this->id";
$db->query($sql);
}
}