From 1f1ce3431daae0998e2ad686fe3a3f0d78c9a87f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 28 Oct 2023 13:57:16 +1100 Subject: [PATCH] Fixes for calculations --- app/Helpers/Invoice/InvoiceItemSum.php | 7 +- tests/Unit/InvoiceItemTest.php | 154 ++++++++++++++++++++++++- tests/Unit/InvoiceTest.php | 2 + 3 files changed, 156 insertions(+), 7 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 50bb29fd08..4f95d5c7a2 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -399,11 +399,8 @@ class InvoiceItemSum $item_tax = 0; - // $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)); - if($this->invoice->discount > 0) - $amount = ($this->sub_total > 0) ? $this->item->line_total - ($this->invoice->discount * ($this->item->line_total / $this->sub_total)) : 0; - else - $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)); + $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount / $this->sub_total)); + //$amount = ($this->sub_total > 0) ? $this->item->line_total - ($this->invoice->discount * ($this->item->line_total / $this->sub_total)) : 0; $item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount); diff --git a/tests/Unit/InvoiceItemTest.php b/tests/Unit/InvoiceItemTest.php index 802c825531..52fa2fbb36 100644 --- a/tests/Unit/InvoiceItemTest.php +++ b/tests/Unit/InvoiceItemTest.php @@ -11,11 +11,13 @@ namespace Tests\Unit; +use Tests\TestCase; +use Tests\MockAccountData; +use App\DataMapper\InvoiceItem; +use App\Factory\InvoiceFactory; use App\Factory\InvoiceItemFactory; use App\Helpers\Invoice\InvoiceItemSum; use Illuminate\Foundation\Testing\DatabaseTransactions; -use Tests\MockAccountData; -use Tests\TestCase; /** * @test @@ -33,6 +35,154 @@ class InvoiceItemTest extends TestCase $this->makeTestData(); } + public function testDicountsWithTaxes() + { + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = false; + $invoice->is_amount_discount =true; + $invoice->discount = 10; + + $line_items = []; + + $line_item = new InvoiceItem; + $line_item->quantity = 1; + $line_item->cost = 100; + $line_item->tax_rate1 = 10; + $line_item->tax_name1 = 'GST'; + $line_item->product_key = 'Test'; + $line_item->notes = 'Test'; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $invoice = $invoice->calc()->getInvoice(); + + $this->assertEquals(99, $invoice->amount); + $this->assertEquals(9, $invoice->total_taxes); + } + + + public function testDicountsWithTaxesNegativeInvoice() + { + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = false; + $invoice->is_amount_discount =true; + $invoice->discount = -10; + + $line_items = []; + + $line_item = new InvoiceItem; + $line_item->quantity = -1; + $line_item->cost = 100; + $line_item->tax_rate1 = 10; + $line_item->tax_name1 = 'GST'; + $line_item->product_key = 'Test'; + $line_item->notes = 'Test'; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $invoice = $invoice->calc()->getInvoice(); + + $this->assertEquals(-99, $invoice->amount); + $this->assertEquals(-9, $invoice->total_taxes); + } + +public function testDicountsWithTaxesPercentage() + { + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = false; + $invoice->is_amount_discount =false; + $invoice->discount = 10; + + $line_items = []; + + $line_item = new InvoiceItem; + $line_item->quantity = 1; + $line_item->cost = 100; + $line_item->tax_rate1 = 10; + $line_item->tax_name1 = 'GST'; + $line_item->product_key = 'Test'; + $line_item->notes = 'Test'; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $invoice = $invoice->calc()->getInvoice(); + + $this->assertEquals(99, $invoice->amount); + $this->assertEquals(9, $invoice->total_taxes); + } + + + public function testDicountsWithTaxesNegativeInvoicePercentage() + { + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = false; + $invoice->is_amount_discount =false; + $invoice->discount = -10; + + $line_items = []; + + $line_item = new InvoiceItem; + $line_item->quantity = -1; + $line_item->cost = 100; + $line_item->tax_rate1 = 10; + $line_item->tax_name1 = 'GST'; + $line_item->product_key = 'Test'; + $line_item->notes = 'Test'; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $invoice = $invoice->calc()->getInvoice(); + + $this->assertEquals(-121, $invoice->amount); + $this->assertEquals(-10, $invoice->discount); + $this->assertEquals(-11, $invoice->total_taxes); + } + + + + public function testDicountPercentageWithTaxes() + { + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = false; + $invoice->is_amount_discount =true; + $invoice->discount = 10; + + $line_items = []; + + $line_item = new InvoiceItem; + $line_item->quantity = 1; + $line_item->cost = 100; + $line_item->tax_rate1 = 10; + $line_item->tax_name1 = 'GST'; + $line_item->product_key = 'Test'; + $line_item->notes = 'Test'; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $invoice = $invoice->calc()->getInvoice(); + + $this->assertEquals(99, $invoice->amount); + $this->assertEquals(9, $invoice->total_taxes); + } + + + + public function testInvoiceItemTotalSimple() { $item = InvoiceItemFactory::create(); diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index cb6f15da13..5becdabb80 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -49,6 +49,8 @@ class InvoiceTest extends TestCase $this->invoice_calc = new InvoiceSum($this->invoice); } + + public function testMarkPaidWithPartial() { $item = InvoiceItemFactory::create();