Introduced functionality for managing department daily reports, including endpoints for creating, updating, listing, and viewing product sales data. Enhanced product handling in reports by adding support for water usage, notes, and detailed product sales metrics. These changes improve tracking and reporting accuracy across departments.
439 lines
20 KiB
PHP
439 lines
20 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\department_daily_reports_o;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
class departmentDailyReportsRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/departments/daily-reports', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_department_daily_reports');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENT_DAILY_REPORTS', 'Successfully listed departments daily reports');
|
|
$department_daily_reports_o = new department_daily_reports_o();
|
|
// Return the list of departments
|
|
$response->success(
|
|
$department_daily_reports_o
|
|
->setSearchableFields([
|
|
// The fields that can be searched. This would otherwise make it possible to get secret information from the database, simply by searching for it and getting the result count back
|
|
'id',
|
|
'water_usage',
|
|
'notes',
|
|
'filled_by',
|
|
'created_at'
|
|
])
|
|
->listObjectsWithPaginationIfSet(
|
|
function ($department_report) use ($user) {
|
|
return [
|
|
'id' => (int)$department_report['id'],
|
|
'department_id' => (int)$department_report['department_id'],
|
|
'water_usage' => (int)$department_report['water_usage'],
|
|
'notes' => (string)$department_report['notes'],
|
|
'filled_by' => (int)$department_report['filled_by'],
|
|
'created_at' => (string)$department_report['created_at'],
|
|
];
|
|
},
|
|
// This makes sure that the user can only see reports from the departments they explicitly have access to
|
|
$department_daily_reports_o->forceRestrictFilters(
|
|
[
|
|
// This makes sure that the user can only see orders from the departments they explicitly have access to
|
|
'department_id' => $user->getGroup()->getDepartments(),
|
|
]
|
|
)
|
|
)
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENT_DAILY_REPORTS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'list_department_daily_reports' => 'List the department daily reports, provided the user has access to the department',
|
|
'department_access_:id' => 'Access the department'
|
|
]
|
|
);
|
|
|
|
$this->post('/departments/daily-reports', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('create_department_daily_reports');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if the required fields are set
|
|
self::requireParameters(
|
|
[
|
|
'department_id',
|
|
'water_usage',
|
|
'water_usage_morning',
|
|
'notes'
|
|
]
|
|
);
|
|
// Validate the department_id
|
|
self::requireType(
|
|
(int)self::fromRequest('department_id'),
|
|
self::type_int()
|
|
);
|
|
// Require the department_id to be above 0
|
|
self::requireMinValue(
|
|
(int)self::fromRequest('department_id'),
|
|
1
|
|
);
|
|
// Validate the water_usage
|
|
self::requireType(
|
|
(int)self::fromRequest('water_usage'),
|
|
self::type_int()
|
|
);
|
|
// Require the water_usage to be at least 0
|
|
self::requireMinValue(
|
|
(int)self::fromRequest('water_usage'),
|
|
0
|
|
);
|
|
// Validate the water_usage_morning
|
|
self::requireType(
|
|
(int)self::fromRequest('water_usage_morning'),
|
|
self::type_int()
|
|
);
|
|
// Require the water_usage_morning to be at least 0
|
|
self::requireMinValue(
|
|
(int)self::fromRequest('water_usage_morning'),
|
|
0
|
|
);
|
|
// Validate the notes
|
|
self::requireType(
|
|
(string)self::fromRequest('notes'),
|
|
self::type_string()
|
|
);
|
|
// Require the notes to be at least 0 characters long
|
|
self::requireMinLength(
|
|
(string)self::fromRequest('notes'),
|
|
0
|
|
);
|
|
// Require the notes to be at most 4000 characters long
|
|
self::requireMaxLength(
|
|
(string)self::fromRequest('notes'),
|
|
4000
|
|
);
|
|
// Determine if the user has access to the department
|
|
self::requireDepartmentAccess((int)self::fromRequest('department_id'));
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, $user->id, 'CREATE_DEPARTMENT_DAILY_REPORTS', 'Successfully created department daily report');
|
|
// Return the list of departments
|
|
$response->success(
|
|
(new department_daily_reports_o())
|
|
->add(
|
|
(int)self::fromRequest('department_id'),
|
|
(int)self::fromRequest('water_usage'),
|
|
(int)self::fromRequest('water_usage_morning'),
|
|
(string)self::fromRequest('notes'),
|
|
$user->id
|
|
)->asArray()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'CREATE_DEPARTMENT_DAILY_REPORTS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'create_department_daily_reports' => 'Create a new department daily report',
|
|
'department_access_:id' => 'Access the department'
|
|
]
|
|
);
|
|
|
|
$this->put('/departments/daily-reports', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('update_department_daily_reports');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if the required fields are set
|
|
self::requireParameters(
|
|
[
|
|
'department_id',
|
|
]
|
|
);
|
|
// Validate the department_id
|
|
self::requireType(
|
|
(int)self::fromRequest('department_id'),
|
|
self::type_int()
|
|
);
|
|
// Require the department_id to be above 0
|
|
self::requireMinValue(
|
|
(int)self::fromRequest('department_id'),
|
|
1
|
|
);
|
|
// Determine if the user has access to the department
|
|
self::requireDepartmentAccess((int)self::fromRequest('department_id'));
|
|
// Check which fields are set
|
|
if (self::isParametersSet(['water_usage'])) {
|
|
// Validate the water_usage
|
|
self::requireType(
|
|
(int)self::fromRequest('water_usage'),
|
|
self::type_int()
|
|
);
|
|
// Require the water_usage to be at least 0
|
|
self::requireMinValue(
|
|
(int)self::fromRequest('water_usage'),
|
|
0
|
|
);
|
|
}
|
|
if (self::isParametersSet(['water_usage_morning'])) {
|
|
// Validate the water_usage_morning
|
|
self::requireType(
|
|
(int)self::fromRequest('water_usage_morning'),
|
|
self::type_int()
|
|
);
|
|
// Require the water_usage_morning to be at least 0
|
|
self::requireMinValue(
|
|
(int)self::fromRequest('water_usage_morning'),
|
|
0
|
|
);
|
|
}
|
|
if (self::isParametersSet(['notes'])) {
|
|
// Validate the notes
|
|
self::requireType(
|
|
(string)self::fromRequest('notes'),
|
|
self::type_string()
|
|
);
|
|
// Require the notes to be at least 0 characters long
|
|
self::requireMinLength(
|
|
(string)self::fromRequest('notes'),
|
|
0
|
|
);
|
|
// Require the notes to be at most 4000 characters long
|
|
self::requireMaxLength(
|
|
(string)self::fromRequest('notes'),
|
|
4000
|
|
);
|
|
}
|
|
// Check if the report exists
|
|
$department_daily_reports_o = new department_daily_reports_o();
|
|
if (!$department_daily_reports_o->doesDepartmentDailyReportExist((int)self::fromRequest('department_id'))) {
|
|
$response->error('Department daily report not found', 404);
|
|
}
|
|
// Update the department daily report
|
|
$department_daily_report = $department_daily_reports_o->selectDepartmentDailyReport((int)self::fromRequest('department_id'));
|
|
if (self::isParametersSet(['water_usage'])) {
|
|
$department_daily_report->water_usage->set((int)self::fromRequest('water_usage'));
|
|
}
|
|
if (self::isParametersSet(['water_usage_morning'])) {
|
|
$department_daily_report->water_usage_morning->set((int)self::fromRequest('water_usage_morning'));
|
|
}
|
|
if (self::isParametersSet(['notes'])) {
|
|
$department_daily_report->notes->set((string)self::fromRequest('notes'));
|
|
}
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, $user->id, 'UPDATE_DEPARTMENT_DAILY_REPORTS', 'Successfully updated department daily report');
|
|
// Return the department daily report
|
|
$response->success($department_daily_report->asArray());
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'UPDATE_DEPARTMENT_DAILY_REPORTS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'update_department_daily_reports' => 'Update a department daily report',
|
|
'department_access_:id' => 'Access the department'
|
|
]
|
|
);
|
|
|
|
$this->get('/departments/daily-reports/product-count', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_department_daily_reports');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if the required fields are set
|
|
self::requireParameters(
|
|
[
|
|
'date',
|
|
'department_id',
|
|
'product_id'
|
|
]
|
|
);
|
|
// Validate the date
|
|
self::requireType(
|
|
(string)self::getParameter('date'),
|
|
self::type_string()
|
|
);
|
|
// Require the date to be at least 0 characters long
|
|
self::requireMinLength(
|
|
'date',
|
|
0
|
|
);
|
|
// Require the date to be at most 10 characters long
|
|
self::requireMaxLength(
|
|
'date',
|
|
10
|
|
);
|
|
// Validate the date format (YYYY-MM-DD)
|
|
self::requireDateFormat(
|
|
(string)self::getParameter('date'),
|
|
'Y-m-d'
|
|
);
|
|
// Validate the department_id
|
|
self::requireType(
|
|
(int)self::getParameter('department_id'),
|
|
self::type_int()
|
|
);
|
|
// Require the department_id to be above 0
|
|
self::requireMinValue(
|
|
(int)self::getParameter('department_id'),
|
|
1
|
|
);
|
|
// Validate the product_id
|
|
self::requireType(
|
|
(int)self::getParameter('product_id'),
|
|
self::type_int()
|
|
);
|
|
// Require the product_id to be above 0
|
|
self::requireMinValue(
|
|
(int)self::getParameter('product_id'),
|
|
1
|
|
);
|
|
// Determine if the user has access to the department
|
|
self::requireDepartmentAccess((int)self::getParameter('department_id'));
|
|
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'Successfully listed departments daily reports products');
|
|
// Return the list of products sold on the selected date
|
|
$response->success(
|
|
[
|
|
'quantity' =>
|
|
(new department_daily_reports_o())
|
|
->getProductsSoldOnDate(
|
|
(string)self::getParameter('date'),
|
|
(int)self::getParameter('department_id'),
|
|
(int)self::getParameter('product_id')
|
|
),
|
|
'date' => (string)self::getParameter('date'),
|
|
'department_id' => (int)self::getParameter('department_id'),
|
|
'product_id' => (int)self::getParameter('product_id'),
|
|
// This is the amount of products sold on the selected date, that this product could have been sold as a part of. (Addons)
|
|
'out_of' => (int)(new department_daily_reports_o())->getProductsSoldWithAddonApplicable(
|
|
(string)self::getParameter('date'),
|
|
(int)self::getParameter('department_id'),
|
|
(int)self::getParameter('product_id')
|
|
)
|
|
]
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'list_department_daily_reports' => 'List the department daily reports, provided the user has access to the department',
|
|
'department_access_:id' => 'Access the department'
|
|
]
|
|
);
|
|
|
|
$this->get('/departments/daily-reports/transaction-count', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_department_daily_reports');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if the required fields are set
|
|
self::requireParameters(
|
|
[
|
|
'date',
|
|
'department_id'
|
|
]
|
|
);
|
|
// Validate the date
|
|
self::requireType(
|
|
(string)self::getParameter('date'),
|
|
self::type_string()
|
|
);
|
|
// Require the date to be at least 0 characters long
|
|
self::requireMinLength(
|
|
'date',
|
|
0
|
|
);
|
|
// Require the date to be at most 10 characters long
|
|
self::requireMaxLength(
|
|
'date',
|
|
10
|
|
);
|
|
// Validate the date format (YYYY-MM-DD)
|
|
self::requireDateFormat(
|
|
(string)self::getParameter('date'),
|
|
'Y-m-d'
|
|
);
|
|
// Validate the department_id
|
|
self::requireType(
|
|
(int)self::getParameter('department_id'),
|
|
self::type_int()
|
|
);
|
|
// Require the department_id to be above 0
|
|
self::requireMinValue(
|
|
(int)self::getParameter('department_id'),
|
|
1
|
|
);
|
|
// Determine if the user has access to the department
|
|
self::requireDepartmentAccess((int)self::getParameter('department_id'));
|
|
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, $user->id, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'Successfully listed departments daily reports products');
|
|
// Return the list of products sold on the selected date
|
|
$response->success(
|
|
[
|
|
'quantity' =>
|
|
(new department_daily_reports_o())
|
|
->getTransactionsOnDateCount(
|
|
(string)self::getParameter('date'),
|
|
(int)self::getParameter('department_id')
|
|
),
|
|
'products' =>
|
|
(new department_daily_reports_o())
|
|
->getTransactionProductsOnDateCount(
|
|
(string)self::getParameter('date'),
|
|
(int)self::getParameter('department_id')
|
|
),
|
|
'date' => (string)self::getParameter('date'),
|
|
'department_id' => (int)self::getParameter('department_id')
|
|
]
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('departments', 'global', 1, 0, 'LIST_DEPARTMENT_DAILY_REPORTS_PRODUCTS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
},
|
|
[
|
|
'list_department_daily_reports' => 'List the department daily reports, provided the user has access to the department',
|
|
'department_access_:id' => 'Access the department'
|
|
]
|
|
);
|
|
}
|
|
} |