Add task helpers and cron job support for xlvask module with user synchronization endpoint integration

This commit is contained in:
Jepp9350
2025-06-03 09:09:50 +02:00
parent a3fc7cf08a
commit b0173193f2
6 changed files with 355 additions and 0 deletions
+18
View File
@@ -4,6 +4,7 @@ namespace classes;
require_once WD . '/modules/xlvask/xlvask_c.php';
require_once WD . '/modules/xlvask/classes/xlvask_endpoints.php';
require_once WD . '/modules/xlvask/xlvask_helpers.php';
/**
@@ -11,9 +12,11 @@ require_once WD . '/modules/xlvask/classes/xlvask_endpoints.php';
*/
use helpers\xlvask_tasks;
use interfaces\xlvask_i;
use xlvask\classes\xlvask_endpoints;
use xlvask\xlvask_c;
use xlvask\xlvask_helpers;
class xlvask extends xlvask_endpoints implements xlvask_i
{
@@ -22,10 +25,16 @@ class xlvask extends xlvask_endpoints implements xlvask_i
* @var xlvask_c
*/
public xlvask_c $config;
/**
* The tasks helper for the xlvask module
* @var xlvask_helpers
*/
public xlvask_helpers $helpers;
public function __construct()
{
$this->config = new xlvask_c();
$this->helpers = new xlvask_helpers();
}
@@ -38,4 +47,13 @@ class xlvask extends xlvask_endpoints implements xlvask_i
throw new \Exception('xlvask module is not enabled');
}
}
/**
* Get the task helper for the xlvask module
* @return xlvask_tasks
*/
public function getTasks(): xlvask_tasks
{
return new $this->helpers->xlvask_tasks();
}
}
@@ -0,0 +1,39 @@
<?php
/**
* This script is used to run the XL Vask module's cron tasks.
* It is run as a cron job.
*
* index.php runs this script.
*/
// prevent direct access
use classes\xlvask;
if (!defined('WD')) {
exit;
}
$start = microtime(true);
// Load the XL Vask module
$xlvask = new xlvask;
try {
// Check if the module is enabled
if ($xlvask->config->enabled->isTrue()) {
// Check if synchronization is enabled
if ($xlvask->config->synchronization_enabled->isTrue()) {
$xlvask->getTasks()->runSyncUsers();
// TODO: Add synchronization for:
// - usageLogs
// - vehicles
} else {
// Synchronization is not enabled, do nothing
}
} else {
// The module is not enabled, do nothing
}
} catch (Exception $e) {
// This is automatically running, so we don't need to log the error
}
$end = microtime(true);
//$slack = new \classes\slack();
//$slack->send_message("The booking sync script has finished. It took " . round($end - $start, 2) . " seconds to run.");
@@ -0,0 +1,213 @@
<?php
namespace helpers;
use Exception;
class xlvask_customer
{
/**
* Example of a customer object:
* [customerId] => 39448c14-f2d8-4ab7-aa68-01388fa11ee3
* [name] => Personalebiler Truckwash
* [vendorId] => 9dde3a0a-197d-4172-8d0d-b4652d867593
* [customerTypeId] => COMPANY
* [discount] => 100
* [phone] => 21754690
* [email] => mikkel@truckwash.dk
* [address] => Amalienborg
* [address2] =>
* [zip] =>
* [city] =>
* [userId] => 1a0fd956-1e5f-431d-95f7-48aaea5a33a8
* [vatnumber] =>
* [excludeFromAutoInvoice] =>
* [country] => dk
* [createDate] => 2023-05-31T10:35:41.98
* [externId] =>
* [active] => 1
* [language] => da
* [note] =>
* [updated] => 2025-03-09T09:42:02.397
*/
/**
* The ID of the customer (XLVask ID)
* @var string $customerId
*/
public string $customerId;
/**
* The name of the customer
* @var string $name
*/
public string $name;
/**
* The vendor ID of the customer
* @var string $vendorId
*/
public string $vendorId;
/**
* The type of the customer (e.g., COMPANY)
* @var string $customerTypeId
*/
public string $customerTypeId;
/**
* The discount percentage for the customer
* @var int $discount
*/
public int $discount;
/**
* The phone number of the customer
* @var int|null $phone
*/
public int|null $phone;
/**
* The email address of the customer
* @var string|null $email
*/
public string|null $email;
/**
* The address of the customer
* @var string|null $address
*/
public string|null $address;
/**
* The second address line of the customer
* @var string|null $address2
*/
public string|null $address2;
/**
* The zip code of the customer
* @var int|null $zip
*/
public int|null $zip;
/**
* The city of the customer
* @var string|null $city
*/
public string|null $city;
/**
* The user ID of the customer
* @var string|null $userId
*/
public string|null $userId;
/**
* The VAT number of the customer
* @var int|null $vatnumber
*/
public int|null $vatnumber;
/**
* Whether the customer is excluded from auto-invoicing
* @var bool $excludeFromAutoInvoice
*/
public bool $excludeFromAutoInvoice;
/**
* The country code of the customer (e.g., 'dk' for Denmark)
* @var string $country
*/
public string $country;
/**
* The creation date of the customer (ISO 8601 format, e.g., '2023-05-31T10:35:41.98')
* @var string $createDate
*/
public string $createDate;
/**
* The external ID of the customer
* @var string|null $externId
*/
public string|null $externId;
/**
* Whether the customer is active
* @var bool $active
*/
public bool $active;
/**
* The language of the customer (e.g., 'da' for Danish)
* @var string $language
*/
public string $language;
/**
* A note associated with the customer
* @var string|null $note
*/
public string|null $note;
/**
* The last updated date of the customer (ISO 8601 format, e.g., '2025-03-09T09:42:02.397')
* @var string $updated
*/
public string $updated;
/**
* Constructor to initialize the customer object with default values
* @throws Exception
*/
public function __construct(array $data = [])
{
$this::reset();
$this::setProperties($data);
}
/**
* Reset this object to its default values.
* This method clears all properties of the customer object,
* effectively resetting it to a clean state.
* @return void
*/
private function reset(): void
{
$this->customerId = '';
$this->name = '';
$this->vendorId = '';
$this->customerTypeId = '';
$this->discount = 0;
$this->phone = null;
$this->email = null;
$this->address = null;
$this->address2 = null;
$this->zip = null;
$this->city = null;
$this->userId = null;
$this->vatnumber = null;
$this->excludeFromAutoInvoice = false;
$this->country = '';
$this->createDate = '';
$this->externId = null;
$this->active = true;
$this->language = '';
$this->note = null;
$this->updated = '';
}
/**
* Set the properties of the customer object based on the provided data.
* @note This method DOES NOT reset the object before setting the properties.
* @param array $data
* @return self
* @throws Exception
* @see reset()
*/
private static function setProperties(array $data): self
{
$self = new self();
foreach ( $data as $key => $value ) {
if (property_exists($self, $key)) {
$self->{$key} = $value;
} else {
// Throw an exception or handle the case where the property does not exist
throw new Exception("Property {$key} does not exist in xlvask_customer class.");
}
}
// Return the populated object
return $self;
}
/**
* Convert the customer object to an array representation.
* This method returns an associative array containing all properties of the customer object.
* @return array
*/
public function toArray(): array
{
return (array)$this;
}
}
@@ -0,0 +1,53 @@
<?php
namespace helpers;
use Exception;
class xlvask_tasks
{
public function __construct()
{
// Define the error messages if they are not already defined
/**
* if (!defined("ERROR_MESSAGE_ECONOMIC_INVOICE_NOT_FOUND")) {
* define("ERROR_MESSAGE_ECONOMIC_INVOICE_NOT_FOUND", 'ECONOMIC_INVOICE_NOT_FOUND');
* }
* if (!defined("ERROR_MESSAGE_ECONOMIC_INVOICE_DRAFT_NOT_FOUND")) {
* define("ERROR_MESSAGE_ECONOMIC_INVOICE_DRAFT_NOT_FOUND", 'ECONOMIC_INVOICE_DRAFT_NOT_FOUND');
* }
*/
}
/**
* Run the synchronize users task
* This task fetches the users from XL Vask, and synchronizes them with this system.
* This includes:
* - Fetching the users from XL Vask
* - Checking if the users exist in this system
* - Creating the users if they do not exist
* - Add/Remove the associated external id to the users in this system
* @return void
* @throws Exception If the task fails
*/
public function runSyncUsers(bool $isSilent = false): void
{
// Define the XL Vask object
$xlvask = new \classes\xlvask();
// Require the module to be enabled
$xlvask->requireModuleEnabled();
// Check if synchronization is enabled
if (!$xlvask->config->synchronization_enabled->isTrue()) {
if (!$isSilent) {
throw new Exception('XL Vask synchronization is not enabled.');
}
return; // Synchronization is not enabled, do nothing
}
// Get the users from XL Vask
$tmp_xlvask = [
'customers' => $xlvask->getCustomers()
];
//echo 'XL Vask users fetched: ' . count($tmp_xlvask['customers']) . PHP_EOL;
//print_r($tmp_xlvask['customers']);
}
}
@@ -0,0 +1,17 @@
<?php
namespace xlvask;
require_once WD . '/modules/xlvask/helpers/xlvask_tasks.php';
use helpers\xlvask_tasks;
class xlvask_helpers
{
/**
* The xlvask tasks helper
* @var string $xlvask_tasks
*/
public string $xlvask_tasks = xlvask_tasks::class;
}
@@ -151,5 +151,20 @@ class moduleXLVaskRoute
'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
$xlvask->getTasks()->runSyncUsers();
// Response
$response->success('Users synchronized successfully', 200);
},
[
'modules_xlvask_sync_users' => 'Synchronize users with the xlvask module'
]
);
}
}