diff --git a/services/nginx/app/classes/statistics.php b/services/nginx/app/classes/statistics.php index 25c9f029..c5651cee 100644 --- a/services/nginx/app/classes/statistics.php +++ b/services/nginx/app/classes/statistics.php @@ -4,6 +4,7 @@ namespace classes; use interfaces\statistics_i; use statistics\bookings_s; +use statistics\orders_s; class statistics implements statistics_i { @@ -15,4 +16,12 @@ class statistics implements statistics_i { return new bookings_s(); } + + /** + * @inheritDoc + */ + public function orders(): orders_s + { + return new orders_s(); + } } \ No newline at end of file diff --git a/services/nginx/app/interfaces/statistics_i.php b/services/nginx/app/interfaces/statistics_i.php index 8d76c1fb..05384834 100644 --- a/services/nginx/app/interfaces/statistics_i.php +++ b/services/nginx/app/interfaces/statistics_i.php @@ -3,6 +3,7 @@ namespace interfaces; use statistics\bookings_s; +use statistics\orders_s; interface statistics_i { @@ -11,4 +12,10 @@ interface statistics_i * @return bookings_s */ public function bookings(): bookings_s; + + /** + * Get the statistics for orders + * @return orders_s + */ + public function orders(): orders_s; } \ No newline at end of file diff --git a/services/nginx/app/routes/statisticsRoute.php b/services/nginx/app/routes/statisticsRoute.php index 2be8c8ae..84bbf59b 100644 --- a/services/nginx/app/routes/statisticsRoute.php +++ b/services/nginx/app/routes/statisticsRoute.php @@ -44,5 +44,35 @@ class statisticsRoute } }); + /** 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) { + // 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() + ->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); + } + }); + } } \ No newline at end of file diff --git a/services/nginx/app/statistics/bookings_s.php b/services/nginx/app/statistics/bookings_s.php index 7cc1c568..082505dc 100644 --- a/services/nginx/app/statistics/bookings_s.php +++ b/services/nginx/app/statistics/bookings_s.php @@ -19,18 +19,25 @@ class bookings_s $bookings = $bookings_o->getFields(['id', 'department', 'date']); // Get the departments $departments = $departments_o->getFields(['id', 'name']); - // Add the labels (months) - $this->labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; - // Add the labels, and the data sets + // 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 ) { - $tmp_department_monthly = []; - foreach ( $this->labels as $label ) { - $tmp_department_monthly[$label] = 0; + $department_data_set = []; + foreach ( $this->get_yearly_months_covered() as $year => $months ) { + foreach ( $months as $month ) { + $count = 0; + foreach ( $bookings as $booking ) { + if ($booking['department'] == $department['id'] && date('F', strtotime($booking['date'])) == $month && date('Y', strtotime($booking['date'])) == $year) { + $count++; + } + } + $department_data_set[$month . ' ' . $year] = $count; + } } - $this->add_data_set($department['name'], $tmp_department_monthly, ['backgroundColor' => '#FF0000']); + // Add the department data set + $this->add_data_set($department['name'], $department_data_set); } return $this; - - } } \ No newline at end of file diff --git a/services/nginx/app/statistics/orders_s.php b/services/nginx/app/statistics/orders_s.php new file mode 100644 index 00000000..e9b6dae2 --- /dev/null +++ b/services/nginx/app/statistics/orders_s.php @@ -0,0 +1,43 @@ +getFields(['id', 'department_id', 'created_at']); + // Get the departments + $departments = $departments_o->getFields(['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; + } +} \ No newline at end of file diff --git a/services/nginx/app/traits/statistic_t.php b/services/nginx/app/traits/statistic_t.php index a0411f70..65845edd 100644 --- a/services/nginx/app/traits/statistic_t.php +++ b/services/nginx/app/traits/statistic_t.php @@ -6,8 +6,42 @@ use statistics\bookings_s; trait statistic_t { + public array $default_background_colors = [ + '#FF0000', + '#00FF00', + '#0000FF', + '#FFFF00', + '#00FFFF', + '#FF00FF', + '#000000', + '#FFFFFF', + '#FFA500', + '#FFC0CB', + '#FF1493', + '#FF4500', + '#FFD700', + '#FF6347', + '#FF69B4', + '#FF8C00', + '#FFA07A', + '#FFB6C1', + '#FFDAB9', + '#FFDEAD', + '#FFE4B5', + '#FFE4C4', + '#FFE4E1', + '#FFEBCD', + '#FFEFD5', + '#FFFAF0', + '#FFFAFA', + '#FFFF00', + '#FFFFE0', + '#FFFFF0', + '#FFFFFF', + ]; public array $data_sets = []; public array $labels = []; + public array $years = []; /** * The start date of the date range * This is set by the set_date_range method, and is used to filter the data. @@ -26,13 +60,22 @@ trait statistic_t */ public string $end; + public function __construct() + { + // Set the date range + $this->set_date_range(); + } + /** * Set the date range for the statistics */ - public function set_date_range(string $start, string $end): self + public function set_date_range(string $start = null, string $end = null): self { - $this->start = $start; - $this->end = $end; + // If the start and end dates are not set, use the default values + $this->start = $start ?? $this->get_default_start_date(); + $this->end = $end ?? $this->get_default_end_date(); + // set the years + $this->years = range(date('Y', strtotime($this->start)), date('Y', strtotime($this->end))); return $this; } @@ -41,6 +84,11 @@ trait statistic_t */ public function get_default_start_date(): string { + // Check if the date is set in the request + if (isset($_GET['start_date'])) { + return $_GET['start_date']; + } + // Return the default start date return date('Y-m-d', strtotime('-1 month')); } @@ -49,20 +97,14 @@ trait statistic_t */ public function get_default_end_date(): string { + // Check if the date is set in the request + if (isset($_GET['end_date'])) { + return $_GET['end_date']; + } + // Return the default end date return date('Y-m-d'); } - /** - * Add a label to the statistics - * @param string $label The label to add to the statistics (e.g. 'January') - * @return bookings_s|statistic_t - */ - public function add_label(string $label): self - { - $this->labels[] = $label; - return $this; - } - /** * Add a data set to the statistics * @param string $label The label for the data set (e.g. 'January') @@ -75,7 +117,7 @@ trait statistic_t // Apply the default options $options = array_merge([ 'label' => 'Dataset ' . (count($this->data_sets) + 1), - 'backgroundColor' => '#FF0000', + 'backgroundColor' => $this->default_background_colors[count($this->data_sets) % count($this->default_background_colors)] ], $options); // Add the data set $this->data_sets[] = [ @@ -94,9 +136,73 @@ trait statistic_t { return [ 'labels' => $this->labels, - 'datasets' => $this->data_sets + 'datasets' => $this->data_sets, + 'timeline' => [ + 'start' => $this->start, + 'end' => $this->end, + 'years' => $this->years + ] ]; } + /** + * Get the years for the statistics + * @return array + */ + public function get_years(): array + { + return $this->years; + } + + /** + * Add the labels for the months covered by the time range (e.g. January 2021, February 2021, March 2021, etc.) + */ + public function add_monthly_labels(): self + { + // For each year, add the months covered + foreach ( $this->get_yearly_months_covered() as $year => $months ) { + foreach ( $months as $month ) { + $this->add_label($month . ' ' . $year); + } + } + return $this; + } + + /** + * Get yearly months covered by the time range + * @return array + * @example ['2021' => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], '2022' => ['January', 'February']] + */ + public function get_yearly_months_covered(): array + { + // Get the start and end dates + $start = strtotime($this->start); + $end = strtotime($this->end); + // Get the years + $years = range(date('Y', $start), date('Y', $end)); + // Loop through the years, and add the months that are covered between the start and end dates for each year + $months = []; + foreach ( $years as $year ) { + $months[$year] = []; + $start_month = $year == date('Y', $start) ? date('n', $start) : 1; + $end_month = $year == date('Y', $end) ? date('n', $end) : 12; + for ( $month = $start_month; $month <= $end_month; $month++ ) { + $months[$year][] = date('F', mktime(0, 0, 0, $month, 1)); + } + } + return $months; + } + + /** + * Add a label to the statistics + * @param string $label The label to add to the statistics (e.g. 'January') + * @return bookings_s|statistic_t + */ + public function add_label(string $label): self + { + $this->labels[] = $label; + return $this; + } + } \ No newline at end of file