Files
api/services/nginx/app/routes/statisticsRoute.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

401 lines
17 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use classes\response;
use classes\statistics;
use objects\logs_o;
use traits\route_t;
class statisticsRoute
{
use route_t;
public function run(): void
{
/** Statistics >> Bookings >> New bookings */
$this->get('/statistics/bookings/new', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_bookings_new');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
$departmentScope = $this->limitedBackofficeDepartmentScope($user);
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'NEW_BOOKINGS', 'User requested new bookings');
$statics = new statistics();
// Return the list of bookings
$response->success(
$statics
->bookings()
->get_new_bookings($departmentScope)
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'NEW_BOOKINGS', 'User requested new bookings');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_bookings_new' => 'List all new bookings'
]
);
/** Statistics >> Orders >> New orders */
$this->get('/statistics/orders/new', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_orders_new');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
$departmentScope = $this->limitedBackofficeDepartmentScope($user);
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'NEW_ORDERS', 'User requested new orders');
$statics = new statistics();
// Return the list of orders
$response->success(
$statics
->orders()
->get_new_orders($departmentScope)
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'NEW_ORDERS', 'User requested new orders');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_orders_new' => 'List all new orders'
]
);
/** Statistics >> Economic >> Total income */
$this->get('/statistics/economic/totals', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_income');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME', 'User requested total income');
$statics = new statistics();
// Return the total income
$response->success(
$statics
->economic()
->get_totals()
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME', 'User requested total income');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_income' => 'Get total income'
]
);
/** Statistics >> Economic >> Department sent invoice totals */
$this->get('/statistics/economic/totals/department_sent_invoice_totals', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_department_sent_invoice_totals');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'DEPARTMENT_SENT_INVOICE_TOTALS', 'User requested department sent invoice totals');
$statics = new statistics();
// Return the department sent invoice totals
$response->success(
$statics
->economic()
->get_department_sent_invoice_totals()
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'DEPARTMENT_SENT_INVOICE_TOTALS', 'User requested department sent invoice totals');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_department_sent_invoice_totals' => 'Get department sent invoice totals'
]
);
/** Statistics >> Economic >> Department draft invoice totals */
$this->get('/statistics/economic/totals/department_draft_invoice_totals', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_department_draft_invoice_totals');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'DEPARTMENT_DRAFT_INVOICE_TOTALS', 'User requested department draft invoice totals');
$statics = new statistics();
// Return the department draft invoice totals
$response->success(
$statics
->economic()
->get_department_draft_invoice_totals()
->get_response()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'DEPARTMENT_DRAFT_INVOICE_TOTALS', 'User requested department draft invoice totals');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_department_draft_invoice_totals' => 'Get department draft invoice totals'
]
);
/** Statistics >> Economic >> Total income today */
$this->get('/statistics/income/today', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_income_today');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME_TODAY', 'User requested total income today');
$statics = new statistics();
// Return the total income today
$response->success(
$statics
->economic()
->get_totals_today()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME_TODAY', 'User requested total income today');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_income_today' => 'Get total income today'
]
);
/** Statistics >> Economic >> Total income yesterday */
$this->get('/statistics/income/yesterday', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_income_yesterday');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME_YESTERDAY', 'User requested total income yesterday');
$statics = new statistics();
// Return the total income yesterday
$response->success(
$statics
->economic()
->get_totals_yesterday()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME_YESTERDAY', 'User requested total income yesterday');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_income_yesterday' => 'Get total income yesterday'
]
);
/** Statistics >> Economic >> Total income this month */
$this->get('/statistics/income/this-month', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_income_this_month');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME_THIS_MONTH', 'User requested total income this month');
$statics = new statistics();
// Return the total income this month
$response->success(
$statics
->economic()
->get_totals_this_month()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME_THIS_MONTH', 'User requested total income this month');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_income_this_month' => 'Get total income this month'
]
);
/** Statistics >> Economic >> Total income last month */
$this->get('/statistics/income/last-month', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_income_last_month');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME_LAST_MONTH', 'User requested total income last month');
$statics = new statistics();
// Return the total income last month
$response->success(
$statics
->economic()
->get_totals_last_month()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME_LAST_MONTH', 'User requested total income last month');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_income_last_month' => 'Get total income last month'
]
);
/** Statistics >> Economic >> Total income this year */
$this->get('/statistics/income/this-year', function () {
// Require the user to be logged in
global
/** @var response $response */
$EMAIL_WASH_CERTIFICATE_TOKEN,
$response;
$this->requirePermission('statistics_economic_income_this_year');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME_THIS_YEAR', 'User requested total income this year');
$statics = new statistics();
// Return the total income this year
$response->success(
$statics
->economic()
->get_totals_this_year()
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME_THIS_YEAR', 'User requested total income this year');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_income_this_year' => 'Get total income this year'
]
);
$this->get('/statistics/income/departments', function () {
// Require the user to be logged in
global
/** @var response $response */
$response;
$this->requirePermission('statistics_economic_income_today_departments');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('statistics', 'global', 1, $user->id, 'TOTAL_INCOME_TODAY_DEPARTMENTS', 'User requested total income today by departments');
$invoices = (new statistics())->economic()->get_department_sent_invoice_totals()->get_raw_data();
$drafts = (new statistics())->economic()->get_department_draft_invoice_totals()->get_raw_data();
$totals = [];
// Loop through every department and add the drafts to the invoices
foreach ( $invoices as $key => $invoice ) {
if (isset($drafts[$key])) {
// Add the totalNetAmount to the drafts in the totals array
$totals[$key]['totalNetAmount'] = $invoice['totalNetAmount'] + $drafts[$key]['totalNetAmount'];
} else {
// Add the totalNetAmount to the invoices in the totals array
$totals[$key]['totalNetAmount'] = $invoice['totalNetAmount'];
}
// Add the department name to the totals array
$totals[$key]['id'] = $key;
$totals[$key]['name'] = $invoice['name'];
}
// Save the response
$response->success(
$totals
);
} else {
// Log the incident
(new logs_o())->add('statistics', 'global', 0, 0, 'TOTAL_INCOME_TODAY_DEPARTMENTS', 'User requested total income today by departments');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'statistics_economic_income_today_departments' => 'Get total income today by departments'
]
);
}
}