diff --git a/app/Http/Controllers/Expenses/Bills.php b/app/Http/Controllers/Expenses/Bills.php index e92381bcf..e6ed086fa 100644 --- a/app/Http/Controllers/Expenses/Bills.php +++ b/app/Http/Controllers/Expenses/Bills.php @@ -11,8 +11,9 @@ use App\Models\Banking\Account; use App\Models\Expense\BillStatus; use App\Models\Expense\Vendor; use App\Models\Expense\Bill; -use App\Models\Expense\BillHistory; use App\Models\Expense\BillItem; +use App\Models\Expense\BillTotal; +use App\Models\Expense\BillHistory; use App\Models\Expense\BillPayment; use App\Models\Item\Item; use App\Models\Setting\Category; @@ -21,9 +22,8 @@ use App\Models\Setting\Tax; use App\Traits\Currencies; use App\Traits\DateTime; use App\Traits\Uploads; -use Jenssegers\Date\Date; - use App\Utilities\Modules; +use Date; class Bills extends Controller { @@ -36,7 +36,7 @@ class Bills extends Controller */ public function index() { - $bills = Bill::with('status')->collect(); + $bills = Bill::with(['vendor', 'status', 'items', 'payments', 'histories'])->collect(); $vendors = collect(Vendor::enabled()->pluck('name', 'id')) ->prepend(trans('general.all_type', ['type' => trans_choice('general.vendors', 2)]), ''); @@ -56,25 +56,15 @@ class Bills extends Controller */ public function show(Bill $bill) { - $sub_total = 0; - $tax_total = 0; $paid = 0; - foreach ($bill->items as $item) { - $sub_total += ($item->price * $item->quantity); - $tax_total += ($item->tax * $item->quantity); - } - foreach ($bill->payments as $item) { $item->default_currency_code = $bill->currency_code; $paid += $item->getDynamicConvertedAmount(); } - $bill->sub_total = $sub_total; - $bill->tax_total = $tax_total; $bill->paid = $paid; - $bill->grand_total = (($sub_total + $tax_total) - $paid); $accounts = Account::enabled()->pluck('name', 'id'); @@ -100,27 +90,17 @@ class Bills extends Controller */ public function printBill($bill_id) { - $sub_total = 0; - $tax_total = 0; $paid = 0; $bill = Bill::where('id', $bill_id)->first(); - foreach ($bill->items as $item) { - $sub_total += ($item->price * $item->quantity); - $tax_total += ($item->tax * $item->quantity); - } - foreach ($bill->payments as $item) { $item->default_currency_code = $bill->currency_code; $paid += $item->getDynamicConvertedAmount(); } - $bill->sub_total = $sub_total; - $bill->tax_total = $tax_total; $bill->paid = $paid; - $bill->grand_total = (($sub_total + $tax_total) - $paid); return view('expenses.bills.bill', compact('bill')); } @@ -134,27 +114,17 @@ class Bills extends Controller */ public function pdfBill($bill_id) { - $sub_total = 0; - $tax_total = 0; $paid = 0; $bill = Bill::where('id', $bill_id)->first(); - foreach ($bill->items as $item) { - $sub_total += ($item->price * $item->quantity); - $tax_total += ($item->tax * $item->quantity); - } - foreach ($bill->payments as $item) { $item->default_currency_code = $bill->currency_code; $paid += $item->getDynamicConvertedAmount(); } - $bill->sub_total = $sub_total; - $bill->tax_total = $tax_total; $bill->paid = $paid; - $bill->grand_total = (($sub_total + $tax_total) - $paid); $html = view('expenses.bills.bill', compact('bill'))->render(); @@ -286,18 +256,24 @@ class Bills extends Controller // Upload attachment $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills'); + if ($attachment_path) { $request['attachment'] = $attachment_path; } $bill = Bill::create($request->input()); - $bill_item = array(); + $taxes = []; + $tax_total = 0; + $sub_total = 0; + + $bill_item = []; $bill_item['company_id'] = $request['company_id']; $bill_item['bill_id'] = $bill->id; if ($request['item']) { foreach ($request['item'] as $item) { + unset($tax_object); $item_sku = ''; if (!empty($item['item_id'])) { @@ -323,16 +299,74 @@ class Bills extends Controller $bill_item['price'] = $item['price']; $bill_item['tax'] = $tax; $bill_item['tax_id'] = $tax_id; - $bill_item['total'] = ($item['price'] + $bill_item['tax']) * $item['quantity']; + $bill_item['total'] = $item['price'] * $item['quantity']; - $request['amount'] += $bill_item['total']; + 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 += $bill_item['total']; BillItem::create($bill_item); } } + $request['amount'] += $sub_total + $tax_total; + $bill->update($request->input()); + // Added bill total sub total + $bill_sub_total = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'code' => 'sub_total', + 'name' => 'bills.sub_total', + 'amount' => $sub_total, + 'sort_order' => 1, + ]; + + BillTotal::create($bill_sub_total); + + $sort_order = 2; + + // Added bill total taxes + if ($taxes) { + foreach ($taxes as $tax) { + $bill_tax_total = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'code' => 'tax', + 'name' => $tax['name'], + 'amount' => $tax['amount'], + 'sort_order' => $sort_order, + ]; + + BillTotal::create($bill_tax_total); + + $sort_order++; + } + } + + // Added bill total total + $bill_total = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'code' => 'total', + 'name' => 'bills.total', + 'amount' => $sub_total + $tax_total, + 'sort_order' => $sort_order, + ]; + + BillTotal::create($bill_total); + $request['bill_id'] = $bill->id; $request['status_code'] = 'new'; $request['notify'] = 0; @@ -401,11 +435,16 @@ class Bills extends Controller // Upload attachment $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills'); + if ($attachment_path) { $request['attachment'] = $attachment_path; } - $bill_item = array(); + $taxes = []; + $tax_total = 0; + $sub_total = 0; + + $bill_item = []; $bill_item['company_id'] = $request['company_id']; $bill_item['bill_id'] = $bill->id; @@ -413,6 +452,7 @@ class Bills extends Controller BillItem::where('bill_id', $bill->id)->delete(); foreach ($request['item'] as $item) { + unset($tax_object); $item_sku = ''; if (!empty($item['item_id'])) { @@ -438,16 +478,76 @@ class Bills extends Controller $bill_item['price'] = $item['price']; $bill_item['tax'] = $tax; $bill_item['tax_id'] = $tax_id; - $bill_item['total'] = ($item['price'] + $bill_item['tax']) * $item['quantity']; + $bill_item['total'] = $item['price'] * $item['quantity']; - $request['amount'] += $bill_item['total']; + 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 += $bill_item['total']; BillItem::create($bill_item); } } + BillTotal::where('bill_id', $bill->id)->delete(); + + // Added bill total sub total + $bill_sub_total = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'code' => 'sub_total', + 'name' => 'bills.sub_total', + 'amount' => $sub_total, + 'sort_order' => 1, + ]; + + BillTotal::create($bill_sub_total); + + $sort_order = 2; + + // Added bill total taxes + if ($taxes) { + foreach ($taxes as $tax) { + $bill_tax_total = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'code' => 'tax', + 'name' => $tax['name'], + 'amount' => $tax['amount'], + 'sort_order' => $sort_order, + ]; + + BillTotal::create($bill_tax_total); + + $sort_order++; + } + } + + $request['amount'] += $sub_total + $tax_total; + $bill->update($request->input()); + // Added bill total total + $bill_total = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'code' => 'total', + 'name' => 'bills.total', + 'amount' => $sub_total + $tax_total, + 'sort_order' => $sort_order, + ]; + + BillTotal::create($bill_total); + // Fire the event to make it extendible event(new BillUpdated($bill)); @@ -476,6 +576,7 @@ class Bills extends Controller */ BillItem::where('bill_id', $bill->id)->delete(); + BillTotal::where('bill_id', $bill->id)->delete(); BillPayment::where('bill_id', $bill->id)->delete(); BillHistory::where('bill_id', $bill->id)->delete(); diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index c90d3d9d4..f538e3048 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -13,6 +13,7 @@ use App\Models\Income\Customer; use App\Models\Income\Invoice; use App\Models\Income\InvoiceHistory; use App\Models\Income\InvoiceItem; +use App\Models\Income\InvoiceTotal; use App\Models\Income\InvoicePayment; use App\Models\Income\InvoiceStatus; use App\Models\Item\Item; @@ -22,10 +23,8 @@ use App\Models\Setting\Tax; use App\Traits\Currencies; use App\Traits\DateTime; use App\Traits\Uploads; - -use Jenssegers\Date\Date; - use App\Utilities\Modules; +use Date; class Invoices extends Controller { @@ -38,7 +37,7 @@ class Invoices extends Controller */ public function index() { - $invoices = Invoice::with('status')->collect(); + $invoices = Invoice::with(['customer', 'status', 'items', 'payments', 'histories'])->collect(); $customers = collect(Customer::enabled()->pluck('name', 'id')) ->prepend(trans('general.all_type', ['type' => trans_choice('general.customers', 2)]), ''); @@ -58,25 +57,15 @@ class Invoices extends Controller */ public function show(Invoice $invoice) { - $sub_total = 0; - $tax_total = 0; $paid = 0; - foreach ($invoice->items as $item) { - $sub_total += ($item->price * $item->quantity); - $tax_total += ($item->tax * $item->quantity); - } - foreach ($invoice->payments as $item) { $item->default_currency_code = $invoice->currency_code; $paid += $item->getDynamicConvertedAmount(); } - $invoice->sub_total = $sub_total; - $invoice->tax_total = $tax_total; $invoice->paid = $paid; - $invoice->grand_total = (($sub_total + $tax_total) - $paid); $accounts = Account::enabled()->pluck('name', 'id'); @@ -102,27 +91,17 @@ class Invoices extends Controller */ public function printInvoice($invoice_id) { - $sub_total = 0; - $tax_total = 0; $paid = 0; $invoice = Invoice::where('id', $invoice_id)->first(); - foreach ($invoice->items as $item) { - $sub_total += ($item->price * $item->quantity); - $tax_total += ($item->tax * $item->quantity); - } - foreach ($invoice->payments as $item) { $item->default_currency_code = $invoice->currency_code; $paid += $item->getDynamicConvertedAmount(); } - $invoice->sub_total = $sub_total; - $invoice->tax_total = $tax_total; $invoice->paid = $paid; - $invoice->grand_total = (($sub_total + $tax_total) - $paid); $invoice->template_path = 'incomes.invoices.invoice'; @@ -140,27 +119,17 @@ class Invoices extends Controller */ public function pdfInvoice($invoice_id) { - $sub_total = 0; - $tax_total = 0; $paid = 0; $invoice = Invoice::where('id', $invoice_id)->first(); - foreach ($invoice->items as $item) { - $sub_total += ($item->price * $item->quantity); - $tax_total += ($item->tax * $item->quantity); - } - foreach ($invoice->payments as $item) { $item->default_currency_code = $invoice->currency_code; $paid += $item->getDynamicConvertedAmount(); } - $invoice->sub_total = $sub_total; - $invoice->tax_total = $tax_total; $invoice->paid = $paid; - $invoice->grand_total = (($sub_total + $tax_total) - $paid); $invoice->template_path = 'incomes.invoices.invoice'; @@ -296,18 +265,24 @@ class Invoices extends Controller // Upload attachment $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices'); + if ($attachment_path) { $request['attachment'] = $attachment_path; } $invoice = Invoice::create($request->input()); - $invoice_item = array(); + $taxes = []; + $tax_total = 0; + $sub_total = 0; + + $invoice_item = []; $invoice_item['company_id'] = $request['company_id']; $invoice_item['invoice_id'] = $invoice->id; if ($request['item']) { foreach ($request['item'] as $item) { + unset($tax_object); $item_sku = ''; if (!empty($item['item_id'])) { @@ -333,16 +308,74 @@ class Invoices extends Controller $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']; + 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']; InvoiceItem::create($invoice_item); } } + $request['amount'] += $sub_total + $tax_total; + $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); + $request['invoice_id'] = $invoice->id; $request['status_code'] = 'draft'; $request['notify'] = 0; @@ -411,11 +444,16 @@ class Invoices extends Controller // Upload attachment $attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices'); + if ($attachment_path) { $request['attachment'] = $attachment_path; } - $invoice_item = array(); + $taxes = []; + $tax_total = 0; + $sub_total = 0; + + $invoice_item = []; $invoice_item['company_id'] = $request['company_id']; $invoice_item['invoice_id'] = $invoice->id; @@ -423,6 +461,7 @@ class Invoices extends Controller InvoiceItem::where('invoice_id', $invoice->id)->delete(); foreach ($request['item'] as $item) { + unset($tax_object); $item_sku = ''; if (!empty($item['item_id'])) { @@ -448,16 +487,76 @@ class Invoices extends Controller $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']; + 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']; InvoiceItem::create($invoice_item); } } + $request['amount'] += $sub_total + $tax_total; + $invoice->update($request->input()); + 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); + // Fire the event to make it extendible event(new InvoiceUpdated($invoice)); @@ -486,6 +585,7 @@ class Invoices extends Controller */ InvoiceItem::where('invoice_id', $invoice->id)->delete(); + InvoiceTotal::where('invoice_id', $invoice->id)->delete(); InvoicePayment::where('invoice_id', $invoice->id)->delete(); InvoiceHistory::where('invoice_id', $invoice->id)->delete(); diff --git a/app/Http/Transformers/Expense/BillTotals.php b/app/Http/Transformers/Expense/BillTotals.php new file mode 100644 index 000000000..e78156261 --- /dev/null +++ b/app/Http/Transformers/Expense/BillTotals.php @@ -0,0 +1,29 @@ + $model->id, + 'company_id' => $model->company_id, + 'bill_id' => $model->bill_id, + 'code' => $model->code, + 'name' => $model->name, + 'amount' => $model->amount, + 'sort_order' => $model->sort_order, + 'created_at' => $model->created_at->toIso8601String(), + 'updated_at' => $model->updated_at->toIso8601String(), + ]; + } +} diff --git a/app/Http/Transformers/Income/InvoiceTotals.php b/app/Http/Transformers/Income/InvoiceTotals.php new file mode 100644 index 000000000..18d612c63 --- /dev/null +++ b/app/Http/Transformers/Income/InvoiceTotals.php @@ -0,0 +1,29 @@ + $model->id, + 'company_id' => $model->company_id, + 'invoice_id' => $model->invoice_id, + 'code' => $model->code, + 'name' => $model->name, + 'amount' => $model->amount, + 'sort_order' => $model->sort_order, + 'created_at' => $model->created_at->toIso8601String(), + 'updated_at' => $model->updated_at->toIso8601String(), + ]; + } +} diff --git a/app/Models/Expense/Bill.php b/app/Models/Expense/Bill.php index f131812cf..332dc25f0 100644 --- a/app/Models/Expense/Bill.php +++ b/app/Models/Expense/Bill.php @@ -69,6 +69,11 @@ class Bill extends Model return $this->hasMany('App\Models\Expense\BillItem'); } + public function totals() + { + return $this->hasMany('App\Models\Income\InvoiceTotal'); + } + public function payment() { return $this->belongsTo('App\Models\Expense\BillPayment', 'id', 'bill_id'); diff --git a/app/Models/Expense/BillTotal.php b/app/Models/Expense/BillTotal.php new file mode 100644 index 000000000..5ad66d03b --- /dev/null +++ b/app/Models/Expense/BillTotal.php @@ -0,0 +1,25 @@ +belongsTo('App\Models\Expense\Bill'); + } +} diff --git a/app/Models/Income/Invoice.php b/app/Models/Income/Invoice.php index 35443d604..a2856be46 100644 --- a/app/Models/Income/Invoice.php +++ b/app/Models/Income/Invoice.php @@ -79,6 +79,11 @@ class Invoice extends Model return $this->hasMany('App\Models\Income\InvoiceItem'); } + public function totals() + { + return $this->hasMany('App\Models\Income\InvoiceTotal'); + } + public function payments() { return $this->hasMany('App\Models\Income\InvoicePayment'); diff --git a/app/Models/Income/InvoiceTotal.php b/app/Models/Income/InvoiceTotal.php new file mode 100644 index 000000000..8ed61ac40 --- /dev/null +++ b/app/Models/Income/InvoiceTotal.php @@ -0,0 +1,25 @@ +belongsTo('App\Models\Income\Invoice'); + } +} diff --git a/database/migrations/2017_10_11_000000_create_bill_totals_table.php b/database/migrations/2017_10_11_000000_create_bill_totals_table.php new file mode 100644 index 000000000..2bd8a5c7c --- /dev/null +++ b/database/migrations/2017_10_11_000000_create_bill_totals_table.php @@ -0,0 +1,135 @@ +increments('id'); + $table->integer('company_id'); + $table->integer('bill_id'); + $table->string('code')->nullable(); + $table->string('name'); + $table->float('amount', 15, 4); + $table->integer('sort_order'); + $table->timestamps(); + $table->softDeletes(); + + $table->index('company_id'); + }); + + Model::unguard(); + + $companies = Company::all(); + + foreach ($companies as $company) { + $bills = Bill::where('company_id', $company->id)->get(); + + foreach ($bills as $bill) { + $bill_items = BillItem::where('bill_id', $bill->id)->get(); + + $taxes = []; + $tax_total = 0; + $sub_total = 0; + + foreach ($bill_items as $bill_item) { + unset($tax_object); + $bill_item->total = $bill_item->price * $bill_item->quantity; + + $bill_item->update(); + + if (!empty($bill_item->tax_id)) { + $tax_object = Tax::find($bill_item->tax_id); + } + + if (isset($tax_object)) { + if (array_key_exists($bill_item->tax_id, $taxes)) { + $taxes[$bill_item->tax_id]['amount'] += $bill_item->tax; + } else { + $taxes[$bill_item->tax_id] = [ + 'name' => $tax_object->name, + 'amount' => $bill_item->tax + ]; + } + } + + $tax_total += $bill_item->tax; + $sub_total += $bill_item->price * $bill_item->quantity; + } + + $bill->amount = $sub_total + $tax_total; + + $bill->update(); + + // Added bill total sub total + $bill_sub_total = [ + 'company_id' => $company->id, + 'bill_id' => $bill->id, + 'code' => 'sub_total', + 'name' => 'bills.sub_total', + 'amount' => $sub_total, + 'sort_order' => 1, + ]; + + BillTotal::create($bill_sub_total); + + $sort_order = 2; + + // Added bill total taxes + if ($taxes) { + foreach ($taxes as $tax) { + $bill_tax_total = [ + 'company_id' => $company->id, + 'bill_id' => $bill->id, + 'code' => 'tax', + 'name' => $tax['name'], + 'amount' => $tax['amount'], + 'sort_order' => $sort_order, + ]; + + BillTotal::create($bill_tax_total); + + $sort_order++; + } + } + + // Added bill total total + $bill_total = [ + 'company_id' => $company->id, + 'bill_id' => $bill->id, + 'code' => 'total', + 'name' => 'bills.total', + 'amount' => $sub_total + $tax_total, + 'sort_order' => $sort_order, + ]; + + BillTotal::create($bill_total); + } + } + + Model::reguard(); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('bill_totals'); + } +} diff --git a/database/migrations/2017_10_11_000000_create_invoice_totals_table.php b/database/migrations/2017_10_11_000000_create_invoice_totals_table.php new file mode 100644 index 000000000..29b55c07a --- /dev/null +++ b/database/migrations/2017_10_11_000000_create_invoice_totals_table.php @@ -0,0 +1,135 @@ +increments('id'); + $table->integer('company_id'); + $table->integer('invoice_id'); + $table->string('code')->nullable(); + $table->string('name'); + $table->float('amount', 15, 4); + $table->integer('sort_order'); + $table->timestamps(); + $table->softDeletes(); + + $table->index('company_id'); + }); + + Model::unguard(); + + $companies = Company::all(); + + foreach ($companies as $company) { + $invoices = Invoice::where('company_id', $company->id)->get(); + + foreach ($invoices as $invoice) { + $invoice_items = InvoiceItem::where('invoice_id', $invoice->id)->get(); + + $taxes = []; + $tax_total = 0; + $sub_total = 0; + + foreach ($invoice_items as $invoice_item) { + unset($tax_object); + $invoice_item->total = $invoice_item->price * $invoice_item->quantity; + + $invoice_item->update(); + + if (!empty($invoice_item->tax_id)) { + $tax_object = Tax::find($invoice_item->tax_id); + } + + if (isset($tax_object)) { + if (array_key_exists($invoice_item->tax_id, $taxes)) { + $taxes[$invoice_item->tax_id]['amount'] += $invoice_item->tax; + } else { + $taxes[$invoice_item->tax_id] = [ + 'name' => $tax_object->name, + 'amount' => $invoice_item->tax + ]; + } + } + + $tax_total += $invoice_item->tax; + $sub_total += $invoice_item->price * $invoice_item->quantity; + } + + $invoice->amount = $sub_total + $tax_total; + + $invoice->update(); + + // Added invoice total sub total + $invoice_sub_total = [ + 'company_id' => $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' => $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' => $company->id, + 'invoice_id' => $invoice->id, + 'code' => 'total', + 'name' => 'invoices.total', + 'amount' => $sub_total + $tax_total, + 'sort_order' => $sort_order, + ]; + + InvoiceTotal::create($invoice_total); + } + } + + Model::reguard(); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('invoice_totals'); + } +} diff --git a/resources/views/expenses/bills/bill.blade.php b/resources/views/expenses/bills/bill.blade.php index 20d7d54dc..e4ff5bbe1 100644 --- a/resources/views/expenses/bills/bill.blade.php +++ b/resources/views/expenses/bills/bill.blade.php @@ -112,26 +112,15 @@
+
+ @foreach($invoice->totals as $total) - - - - - - - - @if($bill->paid) - - - - - @endif - - - + + + @endforeach
{{ trans('bills.sub_total') }}:@money($bill->sub_total, $bill->currency_code, true)
{{ trans('bills.tax_total') }}:@money($bill->tax_total, $bill->currency_code, true)
{{ trans('bills.paid') }}:@money('-' . $bill->paid, $bill->currency_code, true)
{{ trans('bills.total') }}:@money($bill->grand_total, $bill->currency_code, true){{ trans($total['name']) }}:@money($total->amount, $invoice->currency_code, true)
diff --git a/resources/views/expenses/bills/show.blade.php b/resources/views/expenses/bills/show.blade.php index 3de375ba2..4311a1ceb 100644 --- a/resources/views/expenses/bills/show.blade.php +++ b/resources/views/expenses/bills/show.blade.php @@ -95,7 +95,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 @@ -117,24 +117,25 @@
- - - - - - - - - @if($bill->paid) - - - - + @foreach($invoice->totals as $total) + @if($total->code != 'total') + + + + + @else + @if ($invoice->paid) + + + + + @endif + + + + @endif - - - - + @endforeach
{{ trans('bills.sub_total') }}:@money($bill->sub_total, $bill->currency_code, true)
{{ trans('bills.tax_total') }}:@money($bill->tax_total, $bill->currency_code, true)
{{ trans('bills.paid') }}:@money('-' . $bill->paid, $bill->currency_code, true)
{{ trans($total['name']) }}:@money($total->amount, $invoice->currency_code, true)
{{ trans('invoices.paid') }}:@money('-' . $invoice->paid, $invoice->currency_code, true)
{{ trans($total['name']) }}:@money($total->amount - $invoice->paid, $invoice->currency_code, true)
{{ trans('bills.total') }}:@money($bill->grand_total, $bill->currency_code, true)
diff --git a/resources/views/incomes/invoices/invoice.blade.php b/resources/views/incomes/invoices/invoice.blade.php index 5c4bcccc9..40a54c4b5 100644 --- a/resources/views/incomes/invoices/invoice.blade.php +++ b/resources/views/incomes/invoices/invoice.blade.php @@ -114,24 +114,12 @@
+ @foreach($invoice->totals as $total) - - - - - - - - @if($invoice->paid) - - - - - @endif - - - + + + @endforeach
{{ trans('invoices.sub_total') }}:@money($invoice->sub_total, $invoice->currency_code, true)
{{ trans('invoices.tax_total') }}:@money($invoice->tax_total, $invoice->currency_code, true)
{{ trans('invoices.paid') }}:@money('-' . $invoice->paid, $invoice->currency_code, true)
{{ trans('invoices.total') }}:@money($invoice->grand_total, $invoice->currency_code, true){{ trans($total['name']) }}:@money($total->amount, $invoice->currency_code, true)
diff --git a/resources/views/incomes/invoices/show.blade.php b/resources/views/incomes/invoices/show.blade.php index fe4fad45c..bc54be783 100644 --- a/resources/views/incomes/invoices/show.blade.php +++ b/resources/views/incomes/invoices/show.blade.php @@ -95,7 +95,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 @@ -117,24 +117,25 @@
- - - - - - - - - @if ($invoice->paid) - - - - + @foreach($invoice->totals as $total) + @if($total->code != 'total') + + + + + @else + @if ($invoice->paid) + + + + + @endif + + + + @endif - - - - + @endforeach
{{ trans('invoices.sub_total') }}:@money($invoice->sub_total, $invoice->currency_code, true)
{{ trans('invoices.tax_total') }}:@money($invoice->tax_total, $invoice->currency_code, true)
{{ trans('invoices.paid') }}:@money('-' . $invoice->paid, $invoice->currency_code, true)
{{ trans($total['name']) }}:@money($total->amount, $invoice->currency_code, true)
{{ trans('invoices.paid') }}:@money('-' . $invoice->paid, $invoice->currency_code, true)
{{ trans($total['name']) }}:@money($total->amount - $invoice->paid, $invoice->currency_code, true)
{{ trans('invoices.total') }}:@money($invoice->grand_total, $invoice->currency_code, true)