From fe7356cc89768883e9041a5517cf4faf3482486b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sun, 28 Apr 2024 10:56:06 +1000 Subject: [PATCH] Fixes for surcharge tax calculations --- VERSION.txt | 2 +- app/Helpers/Invoice/CustomValuer.php | 2 +- app/Helpers/Invoice/InvoiceSumInclusive.php | 29 +++-- config/ninja.php | 4 +- tests/Unit/InvoiceTest.php | 121 ++++++++++++++++++++ 5 files changed, 139 insertions(+), 19 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index 7a2658070e..b96e850061 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -5.8.51 \ No newline at end of file +5.8.52 \ No newline at end of file diff --git a/app/Helpers/Invoice/CustomValuer.php b/app/Helpers/Invoice/CustomValuer.php index 20f01f3b31..7ef81f074f 100644 --- a/app/Helpers/Invoice/CustomValuer.php +++ b/app/Helpers/Invoice/CustomValuer.php @@ -35,7 +35,7 @@ trait CustomValuer return 0; } - public function multiInclusiveTax($value, $has_custom_invoice_taxes) { + public function multiInclusiveTax($custom_value, $has_custom_invoice_taxes) { if (isset($custom_value) && is_numeric($custom_value) && $has_custom_invoice_taxes !== false) { diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index de47a59147..93990217a2 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -74,8 +74,8 @@ class InvoiceSumInclusive { $this->calculateLineItems() ->calculateDiscount() - ->calculateCustomValues() ->calculateInvoiceTaxes() + ->calculateCustomValues() ->setTaxMap() ->calculateTotals() //just don't add the taxes!! ->calculateBalance() @@ -119,7 +119,6 @@ class InvoiceSumInclusive $this->total_taxes += $this->multiInclusiveTax($this->invoice->custom_surcharge4, $this->invoice->custom_surcharge_tax4); $this->total_custom_values += $this->valuer($this->invoice->custom_surcharge4); - $this->total += $this->total_custom_values; return $this; @@ -138,21 +137,21 @@ class InvoiceSumInclusive } //Handles cases where the surcharge is not taxed - if(is_numeric($this->invoice->custom_surcharge1) && $this->invoice->custom_surcharge1 > 0 && $this->invoice->custom_surcharge_tax1) { - $amount += $this->invoice->custom_surcharge1; - } + // if(is_numeric($this->invoice->custom_surcharge1) && $this->invoice->custom_surcharge1 > 0 && !$this->invoice->custom_surcharge_tax1) { + // $amount += $this->invoice->custom_surcharge1; + // } - if(is_numeric($this->invoice->custom_surcharge2) && $this->invoice->custom_surcharge2 > 0 && $this->invoice->custom_surcharge_tax2) { - $amount += $this->invoice->custom_surcharge2; - } + // if(is_numeric($this->invoice->custom_surcharge2) && $this->invoice->custom_surcharge2 > 0 && !$this->invoice->custom_surcharge_tax2) { + // $amount += $this->invoice->custom_surcharge2; + // } - if(is_numeric($this->invoice->custom_surcharge3) && $this->invoice->custom_surcharge3 > 0 && $this->invoice->custom_surcharge_tax3) { - $amount += $this->invoice->custom_surcharge3; - } + // if(is_numeric($this->invoice->custom_surcharge3) && $this->invoice->custom_surcharge3 > 0 && !$this->invoice->custom_surcharge_tax3) { + // $amount += $this->invoice->custom_surcharge3; + // } - if(is_numeric($this->invoice->custom_surcharge4) && $this->invoice->custom_surcharge4 > 0 && $this->invoice->custom_surcharge_tax4) { - $amount += $this->invoice->custom_surcharge4; - } + // if(is_numeric($this->invoice->custom_surcharge4) && $this->invoice->custom_surcharge4 > 0 && !$this->invoice->custom_surcharge_tax4) { + // $amount += $this->invoice->custom_surcharge4; + // } if (is_string($this->invoice->tax_name1) && strlen($this->invoice->tax_name1) > 1) { $tax = $this->calcInclusiveLineTax($this->invoice->tax_rate1, $amount); @@ -172,7 +171,7 @@ class InvoiceSumInclusive $this->total_taxes += $tax; $this->total_tax_map[] = ['name' => $this->invoice->tax_name3.' '.floatval($this->invoice->tax_rate3).'%', 'total' => $tax]; } - + return $this; } diff --git a/config/ninja.php b/config/ninja.php index c0749919e0..fc12f803c3 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -17,8 +17,8 @@ return [ 'require_https' => env('REQUIRE_HTTPS', true), 'app_url' => rtrim(env('APP_URL', ''), '/'), 'app_domain' => env('APP_DOMAIN', 'invoicing.co'), - 'app_version' => env('APP_VERSION', '5.8.51'), - 'app_tag' => env('APP_TAG', '5.8.51'), + 'app_version' => env('APP_VERSION', '5.8.52'), + 'app_tag' => env('APP_TAG', '5.8.52'), 'minimum_client_version' => '5.0.16', 'terms_version' => '1.0.1', 'api_secret' => env('API_SECRET', false), diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 3c2bb1ccea..819ab9209d 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -210,6 +210,127 @@ class InvoiceTest extends TestCase $this->assertNull($invoice->partial_due_date); } + public function testSurchargesAndTaxes() + { + + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = true; + $invoice->discount = 0; + $invoice->is_amount_discount = true; + $invoice->status_id = 2; + $invoice->tax_name1 = 'GST'; + $invoice->tax_rate1 = 10; + $invoice->custom_surcharge1 = 100; + $invoice->custom_surcharge_tax1 = true; + + $line_items = []; + + $line_item = new InvoiceItem(); + $line_item->quantity = 1; + $line_item->cost = 100; + $line_item->tax_rate1 = 0; + $line_item->tax_name1 = ''; + $line_item->product_key = 'line1'; + $line_item->notes = 'Test'; + $line_item->tax_id = 1; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $calc = $invoice->calc(); + $invoice = $calc->getInvoice(); + + $this->assertEquals(200, $invoice->amount); + $this->assertEquals(200, $invoice->balance); + $this->assertEquals(0,$invoice->paid_to_date); + $this->assertEquals(18.18, $calc->getTotalTaxes()); + + } + + public function testSurchargesAndTaxes2() + { + + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = true; + $invoice->discount = 0; + $invoice->is_amount_discount = true; + $invoice->status_id = 2; + $invoice->tax_name1 = ''; + $invoice->tax_rate1 = 0; + $invoice->custom_surcharge1 = 100; + $invoice->custom_surcharge_tax1 = true; + + $line_items = []; + + $line_item = new InvoiceItem(); + $line_item->quantity = 1; + $line_item->cost = 100; + $line_item->tax_rate1 = 0; + $line_item->tax_name1 = ''; + $line_item->product_key = 'line1'; + $line_item->notes = 'Test'; + $line_item->tax_id = 1; + $line_item->tax_name1 = 'GST'; + $line_item->tax_rate1 = 10; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $calc = $invoice->calc(); + $invoice = $calc->getInvoice(); + + $this->assertEquals(200, $invoice->amount); + $this->assertEquals(200, $invoice->balance); + $this->assertEquals(0,$invoice->paid_to_date); + $this->assertEquals(9.09, $calc->getTotalTaxes()); + + } + + + public function testSurchargesAndTaxesExclusive() + { + + $invoice = InvoiceFactory::create($this->company->id, $this->user->id); + $invoice->client_id = $this->client->id; + $invoice->uses_inclusive_taxes = false; + $invoice->discount = 0; + $invoice->is_amount_discount = true; + $invoice->status_id = 2; + $invoice->tax_name1 = 'GST'; + $invoice->tax_rate1 = 10; + $invoice->custom_surcharge1 = 10; + $invoice->custom_surcharge_tax1 = true; + + $line_items = []; + + $line_item = new InvoiceItem(); + $line_item->quantity = 1; + $line_item->cost = 10; + $line_item->tax_rate1 = 0; + $line_item->tax_name1 = ''; + $line_item->product_key = 'line1'; + $line_item->notes = 'Test'; + $line_item->tax_id = 1; + $line_items[] = $line_item; + + $invoice->line_items = $line_items; + $invoice->save(); + + $calc = $invoice->calc(); + $invoice = $calc->getInvoice(); + + $this->assertEquals(22, $invoice->amount); + $this->assertEquals(22, $invoice->balance); + $this->assertEquals(0,$invoice->paid_to_date); + $this->assertEquals(2, $calc->getTotalTaxes()); + + } + + public function testGrossTaxAmountCalcuations() { $invoice = InvoiceFactory::create($this->company->id, $this->user->id);