Merge pull request #57 from copenhagentruckwash/limble-module

limble-module
This commit is contained in:
Jeppe B
2025-06-17 13:40:22 +02:00
committed by GitHub
6 changed files with 155 additions and 0 deletions
@@ -3,6 +3,7 @@
namespace xlvask\classes;
require_once WD . '/modules/xlvask/interfaces/xlvask_request_i.php';
use classes\slack;
use xlvask\interfaces\xlvask_request_i;
class xlvask_request implements xlvask_request_i
@@ -43,6 +44,11 @@ class xlvask_request implements xlvask_request_i
curl_close($ch);
// Check if the response is successful
if ($httpCode < 200 || $httpCode >= 300) {
$slack = new slack();
$slack->send_message(
'XLVask API Request Failed',
"URL: $url\nMethod: $method\nData: " . json_encode($data) . "\nHeaders: " . implode(', ', $headers) . "\nHTTP Code: $httpCode\nResponse: $response"
);
throw new \Exception('Request failed with status code ' . $httpCode);
}
// Check if the response is valid JSON
@@ -0,0 +1,84 @@
<?php
namespace helpers;
use classes\xlvask;
use Exception;
use objects\users_o;
class xlvask_create_customer
{
/**
* Creates a customer in the XLVask system based on the provided user object.
*
* @param users_o $user The user object containing customer information.
* @return xlvask_customer The created XLVask customer object.
* @throws Exception If the user does not have a customer number or if the customer already exists.
*/
public static function createCustomer(users_o $user): xlvask_customer
{
// Validate user object
$user->requireSelected();
if (empty((int)$user->customer_number->value())) {
throw new Exception('User does not have a customer number');
}
// Get the XL Vask object
$xlvask = new xlvask();
$xlvask->requireModuleEnabled();
// Check if the customer already exists
if ($user->hasXLVaskCustomerAccount()) {
throw new Exception('User already has an XLVask customer account');
}
// Generate a unique customer GUID
$customer_id = self::generateCustomerId();
// TODO: FINISH THE CUSTOMER CREATION, ONCE THE XLVASK API IS READY
throw new Exception('Customer creation is not implemented yet, delayed until the XLVask API is ready');
// Create the customer
$tmp_result = $xlvask->sendRequest($xlvask->config->api_url . '/Customers', 'POST', [
'customerId' => $customer_id,
'name' => $user->getCustomerName((int)$user->customer_number->value()),
'vendorId' => $xlvask->config->vendor_id,
'customerTypeId' => null,
'discount' => 1,
'phone' => null,
'email' => null,
'address' => null,
'address2' => null,
'zip' => null,
'city' => null,
'userId' => null,
'vatnumber' => null,
'excludeFromAutoInvoice' => true,
'country' => null,
'createDate' => null,
'active' => true,
'language' => null,
'note' => null,
'updated' => null,
'externId' => (string)$user->customer_number->value()
], [
$xlvask->getAuthHeader()
]);
print_r($tmp_result);
}
/**
* Generates a unique customer ID for the XLVask system.
*
* @return string The generated customer ID. (UUID format)
*/
private static function generateCustomerId(): string
{
// Generate a UUID for the customer ID
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}
@@ -41,6 +41,11 @@ class xlvask_c
* @var xlvask_password_c
*/
public xlvask_password_c $password;
/**
* The vendor ID for the xlvask module (GUID)
* @var string $vendor_id
*/
public string $vendor_id = 'c92a88b5-7aae-4d41-b284-f91b9b341c46';
public function __construct()
{
@@ -2,11 +2,14 @@
namespace xlvask;
// Helpers
require_once WD . '/modules/xlvask/helpers/xlvask_tasks.php';
require_once WD . '/modules/xlvask/helpers/xlvask_cache.php';
require_once WD . '/modules/xlvask/helpers/xlvask_customer.php';
require_once WD . '/modules/xlvask/helpers/xlvask_usage_log.php';
require_once WD . '/modules/xlvask/helpers/xlvask_wash_item.php';
require_once WD . '/modules/xlvask/helpers/xlvask_create_customer.php';
// Parsers
require_once WD . '/modules/xlvask/helpers/xlvask_product_parser_t.php';
require_once WD . '/modules/xlvask/helpers/xlvask_product_parser.php';
require_once WD . '/modules/xlvask/helpers/xlvask_parser_stor_bil.php';
@@ -33,6 +36,7 @@ require_once WD . '/modules/xlvask/helpers/xlvask_parser_special_s_be_front.php'
require_once WD . '/modules/xlvask/helpers/xlvask_parser_lille_bil.php';
use helpers\xlvask_cache;
use helpers\xlvask_create_customer;
use helpers\xlvask_customer;
use helpers\xlvask_tasks;
use helpers\xlvask_usage_log;
@@ -65,4 +69,9 @@ class xlvask_helpers
* @var string $xlvask_wash_item
*/
public string $xlvask_wash_item = xlvask_wash_item::class;
/**
* The xlvask create customers' helper
* @var string $xlvask_create_customer
*/
public string $xlvask_create_customer = xlvask_create_customer::class;
}
+29
View File
@@ -6,6 +6,7 @@ use classes\db;
use classes\language_packs;
use classes\object_property;
use classes\response;
use classes\xlvask;
use customers\economic_customer_mo;
use customers\economicCustomers;
use Exception;
@@ -31,6 +32,7 @@ class users_o extends db
public user_key_value_pairs_o $keys;
public user_price_overrides_o $price_overrides;
public language_pack_en_us $language_pack;
public object_property $xlvask_customer_id;
protected object_property $password;
protected object_property $phone_country_code;
protected object_property $phone;
@@ -87,6 +89,7 @@ class users_o extends db
$this->phone_country_code = new object_property($this->table, $this->id, 'phone_country_code', 'int', false);
$this->phone = new object_property($this->table, $this->id, 'phone', 'int', false);
$this->email = new object_property($this->table, $this->id, 'email', 'string', false);
$this->xlvask_customer_id = new object_property($this->table, $this->id, 'xlvask_customer_id', 'string', false);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'string', false);
$this->keys = (new user_key_value_pairs_o())->setUser($this->id);
@@ -1203,4 +1206,30 @@ class users_o extends db
return $users;
}
/**
* Check if the user has an XLVask customer account
* @return bool True if the user has an XLVask customer account, false otherwise
* @throws Exception if the user is not selected
*/
public function hasXLVaskCustomerAccount(): bool
{
self::requireSelected();
$customer_number = $this->customer_number->value();
if (empty($customer_number)) {
// If the customer number is not set, the user cannot have an XLVask customer account
return false;
}
if ((new xlvask())->getCache()->isCustomerCached((int)$this->customer_number->value())) {
// If the customer is cached, it means the user has an XLVask customer account
return true;
}
// Check if the user exists in the XLVask system
if (count((new xlvask())->getCustomers(null, null, $customer_number)) > 0) {
// If the user exists in the XLVask system, it means the user has an XLVask customer account
return true;
}
// Check if the user has an XLVask customer account associated internally
return !empty($this->xlvask_customer_id->value());
}
}
@@ -7,6 +7,7 @@ use classes\response;
use classes\router;
use classes\xlvask;
use objects\orders_o;
use objects\users_o;
use traits\route_t;
class moduleXLVaskRoute
@@ -187,5 +188,26 @@ class moduleXLVaskRoute
'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);
echo 'Creating customer in XLVask for user: ' . $user->customer_number->value() . " (" . $user->getCustomerName((int)$user->customer_number->value()) . ")\n";
$new_xlvask_customer = new $xlvask->helpers->xlvask_create_customer();
$result = $new_xlvask_customer->createCustomer($user);
// Response
$response->success(
$result,
200
);
},
[
'modules_xlvask_sync_usage' => 'Synchronize usage with the xlvask module'
]
);
}
}