fix tax-per-item issue

This commit is contained in:
Mohit Panjwani
2021-07-16 11:49:56 +05:30
parent 2b80082996
commit d3a745605e
2 changed files with 13 additions and 25 deletions

View File

@ -476,33 +476,22 @@ class Invoice extends Model implements HasMedia
public function getPDFData() public function getPDFData()
{ {
$taxTypes = []; $taxes = collect();
$taxes = [];
$labels = [];
if ($this->tax_per_item === 'YES') { if ($this->tax_per_item === 'YES') {
foreach ($this->items as $item) { foreach ($this->items as $item) {
foreach ($item->taxes as $tax) { foreach ($item->taxes as $tax) {
if (! in_array($tax->name, $taxTypes)) { $found = $taxes->filter(function ($item) use ($tax) {
array_push($taxTypes, $tax->name); return $item->tax_type_id == $tax->tax_type_id;
array_push($labels, $tax->name.' ('.$tax->percent.'%)'); })->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; $invoiceTemplate = self::find($this->id)->template_name;
@ -521,7 +510,6 @@ class Invoice extends Model implements HasMedia
'billing_address' => $this->getCustomerBillingAddress(), 'billing_address' => $this->getCustomerBillingAddress(),
'notes' => $this->getNotes(), 'notes' => $this->getNotes(),
'logo' => $logo ?? null, 'logo' => $logo ?? null,
'labels' => $labels,
'taxes' => $taxes, 'taxes' => $taxes,
]); ]);

View File

@ -79,16 +79,16 @@
</tr> </tr>
@if ($invoice->tax_per_item === 'YES') @if ($invoice->tax_per_item === 'YES')
@for ($i = 0; $i < count($labels); $i++) @foreach ($taxes as $tax)
<tr> <tr>
<td class="border-0 total-table-attribute-label"> <td class="border-0 total-table-attribute-label">
{{$labels[$i]}} {{$tax->name.' ('.$tax->percent.'%)'}}
</td> </td>
<td class="py-2 border-0 item-cell total-table-attribute-value"> <td class="py-2 border-0 item-cell total-table-attribute-value">
{!! format_money_pdf($taxes[$i], $invoice->user->currency) !!} {!! format_money_pdf($tax->amount, $invoice->user->currency) !!}
</td> </td>
</tr> </tr>
@endfor @endforeach
@else @else
@foreach ($invoice->taxes as $tax) @foreach ($invoice->taxes as $tax)
<tr> <tr>