Enabled setting expense currency

This commit is contained in:
Hillel Coren 2016-02-02 00:07:09 +02:00
parent 1a2c2712d0
commit 1284c29534
7 changed files with 103 additions and 44 deletions

View File

@ -200,8 +200,8 @@ class ExpenseController extends BaseController
} }
if (!$currencyId) { if (!$currencyId) {
$currencyId = $expense->getCurrencyId(); $currencyId = $expense->invoice_currency_id;
} elseif ($currencyId != $expense->getCurrencyId() && $expense->getCurrencyId()) { } elseif ($currencyId != $expense->invoice_currency_id && $expense->invoice_currency_id) {
Session::flash('error', trans('texts.expense_error_multiple_currencies')); Session::flash('error', trans('texts.expense_error_multiple_currencies'));
return Redirect::to('expenses'); return Redirect::to('expenses');
} }

View File

@ -18,7 +18,8 @@ class Expense extends EntityModel
protected $fillable = [ protected $fillable = [
'client_id', 'client_id',
'vendor_id', 'vendor_id',
'currency_id', 'expense_currency_id',
'invoice_currency_id',
'amount', 'amount',
'foreign_amount', 'foreign_amount',
'exchange_rate', 'exchange_rate',
@ -60,11 +61,6 @@ class Expense extends EntityModel
return $this->public_id; return $this->public_id;
} }
public function getCurrencyId()
{
return $this->client ? $this->client->currency_id : $this->currency_id;
}
public function getDisplayName() public function getDisplayName()
{ {
return $this->getName(); return $this->getName();

View File

@ -64,7 +64,6 @@ class ExpenseRepository extends BaseRepository
->orWhere('contacts.is_primary', '=', null); ->orWhere('contacts.is_primary', '=', null);
}) })
->select( ->select(
DB::raw('COALESCE(clients.currency_id, expenses.currency_id, accounts.currency_id) currency_id'),
'expenses.account_id', 'expenses.account_id',
'expenses.amount', 'expenses.amount',
'expenses.deleted_at', 'expenses.deleted_at',
@ -78,9 +77,9 @@ class ExpenseRepository extends BaseRepository
'expenses.public_notes', 'expenses.public_notes',
'expenses.should_be_invoiced', 'expenses.should_be_invoiced',
'expenses.vendor_id', 'expenses.vendor_id',
'expenses.expense_currency_id',
'expenses.invoice_currency_id',
'invoices.public_id as invoice_public_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.name as vendor_name',
'vendors.public_id as vendor_public_id', 'vendors.public_id as vendor_public_id',
'clients.name as client_name', 'clients.name as client_name',
@ -129,8 +128,11 @@ class ExpenseRepository extends BaseRepository
$expense->public_notes = trim($input['public_notes']); $expense->public_notes = trim($input['public_notes']);
$expense->should_be_invoiced = isset($input['should_be_invoiced']) || $expense->client_id ? true : false; $expense->should_be_invoiced = isset($input['should_be_invoiced']) || $expense->client_id ? true : false;
if (! $expense->currency_id) { if ( ! $expense->expense_currency_id) {
$expense->currency_id = \Auth::user()->account->getCurrencyId(); $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; $rate = isset($input['exchange_rate']) ? Utils::parseFloat($input['exchange_rate']) : 1;

View File

@ -92,10 +92,10 @@ class ExpenseService extends BaseService
// show both the amount and the converted amount // show both the amount and the converted amount
if ($model->exchange_rate != 1) { if ($model->exchange_rate != 1) {
$converted = round($model->amount * $model->exchange_rate, 2); $converted = round($model->amount * $model->exchange_rate, 2);
return Utils::formatMoney($model->amount, $model->account_currency_id, $model->account_country_id) . ' | ' . return Utils::formatMoney($model->amount, $model->expense_currency_id) . ' | ' .
Utils::formatMoney($converted, $model->currency_id, $model->client_country_id); Utils::formatMoney($converted, $model->invoice_currency_id);
} else { } else {
return Utils::formatMoney($model->amount, $model->currency_id, $model->account_country_id); return Utils::formatMoney($model->amount, $model->expense_currency_id);
} }
} }
], ],

View File

@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddSourceCurrencyToExpenses extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('expenses', function (Blueprint $table) {
$table->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');
});
}
}

View File

@ -1140,5 +1140,6 @@ return array(
'last_page' => 'last page', 'last_page' => 'last page',
'all_pages_header' => 'Show header on', 'all_pages_header' => 'Show header on',
'all_pages_footer' => 'Show footer on', 'all_pages_footer' => 'Show footer on',
'invoice_currency' => 'Invoice Currency',
); );

View File

@ -28,18 +28,24 @@
->label(trans('texts.vendor')) ->label(trans('texts.vendor'))
->addGroupClass('vendor-select') !!} ->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') {!! Former::text('expense_date')
->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT)) ->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT, DEFAULT_DATE_PICKER_FORMAT))
->addGroupClass('expense_date') ->addGroupClass('expense_date')
->label(trans('texts.date')) ->label(trans('texts.date'))
->append('<i class="glyphicon glyphicon-calendar"></i>') !!} ->append('<i class="glyphicon glyphicon-calendar"></i>') !!}
{!! 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('<span data-bind="html: expenseCurrencyCode"></span>') !!}
{!! Former::select('client_id') {!! Former::select('client_id')
->addOption('', '') ->addOption('', '')
->label(trans('texts.client')) ->label(trans('texts.client'))
@ -54,15 +60,17 @@
@endif @endif
<span style="display:none" data-bind="visible: !client_id()"> <span style="display:none" data-bind="visible: !client_id()">
{!! Former::select('currency_id')->addOption('','') {!! Former::select('invoice_currency_id')->addOption('','')
->data_bind('combobox: currency_id, disable: true') ->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') !!} ->fromQuery($currencies, 'name', 'id') !!}
</span> </span>
<span style="display:none;" data-bind="visible: client_id"> <span style="display:none;" data-bind="visible: client_id">
{!! Former::plaintext('test') {!! Former::plaintext('test')
->value('<span data-bind="html: currencyName"></span>') ->value('<span data-bind="html: invoiceCurrencyName"></span>')
->style('min-height:46px') ->style('min-height:46px')
->label(trans('texts.currency_id')) !!} ->label(trans('texts.invoice_currency')) !!}
</span> </span>
{!! Former::text('exchange_rate') {!! Former::text('exchange_rate')
@ -71,13 +79,13 @@
{!! Former::text('invoice_amount') {!! Former::text('invoice_amount')
->addGroupClass('converted-amount') ->addGroupClass('converted-amount')
->data_bind("value: convertedAmount, enable: enableExchangeRate") ->data_bind("value: convertedAmount, enable: enableExchangeRate")
->append('<span data-bind="html: currencyCode"></span>') !!} ->append('<span data-bind="html: invoiceCurrencyCode"></span>') !!}
</div> </div>
<div class="col-md-6"> <div class="col-md-6">
{!! Former::textarea('public_notes')->rows(9) !!} {!! Former::textarea('public_notes')->style('height:255px') !!}
{!! Former::textarea('private_notes')->rows(9) !!} {!! Former::textarea('private_notes')->style('height:255px') !!}
</div> </div>
</div> </div>
</div> </div>
@ -111,7 +119,7 @@
var clientId = $('select#client_id').val(); var clientId = $('select#client_id').val();
var client = clientMap[clientId]; var client = clientMap[clientId];
if (client) { if (client) {
model.currency_id(client.currency_id); model.invoice_currency_id(client.currency_id);
} }
} }
@ -174,7 +182,8 @@
var ViewModel = function(data) { var ViewModel = function(data) {
var self = this; 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.amount = ko.observable();
self.exchange_rate = ko.observable(1); self.exchange_rate = ko.observable(1);
self.should_be_invoiced = ko.observable(); self.should_be_invoiced = ko.observable();
@ -196,23 +205,27 @@
} }
}, self); }, self);
self.currencyCode = ko.computed(function() {
var currencyId = self.currency_id() || self.account_currency_id(); self.getCurrency = function(currencyId) {
var currency = currencyMap[currencyId]; return currencyMap[currencyId || self.account_currency_id()];
return currency.code; };
self.expenseCurrencyCode = ko.computed(function() {
return self.getCurrency(self.expense_currency_id()).code;
}); });
self.currencyName = ko.computed(function() { self.invoiceCurrencyCode = ko.computed(function() {
var currencyId = self.currency_id() || self.account_currency_id(); return self.getCurrency(self.invoice_currency_id()).code;
var currency = currencyMap[currencyId]; });
return currency.name;
self.invoiceCurrencyName = ko.computed(function() {
return self.getCurrency(self.invoice_currency_id()).name;
}); });
self.enableExchangeRate = ko.computed(function() { self.enableExchangeRate = ko.computed(function() {
if (!self.currency_id()) { var expenseCurrencyId = self.expense_currency_id() || self.account_currency_id();
return false; var invoiceCurrencyId = self.invoice_currency_id() || self.account_currency_id();
} return expenseCurrencyId != invoiceCurrencyId;
return self.currency_id() != self.account_currency_id();
}) })
}; };