From 6fb9ba89ee7e76542ee2eb2dfc6f783878d7cc33 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 17 Oct 2019 14:14:17 +1100 Subject: [PATCH] Woring on invoice is_amount_discounts --- app/Factory/InvoiceItemFactory.php | 2 +- app/Helpers/Invoice/Discounter.php | 3 +- app/Helpers/Invoice/InvoiceItemSum.php | 47 ++++++++++++++++++++++++++ app/Helpers/Invoice/InvoiceSum.php | 3 ++ app/Helpers/Invoice/Taxer.php | 5 +++ app/Models/Invoice.php | 3 +- database/factories/InvoiceFactory.php | 2 +- 7 files changed, 61 insertions(+), 4 deletions(-) diff --git a/app/Factory/InvoiceItemFactory.php b/app/Factory/InvoiceItemFactory.php index 4e6573f7d4d7..80f5add9e58f 100644 --- a/app/Factory/InvoiceItemFactory.php +++ b/app/Factory/InvoiceItemFactory.php @@ -61,7 +61,7 @@ class InvoiceItemFactory $item->quantity = $faker->numberBetween(1,10); $item->cost = $faker->randomFloat(2, 1, 1000); $item->line_total = $item->quantity * $item->cost; - $item->is_amount_discount = false; + $item->is_amount_discount = true; $item->discount = $faker->numberBetween(1,10); $item->notes = $faker->realText(20); $item->product_key = $faker->word(); diff --git a/app/Helpers/Invoice/Discounter.php b/app/Helpers/Invoice/Discounter.php index 42f9216d0457..a0aa833603f4 100644 --- a/app/Helpers/Invoice/Discounter.php +++ b/app/Helpers/Invoice/Discounter.php @@ -20,8 +20,9 @@ trait Discounter public function discount($amount) { - if($this->invoice->is_amount_discount === true) + if($this->invoice->is_amount_discount == true){ return $this->invoice->discount; + } return round($amount * ($this->invoice->discount / 100), 2); diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index ea0dcb25cecd..fcf9789f75aa 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -213,4 +213,51 @@ class InvoiceItemSum return $this; } + + /** + * Invoice Amount Discount + * + * The problem, when calculating invoice level discounts, + * the tax collected changes. + * + * We need to synthetically reduce the line_total amounts + * and recalculate the taxes and then pass back + * the updated map + */ + + public function calcTaxesWithAmountDiscount() + { + $this->setGroupedTaxes(collect([])); + + foreach($this->line_items as $this->item) + { + $item_tax = 0; + + $amount = $this->item->line_total - ($this->item->line_total * ($this->invoice->discount/$this->sub_total)); + $item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount); + + $item_tax += $item_tax_rate1_total; + + if($item_tax_rate1_total > 0) + $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); + + $item_tax_rate2_total = $this->calcAmountLineTax($this->item->tax_rate2, $amount); + + $item_tax += $item_tax_rate2_total; + + if($item_tax_rate2_total > 0) + $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); + + $item_tax_rate3_total = $this->calcAmountLineTax($this->item->tax_rate3, $amount); + + $item_tax += $item_tax_rate3_total; + + if($item_tax_rate3_total > 0) + $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); + + } + + $this->setTotalTaxes($item_tax); + + } } \ No newline at end of file diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index b94bbfbf5bdc..77d3c7756b98 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -233,6 +233,9 @@ class InvoiceSum public function setTaxMap() { + // if($this->invoice->is_amount_discount == true) + // $this->invoice_items->calcTaxesWithAmountDiscount(); + $this->tax_map = collect(); $keys = $this->invoice_items->getGroupedTaxes()->pluck('key')->unique(); diff --git a/app/Helpers/Invoice/Taxer.php b/app/Helpers/Invoice/Taxer.php index 16b42f80f306..481f29702972 100644 --- a/app/Helpers/Invoice/Taxer.php +++ b/app/Helpers/Invoice/Taxer.php @@ -42,6 +42,11 @@ trait Taxer } + public function calcAmountLineTax($tax_rate, $amount) + { + return $this->formatValue(($amount * $tax_rate/100), $this->currency->precision); + } + public function inclusiveTax($tax_rate, $item) { diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 82cb9eace1d6..db8792922bc4 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -267,7 +267,8 @@ class Invoice extends BaseModel */ public function calc() { - +//todo will need to switch between inclusive and exclusive implementations of InvoiceSum + $invoice_calc = new InvoiceSum($this, $this->settings); return $invoice_calc->build(); diff --git a/database/factories/InvoiceFactory.php b/database/factories/InvoiceFactory.php index 5dc66d2d1367..7be01b9669f5 100644 --- a/database/factories/InvoiceFactory.php +++ b/database/factories/InvoiceFactory.php @@ -10,7 +10,7 @@ $factory->define(App\Models\Invoice::class, function (Faker $faker) { 'status_id' => App\Models\Invoice::STATUS_SENT, 'invoice_number' => $faker->ean13(), 'discount' => $faker->numberBetween(1,10), - 'is_amount_discount' => false, + 'is_amount_discount' => true, 'tax_name1' => 'GST', 'tax_rate1' => 10, 'tax_name2' => 'VAT',