diff --git a/app/Http/Controllers/ExpenseController.php b/app/Http/Controllers/ExpenseController.php index bb2a81fbba21..6f2a6e884a30 100644 --- a/app/Http/Controllers/ExpenseController.php +++ b/app/Http/Controllers/ExpenseController.php @@ -200,8 +200,8 @@ class ExpenseController extends BaseController } if (!$currencyId) { - $currencyId = $expense->getCurrencyId(); - } elseif ($currencyId != $expense->getCurrencyId() && $expense->getCurrencyId()) { + $currencyId = $expense->invoice_currency_id; + } elseif ($currencyId != $expense->invoice_currency_id && $expense->invoice_currency_id) { Session::flash('error', trans('texts.expense_error_multiple_currencies')); return Redirect::to('expenses'); } diff --git a/app/Models/Expense.php b/app/Models/Expense.php index b4e1060052df..9fbd5a485c7d 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -18,7 +18,8 @@ class Expense extends EntityModel protected $fillable = [ 'client_id', 'vendor_id', - 'currency_id', + 'expense_currency_id', + 'invoice_currency_id', 'amount', 'foreign_amount', 'exchange_rate', @@ -60,11 +61,6 @@ class Expense extends EntityModel return $this->public_id; } - public function getCurrencyId() - { - return $this->client ? $this->client->currency_id : $this->currency_id; - } - public function getDisplayName() { return $this->getName(); diff --git a/app/Ninja/Repositories/ExpenseRepository.php b/app/Ninja/Repositories/ExpenseRepository.php index fd2f78afc7de..442df6ef2979 100644 --- a/app/Ninja/Repositories/ExpenseRepository.php +++ b/app/Ninja/Repositories/ExpenseRepository.php @@ -64,7 +64,6 @@ class ExpenseRepository extends BaseRepository ->orWhere('contacts.is_primary', '=', null); }) ->select( - DB::raw('COALESCE(clients.currency_id, expenses.currency_id, accounts.currency_id) currency_id'), 'expenses.account_id', 'expenses.amount', 'expenses.deleted_at', @@ -78,9 +77,9 @@ class ExpenseRepository extends BaseRepository 'expenses.public_notes', 'expenses.should_be_invoiced', 'expenses.vendor_id', + 'expenses.expense_currency_id', + 'expenses.invoice_currency_id', 'invoices.public_id as invoice_public_id', - 'accounts.country_id as account_country_id', - 'accounts.currency_id as account_currency_id', 'vendors.name as vendor_name', 'vendors.public_id as vendor_public_id', 'clients.name as client_name', @@ -129,8 +128,11 @@ class ExpenseRepository extends BaseRepository $expense->public_notes = trim($input['public_notes']); $expense->should_be_invoiced = isset($input['should_be_invoiced']) || $expense->client_id ? true : false; - if (! $expense->currency_id) { - $expense->currency_id = \Auth::user()->account->getCurrencyId(); + if ( ! $expense->expense_currency_id) { + $expense->expense_currency_id = \Auth::user()->account->getCurrencyId(); + } + if ( ! $expense->invoice_currency_id) { + $expense->invoice_currency_id = \Auth::user()->account->getCurrencyId(); } $rate = isset($input['exchange_rate']) ? Utils::parseFloat($input['exchange_rate']) : 1; diff --git a/app/Services/ExpenseService.php b/app/Services/ExpenseService.php index 874c17ea71e3..491c0e7957d8 100644 --- a/app/Services/ExpenseService.php +++ b/app/Services/ExpenseService.php @@ -92,10 +92,10 @@ class ExpenseService extends BaseService // show both the amount and the converted amount if ($model->exchange_rate != 1) { $converted = round($model->amount * $model->exchange_rate, 2); - return Utils::formatMoney($model->amount, $model->account_currency_id, $model->account_country_id) . ' | ' . - Utils::formatMoney($converted, $model->currency_id, $model->client_country_id); + return Utils::formatMoney($model->amount, $model->expense_currency_id) . ' | ' . + Utils::formatMoney($converted, $model->invoice_currency_id); } else { - return Utils::formatMoney($model->amount, $model->currency_id, $model->account_country_id); + return Utils::formatMoney($model->amount, $model->expense_currency_id); } } ], diff --git a/database/migrations/2016_02_01_135956_add_source_currency_to_expenses.php b/database/migrations/2016_02_01_135956_add_source_currency_to_expenses.php new file mode 100644 index 000000000000..bdce9926eac7 --- /dev/null +++ b/database/migrations/2016_02_01_135956_add_source_currency_to_expenses.php @@ -0,0 +1,47 @@ +dropColumn('foreign_amount'); + + $table->unsignedInteger('currency_id')->nullable(false)->change(); + $table->renameColumn('currency_id', 'invoice_currency_id'); + $table->unsignedInteger('expense_currency_id'); + + // set account value so we're able to create foreign constraint + DB::statement('update expenses e + left join accounts a on a.id = e.account_id + set e.expense_currency_id = COALESCE(a.currency_id, 1)'); + + $table->foreign('invoice_currency_id')->references('id')->on('currencies'); + $table->foreign('expense_currency_id')->references('id')->on('currencies'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('expenses', function($table) { + + $table->dropColumn('expense_currency_id'); + $table->renameColumn('invoice_currency_id', 'currency_id'); + $table->renameColumn('invoice_amount', 'foreign_amount'); + }); + } +} diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 76129376c421..76781a31b717 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -1140,5 +1140,6 @@ return array( 'last_page' => 'last page', 'all_pages_header' => 'Show header on', 'all_pages_footer' => 'Show footer on', - + 'invoice_currency' => 'Invoice Currency', + ); diff --git a/resources/views/expenses/edit.blade.php b/resources/views/expenses/edit.blade.php index 4436416a1e49..934d339db21d 100644 --- a/resources/views/expenses/edit.blade.php +++ b/resources/views/expenses/edit.blade.php @@ -28,18 +28,24 @@ ->label(trans('texts.vendor')) ->addGroupClass('vendor-select') !!} - {!! Former::text('amount') - ->label(trans('texts.amount')) - ->data_bind("value: amount, valueUpdate: 'afterkeydown'") - ->addGroupClass('amount') - ->append($account->present()->currencyCode) !!} - {!! Former::text('expense_date') ->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT)) ->addGroupClass('expense_date') ->label(trans('texts.date')) ->append('') !!} + {!! Former::select('expense_currency_id')->addOption('','') + ->data_bind('combobox: expense_currency_id') + ->label(trans('texts.currency_id')) + ->data_placeholder(Utils::getFromCache($account->getCurrencyId(), 'currencies')->name) + ->fromQuery($currencies, 'name', 'id') !!} + + {!! Former::text('amount') + ->label(trans('texts.amount')) + ->data_bind("value: amount, valueUpdate: 'afterkeydown'") + ->addGroupClass('amount') + ->append('') !!} + {!! Former::select('client_id') ->addOption('', '') ->label(trans('texts.client')) @@ -54,15 +60,17 @@ @endif - {!! Former::select('currency_id')->addOption('','') - ->data_bind('combobox: currency_id, disable: true') + {!! Former::select('invoice_currency_id')->addOption('','') + ->label(trans('texts.invoice_currency')) + ->data_placeholder(Utils::getFromCache($account->getCurrencyId(), 'currencies')->name) + ->data_bind('combobox: invoice_currency_id, disable: true') ->fromQuery($currencies, 'name', 'id') !!} {!! Former::plaintext('test') - ->value('') + ->value('') ->style('min-height:46px') - ->label(trans('texts.currency_id')) !!} + ->label(trans('texts.invoice_currency')) !!} {!! Former::text('exchange_rate') @@ -71,13 +79,13 @@ {!! Former::text('invoice_amount') ->addGroupClass('converted-amount') ->data_bind("value: convertedAmount, enable: enableExchangeRate") - ->append('') !!} + ->append('') !!}
- {!! Former::textarea('public_notes')->rows(9) !!} - {!! Former::textarea('private_notes')->rows(9) !!} + {!! Former::textarea('public_notes')->style('height:255px') !!} + {!! Former::textarea('private_notes')->style('height:255px') !!}
@@ -111,7 +119,7 @@ var clientId = $('select#client_id').val(); var client = clientMap[clientId]; if (client) { - model.currency_id(client.currency_id); + model.invoice_currency_id(client.currency_id); } } @@ -174,7 +182,8 @@ var ViewModel = function(data) { var self = this; - self.currency_id = ko.observable(); + self.expense_currency_id = ko.observable(); + self.invoice_currency_id = ko.observable(); self.amount = ko.observable(); self.exchange_rate = ko.observable(1); self.should_be_invoiced = ko.observable(); @@ -196,23 +205,27 @@ } }, self); - self.currencyCode = ko.computed(function() { - var currencyId = self.currency_id() || self.account_currency_id(); - var currency = currencyMap[currencyId]; - return currency.code; + + self.getCurrency = function(currencyId) { + return currencyMap[currencyId || self.account_currency_id()]; + }; + + self.expenseCurrencyCode = ko.computed(function() { + return self.getCurrency(self.expense_currency_id()).code; }); - self.currencyName = ko.computed(function() { - var currencyId = self.currency_id() || self.account_currency_id(); - var currency = currencyMap[currencyId]; - return currency.name; + self.invoiceCurrencyCode = ko.computed(function() { + return self.getCurrency(self.invoice_currency_id()).code; + }); + + self.invoiceCurrencyName = ko.computed(function() { + return self.getCurrency(self.invoice_currency_id()).name; }); self.enableExchangeRate = ko.computed(function() { - if (!self.currency_id()) { - return false; - } - return self.currency_id() != self.account_currency_id(); + var expenseCurrencyId = self.expense_currency_id() || self.account_currency_id(); + var invoiceCurrencyId = self.invoice_currency_id() || self.account_currency_id(); + return expenseCurrencyId != invoiceCurrencyId; }) };