diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index b03df9fca1a0..53ce924e2a04 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -18,11 +18,14 @@ class InvoiceItemCalc protected $total_taxes; + protected $tax_collection; + public function __construct(\stdClass $item, int $precision = 2, bool $inclusive_tax) { $this->item = $item; $this->precision = $precision; $this->inclusive_tax = $inclusive_tax; + $this->tax_collection = collect(); } public function process() @@ -30,6 +33,7 @@ class InvoiceItemCalc $this->setLineTotal($this->formatValue($this->item->cost, $this->precision) * $this->formatValue($this->item->qty, $this->precision)); $this->setDiscount(); $this->calcTaxes(); + $this->groupTaxes(); } private function setDiscount() @@ -53,9 +57,13 @@ class InvoiceItemCalc if($tax_rate1 != 0) { if($this->inclusive_tax) - $item_tax += $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate1/100))) , $this->precision); + $item_tax_rate1_total = $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate1/100))) , $this->precision); else - $item_tax += $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; + + $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); } $tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->precision); @@ -72,6 +80,25 @@ class InvoiceItemCalc $this->setTotalTaxes($item_tax); } + private function groupTax($tax_name, $tax_rate, $tax_total) : array + { + $group_tax = []; + + $key = str_replace(" ", "", $tax_name.$tax_rate); + $group_tax[$key] = $tax_total + + return $group_tax; + + } + + private function groupTaxes($group_tax) + { + + $this->tax_collection->merge(collect($group_tax)); + + return $this; + } + public function getLineTotal() { return $this->item->line_total; diff --git a/database/factories/InvoiceFactory.php b/database/factories/InvoiceFactory.php index 33eec50c1bf3..ec1f38d486a1 100644 --- a/database/factories/InvoiceFactory.php +++ b/database/factories/InvoiceFactory.php @@ -5,7 +5,7 @@ use Faker\Generator as Faker; $factory->define(App\Models\Invoice::class, function (Faker $faker) { return [ 'invoice_status_id' => App\Models\Invoice::STATUS_PAID, - 'invoice_number' => $faker->text(20), + 'invoice_number' => $faker->text(256), 'discount' => $faker->numberBetween(1,10), 'is_amount_discount' => $faker->boolean(), 'tax_name1' => 'GST',