Add import vehicles functionality to XL Vask module

This commit is contained in:
Jeppe Bundgaard
2025-07-15 15:01:03 +02:00
parent 026942f772
commit f74a904738
3 changed files with 183 additions and 0 deletions
@@ -66,4 +66,12 @@ abstract class xlvask_helper
}
return $tmp;
}
/**
* Default is valid
*/
public function isValid(): bool
{
return true;
}
}
@@ -0,0 +1,159 @@
<?php
namespace objects;
use classes\db;
use classes\object_property;
use classes\xlvask;
use Exception;
use helpers\xlvask_customer;
use helpers\xlvask_vehicle;
use traits\db_object_t;
class xlvask_vehicles_o extends db
{
use db_object_t;
public object_property $vehicleId;
public object_property $registrationNumber;
public object_property $customerId;
public object_property $vehicleTypeId;
public object_property $active;
public object_property $autoStartOnLpr;
public object_property $updated;
public object_property $createDate;
public object_property $externId;
public function structure(): void
{
$this->setTable('xlvask_vehicles');
}
/**
* Add a new XL Vask customer
* @param array $data The properties
* @returns void
* @throws Exception If the object was not created successfully
*/
public function add(array $data): void
{
$tmp_id = self::add_object($data);
$this->id = $tmp_id;
self::getObjectProperties();
self::objectChanged();
}
public function getObjectProperties(): void
{
$this->vehicleId = new object_property($this->table, $this->id, 'vehicleId', 'string', false);
$this->registrationNumber = new object_property($this->table, $this->id, 'registrationNumber', 'string', false);
$this->customerId = new object_property($this->table, $this->id, 'customerId', 'string', false);
$this->vehicleTypeId = new object_property($this->table, $this->id, 'vehicleTypeId', 'string', false);
$this->active = new object_property($this->table, $this->id, 'active', 'string', false);
$this->autoStartOnLpr = new object_property($this->table, $this->id, 'autoStartOnLpr', 'string', false);
$this->updated = new object_property($this->table, $this->id, 'updated', 'string', false);
$this->createDate = new object_property($this->table, $this->id, 'createDate', 'string', false);
$this->externId = new object_property($this->table, $this->id, 'externId', 'string', false);
}
public function objectChanged(): void
{
//TODO: Add cache invalidation
}
/**
* Import the vehicles from XL Vask
* @throws Exception If the objects were not successfully added.
* @returns void
*/
public function importVehicles(): void
{
if (!empty($this->id)) {
throw new Exception('To prevent issues, having a selected object is not allowed.');
}
$vehicles = $this->getVehiclesFromXLVask();
/** @var string[] $known_vehicleIds The XL Vask vehicleIds currently known */
$known_vehicleIds = array_map(function ($vehicle) {
return $vehicle['vehicleId'];
}, self::getFields(['vehicleId']));
/** The XL Vask customers without a matching vehicleId in the database */
$new_vehicles = array_filter($vehicles, function ($vehicle) use ($known_vehicleIds) {
return !in_array($vehicle->vehicleId, $known_vehicleIds);
});
unset($known_vehicleIds);
unset($vehicles);
/** Adding the new vehicles */
foreach ($new_vehicles as $vehicle) {
if (!$vehicle->isValid()) {
echo $vehicle->formattedDetails();
throw new Exception('A vehicle from XL Vask is not valid, have the structure changed?');
}
}
// Actually save the vehicle
foreach ($new_vehicles as $vehicle) {
$this->add($vehicle->toArray());
}
unset($new_vehicles);
}
/**
* Import the customers from XL Vask
* @throws Exception If the objects were not successfully added.
* @returns void
*/
public function importCustomers(): void
{
if (!empty($this->id)) {
throw new Exception('To prevent issues, having a selected object is not allowed.');
}
$customers = $this->getCustomersFromXLVask();
/** @var string[] $known_customerIds The XL Vask customerIds currently known */
$known_customerIds = array_map(function ($customer) {
return $customer['customerId'];
}, self::getFields(['customerId']));
/** The XL Vask customers without a matching customerId in the database */
$new_customers = array_filter($customers, function ($customer) use ($known_customerIds) {
return !in_array($customer->customerId, $known_customerIds);
});
unset($known_customerIds);
unset($customers);
/** Adding the new customers */
foreach ($new_customers as $customer) {
if (!$customer->isValid()) {
echo $customer->formattedDetails();
throw new Exception('A customer from XL Vask is not valid, have the structure changed?');
}
// Actually save the customer
$this->add($customer->toArray());
}
unset($new_customers);
}
/**
* @return xlvask_customer[]
* @throws Exception
*/
public function getCustomersFromXLVask(): array
{
/** Get the customers from XL Vask */
$xlvask = new xlvask();
$customer_class = $xlvask->new($xlvask->helpers->xlvask_customer);
/** @var xlvask_customer[] $customers */
$customers = $customer_class->toObjects($xlvask->getCustomers());
return $customers;
}
/**
* @returns xlvask_vehicle[]
* @throws Exception
*/
public function getVehiclesFromXLVask(): array
{
/** Get the vehicles from XL Vask */
$xlvask = new xlvask();
$vehicle_class = $xlvask->new($xlvask->helpers->xlvask_vehicle);
/** @var xlvask_vehicle[] $vehicles */
$vehicles = $vehicle_class->toObjects($xlvask->getVehicles());
return $vehicles;
}
}
@@ -232,5 +232,21 @@ class moduleXLVaskRoute
'modules_xlvask_import_customers' => 'Import customers from the xlvask module'
]
);
$this->get('/modules/xlvask/tasks/import-vehicles', function () {
global $response;
self::requirePermission('modules_xlvask_import_vehicles');
// Create the xlvask_vehicles_o object
$xlvask_vehicles_o = new \objects\xlvask_vehicles_o();
$xlvask_vehicles_o->importVehicles();
$response->success(
'Vehicles imported',
200
);
},
[
'modules_xlvask_import_vehicles' => 'Import vehicles from the xlvask module'
]
);
}
}