From 743928459ee566f741ea9173602c3a493ec5b4cb Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Mon, 26 May 2025 16:58:09 +0200 Subject: [PATCH] Add customer default department handling and routes --- services/nginx/app/classes/entra.php | 38 +++++ .../helpers/economic_invoice_draft.php | 10 ++ .../objects/customer_default_department_o.php | 136 +++++++++++++++ services/nginx/app/objects/users_o.php | 21 +++ .../routes/customerDefaultDepartmentRoute.php | 159 ++++++++++++++++++ .../nginx/app/routes/economicInvoiceRoute.php | 1 + .../nginx/app/routes/moduleEntraRoute.php | 40 +++++ 7 files changed, 405 insertions(+) create mode 100644 services/nginx/app/objects/customer_default_department_o.php create mode 100644 services/nginx/app/routes/customerDefaultDepartmentRoute.php create mode 100644 services/nginx/app/routes/moduleEntraRoute.php diff --git a/services/nginx/app/classes/entra.php b/services/nginx/app/classes/entra.php index 92d67625..7070cb3c 100644 --- a/services/nginx/app/classes/entra.php +++ b/services/nginx/app/classes/entra.php @@ -7,6 +7,8 @@ require_once WD . '/modules/entra/entra_c.php'; use entra\entra_c; use Microsoft\Graph\GraphServiceClient; use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext; +use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContextBuilder; + class entra { @@ -21,6 +23,19 @@ class entra $this->config = new entra_c(); } + public function get_calendars(): array|object + { + // List all the user calendars + $graphClient = $this->getGraphClient(); + $calendars = $graphClient->Calendars() + ->get() + ->wait() + ->getValue(); + //TODO: This does not work, need to find a way to get the calendars + $result = []; + return $result; + } + public function getGraphClient(): GraphServiceClient { return new GraphServiceClient( @@ -36,4 +51,27 @@ class entra $this->config->client_secret->getVariableValue() ); } + + public function get_users($array = false): array|object + { + $graphClient = $this->getGraphClient(); + + $users = $graphClient->users() + ->get() + ->wait() + ->getValue(); + if (!$array) { + return $users; + } + $result = []; + foreach ( $users as $user ) { + $result[] = [ + 'id' => $user->getId(), + 'displayName' => $user->getDisplayName(), + 'mail' => $user->getMail(), + 'userPrincipalName' => $user->getUserPrincipalName(), + ]; + } + return $result; + } } \ No newline at end of file diff --git a/services/nginx/app/modules/economic/helpers/economic_invoice_draft.php b/services/nginx/app/modules/economic/helpers/economic_invoice_draft.php index e626baea..2c914f13 100644 --- a/services/nginx/app/modules/economic/helpers/economic_invoice_draft.php +++ b/services/nginx/app/modules/economic/helpers/economic_invoice_draft.php @@ -209,6 +209,16 @@ class economic_invoice_draft $order_items = $order->applyDepartmentPrices($order_items, $order->department_id->value()); // Get the department $department = $order->getDepartmentByOrderId($order->id); + // If the department id is 10, check if the user has a default department set + if ($department['id'] == 10) { + // Get the default department for the customer + $customer_default_department_o = new \objects\customer_default_department_o(); + $default_department_id = $customer_default_department_o->getDefaultDepartment((int)$order->customer_id->value()); + // If the default department is set, use it + if ($default_department_id !== null) { + $department = (new departments_o())->getDepartmentById($default_department_id); + } + } // Remove all the order items that are not included in the invoice $order_items = array_filter($order_items, function ($order_item) { return (bool)$order_item['include_in_invoice']; diff --git a/services/nginx/app/objects/customer_default_department_o.php b/services/nginx/app/objects/customer_default_department_o.php new file mode 100644 index 00000000..90228aef --- /dev/null +++ b/services/nginx/app/objects/customer_default_department_o.php @@ -0,0 +1,136 @@ + (int)$this->id, + 'customer_number' => (int)$this->customer_number->value(), + 'department' => (int)$this->department->value(), + 'created_at' => (string)$this->created_at->value(), + 'updated_at' => (string)$this->updated_at->value(), + ]; + } + + + /** + * @throws Exception If the creation of the object fails + */ + public function add(int $customer_number, int $department): void + { + global $db, $response; + $tmp_id = self::add_object([ + 'customer_number' => (int)$customer_number, + 'department' => (int)$department, + ]); + self::select((int)$tmp_id); + self::objectChanged(); + } + + public function objectChanged(): void + { + // Since the customer_vehicles object is not cached, there is no need to invalidate the cache + } + + public function structure(): void + { + $this->setTable('customer_default_department'); + } + + public function getObjectProperties(): void + { + $this->customer_number = new object_property($this->table, $this->id, 'customer_number', 'int', true); + $this->department = new object_property($this->table, $this->id, 'department', 'int', true); + $this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', true); + $this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', true); + } + + /** + * Get the default department for a customer by customer number + * @param int $customer_number + * @return int|null Returns the department ID if it exists, null otherwise. + * @throws Exception If the user does not have a default department set. + */ + public function getDefaultDepartment(int $customer_number): int|null + { + if (!$this->doesUserHaveDefaultDepartment($customer_number)) { + return null; + } + $this->selectByCustomerNumber($customer_number); + if ($this->exists()) { + return (int)$this->department->value(); + } else { + return null; + } + } + + /** + * Check if the user has a default department set + * @param int $customer_number + * @return bool Returns true if the user has a default department set, false otherwise. + */ + public function doesUserHaveDefaultDepartment(int $customer_number): bool + { + return $this->getIdByCustomerNumber($customer_number) !== null; + } + + /** + * Get the ID of the customer_default_department object by customer number + * @param int $customer_number + * @return int|null Returns the ID of the customer_default_department object, if it exists. Returns null, if it does not exist. + */ + public function getIdByCustomerNumber(int $customer_number): ?int + { + $matches = self::getFieldsWhere([ + 'customer_number' => (int)$customer_number, + ], [ + 'id' + ]); + if (count($matches) > 0) { + // There is a match, so return the ID + return (int)$matches[0]['id']; + } else { + // There's no match, so return null + return null; + } + } + + /** + * Get the customer_default_department object by customer number + * @param int $customer_number + * @return customer_default_department_o|null Returns the customer_default_department_o object if it exists, null otherwise. + * @throws Exception If the user does not have a default department set. + * @see doesUserHaveDefaultDepartment() + * @see getIdByCustomerNumber() + */ + public function selectByCustomerNumber(int $customer_number): ?customer_default_department_o + { + $this->select($this->getIdByCustomerNumber($customer_number)); + if ($this->exists()) { + return $this; + } else { + throw new Exception('Unable to select customer default department object for customer number: ' . $customer_number); + } + } +} \ No newline at end of file diff --git a/services/nginx/app/objects/users_o.php b/services/nginx/app/objects/users_o.php index 7cca350f..e3c5333f 100644 --- a/services/nginx/app/objects/users_o.php +++ b/services/nginx/app/objects/users_o.php @@ -1101,4 +1101,25 @@ class users_o extends db } return false; } + + /** + * @throws Exception + */ + public function doesUserHaveDefaultDepartment(): bool + { + // Check if the user has a default department set + self::requireSelected(); + return (new customer_default_department_o())->doesUserHaveDefaultDepartment((int)$this->customer_number->value()); + } + + /** + * @throws Exception + */ + public function getDefaultDepartment(): int + { + // Get the default department for the user + self::requireSelected(); + return (new customer_default_department_o())->getDefaultDepartment((int)$this->customer_number->value()); + } + } \ No newline at end of file diff --git a/services/nginx/app/routes/customerDefaultDepartmentRoute.php b/services/nginx/app/routes/customerDefaultDepartmentRoute.php new file mode 100644 index 00000000..aaf13e16 --- /dev/null +++ b/services/nginx/app/routes/customerDefaultDepartmentRoute.php @@ -0,0 +1,159 @@ +get('/customer/department/default', function () { + // Require the user to be logged in + global $response; + self::requirePermission('get_customer_default_department'); + $user = (new authentication())->get_user(); + if (!$user) { + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_GET_DEFAULT_DEPARTMENT', 'User not logged in'); + $response->error('Invalid session', 400); + } + // Require the customer number parameter + self::requireParameters(['customer_number']); + // Check if the customer number is valid + $customer_number = (int)self::getParameter('customer_number'); + self::requireMinLength('customer_number', 1); + self::requireMaxLength('customer_number', 255); + self::requireType($customer_number, self::type_int()); + self::requireMinValue($customer_number, 1); + // Check if the customer number exists + $customer = (new users_o())->getUserByCustomerNumber((int)$customer_number); + if (!$customer->exists()) { + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_GET_DEFAULT_DEPARTMENT', 'Customer not found'); + $response->error('Customer not found', 400); + } + // Get the default department for the customer + $customer_default_department_o = new customer_default_department_o(); + $default_department_object_id = $customer_default_department_o->getIdByCustomerNumber((int)$customer_number); + if (empty($default_department_object_id)) { + // Return "Not found" + $response->error('This customer does not have a default department', 400); + } + // Select the object + $customer_default_department_o->select((int)$default_department_object_id); + // Log the action + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_GET_DEFAULT_DEPARTMENT', 'Default department retrieved'); + // Return success + $response->success($customer_default_department_o->asArray()); + }, + [ + 'get_customer_default_department' => 'Get the default department for a customer', + ] + ); + + $this->post('/customer/department/default', function () { + // Require the user to be logged in + global $response; + self::requirePermission('add_customer_default_department'); + $user = (new authentication())->get_user(); + if (!$user) { + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_ADD_DEFAULT_DEPARTMENT', 'User not logged in'); + $response->error('Invalid session', 400); + } + // Require the customer number and department parameters + self::requireParameters(['customer_number', 'department']); + // Check if the customer number is valid + $customer_number = (int)self::getParameter('customer_number'); + self::requireMinLength('customer_number', 1); + self::requireMaxLength('customer_number', 255); + self::requireType($customer_number, self::type_int()); + self::requireMinValue($customer_number, 1); + // Check if the department is valid + $department = (int)self::getParameter('department'); + self::requireMinLength('department', 1); + self::requireMaxLength('department', 255); + self::requireType($department, self::type_int()); + self::requireMinValue($department, 1); + // Check if the department actually exists + $departments_o = new departments_o(); + $department_object = $departments_o->selectId((int)$department); + if (!$department_object->exists()) { + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_ADD_DEFAULT_DEPARTMENT', 'Department not found'); + $response->error('Department not found', 400); + } + // Check if the customer number exists + $customer = (new users_o())->getUserByCustomerNumber((int)$customer_number); + if (!$customer->exists()) { + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_ADD_DEFAULT_DEPARTMENT', 'Customer not found'); + $response->error('Customer not found', 400); + } + // Check if the customer already has a default department + $customer_default_department_o = new customer_default_department_o(); + $default_department_object_id = $customer_default_department_o->getIdByCustomerNumber((int)$customer_number); + if (!empty($default_department_object_id)) { + // Return "Already exists" + $response->error('This customer already has a default department', 400); + } + // Add the default department + $customer_default_department_o->add((int)$customer_number, (int)$department); + // Log the action + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_ADD_DEFAULT_DEPARTMENT', 'Default department added'); + // Return success + $response->success($customer_default_department_o->asArray()); + }, + [ + 'add_customer_default_department' => 'Add a default department for a customer', + ] + ); + + $this->delete('/customer/department/default', function () { + // Require the user to be logged in + global $response; + self::requirePermission('delete_customer_default_department'); + $user = (new authentication())->get_user(); + if (!$user) { + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_DELETE_DEFAULT_DEPARTMENT', 'User not logged in'); + $response->error('Invalid session', 400); + } + // Require the customer number parameter + self::requireParameters(['customer_number']); + // Check if the customer number is valid + $customer_number = (int)self::getParameter('customer_number'); + self::requireMinLength('customer_number', 1); + self::requireMaxLength('customer_number', 255); + self::requireType($customer_number, self::type_int()); + self::requireMinValue($customer_number, 1); + // Check if the customer number exists + $customer = (new users_o())->getUserByCustomerNumber((int)$customer_number); + if (!$customer->exists()) { + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_DELETE_DEFAULT_DEPARTMENT', 'Customer not found'); + $response->error('Customer not found', 400); + } + // Check if the customer already has a default department + $customer_default_department_o = new customer_default_department_o(); + $default_department_object_id = $customer_default_department_o->getIdByCustomerNumber((int)$customer_number); + if (empty($default_department_object_id)) { + // Return "Not found" + $response->error('This customer does not have a default department', 400); + } + // Delete the default department + $customer_default_department_o->select((int)$default_department_object_id); + $customer_default_department_o->delete(); + // Log the action + (new logs_o())->add('customer_default_department', 'global', 0, $user->id, 'CUSTOMER_DELETE_DEFAULT_DEPARTMENT', 'Default department deleted'); + // Return success + $response->success('Default department deleted'); + }, + [ + 'delete_customer_default_department' => 'Delete a default department for a customer', + ] + ); + + } +} \ No newline at end of file diff --git a/services/nginx/app/routes/economicInvoiceRoute.php b/services/nginx/app/routes/economicInvoiceRoute.php index 01dc4330..bb1226b9 100644 --- a/services/nginx/app/routes/economicInvoiceRoute.php +++ b/services/nginx/app/routes/economicInvoiceRoute.php @@ -308,6 +308,7 @@ class economicInvoiceRoute * @param economic_invoice_draft_mo $economic_invoice_draft * @param int $quantity * @return void + * @throws \Exception */ function addOrderItemToInvoice(users_o $customer, orders_o $order, mixed $order_item, economic_invoice_draft_mo $economic_invoice_draft, int $quantity = 1): void { diff --git a/services/nginx/app/routes/moduleEntraRoute.php b/services/nginx/app/routes/moduleEntraRoute.php new file mode 100644 index 00000000..df3a7679 --- /dev/null +++ b/services/nginx/app/routes/moduleEntraRoute.php @@ -0,0 +1,40 @@ + Entra > Calendar groups > GET */ + $this->get('/modules/entra/calendar-groups', function () { + global $response; + self::requirePermission('modules_entra_calendar_groups'); + $user = (new authentication())->get_user(); + if ($user) { + $result = (new \classes\entra())->get_calendars(); + $response->success($result); + } else { + (new logs_o())->add('modules_entra_calendar_groups', 'global', 1, 0, 'MODULES_ENTRA_CALENDAR_GROUPS', 'User accessed the calendar groups without being logged in'); + $response->error('Invalid session', 400); + } + }, + [ + 'modules_entra_calendar_groups' => 'List calendar groups', + ] + ); + } +} \ No newline at end of file