diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 0f8c339d..ac300798 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -476,33 +476,22 @@ class Invoice extends Model implements HasMedia public function getPDFData() { - $taxTypes = []; - $taxes = []; - $labels = []; + $taxes = collect(); if ($this->tax_per_item === 'YES') { foreach ($this->items as $item) { foreach ($item->taxes as $tax) { - if (! in_array($tax->name, $taxTypes)) { - array_push($taxTypes, $tax->name); - array_push($labels, $tax->name.' ('.$tax->percent.'%)'); + $found = $taxes->filter(function ($item) use ($tax) { + return $item->tax_type_id == $tax->tax_type_id; + })->first(); + + if ($found) { + $found->amount += $tax->amount; + } else { + $taxes->push($tax); } } } - - foreach ($taxTypes as $taxType) { - $total = 0; - - foreach ($this->items as $item) { - foreach ($item->taxes as $tax) { - if ($tax->name == $taxType) { - $total += $tax->amount; - } - } - } - - array_push($taxes, $total); - } } $invoiceTemplate = self::find($this->id)->template_name; @@ -521,7 +510,6 @@ class Invoice extends Model implements HasMedia 'billing_address' => $this->getCustomerBillingAddress(), 'notes' => $this->getNotes(), 'logo' => $logo ?? null, - 'labels' => $labels, 'taxes' => $taxes, ]); diff --git a/resources/views/app/pdf/invoice/partials/table.blade.php b/resources/views/app/pdf/invoice/partials/table.blade.php index f7b068de..98ee76db 100644 --- a/resources/views/app/pdf/invoice/partials/table.blade.php +++ b/resources/views/app/pdf/invoice/partials/table.blade.php @@ -79,16 +79,16 @@ @if ($invoice->tax_per_item === 'YES') - @for ($i = 0; $i < count($labels); $i++) + @foreach ($taxes as $tax)