diff --git a/app/Models/Estimate.php b/app/Models/Estimate.php index fade55e5..ba8558e6 100644 --- a/app/Models/Estimate.php +++ b/app/Models/Estimate.php @@ -395,33 +395,22 @@ class Estimate 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); - } } $estimateTemplate = self::find($this->id)->template_name; @@ -440,7 +429,6 @@ class Estimate extends Model implements HasMedia 'shipping_address' => $this->getCustomerShippingAddress(), 'billing_address' => $this->getCustomerBillingAddress(), 'notes' => $this->getNotes(), - 'labels' => $labels, 'taxes' => $taxes, ]); diff --git a/resources/views/app/pdf/estimate/partials/table.blade.php b/resources/views/app/pdf/estimate/partials/table.blade.php index 288c68d0..a68255bc 100644 --- a/resources/views/app/pdf/estimate/partials/table.blade.php +++ b/resources/views/app/pdf/estimate/partials/table.blade.php @@ -72,16 +72,16 @@ @if ($estimate->tax_per_item === 'YES') - @for ($i = 0; $i < count($labels); $i++) + @foreach ($taxes as $tax) - {{$labels[$i]}} + {{$tax->name.' ('.$tax->percent.'%)'}} - - {!! format_money_pdf($taxes[$i], $estimate->user->currency) !!} + + {!! format_money_pdf($tax->amount, $estimate->user->currency) !!} - @endfor + @endforeach @else @foreach ($estimate->taxes as $tax)