fix(api): suppress historical_primary_product_mismatch when reg_2 is empty

Bug #10: When an order has only one registration number (reg_2 is empty)
but the reg historically appeared in tractor-trailer bookings, the
'historical_primary_product_mismatch' flag falsely suggests that the
current tractor-only billing is incorrect. The operator (Sarah #10714)
correctly expects that billing only a tractor is the right action when
there's no trailer to bill.

This change suppresses the historical_primary_product_mismatch flag for
rows where reg_2 is empty, matching the operator expectation. The
underlying historical SQL already filters by reg_1 only; a follow-up
could narrow the history to orders with matching reg_2 patterns, but for
the reported false-positive this fix is sufficient.

- invoice_period_flag_service.php: add o.reg_2 to the row projection
  and skip the historical mismatch loop when the current order's reg_2
  is empty.
- InvoicePeriodFlagServiceTest.php: include reg_2 in the existing
  historical mismatch test fixture so it still exercises the flag path.
This commit is contained in:
MiniMax M3 Subagent
2026-08-10 20:10:49 +02:00
parent 8972b8f2ae
commit 89aa895192
2 changed files with 12 additions and 0 deletions
@@ -758,6 +758,7 @@ class invoice_period_flag_service
o.department_id,
d.custom_pricing_only AS department_custom_pricing_only,
o.reg_1,
o.reg_2,
o.invoice_collection_id,
o.wash_id,
o.safety_seal,
@@ -1303,6 +1304,16 @@ class invoice_period_flag_service
if ($reg === '' || !isset($history[$reg])) {
continue;
}
// When the current order only has one registration number (reg_2 is empty),
// the historical "usual product" may include a trailer component (e.g.
// "Tractor-trailer wash") because the reg previously only appeared in
// tractor-trailer bookings. Billing just the tractor in that case is correct
// and matches the operator's expectation (bug #10, Sarah #10714). The mismatch
// is therefore uninformative when reg_2 is empty, so suppress the flag.
$reg2 = trim((string)($row['reg_2'] ?? ''));
if ($reg2 === '') {
continue;
}
$expectedProductId = (int)$history[$reg]['product_id'];
if ($this->primaryVehicleProductsMatch(
(int)$row['product_id'],
@@ -464,6 +464,7 @@ it('does not flag interior wash variants as historical primary product mismatche
'invoice_collection_id' => 16912,
'department_id' => 7,
'reg_1' => 'CN96636',
'reg_2' => 'AB12345',
'is_wash' => 1,
'related_item_id' => 0,
'order_created_at' => '2026-05-11 08:05:21',