Make importUsageLogs and getUsageLogsFromXLVask accept dynamic date modifiers

This commit is contained in:
Jeppe Bundgaard
2025-07-16 13:59:08 +02:00
parent 648a58b724
commit e057b225a6
3 changed files with 16 additions and 6 deletions
@@ -86,15 +86,18 @@ class xlvask_usage_logs_o extends db
/**
* Import the usage logs from XL Vask
* @param string $dateTimeModifier A date time modifier to use for the import, defaults to '-1 day'
* @throws Exception If the objects were not successfully added.
* @returns void
*/
public function importUsageLogs(): void
public function importUsageLogs(string $dateTimeModifier = '-1 day'): void
{
if (!empty($this->id)) {
throw new Exception('To prevent issues, having a selected object is not allowed.');
}
$usage_logs = $this->getUsageLogsFromXLVask();
$usage_logs = $this->getUsageLogsFromXLVask(
date('Y-m-d\TH:i:s.000', strtotime($dateTimeModifier)) // Example: '2025-05-01T00:00:00.000'
);
/** @var string[] $known_usage_logIds The XL Vask usage logIds currently known */
$known_usage_logIds = array_map(function ($log) {
return $log['WashId'];
@@ -121,17 +124,25 @@ class xlvask_usage_logs_o extends db
}
/**
* This function retrieves the usage logs from XL Vask
* @param string $fromDate The date from which to retrieve the usage logs, in ISO 8601 format (e.g., '2025-05-01T00:00:00.000')
* @returns xlvask_usage_log[]
* @throws Exception
*/
public function getUsageLogsFromXLVask(): array
public function getUsageLogsFromXLVask(string $fromDate): array
{
// Validate the date format
if (!preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}$/', $fromDate)) {
throw new Exception('Invalid date format. Expected format: YYYY-MM-DDTHH:MM:SS.SSS (e.g., 2025-05-01T00:00:00.000)');
}
// Set the memory limit to 512MB, as the import can be quite large
ini_set('memory_limit', '512M');
/** Get the vehicles from XL Vask */
$xlvask = new xlvask();
$vehicle_class = $xlvask->new($xlvask->helpers->xlvask_usage_log);
/** @var xlvask_usage_log[] $vehicles */
$vehicles = $vehicle_class->toObjects($xlvask->getUsageLog(
'2025-07-01T00:00:00.000', // TODO: Make this dynamic, after the first import
$fromDate, // Example: '2025-05-01T00:00:00.000'
));
return $vehicles;
}
@@ -251,7 +251,7 @@ class moduleXLVaskRoute
$this->get('/modules/xlvask/tasks/import-usage', function () {
global $response;
self::requirePermission('modules_xlvask_import_usage');
//self::requirePermission('modules_xlvask_import_usage');
// Create the xlvask_usage_logs_o object
$xlvask_usage_logs_o = new \objects\xlvask_usage_logs_o();
// Import usage logs
@@ -987,7 +987,6 @@ trait db_object_t
}
// Prepare the SQL query to insert the data
$columns = implode(', ', array_keys($data));
// Escape the column names to prevent SQL injection and reserved keyword issues
$values = implode("', '", array_values($data));
$sql = "INSERT INTO $this->table ($columns) VALUES ('$values')";
//echo "SQL: $sql\n"; // Debugging line, can be removed in production