Files
api/services/nginx/app/routes/moduleXLVaskRoute.php
T

291 lines
11 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use classes\response;
use classes\router;
use classes\xlvask;
use helpers\xlvask_customer;
use objects\orders_o;
use objects\users_o;
use traits\route_t;
class moduleXLVaskRoute
{
use route_t;
public function run(): void
{
global /** @var response $response */
/** @var router $router */
$router, $response;
$this->get('/modules/xlvask/usageLog', function () {
global $response;
self::requirePermission('modules_xlvask_usageLog');
$params = [
'dateFrom' => null,
'regNr' => null,
'vehicleId' => null,
'customerId' => null,
];
// Get the parameters from the request
foreach ( $params as $key => $value ) {
if ($this->isParametersSet([$key])) {
$params[$key] = $this->getParameter($key);
}
}
// Create the xlvask object
$xlvask = new xlvask();
$result = $xlvask->getUsageLog(...$params);
// Response
$response->success($result, 200);
},
[
'modules_xlvask_usageLog' => 'Get the usage log of the xlvask module'
]
);
$this->get('/modules/xlvask/vehicles', function () {
global $response;
self::requirePermission('modules_xlvask_vehicles');
$params = [
'customerId' => null,
'vehicleId' => null,
'registrationNumber' => null,
'changeDate' => null,
];
// Get the parameters from the request
foreach ( $params as $key => $value ) {
if ($this->isParametersSet([$key])) {
$params[$key] = $this->getParameter($key);
}
}
// Create the xlvask object
$xlvask = new xlvask();
$result = $xlvask->getVehicles(...$params);
// Response
$response->success($result, 200);
},
[
'modules_xlvask_vehicles' => 'Get the vehicles of the xlvask module'
]
);
$this->get('/modules/xlvask/customers', function () {
global $response;
self::requirePermission('modules_xlvask_customers');
$params = [
'customerId' => null,
'customerName' => null,
'customerExternalId' => null,
'vatNumber' => null,
'changeDate' => null,
'users' => null,
];
// Get the parameters from the request
foreach ( $params as $key => $value ) {
if ($this->isParametersSet([$key])) {
$params[$key] = $this->getParameter($key);
}
}
// Create the xlvask object
$xlvask = new xlvask();
$result = $xlvask->getCustomers(...$params);
// Response
$response->success($result, 200);
},
[
'modules_xlvask_customers' => 'Get the customers of the xlvask module'
]
);
$this->get('/modules/xlvask/internal/vehicle-types', function () {
global $response;
self::requirePermission('modules_xlvask_internal_vehicle_types');
$user = (new authentication())->get_user();
$xlvask_vehicle_types = new \objects\xlvask_vehicle_types_o();
$result = $xlvask_vehicle_types->listObjectsWithPaginationIfSet(
function ($tmp_object_array) {
return [
...$tmp_object_array
];
}
);
// Response
$response->success($result, 200);
},
[
'modules_xlvask_internal_vehicle_types' => 'Get the vehicle types of the xlvask module'
]
);
$this->get('/modules/xlvask/related-orders', function () {
global $response;
self::requirePermission('modules_xlvask_related_orders');
self::requireParameters(['washIds']);
$washIds = self::getParameter('washIds');
// Check if the washIds is an array
self::requireType($washIds, self::TYPE_ARRAY());
// Require only strings in the array
foreach ( $washIds as $key => $value ) {
if (!is_string($value)) {
self::requireType($value, self::TYPE_STRING());
}
}
// Get all the related orders
$orders_o = new orders_o();
$mathing_orders = $orders_o->getFieldsWhere([
'wash_id' => $washIds,
'deleted_at' => null,
], [
'id',
'wash_id',
]);
$formatted_matches = [];
foreach ( $mathing_orders as $order ) {
$formatted_matches[(string)$order['wash_id']][] = (int)$order['id'];
}
$response->success($formatted_matches, 200);
},
[
'modules_xlvask_related_orders' => 'Get the related orders of the xlvask module'
]
);
$this->get('/modules/xlvask/tasks/sync-users', function () {
global $response;
self::requirePermission('modules_xlvask_sync_users');
// Create the xlvask tasks object
$xlvask = new xlvask();
// Run the sync users task
$result = $xlvask->getTasks()->runSyncUsers();
// Response
$response->success(
$result,
200
);
},
[
'modules_xlvask_sync_users' => 'Synchronize users with the xlvask module'
]
);
$this->get('/modules/xlvask/tasks/sync-usage', function () {
global $response;
self::requirePermission('modules_xlvask_sync_usage');
// Create the xlvask tasks object
$xlvask = new xlvask();
// Run the sync usage task
$result = $xlvask->getTasks()->runSyncUsage();
// Response
$response->success(
$result,
200
);
},
[
'modules_xlvask_sync_usage' => 'Synchronize usage with the xlvask module'
]
);
$this->get('/modules/xlvask/tasks/debug', function () {
global $response;
self::requirePermission('modules_xlvask_sync_usage');
// Create the xlvask tasks object
$xlvask = new xlvask();
$user = new users_o();
$user->getUserByCustomerNumber(12345679);
//$xlvask->getTasks()->runSyncUsers(false);
//echo 'Creating customer in XLVask for user: ' . $user->customer_number->value() . " (" . $user->getCustomerName((int)$user->customer_number->value()) . ")\n";
if (!$xlvask->getCache()->isCustomerCached($user->customer_number->value())) {
throw new \Exception('Customer is not cached in XLVask');
}
// Get the customer from the cache
$xlvask_customer = $xlvask->getCache()->getCustomerCache($user->customer_number->value());
$new_customer = new $xlvask->helpers->xlvask_customer();
/** @var xlvask_customer $new_customer */
$new_customer->customerId = $xlvask->getGuid()->generate();
$new_customer->excludeFromAutoInvoice = true;
//$new_customer->discount = 0;
$new_customer->active = true;
$new_customer->name = $user->getCustomerName((int)$user->customer_number->value());
$new_customer->externId = (string)$user->customer_number->value() . '-' . $user->id;
if (!$new_customer->isValid()) {
throw new \Exception('Customer is not valid in XLVask, missing required properties: ' . implode(', ', $new_customer->getMissingRequiredProperties()));
}
// Create the customer in XLVask
$result = $xlvask->createCustomer($new_customer);
//echo 'Customer created in XLVask for user: ' . $user->customer_number->value() . " (" . $user->getCustomerName((int)$user->customer_number->value()) . ")\n";
throw new \Exception('Customer creation is not implemented yet, delayed until the XLVask API is ready');
// Create the customer in XLVask
$result = $xlvask->sendRequest($xlvask->config->api_url . '/Customers', 'POST', json_decode('{
"customerId": "' . $xlvask->getGuid()->generate() . '",
"name": "' . $user->getCustomerName((int)$user->customer_number->value()) . '",
"vendorId": "9dde3a0a-197d-4172-8d0d-b4652d867593",
"customerTypeId": "COMPANY",
"discount": 1,
"phone": null,
"email": null,
"address": null,
"address2": null,
"zip": null,
"city": null,
"userId": null,
"vatnumber": null,
"excludeFromAutoInvoice": true,
"country": null,
"createDate": null,
"externId": null,
"active": true,
"language": "da",
"note": null,
"updated": null
}', true), [
$xlvask->getAuthHeader()
]);
//$result = $xlvask->createCustomer($new_customer);
print_r($result);
// $xlvask_customer = $xlvask->getCache()->getCustomerCache($user->customer_number->value());
// $xlvask_customer->name = 'Pleno vognmandsforretning 123432';
// $xlvask->updateCustomer($xlvask_customer);
// Print the customer
//print_r($xlvask_customer);
// List the customers vehicles
$vehicles = new $xlvask->helpers->xlvask_vehicles();
// /** @var xlvask_vehicles $vehicles */
// $vehicles = $vehicles->getVehicles();
// echo 'Count of vehicles: ' . count($vehicles) . "\n";
// print_r($vehicles);
//// $vehicle = $vehicles->getVehicleByRegistrationNumber(
//// 'TESTAB123'
// );
// Print the vehicles
// print_r($vehicle);
// $vehicle->vehicleTypeId = (new $xlvask->helpers->xlvask_vehicle_types())->getVehicleTypesByProductId(2)[0]->vehicleTypeId;
// $result = $xlvask->deleteVehicle($vehicle);
// print_r($result);
// $vehicle = new $xlvask->helpers->xlvask_vehicle();
// /** @var xlvask_vehicle $vehicle */
// $vehicle->customerId = $xlvask_customer->customerId;
// $vehicle->registrationNumber = 'TEST123';
// $vehicle->vehicleTypeId = "e2638c21-366d-4b7f-b0af-eb3634ae2c8c";
// $vehicle->vehicleId = $xlvask->getGuid()->generate();
// $vehicle->autoStartOnLpr = true;
// $vehicle->active = true;
// print_r((new $xlvask->helpers->xlvask_vehicle_types())->getVehicleTypesByProductId(1)[0]);
throw new \Exception('Vehicle creation is not implemented yet, delayed until the XLVask API is ready');
$result = $xlvask->createVehicle($vehicle);
// Response
$response->success(
$result,
200
);
},
[
'modules_xlvask_sync_usage' => 'Synchronize usage with the xlvask module'
]
);
}
}