Compare commits

...
Author SHA1 Message Date
Jeppe B 0d2e906763 Guard wash subscription distribution SQL 2026-07-09 10:22:33 +02:00
2 changed files with 22 additions and 6 deletions
@@ -490,18 +490,19 @@ class customer_vehicles_o extends db
return [];
}
//print_r($transaction_ids);
// Convert the array of transaction ids to a comma separated string
$orders = '';
$orders = [];
foreach ($transaction_ids as $transaction_id) {
// Check if the transaction is included in the invoicing.
$tmp = (new orders_o())->select((int)$transaction_id);
if (!$tmp->isIncludedInInvoicing()) {
continue; // The transaction is not included in the invoicing, skip it
}
$orders .= (int)$transaction_id . ',';
$orders[] = (int)$transaction_id;
}
// Remove the last comma
$orders = rtrim($orders, ',');
if (empty($orders)) {
return [];
}
$order_ids = implode(',', $orders);
// Get the first two transactions that are not deleted and contains at least one order item with the 'product_id' of the vehicle type for the vehicle
$query = "
SELECT o.id
@@ -509,7 +510,7 @@ class customer_vehicles_o extends db
JOIN order_items oi ON oi.order_id = o.id
WHERE o.deleted_at IS NULL
AND oi.deleted_at IS NULL
AND o.id IN ($orders)
AND o.id IN ($order_ids)
AND oi.product_id = " . (int)$this->type->value() . "
GROUP BY o.id
ORDER BY o.created_at ASC
@@ -0,0 +1,15 @@
<?php
it('does not query subscription-applied transactions with an empty eligible order list', function (): void {
$content = file_get_contents(dirname(__DIR__, 3) . '/objects/customer_vehicles_o.php');
expect($content)->not->toBeFalse();
$content = (string)$content;
expect($content)
->toContain('$orders = [];')
->toMatch('/\$orders\[\] = \(int\)\$transaction_id;\s*}\s*if \(empty\(\$orders\)\) {\s*return \[\];\s*}\s*\$order_ids = implode/s')
->toContain('AND o.id IN ($order_ids)')
->not->toContain('rtrim($orders, \',\')')
->not->toContain('AND o.id IN ($orders)');
});