153 lines
6.0 KiB
PHP
153 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace helpers;
|
|
|
|
use Exception;
|
|
use objects\users_o;
|
|
|
|
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 null|array['customers'] The customers fetched from XL Vask, null if synchronization is not enabled
|
|
* @throws Exception If the task fails
|
|
*/
|
|
public function runSyncUsers(bool $isSilent = false): array|null
|
|
{
|
|
// 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 null; // Synchronization is not enabled, do nothing
|
|
}
|
|
// Get the users from XL Vask
|
|
$customer_objects = $this->fetchCustomers();
|
|
// Return the customers with an external ID
|
|
$customers = [
|
|
'with_external_id' => [...self::filterCustomersWithExternalId($customer_objects)],
|
|
'without_external_id' => [...self::filterCustomersWithoutExternalId($customer_objects)],
|
|
'all' => $customer_objects,
|
|
];
|
|
|
|
return self::synchronize_customers($customers['with_external_id']);
|
|
}
|
|
|
|
/**
|
|
* Get the customers registered in XL Vask
|
|
* This method fetches the customers from XL Vask and returns them as an array of customer objects.
|
|
* @return array[xlvask_customer] An array of customer objects fetched from XL Vask
|
|
* @throws Exception If the task fails
|
|
*/
|
|
public function fetchCustomers(): array
|
|
{
|
|
// Define the XL Vask object
|
|
$xlvask = new \classes\xlvask();
|
|
// Require the module to be enabled
|
|
$xlvask->requireModuleEnabled();
|
|
// Get the users from XL Vask
|
|
$customer_objects_raw = $xlvask->getCustomers();
|
|
// Parse the users into customer objects
|
|
return array_map(function ($customer) use ($xlvask) {
|
|
return (new $xlvask->helpers->xlvask_customer())->setProperties($customer);
|
|
}, $customer_objects_raw);
|
|
}
|
|
|
|
/**
|
|
* Get the customers with an external ID
|
|
* @param array $customers Customers to filter
|
|
* @return array The customers with an external ID
|
|
*/
|
|
public static function filterCustomersWithExternalId(array $customers): array
|
|
{
|
|
return array_filter($customers, function ($customer) {
|
|
/** @var xlvask_customer $customer */
|
|
return !empty($customer->externId);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the customers without an external ID
|
|
* @param array $customers Customers to filter
|
|
* @return array The customers without an external ID
|
|
*/
|
|
public static function filterCustomersWithoutExternalId(array $customers): array
|
|
{
|
|
return array_filter($customers, function ($customer) {
|
|
/** @var xlvask_customer $customer */
|
|
return empty($customer->externId);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Synchronize customers with external IDs
|
|
* This method synchronizes the customers with external IDs from XL Vask.
|
|
* It checks if the customers exist in this system, creates them if they do not exist,
|
|
* and adds/removes the associated external ID to the users in this system.
|
|
* @param array $with_external_id Customers with external IDs to synchronize
|
|
* @return array|null The synchronized customers, null if synchronization is not enabled
|
|
* @throws Exception
|
|
*/
|
|
private static function synchronize_customers(array $with_external_id): array|null
|
|
{
|
|
// 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()) {
|
|
return null; // Synchronization is not enabled, do nothing
|
|
}
|
|
|
|
// Get a list of customer numbers that we need to synchronize
|
|
$customer_numbers = array_map(function ($customer) {
|
|
/** @var xlvask_customer $customer */
|
|
return $customer->externId; // The customer number is stored in the externId property in XL Vask
|
|
}, $with_external_id);
|
|
|
|
// Get all the user ids associated with the customer numbers
|
|
$users = (new users_o())->getUsersByCustomerNumbers($customer_numbers, true);
|
|
|
|
$cache = $xlvask->getCache();
|
|
|
|
/** @var xlvask_customer $customer */
|
|
foreach ( $with_external_id as $customer ) {
|
|
// Check if the customer exists in this system
|
|
if (isset($users[$customer->externId])) {
|
|
// The user exists, update the user with the external ID
|
|
/** @var users_o $user */
|
|
$user = $users[$customer->externId];
|
|
// Cache the customer
|
|
$cache->setCustomerCache((int)$customer->externId, $customer);
|
|
} else {
|
|
//echo 'MISSING USER: ' . $customer->name . ' (' . $customer->externId . ')' . PHP_EOL;
|
|
}
|
|
}
|
|
return $with_external_id; // Return the synchronized customers
|
|
}
|
|
|
|
|
|
} |