From 475a39c81881a2eb68fd17791edfbcde24d898cc Mon Sep 17 00:00:00 2001 From: cuneytsenturk Date: Tue, 6 Nov 2018 18:39:57 +0300 Subject: [PATCH] Invoice create tax rate issue solved --- app/Jobs/Income/CreateInvoice.php | 12 +++++++ app/Jobs/Income/CreateInvoiceItem.php | 48 ++++++++++++++++++++------- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/app/Jobs/Income/CreateInvoice.php b/app/Jobs/Income/CreateInvoice.php index 936750bb3..a9ba6d31c 100644 --- a/app/Jobs/Income/CreateInvoice.php +++ b/app/Jobs/Income/CreateInvoice.php @@ -58,6 +58,18 @@ class CreateInvoice // 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'] + ]; + } + } } } diff --git a/app/Jobs/Income/CreateInvoiceItem.php b/app/Jobs/Income/CreateInvoiceItem.php index bbaf539df..c2cfc1a68 100644 --- a/app/Jobs/Income/CreateInvoiceItem.php +++ b/app/Jobs/Income/CreateInvoiceItem.php @@ -98,7 +98,7 @@ class CreateInvoiceItem $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); @@ -112,6 +112,8 @@ class CreateInvoiceItem break; case 'normal': default: + $taxes[] = $tax; + $tax_amount = ($item_discount_amount / 100) * $tax->rate; $item_taxes[] = [ @@ -128,20 +130,42 @@ class CreateInvoiceItem } 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; + } + } - $item_amount = $item_sub_and_tax_total - $item_tax_total; + 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_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; + } } }