diff --git a/services/nginx/app/objects/orders_o.php b/services/nginx/app/objects/orders_o.php index 2a32bbc9..c810795b 100644 --- a/services/nginx/app/objects/orders_o.php +++ b/services/nginx/app/objects/orders_o.php @@ -443,7 +443,7 @@ class orders_o extends db * Get the wash subscription transactions for a customer * @throws Exception If something goes wrong */ - public function getWashSubscriptionTransactions(int $customer_number): array + public function getWashSubscriptionTransactions(int $customer_number, bool $asArray = true, ?string $dateFrom = null, ?string $dateTo = null): array { return self::listObjectsWithPagination( 1, @@ -453,12 +453,18 @@ class orders_o extends db 'customer_id' => $customer_number, 'deleted_at' => null, 'cashier_id' => (new collected_order_invoices_o())->economic_wash_subscription_user_id, + ...(!empty($dateFrom) ? ['created_at-date_from' => $dateFrom] : []), + ...(!empty($dateTo) ? ['created_at-date_to' => $dateTo] : []), ], [ 'id' => 'DESC', ], - function ($object) { - return (new orders_o())->select($object['id'])->asArray(); + function ($object) use ($asArray) { + $res = (new orders_o())->select($object['id']); + if ($asArray) { + return $res->asArray(); + } + return $res; } ); } diff --git a/services/nginx/app/objects/users_o.php b/services/nginx/app/objects/users_o.php index e3c5333f..b18a1a46 100644 --- a/services/nginx/app/objects/users_o.php +++ b/services/nginx/app/objects/users_o.php @@ -1122,4 +1122,17 @@ class users_o extends db return (new customer_default_department_o())->getDefaultDepartment((int)$this->customer_number->value()); } + public function getCustomersWithVehicleSubscriptions(): array + { + global $db; + // Get all customers with vehicle subscriptions + $sql = "SELECT DISTINCT customer_id FROM customer_vehicles WHERE wash_subscription = 1"; + $result = $db->query($sql); + $customer_numbers = []; + while ($row = $result->fetch_assoc()) { + $customer_numbers[] = (int)$row['customer_id']; + } + return $customer_numbers; + } + } \ No newline at end of file diff --git a/services/nginx/app/routes/InvoicingPeriodRoute.php b/services/nginx/app/routes/InvoicingPeriodRoute.php new file mode 100644 index 00000000..03947000 --- /dev/null +++ b/services/nginx/app/routes/InvoicingPeriodRoute.php @@ -0,0 +1,110 @@ +get('/superuser/invoicing/period', function () { + // Require the user to be logged in + global $response; + $this->requirePermission('superuser_invoicing_period'); + // Get the user object + $user = (new authentication())->get_user(); + // Check if the request was successful + if ($user) { + self::requireParameters([ + 'dateFrom', + 'dateTo', + ]); + $dateFrom = $this->getParameter('dateFrom'); + $dateTo = $this->getParameter('dateTo'); + // Require the dateFrom and dateTo parameters to be valid dates + self::requireDateFormat($dateFrom, 'Y-m-d'); + self::requireDateFormat($dateTo, 'Y-m-d'); + // Get the invoicing period for the user + $response->success([...self::getInvoicingPeriod($dateFrom, $dateTo)]); + // Log the incident + (new logs_o())->add('invoicing_period', 'global', 1, $user->id, 'GET_INVOICING_PERIOD', 'Successfully retrieved invoicing period'); + $response->success( + $vehicles_o->listObjectsWithPaginationIfSet( + function ($vehicle) use ($user) { + // Return the object as an array + return [ + ...(new customer_vehicles_o())->select($vehicle['id'])->asArray(), + ]; + }, + $vehicles_o->forceRestrictFilters([ + ...$restrict ?? [] + ]) + ) + ); + } else { + // Log the incident + (new logs_o())->add('vehicles', 'global', 1, 0, 'LIST_OWN_VEHICLES', 'No user found, or invalid session'); + // Return an error + $response->error('Invalid session', 400); + } + }, + [ + 'superuser_invoicing_period' => 'Get the invoicing period for superusers', + ] + ); + } + + /** + * @throws \Exception + */ + private static function getInvoicingPeriod(string $dateFrom, string $dateTo): array + { + return [ + 'dateFrom' => $dateFrom, + 'dateTo' => $dateTo, + 'types' => [ + 'vehicle_subscriptions' => self::getVehicleSubscriptions($dateFrom, $dateTo), + 'fixed_pricing' => [], + ], + ]; + } + + /** + * @throws \Exception + */ + private static function getVehicleSubscriptions(string $dateFrom, string $dateTo): array + { + // Get all customers with vehicle subscriptions + $customer_numbers = (new \objects\users_o())->getCustomersWithVehicleSubscriptions(); + $subscriptions = []; + /** @var int $customer_number */ + foreach ( $customer_numbers as $customer_number ) { + $tmp_subscription = [ + 'customer_number' => $customer_number, + 'customer_name' => (new \objects\users_o())->getCustomerName($customer_number), + 'transactions' => [], + ]; + // Check if the customer has a vehicle subscription order within the date range + $orders = (new \objects\orders_o())->getWashSubscriptionTransactions($customer_number, false, $dateFrom, $dateTo); + /** @var orders_o $order */ + foreach ( $orders as $order ) { + //echo "Checking order {$order->id} for customer {$customer_number}...\n"; + // Add the order to the subscription transactions + $tmp_subscription['transactions'][] = [ + 'id' => $order->id, + ]; + } + // Add the subscription to the list if it has transactions + $subscriptions[] = $tmp_subscription; + } + return $subscriptions; + } +} \ No newline at end of file diff --git a/services/nginx/app/routes/vehiclesRoute.php b/services/nginx/app/routes/vehiclesRoute.php index de8701fc..79689d3b 100644 --- a/services/nginx/app/routes/vehiclesRoute.php +++ b/services/nginx/app/routes/vehiclesRoute.php @@ -417,6 +417,49 @@ class vehiclesRoute 'list_vehicle_customer_suggestions' => 'List customer suggestions for a vehicle', ] ); + + $this->get('/superuser/users-with-vehicle-subscriptions', function () { + // Require the user to be logged in + global $response; + $this->requirePermission('list_users_with_vehicle_subscriptions'); + // Get the user object + $user = (new authentication())->get_user(); + // Check if the request was successful + if ($user) { + // Log the incident + (new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_USERS_WITH_VEHICLE_SUBSCRIPTIONS', 'Successfully listed users with vehicle subscriptions'); + // Return the list of users with vehicle subscriptions + $vehicles_o = new customer_vehicles_o(); + $customer_ids = $vehicles_o->getFieldsWhere( + [ + 'wash_subscription' => 1, + ], + ['customer_id'] + ); + // Get all the customer_ids that has vehicle subscriptions + $customer_ids = array_map(function ($customer_id) { + return (int)$customer_id['customer_id']; + }, $customer_ids); + // Remove duplicates + $customer_ids = array_unique($customer_ids); + $response->success( + $customer_ids ? array_map(function ($customer_id) { + $tmp_user = new users_o(); + $tmp_user->getUserByCustomerNumber((int)$customer_id); + return [...$tmp_user->asArray()]; + }, $customer_ids) : [] + ); + } else { + // Log the incident + (new logs_o())->add('vehicles', 'global', 1, 0, 'LIST_USERS_WITH_VEHICLE_SUBSCRIPTIONS', 'No user found, or invalid session'); + // Return an error + $response->error('Invalid session', 400); + } + }, + [ + 'list_users_with_vehicle_subscriptions' => 'List users with vehicle subscriptions', + ] + ); } private function getData(mixed $data, $response)