From 2008b384731466cef0ad35cda4ea10d345fe13f5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 11 Apr 2019 14:40:36 +1000 Subject: [PATCH] Invoice Test Assertions --- app/Helpers/Invoice/InvoiceCalc.php | 7 ++- tests/Unit/InvoiceTest.php | 71 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceCalc.php b/app/Helpers/Invoice/InvoiceCalc.php index 90e427dc32..0825a07b44 100644 --- a/app/Helpers/Invoice/InvoiceCalc.php +++ b/app/Helpers/Invoice/InvoiceCalc.php @@ -33,6 +33,8 @@ class InvoiceCalc private $tax_map; + private $total_item_taxes; + private $total_taxes; private $total_discount; @@ -147,8 +149,9 @@ class InvoiceCalc if (! $this->settings->inclusive_taxes) { $taxAmount1 = round($this->total * ($this->invoice->tax_rate1 ? $this->invoice->tax_rate1 : 0) / 100, 2); $taxAmount2 = round($this->total * ($this->invoice->tax_rate2 ? $this->invoice->tax_rate2 : 0) / 100, 2); - $this->total = round($this->total + $taxAmount1 + $taxAmount2, 2); + $this->total_taxes = round($taxAmount1 + $taxAmount2, 2); $this->total += $this->total_taxes; + $this->total += $this->total_item_taxes; } return $this; @@ -176,7 +179,7 @@ class InvoiceCalc $this->tax_map->merge($item_calc->getGroupedTaxes()); //set running total of taxes - $this->total_taxes += $item_calc->getTotalTaxes(); + $this->total_item_taxes += $item_calc->getTotalTaxes(); //set running total of discounts $this->total_discount += $item_calc->getTotalDiscounts(); diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 5ddf3b9e1c..1634aebd24 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -83,4 +83,75 @@ class InvoiceTest extends TestCase $this->assertEquals($this->invoice_calc->getTotal(), 15); $this->assertEquals($this->invoice_calc->getBalance(), 15); } + + public function testInvoiceTotalsWithDiscountWithSurcharge() + { + $this->invoice->discount = 5; + $this->invoice->custom_value1 = 5; + + $this->invoice_calc->build(); + + $this->assertEquals($this->invoice_calc->getSubTotal(), 20); + $this->assertEquals($this->invoice_calc->getTotal(), 20); + $this->assertEquals($this->invoice_calc->getBalance(), 20); + } + + public function testInvoiceTotalsWithDiscountWithSurchargeWithInclusiveTax() + { + $this->invoice->discount = 5; + $this->invoice->custom_value1 = 5; + $this->invoice->tax_name1 = 'GST'; + $this->invoice->tax_rate1 = 10; + + + $this->invoice_calc->build(); + + $this->assertEquals($this->invoice_calc->getSubTotal(), 20); + $this->assertEquals($this->invoice_calc->getTotal(), 20); + $this->assertEquals($this->invoice_calc->getBalance(), 20); + } + + public function testInvoiceTotalsWithDiscountWithSurchargeWithExclusiveTax() + { + + $this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings); + + $this->invoice->discount = 5; + $this->invoice->custom_value1 = 5; + $this->invoice->tax_name1 = 'GST'; + $this->invoice->tax_rate1 = 10; + $this->settings->inclusive_taxes = false; + + $this->invoice_calc->build(); + + $this->assertEquals($this->invoice_calc->getSubTotal(), 20); + $this->assertEquals($this->invoice_calc->getTotal(), 22); + $this->assertEquals($this->invoice_calc->getBalance(), 22); + $this->assertEquals($this->invoice_calc->getTotalTaxes(), 2); + } + + public function testInvoiceTotalsWithDiscountWithSurchargeWithDoubleExclusiveTax() + { + + $this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings); + + $this->invoice->discount = 5; + $this->invoice->custom_value1 = 5; + $this->invoice->tax_name1 = 'GST'; + $this->invoice->tax_rate1 = 10; + $this->invoice->tax_name2 = 'GST'; + $this->invoice->tax_rate2 = 10; + $this->settings->inclusive_taxes = false; + + $this->invoice_calc->build(); + + $this->assertEquals($this->invoice_calc->getSubTotal(), 20); + $this->assertEquals($this->invoice_calc->getTotal(), 24); + $this->assertEquals($this->invoice_calc->getBalance(), 24); + $this->assertEquals($this->invoice_calc->getTotalTaxes(), 4); + } + + + + } \ No newline at end of file