From 56f05f71221c98df75b58e7053701bc4b08e9019 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 5 Apr 2019 14:58:26 +1100 Subject: [PATCH] Working on getters and setters for invoice attributes --- app/Helpers/Invoice/InvoiceCalc.php | 21 +++---- app/Helpers/Invoice/InvoiceItemCalc.php | 74 ++++++++++++++++++++----- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceCalc.php b/app/Helpers/Invoice/InvoiceCalc.php index a71a07ea52ac..39f287c474ef 100644 --- a/app/Helpers/Invoice/InvoiceCalc.php +++ b/app/Helpers/Invoice/InvoiceCalc.php @@ -2,6 +2,7 @@ namespace App\Helpers\Invoice; +use App\Helpers\Invoice\InvoiceItemCalc; use App\Models\Invoice; use App\Utils\Traits\NumberFormatter; @@ -44,22 +45,22 @@ class InvoiceCalc foreach($this->invoice->line_items as $item) { - $total = $this->formatValue($item->cost) * $this->formatValue($item->qty); - $total = $this->setDiscount($total, $item->discount, $item->is_amount_discount); - $total = $this->setTaxRate($total, $item->tax_name1, $item->tax_rate1); - - $item->line_total = $total; - - $new_line_items[] = $item; + $item_calc = new InvoiceItemCalc($item); + $item_calc->process(); - $this->setInvoiceTotal($total); + + $new_line_items[] = $item_calc->getLineItem(); + + //set collection of itemised taxes + //set running total of taxes + } $this->invoice->line_items = $new_line_items; } - +/* private function setDiscount($amount, $discount, $is_amount_discount) { @@ -80,7 +81,7 @@ class InvoiceCalc $this->invoice_total = $invoice_total; } - +*/ diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index 53ce924e2a04..a590829e7d0a 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -18,6 +18,8 @@ class InvoiceItemCalc protected $total_taxes; + protected $total_dicounts; + protected $tax_collection; public function __construct(\stdClass $item, int $precision = 2, bool $inclusive_tax) @@ -30,19 +32,33 @@ class InvoiceItemCalc public function process() { - $this->setLineTotal($this->formatValue($this->item->cost, $this->precision) * $this->formatValue($this->item->qty, $this->precision)); - $this->setDiscount(); - $this->calcTaxes(); - $this->groupTaxes(); + + $this->setLineTotal($this->formatValue($this->item->cost, $this->precision) * $this->formatValue($this->item->qty, $this->precision)) + ->setDiscount() + ->calcTaxes(); + } private function setDiscount() { if($this->item->is_amount_discount) - $this->setLineTotal($this->getLineTotal() - $this->formatValue($this->item->discount, $this->precision)); - else - $this->setLineTotal($this->getLineTotal() - $this->formatValue(($this->getLineTotal() * $this->item->discount / 100), $this->precision)); + { + $discount = $this->formatValue($this->item->discount, $this->precision); + + $this->setLineTotal($this->getLineTotal() - $discount); + + $this->setTotalDiscounts($this->getTotalDiscounts() + $discount); + } + else + { + $discount = $this->formatValue(($this->getLineTotal() * $this->item->discount / 100), $this->precision); + + $this->setLineTotal($this->getLineTotal() - $discount); + + $this->setTotalDiscounts($this->getTotalDiscounts() + $discount); + + } return $this; @@ -59,7 +75,7 @@ class InvoiceItemCalc if($this->inclusive_tax) $item_tax_rate1_total = $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate1/100))) , $this->precision); else - $item_tax_rate1_total += $this->formatValue(($this->getLineTotal() * $tax_rate1/100), $this->precision); + $item_tax_rate1_total = $this->formatValue(($this->getLineTotal() * $tax_rate1/100), $this->precision); $item_tax += $item_tax_rate1_total; @@ -71,23 +87,29 @@ class InvoiceItemCalc if($tax_rate2 != 0) { if($this->inclusive_tax) - $item_tax += $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate2/100))) , $this->precision); + $item_tax_rate2_total = $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate2/100))) , $this->precision); else - $item_tax += $this->formatValue(($this->getLineTotal() * $tax_rate2/100), $this->precision); + $item_tax_rate2_total = $this->formatValue(($this->getLineTotal() * $tax_rate2/100), $this->precision); + + $item_tax += $item_tax_rate2_total; + + $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); + } $this->setTotalTaxes($item_tax); } - private function groupTax($tax_name, $tax_rate, $tax_total) : array + private function groupTax($tax_name, $tax_rate, $tax_total) { $group_tax = []; $key = str_replace(" ", "", $tax_name.$tax_rate); - $group_tax[$key] = $tax_total - return $group_tax; + $group_tax[$key] = ['total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate]; + + $this->groupTaxes($group_tax); } @@ -99,6 +121,19 @@ class InvoiceItemCalc return $this; } + /**************** + * + * Getters and Setters + * + * + */ + public function getLimeItem() + { + + return $this->item; + + } + public function getLineTotal() { return $this->item->line_total; @@ -123,4 +158,17 @@ class InvoiceItemCalc return $this; } + public function getTotalDiscounts() + { + return $this->total_dicounts; + } + + public function setTotalDiscounts($total) + { + $this->total_dicounts = $total; + + return $this; + } + + } \ No newline at end of file