Skip to content

Commit

Permalink
fix tax-per-item issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpanjwani committed Jul 16, 2021
1 parent 2b80082 commit d3a7456
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
30 changes: 9 additions & 21 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,33 +476,22 @@ public static function createTaxes($invoice, $request)

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;
Expand All @@ -521,7 +510,6 @@ public function getPDFData()
'billing_address' => $this->getCustomerBillingAddress(),
'notes' => $this->getNotes(),
'logo' => $logo ?? null,
'labels' => $labels,
'taxes' => $taxes,
]);

Expand Down
8 changes: 4 additions & 4 deletions resources/views/app/pdf/invoice/partials/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ class="text-right item-cell"
</tr>

@if ($invoice->tax_per_item === 'YES')
@for ($i = 0; $i < count($labels); $i++)
@foreach ($taxes as $tax)
<tr>
<td class="border-0 total-table-attribute-label">
{{$labels[$i]}}
{{$tax->name.' ('.$tax->percent.'%)'}}
</td>
<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>
</tr>
@endfor
@endforeach
@else
@foreach ($invoice->taxes as $tax)
<tr>
Expand Down

0 comments on commit d3a7456

Please sign in to comment.