Files
api/services/nginx/app/statistics/orders_s.php
T
Jeppe B 1e0e051775 Harden Sæby demo registration and department scope (#335)
Complete and secure public customer/driver registration, authoritative limited-backoffice department scope, one-time employee QR login, and pricing concurrency for the Sæby demo.
2026-08-02 11:50:56 +02:00

57 lines
2.0 KiB
PHP

<?php
namespace statistics;
use traits\statistic_t;
class orders_s
{
use statistic_t;
/**
* @param array<int, int>|null $departmentIds Null keeps the existing unrestricted contract.
*/
public function get_new_orders(?array $departmentIds = null): self
{
// Get the departments
$departments_o = new \objects\departments_o();
// Get the orders
$orders_o = new \objects\orders_o();
// Get the orders
$orders = $departmentIds === null
? $orders_o->getFields(['id', 'department_id', 'created_at'])
: ($departmentIds === [] ? [] : $orders_o->getFieldsWhere(
['department_id' => $departmentIds],
['id', 'department_id', 'created_at']
));
// Get the departments
$departments = $departmentIds === null
? $departments_o->getFields(['id', 'name'])
: ($departmentIds === [] ? [] : $departments_o->getFieldsWhere(
['id' => $departmentIds],
['id', 'name']
));
// For each year, add the months covered
$this->add_monthly_labels();
// Add the counts for each department, for each month in each year
foreach ( $departments as $department ) {
$department_data_set = [];
foreach ( $this->get_yearly_months_covered() as $year => $months ) {
foreach ( $months as $month ) {
$count = 0;
foreach ( $orders as $order ) {
if ($order['department_id'] == $department['id'] && date('F', strtotime($order['created_at'])) == $month && date('Y', strtotime($order['created_at'])) == $year) {
$count++;
}
}
$department_data_set[$month . ' ' . $year] = $count;
}
}
// Add the department data set
$this->add_data_set($department['name'], $department_data_set);
}
return $this;
}
}