mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-01 21:00:58 +00:00
* test(taxes): cover compound_tax API semantics and validate it as boolean * feat(taxes): restore the compound tax toggle in tax type settings * feat(taxes): compute document-level compound tax amounts Two-pass recalculation: simple taxes on the discounted subtotal, compound taxes on the subtotal plus all simple taxes (v2 parity; branch order fixed -> compound -> inclusive back-out -> simple). Amounts now also recalculate on tax add/remove and on the tax-inclusive toggle, which previously never triggered recalculation. * feat(taxes): support compound taxes in per-item mode Per-item tax rows now carry the compound flag and charge compound taxes on the item's discounted base plus its simple taxes, through the same calcTaxAmount branch order as document-level taxes. The item row splits its tax sums into simple and compound so a compound row can never widen its own base. Also fixes two pre-existing bugs: the per-item tax dropdown read window.__taxTypes, which nothing ever assigned, so it always rendered empty (tax types are now fetched by the items table and passed down); and removing a tax row hard-zeroed the item's totals instead of re-syncing them. * fix(invoices): skip placeholder tax rows when persisting item taxes The document form keeps one empty placeholder tax row per item in per-item tax mode. Its empty name is nullified by the framework's empty-string middleware, so inserting it violated the NOT NULL constraint and turned every per-item save into a 500. The equivalent v2 guard keyed on a null amount, which the v3 stub (amount: 0) evades; keying on the missing tax_type_id catches it.
90 lines
2.8 KiB
PHP
90 lines
2.8 KiB
PHP
<?php
|
|
|
|
use App\Support\DocumentTotals;
|
|
|
|
test('sums item totals from price and quantity, ignoring client-supplied item totals', function () {
|
|
$totals = DocumentTotals::compute(
|
|
[['price' => 10000, 'quantity' => 10, 'total' => 1, 'taxes' => []]],
|
|
[], 0, 'NO', false, 'NO'
|
|
);
|
|
|
|
expect($totals['sub_total'])->toBe(100000)
|
|
->and($totals['total'])->toBe(100000)
|
|
->and($totals['tax'])->toBe(0);
|
|
});
|
|
|
|
test('applies document discount and document-level tax', function () {
|
|
$totals = DocumentTotals::compute(
|
|
[['price' => 1000, 'quantity' => 2]],
|
|
[['amount' => 150]],
|
|
500, 'NO', false, 'NO'
|
|
);
|
|
|
|
expect($totals['sub_total'])->toBe(2000)
|
|
->and($totals['tax'])->toBe(150)
|
|
->and($totals['total'])->toBe(1650);
|
|
});
|
|
|
|
test('uses per-item taxes (not document taxes) when tax_per_item is YES', function () {
|
|
$totals = DocumentTotals::compute(
|
|
[
|
|
['price' => 1000, 'quantity' => 1, 'taxes' => [['amount' => 100]]],
|
|
['price' => 2000, 'quantity' => 1, 'taxes' => [['amount' => 200]]],
|
|
],
|
|
[['amount' => 9999]],
|
|
0, 'YES', false, 'YES'
|
|
);
|
|
|
|
expect($totals['sub_total'])->toBe(3000)
|
|
->and($totals['tax'])->toBe(300)
|
|
->and($totals['total'])->toBe(3300);
|
|
});
|
|
|
|
test('excludes tax from total when tax_included', function () {
|
|
$totals = DocumentTotals::compute(
|
|
[['price' => 1000, 'quantity' => 1]],
|
|
[['amount' => 100]],
|
|
0, 'NO', true, 'NO'
|
|
);
|
|
|
|
expect($totals['total'])->toBe(1000)->and($totals['tax'])->toBe(100);
|
|
});
|
|
|
|
test('applies per-item discount only when discount_per_item is YES', function () {
|
|
$with = DocumentTotals::compute(
|
|
[['price' => 1000, 'quantity' => 2, 'discount_val' => 300]],
|
|
[], 0, 'NO', false, 'YES'
|
|
);
|
|
$without = DocumentTotals::compute(
|
|
[['price' => 1000, 'quantity' => 2, 'discount_val' => 300]],
|
|
[], 0, 'NO', false, 'NO'
|
|
);
|
|
|
|
expect($with['sub_total'])->toBe(1700)
|
|
->and($without['sub_total'])->toBe(2000);
|
|
});
|
|
|
|
test('supports negative quantities', function () {
|
|
$totals = DocumentTotals::compute(
|
|
[
|
|
['price' => 100, 'quantity' => -2],
|
|
['price' => 50, 'quantity' => 1],
|
|
['price' => 75, 'quantity' => 0],
|
|
],
|
|
[], 0, 'NO', false, 'NO'
|
|
);
|
|
|
|
expect($totals['sub_total'])->toBe(-150)->and($totals['total'])->toBe(-150);
|
|
});
|
|
|
|
test('sums a compound tax line just like any other document-level tax line', function () {
|
|
$totals = DocumentTotals::compute(
|
|
[['price' => 10000, 'quantity' => 1]],
|
|
[['amount' => 1900], ['amount' => 119, 'compound_tax' => true]],
|
|
0, 'NO', false, 'NO'
|
|
);
|
|
|
|
expect($totals['tax'])->toBe(2019)
|
|
->and($totals['total'])->toBe(12019);
|
|
});
|