Skip to content

Commit

Permalink
Invoice create tax rate issue solved
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Nov 6, 2018
1 parent 79ebd9d commit 475a39c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
12 changes: 12 additions & 0 deletions app/Jobs/Income/CreateInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public function handle()
// Calculate totals
$tax_total += $invoice_item->tax;
$sub_total += $invoice_item->total;

// Set taxes
foreach ($invoice_item->item_taxes as $item_tax) {
if (isset($taxes) && in_array($item['tax_id'], $taxes)) {
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
} else {
$taxes[$item_tax['tax_id']] = [
'name' => $item_tax['name'],
'amount' => $item_tax['amount']
];
}
}
}
}

Expand Down
48 changes: 36 additions & 12 deletions app/Jobs/Income/CreateInvoiceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function handle()
$item_tax_total = 0;

if (!empty($this->data['tax_id'])) {
$calculates = $compounds = [];
$calculates = $compounds = $taxes = [];

foreach ((array) $this->data['tax_id'] as $tax_id) {
$tax = Tax::find($tax_id);
Expand All @@ -112,6 +112,8 @@ public function handle()
break;
case 'normal':
default:
$taxes[] = $tax;

$tax_amount = ($item_discount_amount / 100) * $tax->rate;

$item_taxes[] = [
Expand All @@ -128,20 +130,42 @@ public function handle()
}

if ($calculates) {
foreach ($calculates as $calculate) {
$item_sub_and_tax_total = $item_discount_amount + $item_tax_total;
if ($this->discount) {
$item_tax_total = 0;

$item_tax_total = $tax_amount = $item_sub_and_tax_total - ($item_sub_and_tax_total / (1 + ($calculate->rate / 100)));
if ($taxes) {
foreach ($taxes as $tax) {
$item_tax_amount = ($item_amount / 100) * $tax->rate;

$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $calculate->id,
'name' => $calculate->name,
'amount' => $tax_amount,
];
$item_tax_total += $item_tax_amount;
}
}

foreach ($calculates as $calculate) {
$item_sub_and_tax_total = $item_amount + $item_tax_total;

$item_tax_total = $item_sub_and_tax_total - (($item_sub_and_tax_total * (100 - $calculate->rate)) / 100);

$item_sub_total = $item_sub_and_tax_total - $item_tax_total;

$item_amount = $item_sub_and_tax_total - $item_tax_total;
$item_discount_amount = $item_sub_total - ($item_sub_total * ($this->discount / 100));
}
} else {
foreach ($calculates as $calculate) {
$item_sub_and_tax_total = $item_discount_amount + $item_tax_total;

$item_tax_total = $tax_amount = $item_sub_and_tax_total - ($item_sub_and_tax_total / (1 + ($calculate->rate / 100)));

$item_taxes[] = [
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'tax_id' => $calculate->id,
'name' => $calculate->name,
'amount' => $tax_amount,
];

$item_amount = $item_sub_and_tax_total - $item_tax_total;
}
}
}

Expand Down

0 comments on commit 475a39c

Please sign in to comment.