Files
api/services/nginx/app/modules/xlvask/helpers/xlvask_create_customer.php
T

84 lines
2.9 KiB
PHP

<?php
namespace helpers;
use classes\xlvask;
use Exception;
use objects\users_o;
class xlvask_create_customer extends xlvask_helper
{
/**
* 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)
);
}
}