diff --git a/services/nginx/app/classes/xlvask.php b/services/nginx/app/classes/xlvask.php index 5c2258d1..db72985c 100644 --- a/services/nginx/app/classes/xlvask.php +++ b/services/nginx/app/classes/xlvask.php @@ -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(); + } } \ No newline at end of file diff --git a/services/nginx/app/cron/RunXLVaskModuleCron.php b/services/nginx/app/cron/RunXLVaskModuleCron.php new file mode 100644 index 00000000..fa101820 --- /dev/null +++ b/services/nginx/app/cron/RunXLVaskModuleCron.php @@ -0,0 +1,39 @@ +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."); \ No newline at end of file diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_customer.php b/services/nginx/app/modules/xlvask/helpers/xlvask_customer.php new file mode 100644 index 00000000..ff2b8d77 --- /dev/null +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_customer.php @@ -0,0 +1,213 @@ + 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; + } +} \ No newline at end of file diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php b/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php new file mode 100644 index 00000000..878bb230 --- /dev/null +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_tasks.php @@ -0,0 +1,53 @@ +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']); + } +} \ No newline at end of file diff --git a/services/nginx/app/modules/xlvask/xlvask_helpers.php b/services/nginx/app/modules/xlvask/xlvask_helpers.php new file mode 100644 index 00000000..6f7d4d08 --- /dev/null +++ b/services/nginx/app/modules/xlvask/xlvask_helpers.php @@ -0,0 +1,17 @@ + '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' + ] + ); } } \ No newline at end of file