From 3cab690af4763217c83e5740a77ec6e43e410905 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 12 Sep 2023 17:06:40 +1000 Subject: [PATCH] Fixes for large discounts --- app/Helpers/Invoice/InvoiceItemSum.php | 25 +++++++++++++++++++++---- app/Helpers/Invoice/InvoiceSum.php | 15 ++------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php index 57d26d43b9cf..ce651f0997c3 100644 --- a/app/Helpers/Invoice/InvoiceItemSum.php +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -391,15 +391,21 @@ class InvoiceItemSum { $this->setGroupedTaxes(collect([])); - $item_tax = 0; + - foreach ($this->line_items as $this->item) { + foreach ($this->line_items as $key => $this->item) { if ($this->item->line_total == 0) { continue; } + + $item_tax = 0; //$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->item->line_total * ($this->invoice->discount / $this->sub_total)) : 0; + $amount = ($this->sub_total > 0) ? $this->item->line_total - ($this->invoice->discount * ( $this->item->line_total / $this->sub_total)) : 0; + + nlog("amount discount tax calcs : {$this->item->line_total} - {$this->sub_total} {$amount} {$this->item->line_total} {$this->invoice->discount} / {$this->sub_total}"); + nlog(( $this->item->line_total / $this->sub_total)); + nlog(($this->invoice->discount * ( $this->item->line_total / $this->sub_total))); $item_tax_rate1_total = $this->calcAmountLineTax($this->item->tax_rate1, $amount); @@ -424,9 +430,20 @@ class InvoiceItemSum if ($item_tax_rate3_total != 0) { $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); } + + $this->item->gross_line_total = $this->getLineTotal() + $item_tax; + $this->item->tax_amount = $item_tax; + + $this->line_items[$key] = $this->item; + nlog("amount discount tax calcs : {$this->getLineTotal()} {$this->sub_total} {$amount} {$item_tax} {$this->item->gross_line_total} "); + + $this->setTotalTaxes($this->getTotalTaxes() + $item_tax); + } - $this->setTotalTaxes($item_tax); + + + return $this; } /** diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index 16687aed0298..3f32ed486f78 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -308,8 +308,9 @@ class InvoiceSum public function setTaxMap(): self { - if ($this->invoice->is_amount_discount == true) { + if ($this->invoice->is_amount_discount) { $this->invoice_items->calcTaxesWithAmountDiscount(); + $this->invoice->line_items = $this->invoice_items->getLineItems(); } $this->tax_map = collect(); @@ -327,8 +328,6 @@ class InvoiceSum return $value['key'] == $key; })->sum('total'); - //$total_line_tax -= $this->discount($total_line_tax); - $this->tax_map[] = ['name' => $tax_name, 'total' => $total_line_tax]; $this->total_taxes += $total_line_tax; @@ -377,16 +376,6 @@ class InvoiceSum public function purgeTaxes(): self { - // $this->tax_rate1 = 0; - // $this->tax_name1 = ''; - - // $this->tax_rate2 = 0; - // $this->tax_name2 = ''; - - // $this->tax_rate3 = 0; - // $this->tax_name3 = ''; - - // $this->discount = 0; $line_items = collect($this->invoice->line_items);