From 82c1df5c6d5d1b212b471862a7fa99552fc459e4 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 11 Apr 2019 10:35:30 +1000 Subject: [PATCH] Fixes baseline errors in tests --- app/Helpers/Invoice/InvoiceCalc.php | 43 ++++++++++++++----------- app/Helpers/Invoice/InvoiceItemCalc.php | 9 +++--- tests/Unit/InvoiceTest.php | 9 ++++++ 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceCalc.php b/app/Helpers/Invoice/InvoiceCalc.php index 643078b61f30..f57eeff5960d 100644 --- a/app/Helpers/Invoice/InvoiceCalc.php +++ b/app/Helpers/Invoice/InvoiceCalc.php @@ -5,6 +5,7 @@ namespace App\Helpers\Invoice; use App\Helpers\Invoice\InvoiceItemCalc; use App\Models\Invoice; use App\Utils\Traits\NumberFormatter; +use Illuminate\Support\Collection; /** * Class for invoice calculations. @@ -43,10 +44,11 @@ class InvoiceCalc * @param \App\Models\Invoice $invoice The invoice * @param \stdClass $settings The settings */ - public function __construct(Invoice $invoice, \stdClass $settings) + public function __construct($invoice, \stdClass $settings) { $this->invoice = $invoice; $this->settings = $settings; + $this->tax_map = new Collection; } /** @@ -65,7 +67,7 @@ class InvoiceCalc private function calcPartial() { - if ( !$this->invoice->id && isset($this->invoice->partial) ) { + if ( !isset($this->invoice->id) && isset($this->invoice->partial) ) { $this->invoice->partial = max(0, min($this->formatValue($this->invoice->partial, 2), $this->invoice->balance)); } } @@ -117,21 +119,23 @@ class InvoiceCalc // custom fields charged taxes if ($this->invoice->custom_value1 && $this->settings->custom_taxes1) { - $this->total += $invoice->custom_value1; + $this->total += $this->invoice->custom_value1; } - if ($invoice->custom_value2 && $invoice->custom_taxes2) { - $this->total += $invoice->custom_value2; + if ($this->invoice->custom_value2 && $this->invoice->custom_taxes2) { + $this->total += $this->invoice->custom_value2; } $this->calcTaxes(); // custom fields not charged taxes - if ($invoice->custom_value1 && ! $this->settings->custom_taxes1) { - $this->total += $invoice->custom_value1; + if ($this->invoice->custom_value1 && ! $this->settings->custom_taxes1) { + $this->total += $this->invoice->custom_value1; } - if ($invoice->custom_value2 && ! $this->settings->custom_taxes2) { - $this->total += $invoice->custom_value2; + if ($this->invoice->custom_value2 && ! $this->settings->custom_taxes2) { + $this->total += $this->invoice->custom_value2; } + + return $this; } /** @@ -147,6 +151,7 @@ class InvoiceCalc $this->total += $this->total_taxes; } + return $this; } /** @@ -161,7 +166,7 @@ class InvoiceCalc foreach($this->invoice->line_items as $item) { - $item_calc = new InvoiceItemCalc($item); + $item_calc = new InvoiceItemCalc($item, $this->settings); $item_calc->process(); @@ -197,9 +202,9 @@ class InvoiceCalc * * @return float The sub total. */ - private function getSubTotal() + public function getSubTotal() { - return $this->subtotal; + return $this->sub_total; } /** @@ -209,7 +214,7 @@ class InvoiceCalc * * @return self $this */ - private function setSubTotal($value) + public function setSubTotal($value) { $this->sub_total = $value; @@ -221,7 +226,7 @@ class InvoiceCalc * * @return Collection The tax map. */ - private function getTaxMap() + public function getTaxMap() { return $this->tax_map; } @@ -233,7 +238,7 @@ class InvoiceCalc * * @return self $this */ - private function setTaxMap($value) + public function setTaxMap($value) { $htis->tax_map = $value; @@ -245,7 +250,7 @@ class InvoiceCalc * * @return float The total discount. */ - private function getTotalDiscount() + public function getTotalDiscount() { return $this->total_discount; } @@ -257,7 +262,7 @@ class InvoiceCalc * * @return self $this */ - private function setTotalDiscount($value) + public function setTotalDiscount($value) { $this->total_discount = $value; @@ -269,7 +274,7 @@ class InvoiceCalc * * @return float The total taxes. */ - private function getTotalTaxes() + public function getTotalTaxes() { return $this->total_taxes; } @@ -281,7 +286,7 @@ class InvoiceCalc * * @return self ( $this ) */ - private function setTotalTaxes($value) + public function setTotalTaxes($value) { $this->total_taxes = $value; diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index 61ed08612857..2954c0549855 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -4,6 +4,7 @@ namespace App\Helpers\Invoice; use App\Models\Invoice; use App\Utils\Traits\NumberFormatter; +use Illuminate\Support\Collection; class InvoiceItemCalc { @@ -29,7 +30,7 @@ class InvoiceItemCalc $this->settings = $settings; - $this->tax_collection = collect(); + $this->tax_collection = new Collection; } @@ -122,7 +123,7 @@ class InvoiceItemCalc * * */ - public function getLimeItem() + public function getLineItem() { return $this->item; @@ -155,12 +156,12 @@ class InvoiceItemCalc public function getTotalDiscounts() { - return $this->total_dicounts; + return $this->total_discounts; } public function setTotalDiscounts($total) { - $this->total_dicounts = $total; + $this->total_discounts = $total; return $this; } diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 2d3b24c97aa9..0cdb5a2d1aeb 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit; use App\Factory\InvoiceFactory; use App\Factory\InvoiceItemFactory; +use App\Helpers\Invoice\InvoiceCalc; use Tests\TestCase; /** @@ -39,6 +40,7 @@ class InvoiceTest extends TestCase $settings->custom_taxes1 = true; $settings->custom_taxes2 = true; $settings->inclusive_taxes = true; + $settings->precision = 2; return $settings; } @@ -62,4 +64,11 @@ class InvoiceTest extends TestCase return $line_items; } + + public function testInvoiceTotals() + { + $this->invoice_calc->build(); + + $this->assertEquals($this->invoice_calc->getSubTotal(), 20); + } } \ No newline at end of file