From 386c60655892c2c85e89874429636d0edc6d9711 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 7 Jul 2016 19:54:06 +0300 Subject: [PATCH] Added support for taxes to expenses --- app/Http/Controllers/ExpenseController.php | 2 + app/Models/Expense.php | 4 ++ app/Models/TaxRate.php | 9 +++ resources/lang/en/texts.php | 2 + resources/views/expenses/edit.blade.php | 76 +++++++++++++++++++++- resources/views/invoices/edit.blade.php | 4 ++ 6 files changed, 96 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index d41b68c01a..425b5d4b8c 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -12,6 +12,7 @@ use App\Models\Vendor; use App\Models\Expense; use App\Models\ExpenseCategory; use App\Models\Client; +use App\Models\TaxRate; use App\Services\ExpenseService; use App\Ninja\Repositories\ExpenseRepository; use App\Http\Requests\ExpenseRequest; @@ -237,6 +238,7 @@ class ExpenseController extends BaseController 'customLabel1' => Auth::user()->account->custom_vendor_label1, 'customLabel2' => Auth::user()->account->custom_vendor_label2, 'categories' => ExpenseCategory::whereAccountId(Auth::user()->account_id)->orderBy('name')->get(), + 'taxRates' => TaxRate::scope()->orderBy('name')->get(), ]; } diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 3b737ca92c..d5ad0e0405 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -40,6 +40,10 @@ class Expense extends EntityModel 'bank_id', 'transaction_id', 'expense_category_id', + 'tax_rate1', + 'tax_name1', + 'tax_rate2', + 'tax_name2', ]; /** diff --git a/app/Models/TaxRate.php b/app/Models/TaxRate.php index a69b6a5637..f041c735d9 100644 --- a/app/Models/TaxRate.php +++ b/app/Models/TaxRate.php @@ -36,4 +36,13 @@ class TaxRate extends EntityModel { return $this->belongsTo('App\Models\User')->withTrashed(); } + + /** + * @return bool|string + */ + public function __toString() + { + return sprintf('%s: %s%%', $this->name, $this->rate); + } + } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 43c4413ef6..0e6d051fd1 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2022,6 +2022,8 @@ $LANG = array( 'archived_expense_categories' => 'Successfully archived :count expense category', 'restore_expense_category' => 'Restore expense category', 'restored_expense_category' => 'Successfully restored expense category', + 'apply_taxes' => 'Apply taxes', + ); diff --git a/resources/views/expenses/edit.blade.php b/resources/views/expenses/edit.blade.php index c0d36eab7f..a15c0c349c 100644 --- a/resources/views/expenses/edit.blade.php +++ b/resources/views/expenses/edit.blade.php @@ -81,9 +81,10 @@ ->data_bind('checked: convert_currency') ->label(' ') !!} @endif -
+
+
{!! Former::select('invoice_currency_id')->addOption('','') ->label(trans('texts.invoice_currency')) @@ -106,6 +107,41 @@ ->data_bind("value: convertedAmount, enable: enableExchangeRate") ->append('') !!}
+ + + @if (!$expense || ($expense && (!$expense->tax_name1 && !$expense->tax_name2))) + {!! Former::checkbox('apply_taxes') + ->text(trans('texts.apply_taxes')) + ->data_bind('checked: apply_taxes') + ->label(' ') !!} + @endif + +
+
+ {!! Former::select('tax_select1') + ->addOption('','') + ->label(trans('texts.tax_rate')) + ->onchange('taxSelectChange(event)') + ->fromQuery($taxRates) !!} + +
+ {!! Former::input('tax_rate1') !!} + {!! Former::input('tax_name1') !!} +
+ +
+ {!! Former::select('tax_select2') + ->addOption('','') + ->label(trans('texts.tax_rate')) + ->onchange('taxSelectChange(event)') + ->fromQuery($taxRates) !!} + +
+ {!! Former::input('tax_rate2') !!} + {!! Former::input('tax_name2') !!} +
+
+
@@ -158,6 +194,7 @@ var vendors = {!! $vendors !!}; var clients = {!! $clients !!}; var categories = {!! $categories !!}; + var taxRates = {!! $taxRates !!}; var clientMap = {}; for (var i=0; itax_name1 || $expense->tax_name2)) ? 'true' : 'false' }}); self.mapping = { 'documents': { @@ -436,6 +477,39 @@ window.countUploadingDocuments--; } + function taxSelectChange(event) { + var $select = $(event.target); + var tax = $select.find('option:selected').text(); + + var index = tax.lastIndexOf(': '); + var taxName = tax.substring(0, index); + var taxRate = tax.substring(index + 2, tax.length - 1); + + var selectName = $select.attr('name'); + var instance = selectName.substring(selectName.length - 1); + + $('#tax_name' + instance).val(taxName); + $('#tax_rate' + instance).val(taxRate); + } + + function setTaxSelect(instance) { + var $select = $('#tax_select' + instance); + var taxName = $('#tax_name' + instance).val(); + var taxRate = $('#tax_rate' + instance).val(); + if (!taxRate || !taxName) { + return; + } + var tax = _.findWhere(taxRates, {name:taxName, rate:taxRate}); + if (tax) { + $select.val(tax.public_id); + } else { + var option = new Option(taxName + ': ' + taxRate + '%', ''); + option.selected = true; + $select.append(option); + } + } + + @stop diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index e5f78562e7..7314d0b3f7 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -867,6 +867,10 @@ item.qty(1); item.expense_public_id(expense.public_id()); item.cost(expense.converted_amount()); + item.tax_rate1(expense.tax_rate1()); + item.tax_name1(expense.tax_name1()); + item.tax_rate2(expense.tax_rate2()); + item.tax_name2(expense.tax_name2()); } model.invoice().invoice_items.push(blank); model.invoice().has_expenses(true);