Guard wash subscription distribution SQL
Guard the wash subscription distribution query after invoice-inclusion filtering removes all candidate orders, preventing an empty IN () clause on the invoicing distribution endpoint. Verified with focused syntax, Pest, PHPStan, and invoicing unit-suite checks.
This commit is contained in:
@@ -490,18 +490,19 @@ class customer_vehicles_o extends db
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
//print_r($transaction_ids);
|
//print_r($transaction_ids);
|
||||||
// Convert the array of transaction ids to a comma separated string
|
$orders = [];
|
||||||
$orders = '';
|
|
||||||
foreach ($transaction_ids as $transaction_id) {
|
foreach ($transaction_ids as $transaction_id) {
|
||||||
// Check if the transaction is included in the invoicing.
|
// Check if the transaction is included in the invoicing.
|
||||||
$tmp = (new orders_o())->select((int)$transaction_id);
|
$tmp = (new orders_o())->select((int)$transaction_id);
|
||||||
if (!$tmp->isIncludedInInvoicing()) {
|
if (!$tmp->isIncludedInInvoicing()) {
|
||||||
continue; // The transaction is not included in the invoicing, skip it
|
continue; // The transaction is not included in the invoicing, skip it
|
||||||
}
|
}
|
||||||
$orders .= (int)$transaction_id . ',';
|
$orders[] = (int)$transaction_id;
|
||||||
}
|
}
|
||||||
// Remove the last comma
|
if (empty($orders)) {
|
||||||
$orders = rtrim($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
|
// 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 = "
|
$query = "
|
||||||
SELECT o.id
|
SELECT o.id
|
||||||
@@ -509,7 +510,7 @@ class customer_vehicles_o extends db
|
|||||||
JOIN order_items oi ON oi.order_id = o.id
|
JOIN order_items oi ON oi.order_id = o.id
|
||||||
WHERE o.deleted_at IS NULL
|
WHERE o.deleted_at IS NULL
|
||||||
AND oi.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() . "
|
AND oi.product_id = " . (int)$this->type->value() . "
|
||||||
GROUP BY o.id
|
GROUP BY o.id
|
||||||
ORDER BY o.created_at ASC
|
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)');
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user