Implemented Redis caching for improved department and customer data retrieval. Added Slack integration to send department notifications. Enhanced booking features and status handling, including parsing logic and wash certificate processing.
150 lines
5.1 KiB
PHP
150 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use traits\db_object_t;
|
|
|
|
class departments_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $name;
|
|
public object_property $description;
|
|
public object_property $economic_department_id; // The id of the department in the economic system (Can be null)
|
|
public object_property $slack_webhook; // The slack webhook for the department
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('departments');
|
|
}
|
|
|
|
public function create(string $name, string $description): void
|
|
{
|
|
global $db;
|
|
// Avoid SQL injection
|
|
$name = $db->escape_string($name);
|
|
$description = $db->escape_string($description);
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO $this->table (name, description) VALUES ('$name', '$description')";
|
|
$db->query($sql);
|
|
|
|
// Get the id of the new record
|
|
$this->id = $db->insert_id();
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->name = new object_property($this->table, $this->id, 'name', 'string', true);
|
|
$this->description = new object_property($this->table, $this->id, 'description', 'string', false);
|
|
$this->economic_department_id = new object_property($this->table, $this->id, 'economic_department_id', 'int', false);
|
|
$this->slack_webhook = new object_property($this->table, $this->id, 'slack_webhook', 'string', false);
|
|
}
|
|
|
|
public function edit(int $id, string $name, string $description, int $economic_department_id): void
|
|
{
|
|
global $db;
|
|
$this->id = $id;
|
|
// Avoid SQL injection
|
|
$name = $db->escape_string($name);
|
|
$description = $db->escape_string($description);
|
|
// If the economic_department_id is 0, set it to null
|
|
if ($economic_department_id === 0) {
|
|
$economic_department_id = null;
|
|
}
|
|
// Update the record in the database
|
|
$sql = "UPDATE $this->table SET name = '$name', description = '$description', economic_department_id = " . ($economic_department_id ?? 'NULL') . " WHERE id = $this->id";
|
|
$db->query($sql);
|
|
|
|
// Set the values of the object properties
|
|
$this->getObjectProperties();
|
|
}
|
|
|
|
public function list($superUser = false): array
|
|
{
|
|
global $db;
|
|
$sql = "SELECT * FROM $this->table";
|
|
$result = $db->query($sql);
|
|
$result = $db->fetch_all($result);
|
|
// Only show the name, description and id if the user isn't a super user
|
|
if (!$superUser) {
|
|
$result = array_map(function ($department) {
|
|
return [
|
|
'name' => $department['name'],
|
|
'description' => $department['description'],
|
|
'id' => $department['id']
|
|
];
|
|
}, $result);
|
|
return $result;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function getDepartmentById(int $id): array
|
|
{
|
|
global $db;
|
|
$sql = "SELECT * FROM $this->table WHERE id = $id";
|
|
$result = $db->query($sql);
|
|
return $db->fetch_assoc($result);
|
|
}
|
|
|
|
/**
|
|
* Get the price of a product in a department
|
|
* @param int $department_id
|
|
* @param int $product_id
|
|
* @param int $price
|
|
* @return void
|
|
*/
|
|
public function setDepartmentProductPrice(int $department_id, int $product_id, int $price): void
|
|
{
|
|
global $db;
|
|
// Check if the record already exists
|
|
$this->removeDepartmentProductPriceIfExist($department_id, $product_id);
|
|
// If the price is 0, return
|
|
if ($price === 0) {
|
|
return;
|
|
}
|
|
// Create a new record in the database
|
|
$sql = "INSERT INTO product_department_prices (department_id, product_id, price) VALUES ($department_id, $product_id, $price)";
|
|
$db->query($sql);
|
|
}
|
|
|
|
/**
|
|
* Remove the price of a product in a department
|
|
* @param int $department_id
|
|
* @param int $product_id
|
|
* @return void
|
|
*/
|
|
private function removeDepartmentProductPriceIfExist(int $department_id, int $product_id): void
|
|
{
|
|
global $db;
|
|
// Get the price from the database
|
|
$sql = "SELECT * FROM product_department_prices WHERE department_id = $department_id AND product_id = $product_id";
|
|
$result = $db->query($sql);
|
|
|
|
if ($result->num_rows > 0) {
|
|
// Remove the record from the database
|
|
$sql = "DELETE FROM product_department_prices WHERE department_id = $department_id AND product_id = $product_id";
|
|
$db->query($sql);
|
|
}
|
|
}
|
|
|
|
public function getDepartmentProductPrices(int $department_id): array
|
|
{
|
|
global $db;
|
|
$sql = "SELECT * FROM product_department_prices WHERE department_id = $department_id";
|
|
$result = $db->query($sql);
|
|
return $db->fetch_all($result);
|
|
}
|
|
|
|
public function selectId(int $department_id): self
|
|
{
|
|
$this->id = $department_id;
|
|
$this->getObjectProperties();
|
|
return $this;
|
|
}
|
|
} |