Add customer default department handling and routes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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'];
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace objects;
|
||||
|
||||
use classes\db;
|
||||
use classes\object_property;
|
||||
use Exception;
|
||||
use traits\db_object_t;
|
||||
|
||||
class customer_default_department_o extends db
|
||||
{
|
||||
use db_object_t;
|
||||
|
||||
public object_property $customer_number; // The customer number (E-conomic)
|
||||
public object_property $department; // The department ID
|
||||
public object_property $created_at; // The timestamp of when the record was created
|
||||
public object_property $updated_at; // The timestamp of when the record was updated
|
||||
|
||||
/**
|
||||
* Convert the object to an array
|
||||
* @return array
|
||||
* @throws Exception If the object is not selected
|
||||
*/
|
||||
public function asArray(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
return [
|
||||
'id' => (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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use objects\customer_default_department_o;
|
||||
use objects\departments_o;
|
||||
use objects\logs_o;
|
||||
use objects\users_o;
|
||||
use traits\route_t;
|
||||
|
||||
class customerDefaultDepartmentRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->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',
|
||||
]
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use classes\response;
|
||||
use classes\router;
|
||||
use objects\logs_o;
|
||||
use traits\route_t;
|
||||
|
||||
class moduleEntraRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
global /** @var response $response */
|
||||
/** @var router $router */
|
||||
$router, $response;
|
||||
|
||||
|
||||
/** Modules > 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',
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user