Add earnings calculation and enhance department data handling
Introduced a method to calculate department earnings on a given date. Enhanced department object with new properties (dimension, created_at, updated_at) and improved data serialization for flexibility. Updated routes to accommodate these additions for more robust department insights.
This commit is contained in:
@@ -361,4 +361,36 @@ class department_daily_reports_o extends db
|
||||
$this->select($id);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getTransactionsOnDateEarnings(string $date, int $department_id): int
|
||||
{
|
||||
global /** @var db $db */
|
||||
$db;
|
||||
$conn = $db->conn();
|
||||
// Get all the product prices, in all the orders (Not counting removed orders), for the given date and department, and sum them
|
||||
$stmt = $conn->prepare(
|
||||
'SELECT SUM(oi.price * oi.quantity) as amount FROM orders o
|
||||
JOIN order_items oi ON o.id = oi.order_id
|
||||
WHERE o.department_id = ? AND DATE(o.created_at) = ? AND o.deleted_at IS NULL'
|
||||
);
|
||||
if ($stmt) {
|
||||
$stmt->bind_param('is', $department_id, $date); // Bind parameters (i = integer, s = string)
|
||||
$stmt->execute();
|
||||
$result = $stmt->get_result(); // Get the result set from the statement
|
||||
$data = $result->fetch_assoc(); // Fetch the result as an associative array
|
||||
|
||||
// Access the "amount" field
|
||||
if (!$data) {
|
||||
// If there are no orders, set the amount to 0
|
||||
$amount = 0;
|
||||
} else {
|
||||
$amount = $data['amount'];
|
||||
}
|
||||
$stmt->close(); // Close the statement
|
||||
} else {
|
||||
// Handle query preparation error
|
||||
die('Query preparation failed: ' . $conn->error);
|
||||
}
|
||||
return (int)$amount;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,9 @@ class departments_o extends db
|
||||
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 department_variables_o $variables; // The department variables object
|
||||
public object_property $dimension; // The dimension of the department
|
||||
public object_property $created_at;
|
||||
public object_property $updated_at;
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
@@ -67,6 +70,10 @@ class departments_o extends db
|
||||
$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);
|
||||
$this->variables = (new department_variables_o())->selectDepartment($this->id);
|
||||
$this->dimension = new object_property($this->table, $this->id, 'dimension', 'int', false);
|
||||
|
||||
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
|
||||
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'string', false);
|
||||
}
|
||||
|
||||
public function edit(int $id, string $name, string $description, int $economic_department_id): void
|
||||
@@ -271,4 +278,29 @@ class departments_o extends db
|
||||
}
|
||||
return (new stripe())->readers->listLocation($this->variables->getVariable('stripe_terminal_location'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the object to an array
|
||||
* @param array|null $options The options for the conversion (e.g. ['slack_webhook' => true])
|
||||
* @return array The object as an array
|
||||
* @throws Exception If the object is not selected
|
||||
*/
|
||||
public function asArray(array $options = null): array
|
||||
{
|
||||
self::requireSelected();
|
||||
$tmp = [
|
||||
'id' => (int)$this->id,
|
||||
'name' => (string)$this->name->value(),
|
||||
'description' => (string)$this->description->value(),
|
||||
'economic_department_id' => (int)$this->economic_department_id->value(),
|
||||
'dimension' => (int)$this->dimension->value(),
|
||||
'created_at' => (string)$this->created_at->value(),
|
||||
'updated_at' => (string)$this->updated_at->value(),
|
||||
];
|
||||
// Check if the webhook should be included
|
||||
if ($options && $options['slack_webhook']) {
|
||||
$tmp['slack_webhook'] = (string)$this->slack_webhook->value();
|
||||
}
|
||||
return $tmp;
|
||||
}
|
||||
}
|
||||
@@ -478,6 +478,12 @@ class departmentDailyReportsRoute
|
||||
(string)self::getParameter('date'),
|
||||
(int)self::getParameter('department_id')
|
||||
),
|
||||
'earnings' =>
|
||||
(new department_daily_reports_o())
|
||||
->getTransactionsOnDateEarnings(
|
||||
(string)self::getParameter('date'),
|
||||
(int)self::getParameter('department_id')
|
||||
),
|
||||
'date' => (string)self::getParameter('date'),
|
||||
'department_id' => (int)self::getParameter('department_id')
|
||||
]
|
||||
|
||||
@@ -26,6 +26,15 @@ class departmentsRoute
|
||||
if ($user) {
|
||||
// Log the incident
|
||||
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENTS', 'Successfully listed departments');
|
||||
// Check if the id is set in the request
|
||||
if (self::isParametersSet(['id'])) {
|
||||
// Return the department
|
||||
$response->success(
|
||||
(new departments_o())->select((int)self::getParameter('id'))->asArray([
|
||||
'slack_webhook' => $user->hasPermission('view_slack_webhook')
|
||||
])
|
||||
);
|
||||
}
|
||||
// Return the list of departments
|
||||
$response->success(
|
||||
(new departments_o())
|
||||
|
||||
Reference in New Issue
Block a user