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) {
$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');
}

View File

@ -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();

View File

@ -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;

View File

@ -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);
}
}
],

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',
'all_pages_header' => 'Show header on',
'all_pages_footer' => 'Show footer on',
'invoice_currency' => 'Invoice Currency',
);

View File

@ -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('<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')
->addOption('', '')
->label(trans('texts.client'))
@ -54,15 +60,17 @@
@endif
<span style="display:none" data-bind="visible: !client_id()">
{!! 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') !!}
</span>
<span style="display:none;" data-bind="visible: client_id">
{!! Former::plaintext('test')
->value('<span data-bind="html: currencyName"></span>')
->value('<span data-bind="html: invoiceCurrencyName"></span>')
->style('min-height:46px')
->label(trans('texts.currency_id')) !!}
->label(trans('texts.invoice_currency')) !!}
</span>
{!! Former::text('exchange_rate')
@ -71,13 +79,13 @@
{!! Former::text('invoice_amount')
->addGroupClass('converted-amount')
->data_bind("value: convertedAmount, enable: enableExchangeRate")
->append('<span data-bind="html: currencyCode"></span>') !!}
->append('<span data-bind="html: invoiceCurrencyCode"></span>') !!}
</div>
<div class="col-md-6">
{!! 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') !!}
</div>
</div>
</div>
@ -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;
})
};