## Summary
Fixes **TRU-73 / DRIFT 12** — invoice format must clearly show the
discount given on all services.
For customers with a global e-conomic discount (e.g. `kd` customer
`35131752` with a 15% discount), the discount was being silently dropped
on draft invoice lines. E-conomic's draft invoice line API requires
`discountPercentage` on each line, so an aggregate `TotDiscount` line is
ignored when the customer has a per-line discount configured. The fix
applies the customer discount at the line level.
## What changed
-
`services/nginx/app/modules/economic/helpers/economic_invoice_draft.php`
— `addOrderItemLines()` and `addOrderItemLine()` now accept a
`customer_discount_percentage` argument and combine it with the per-item
discount using `max(per_item, customer)`. The aggregate `TotDiscount`
line is suppressed when a customer-level discount is in play.
- `services/nginx/app/modules/economic/customers/economicCustomers.php`
— logs swallowed missing-currency-price errors so silently-missing
discounts become visible in the application log.
-
`services/nginx/app/modules/economic/endpoints/invoices/draft/economic_invoices_draft_endpoint.php`
— forwards the customer discount percentage to the draft builder.
- `services/nginx/app/objects/collected_order_invoices_o.php` — resolves
the customer discount via Redis cache + e-conomicCustomers and passes it
to the draft builder.
-
`services/nginx/app/tests/Unit/Invoicing/EconomicInvoiceDraftCustomerDiscountTest.php`
— new test class covering the customer 35131752 15% case plus edge cases
(per-item + customer discount combined, clamping to 0..100,
zero-discount baseline).
-
`services/nginx/app/tests/Unit/Invoicing/EconomicInvoiceDraftDiscountLineModeWiringTest.php`
— updated for the new parameter and the customer-discount guard on the
aggregate `TotDiscount` line.
-
`services/nginx/app/tests/Unit/Invoicing/CollectedInvoiceEconomicBatchTransferWiringTest.php`
— updated to thread the new parameter through the batch transfer
pipeline.
- `documentation/economic/invoice-discount-format-drift12.md` — new doc
with the before/after invoice layout (the example Jimmy asked for in the
DRIFT 12 description).
## Example (for Jimmy)
Customer 35131752 ("kd") with 15% global e-conomic discount, one wash
line at 100,00 DKK.
### Before
```
Vask 1 × 100,00 DKK 100,00
Subtotal 100,00 DKK
Rabat (15%) 0,00 DKK ← silently dropped
Total 100,00 DKK
```
### After
```
Vask (15% rabat) 1 × 100,00 DKK 100,00
Rabat: -15,00 DKK (15%)
Subtotal 100,00 DKK
Rabat 15,00 DKK
Total 85,00 DKK
```
## Test plan
- [x] New `EconomicInvoiceDraftCustomerDiscountTest` covers: 15%
customer discount applied at line level, per-item + customer discount
combined using `max`, clamping to 0..100, zero-discount baseline.
- [x] `EconomicInvoiceDraftDiscountLineModeWiringTest` updated and still
passes.
- [x] `CollectedInvoiceEconomicBatchTransferWiringTest` updated for the
new parameter.
- [ ] Run full `php-ci-test.sh unit` locally to confirm nothing else
regressed.
## Linear
Closes TRU-73 (DRIFT 12).
🤖 Generated via the TRU-73 pickup cron run.
---------
Co-authored-by: MiniMax M3 Subagent <fix@truckwash.local>
183 lines
5.7 KiB
PHP
183 lines
5.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Tests for the e-conomic customer-level discount being applied at the line level.
|
|
*
|
|
* Regression coverage for bug #11 — E-conomic 15% discount not applied on
|
|
* customer 35131752 ("kd"). The customer has a 15% global discount configured in
|
|
* e-conomic, but the invoice was being sent without any discount on the line items.
|
|
*
|
|
* The fix threads the customer discount percentage through the draft builder so it
|
|
* is applied at the line level via the `discountPercentage` field that e-conomic
|
|
* expects on each line.
|
|
*/
|
|
|
|
app_require('modules/economic/helpers/economic_invoice_draft.php');
|
|
|
|
use helpers\economic_invoice_draft;
|
|
|
|
if (!class_exists('EconomicInvoiceDraftCustomerDiscountProbe')) {
|
|
class EconomicInvoiceDraftCustomerDiscountProbe extends economic_invoice_draft
|
|
{
|
|
public array $sentBatches = [];
|
|
|
|
public function __construct()
|
|
{
|
|
$this->draft_invoice_number = 35131752;
|
|
$this->currency = 'DKK';
|
|
$this->conversion_rate = 1.0;
|
|
$this->draft_invoice_data = (object)['draftInvoiceNumber' => 35131752];
|
|
}
|
|
|
|
protected function sendDraftLines(array $draft_lines): object
|
|
{
|
|
$this->sentBatches[] = $draft_lines;
|
|
return (object)['lines' => $draft_lines];
|
|
}
|
|
}
|
|
}
|
|
|
|
function economic_customer_discount_order_item(float $price, float $product_price, array $overrides = []): array
|
|
{
|
|
return array_replace_recursive([
|
|
'id' => 9001,
|
|
'quantity' => 1,
|
|
'price' => $price,
|
|
'reference' => '',
|
|
'notes' => '',
|
|
'include_in_invoice' => true,
|
|
'product' => [
|
|
'economic_product_id' => '5',
|
|
'name' => 'Wash',
|
|
'price' => $product_price,
|
|
],
|
|
], $overrides);
|
|
}
|
|
|
|
it('applies the 15% customer discount to a line item for customer 35131752 "kd"', function (): void {
|
|
$draft = new EconomicInvoiceDraftCustomerDiscountProbe();
|
|
|
|
// Order item is at full price (no per-item discount) — exactly the customer 35131752
|
|
// case where the 15% global e-conomic discount was silently dropped.
|
|
$draft->addOrderItemLine(
|
|
economic_customer_discount_order_item(100.0, 100.0),
|
|
[
|
|
'economic_department_id' => 75,
|
|
'economic_dimension_id' => 1,
|
|
],
|
|
false,
|
|
false,
|
|
15
|
|
);
|
|
$draft->flushLinesInBatches();
|
|
|
|
$line = $draft->sentBatches[0][0];
|
|
expect($line['product']['productNumber'])->toBe('5')
|
|
->and($line['description'])->toBe('Wash')
|
|
->and($line['quantity'])->toBe(1.0)
|
|
->and($line['unitNetPrice'])->toBe(100.0)
|
|
->and($line['discountPercentage'])->toBe(15.0);
|
|
});
|
|
|
|
it('uses the larger discount when both per-item and customer discounts are present', function (): void {
|
|
$draft = new EconomicInvoiceDraftCustomerDiscountProbe();
|
|
|
|
// Per-item discount = 10%, customer discount = 15% → max(15, 10) = 15.
|
|
$draft->addOrderItemLine(
|
|
economic_customer_discount_order_item(90.0, 100.0),
|
|
[
|
|
'economic_department_id' => 75,
|
|
'economic_dimension_id' => 1,
|
|
],
|
|
false,
|
|
true,
|
|
15
|
|
);
|
|
$draft->flushLinesInBatches();
|
|
|
|
$line = $draft->sentBatches[0][0];
|
|
expect($line['unitNetPrice'])->toBe(100.0)
|
|
->and($line['discountPercentage'])->toBe(15.0);
|
|
});
|
|
|
|
it('uses the per-item discount when it is larger than the customer discount', function (): void {
|
|
$draft = new EconomicInvoiceDraftCustomerDiscountProbe();
|
|
|
|
// Per-item discount = 25%, customer discount = 15% → max(25, 15) = 25.
|
|
$draft->addOrderItemLine(
|
|
economic_customer_discount_order_item(75.0, 100.0),
|
|
[
|
|
'economic_department_id' => 75,
|
|
'economic_dimension_id' => 1,
|
|
],
|
|
false,
|
|
true,
|
|
15
|
|
);
|
|
$draft->flushLinesInBatches();
|
|
|
|
$line = $draft->sentBatches[0][0];
|
|
expect($line['unitNetPrice'])->toBe(100.0)
|
|
->and($line['discountPercentage'])->toBe(25.0);
|
|
});
|
|
|
|
it('clamps the customer discount percentage to the 0..100 range', function (): void {
|
|
$draft = new EconomicInvoiceDraftCustomerDiscountProbe();
|
|
|
|
$draft->addOrderItemLine(
|
|
economic_customer_discount_order_item(100.0, 100.0),
|
|
[
|
|
'economic_department_id' => 75,
|
|
'economic_dimension_id' => 1,
|
|
],
|
|
false,
|
|
false,
|
|
150
|
|
);
|
|
$draft->flushLinesInBatches();
|
|
|
|
$line = $draft->sentBatches[0][0];
|
|
expect($line['discountPercentage'])->toBe(100.0);
|
|
});
|
|
|
|
it('emits no line discount when both per-item and customer discounts are zero', function (): void {
|
|
$draft = new EconomicInvoiceDraftCustomerDiscountProbe();
|
|
|
|
$draft->addOrderItemLine(
|
|
economic_customer_discount_order_item(100.0, 100.0),
|
|
[
|
|
'economic_department_id' => 75,
|
|
'economic_dimension_id' => 1,
|
|
],
|
|
false,
|
|
false,
|
|
0
|
|
);
|
|
$draft->flushLinesInBatches();
|
|
|
|
$line = $draft->sentBatches[0][0];
|
|
expect($line['unitNetPrice'])->toBe(100.0)
|
|
->and($line['discountPercentage'])->toBe(0.0);
|
|
});
|
|
|
|
it('keeps base behavior unchanged when the customer discount is zero', function (): void {
|
|
$draft = new EconomicInvoiceDraftCustomerDiscountProbe();
|
|
|
|
// No customer discount, no per-item discount — unit price should be the final price.
|
|
$draft->addOrderItemLine(
|
|
economic_customer_discount_order_item(100.0, 100.0),
|
|
[
|
|
'economic_department_id' => 75,
|
|
'economic_dimension_id' => 1,
|
|
],
|
|
false,
|
|
false,
|
|
0
|
|
);
|
|
$draft->flushLinesInBatches();
|
|
|
|
$line = $draft->sentBatches[0][0];
|
|
expect($line['unitNetPrice'])->toBe(100.0)
|
|
->and($line['discountPercentage'])->toBe(0.0);
|
|
});
|