From fc9e62dc8d4e2d2613bca1facf9847cb279bbec9 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Thu, 12 Oct 2017 18:06:40 +0300 Subject: [PATCH] fixed api #46 --- app/Http/Controllers/Api/Incomes/Invoices.php | 138 +++++++++++++++- app/Http/Controllers/Incomes/Invoices.php | 151 +++++++----------- resources/lang/en-GB/demo.php | 2 +- resources/views/expenses/bills/bill.blade.php | 2 +- .../views/incomes/invoices/invoice.blade.php | 2 +- 5 files changed, 198 insertions(+), 97 deletions(-) diff --git a/app/Http/Controllers/Api/Incomes/Invoices.php b/app/Http/Controllers/Api/Incomes/Invoices.php index 7ba02179e..7fcdb1a8b 100644 --- a/app/Http/Controllers/Api/Incomes/Invoices.php +++ b/app/Http/Controllers/Api/Incomes/Invoices.php @@ -11,7 +11,7 @@ use App\Models\Income\Invoice; use App\Models\Income\InvoiceHistory; use App\Models\Income\InvoiceItem; use App\Models\Income\InvoicePayment; -use App\Models\Income\InvoiceStatus; +use App\Models\Income\InvoiceTotal; use App\Models\Item\Item; use App\Models\Setting\Tax; use Dingo\Api\Routing\Helpers; @@ -53,6 +53,10 @@ class Invoices extends ApiController { $invoice = Invoice::create($request->all()); + $taxes = []; + $tax_total = 0; + $sub_total = 0; + $invoice_item = array(); $invoice_item['company_id'] = $request['company_id']; $invoice_item['invoice_id'] = $invoice->id; @@ -91,16 +95,38 @@ class Invoices extends ApiController $invoice_item['price'] = $item['price']; $invoice_item['tax'] = $tax; $invoice_item['tax_id'] = $tax_id; - $invoice_item['total'] = ($item['price'] + $invoice_item['tax']) * $item['quantity']; - - $request['amount'] += $invoice_item['total']; + $invoice_item['total'] = $item['price'] * $item['quantity']; InvoiceItem::create($invoice_item); + + if (isset($tax_object)) { + if (array_key_exists($tax_object->id, $taxes)) { + $taxes[$tax_object->id]['amount'] += $tax; + } else { + $taxes[$tax_object->id] = [ + 'name' => $tax_object->name, + 'amount' => $tax + ]; + } + } + + $tax_total += $tax; + $sub_total += $invoice_item['total']; + + unset($item_object); + unset($tax_object); } } + if (empty($request['amount'])) { + $request['amount'] += $sub_total + $tax_total; + } + $invoice->update($request->input()); + // Add invoice totals + $this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total); + $request['invoice_id'] = $invoice->id; $request['status_code'] = $request['invoice_status_code']; $request['notify'] = 0; @@ -123,6 +149,10 @@ class Invoices extends ApiController */ public function update(Invoice $invoice, Request $request) { + $taxes = []; + $tax_total = 0; + $sub_total = 0; + $invoice_item = array(); $invoice_item['company_id'] = $request['company_id']; $invoice_item['invoice_id'] = $invoice->id; @@ -163,16 +193,42 @@ class Invoices extends ApiController $invoice_item['price'] = $item['price']; $invoice_item['tax'] = $tax; $invoice_item['tax_id'] = $tax_id; - $invoice_item['total'] = ($item['price'] + $invoice_item['tax']) * $item['quantity']; + $invoice_item['total'] = $item['price'] * $item['quantity']; $request['amount'] += $invoice_item['total']; InvoiceItem::create($invoice_item); + + if (isset($tax_object)) { + if (array_key_exists($tax_object->id, $taxes)) { + $taxes[$tax_object->id]['amount'] += $tax; + } else { + $taxes[$tax_object->id] = [ + 'name' => $tax_object->name, + 'amount' => $tax + ]; + } + } + + $tax_total += $tax; + $sub_total += $invoice_item['total']; + + unset($item_object); + unset($tax_object); } } + if (empty($request['amount'])) { + $request['amount'] += $sub_total + $tax_total; + } + $invoice->update($request->input()); + // Delete previous invoice totals + InvoiceTotal::where('invoice_id', $invoice->id)->delete(); + + $this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total); + // Fire the event to make it extendible event(new InvoiceUpdated($invoice)); @@ -195,4 +251,76 @@ class Invoices extends ApiController return $this->response->noContent(); } + + protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total) { + // Add invoice total taxes + if ($request['totals']) { + $sort_order = 1; + + foreach ($request['totals'] as $total) { + if (!empty($total['sort_order'])) { + $sort_order = $total['sort_order']; + } + + $invoice_total = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'code' => $total['code'], + 'name' => $total['name'], + 'amount' => $total['amount'], + 'sort_order' => $sort_order, + ]; + + InvoiceTotal::create($invoice_total); + + if (empty($total['sort_order'])) { + $sort_order++; + } + } + } else { + // Added invoice total sub total + $invoice_sub_total = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'code' => 'sub_total', + 'name' => 'invoices.sub_total', + 'amount' => $sub_total, + 'sort_order' => 1, + ]; + + InvoiceTotal::create($invoice_sub_total); + + $sort_order = 2; + + // Added invoice total taxes + if ($taxes) { + foreach ($taxes as $tax) { + $invoice_tax_total = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'code' => 'tax', + 'name' => $tax['name'], + 'amount' => $tax['amount'], + 'sort_order' => $sort_order, + ]; + + InvoiceTotal::create($invoice_tax_total); + + $sort_order++; + } + } + + // Added invoice total total + $invoice_total = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'code' => 'total', + 'name' => 'invoices.total', + 'amount' => $sub_total + $tax_total, + 'sort_order' => $sort_order, + ]; + + InvoiceTotal::create($invoice_total); + } + } } diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index f538e3048..acd64c4e8 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -282,7 +282,6 @@ class Invoices extends Controller if ($request['item']) { foreach ($request['item'] as $item) { - unset($tax_object); $item_sku = ''; if (!empty($item['item_id'])) { @@ -310,6 +309,9 @@ class Invoices extends Controller $invoice_item['tax_id'] = $tax_id; $invoice_item['total'] = $item['price'] * $item['quantity']; + InvoiceItem::create($invoice_item); + + // Set taxes if (isset($tax_object)) { if (array_key_exists($tax_object->id, $taxes)) { $taxes[$tax_object->id]['amount'] += $tax; @@ -321,10 +323,11 @@ class Invoices extends Controller } } + // Calculate totals $tax_total += $tax; $sub_total += $invoice_item['total']; - InvoiceItem::create($invoice_item); + unset($tax_object); } } @@ -332,49 +335,8 @@ class Invoices extends Controller $invoice->update($request->input()); - // Added invoice total sub total - $invoice_sub_total = [ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'sub_total', - 'name' => 'invoices.sub_total', - 'amount' => $sub_total, - 'sort_order' => 1, - ]; - - InvoiceTotal::create($invoice_sub_total); - - $sort_order = 2; - - // Added invoice total taxes - if ($taxes) { - foreach ($taxes as $tax) { - $invoice_tax_total = [ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'tax', - 'name' => $tax['name'], - 'amount' => $tax['amount'], - 'sort_order' => $sort_order, - ]; - - InvoiceTotal::create($invoice_tax_total); - - $sort_order++; - } - } - - // Added invoice total total - $invoice_total = [ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'total', - 'name' => 'invoices.total', - 'amount' => $sub_total + $tax_total, - 'sort_order' => $sort_order, - ]; - - InvoiceTotal::create($invoice_total); + // Add invoice totals + $this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total); $request['invoice_id'] = $invoice->id; $request['status_code'] = 'draft'; @@ -507,55 +469,17 @@ class Invoices extends Controller } } - $request['amount'] += $sub_total + $tax_total; + if (empty($request['amount'])) { + $request['amount'] += $sub_total + $tax_total; + } $invoice->update($request->input()); + // Delete previous invoice totals InvoiceTotal::where('invoice_id', $invoice->id)->delete(); - // Added invoice total sub total - $invoice_sub_total = [ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'sub_total', - 'name' => 'invoices.sub_total', - 'amount' => $sub_total, - 'sort_order' => 1, - ]; - - InvoiceTotal::create($invoice_sub_total); - - $sort_order = 2; - - // Added invoice total taxes - if ($taxes) { - foreach ($taxes as $tax) { - $invoice_tax_total = [ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'tax', - 'name' => $tax['name'], - 'amount' => $tax['amount'], - 'sort_order' => $sort_order, - ]; - - InvoiceTotal::create($invoice_tax_total); - - $sort_order++; - } - } - - // Added invoice total total - $invoice_total = [ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'total', - 'name' => 'invoices.total', - 'amount' => $sub_total + $tax_total, - 'sort_order' => $sort_order, - ]; - - InvoiceTotal::create($invoice_total); + // Add invoice totals + $this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total); // Fire the event to make it extendible event(new InvoiceUpdated($invoice)); @@ -613,4 +537,53 @@ class Invoices extends Controller return redirect('incomes/invoices'); } + + protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total) + { + $sort_order = 1; + + // Added invoice total sub total + $invoice_sub_total = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'code' => 'sub_total', + 'name' => 'invoices.sub_total', + 'amount' => $sub_total, + 'sort_order' => $sort_order, + ]; + + InvoiceTotal::create($invoice_sub_total); + + $sort_order++; + + // Added invoice total taxes + if ($taxes) { + foreach ($taxes as $tax) { + $invoice_tax_total = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'code' => 'tax', + 'name' => $tax['name'], + 'amount' => $tax['amount'], + 'sort_order' => $sort_order, + ]; + + InvoiceTotal::create($invoice_tax_total); + + $sort_order++; + } + } + + // Added invoice total total + $invoice_total = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'code' => 'total', + 'name' => 'invoices.total', + 'amount' => $sub_total + $tax_total, + 'sort_order' => $sort_order, + ]; + + InvoiceTotal::create($invoice_total); + } } diff --git a/resources/lang/en-GB/demo.php b/resources/lang/en-GB/demo.php index 40390f185..47c82f5dc 100644 --- a/resources/lang/en-GB/demo.php +++ b/resources/lang/en-GB/demo.php @@ -11,7 +11,7 @@ return [ 'currencies_gbp' => 'British Pound', 'currencies_try' => 'Turkish Lira', 'taxes_exempt' => 'Tax Exempt', - 'taxes_normal' => 'Normal', + 'taxes_normal' => 'Normal Tax', 'taxes_sales' => 'Sales Tax', ]; diff --git a/resources/views/expenses/bills/bill.blade.php b/resources/views/expenses/bills/bill.blade.php index e4ff5bbe1..b703c08b3 100644 --- a/resources/views/expenses/bills/bill.blade.php +++ b/resources/views/expenses/bills/bill.blade.php @@ -92,7 +92,7 @@ {{ $item->quantity }} @money($item->price, $bill->currency_code, true) - @money($item->total - $item->tax, $bill->currency_code, true) + @money($item->total, $bill->currency_code, true) @endforeach diff --git a/resources/views/incomes/invoices/invoice.blade.php b/resources/views/incomes/invoices/invoice.blade.php index 40a54c4b5..cd51919ad 100644 --- a/resources/views/incomes/invoices/invoice.blade.php +++ b/resources/views/incomes/invoices/invoice.blade.php @@ -92,7 +92,7 @@ {{ $item->quantity }} @money($item->price, $invoice->currency_code, true) - @money($item->total - $item->tax, $invoice->currency_code, true) + @money($item->total, $invoice->currency_code, true) @endforeach