mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-18 02:03:40 +00:00
Clean up and consistency in foreign and native amounts.
This commit is contained in:
@@ -250,8 +250,8 @@ class SingleController extends Controller
|
|||||||
'source_account_name' => $sourceAccounts->first()->edit_name,
|
'source_account_name' => $sourceAccounts->first()->edit_name,
|
||||||
'destination_account_id' => $destinationAccounts->first()->id,
|
'destination_account_id' => $destinationAccounts->first()->id,
|
||||||
'destination_account_name' => $destinationAccounts->first()->edit_name,
|
'destination_account_name' => $destinationAccounts->first()->edit_name,
|
||||||
'amount' => $journal->amountPositive(),
|
'amount' => $journal->amountPositive(),
|
||||||
'currency' => $journal->transactionCurrency,
|
'currency' => $journal->transactionCurrency,
|
||||||
|
|
||||||
// new custom fields:
|
// new custom fields:
|
||||||
'due_date' => $journal->dateAsString('due_date'),
|
'due_date' => $journal->dateAsString('due_date'),
|
||||||
@@ -261,16 +261,16 @@ class SingleController extends Controller
|
|||||||
'notes' => $journal->getMeta('notes'),
|
'notes' => $journal->getMeta('notes'),
|
||||||
|
|
||||||
// exchange rate fields
|
// exchange rate fields
|
||||||
'exchanged_amount' => $journal->amountPositive(),
|
'native_amount' => $journal->amountPositive(),
|
||||||
'exchanged_currency' => $journal->transactionCurrency,
|
'native_currency' => $journal->transactionCurrency,
|
||||||
];
|
];
|
||||||
|
|
||||||
// catch possibly exchanged currencies and what-not.
|
// if user has entered a foreign currency, update some fields
|
||||||
$originalCurrencyId = intval($journal->getMeta('original_currency_id'));
|
$foreignCurrencyId = intval($journal->getMeta('foreign_currency_id'));
|
||||||
if ($originalCurrencyId > 0) {
|
if ($foreignCurrencyId > 0) {
|
||||||
// update some fields in pre-filled.
|
// update some fields in pre-filled.
|
||||||
$preFilled['amount'] = $journal->getMeta('original_amount');
|
$preFilled['amount'] = $journal->getMeta('foreign_amount');
|
||||||
$preFilled['currency'] = $this->currency->find(intval($journal->getMeta('original_currency_id')));
|
$preFilled['currency'] = $this->currency->find(intval($journal->getMeta('foreign_currency_id')));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($journal->isWithdrawal() && $destinationAccounts->first()->accountType->type == AccountType::CASH) {
|
if ($journal->isWithdrawal() && $destinationAccounts->first()->accountType->type == AccountType::CASH) {
|
||||||
|
@@ -182,15 +182,15 @@ class TransactionController extends Controller
|
|||||||
$transactions = $tasker->getTransactionsOverview($journal);
|
$transactions = $tasker->getTransactionsOverview($journal);
|
||||||
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
|
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
|
||||||
$subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"';
|
$subTitle = trans('firefly.' . $what) . ' "' . e($journal->description) . '"';
|
||||||
$originalCurrency = null;
|
$foreignCurrency = null;
|
||||||
|
|
||||||
if ($journal->hasMeta('original_currency_id')) {
|
if ($journal->hasMeta('foreign_currency_id')) {
|
||||||
/** @var CurrencyRepositoryInterface $repository */
|
/** @var CurrencyRepositoryInterface $repository */
|
||||||
$repository = app(CurrencyRepositoryInterface::class);
|
$repository = app(CurrencyRepositoryInterface::class);
|
||||||
$originalCurrency = $repository->find(intval($journal->getMeta('original_currency_id')));
|
$foreignCurrency = $repository->find(intval($journal->getMeta('foreign_currency_id')));
|
||||||
}
|
}
|
||||||
|
|
||||||
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'originalCurrency'));
|
return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'foreignCurrency'));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -67,8 +67,8 @@ class JournalFormRequest extends Request
|
|||||||
'destination_account_name' => $this->string('destination_account_name'),
|
'destination_account_name' => $this->string('destination_account_name'),
|
||||||
'piggy_bank_id' => $this->integer('piggy_bank_id'),
|
'piggy_bank_id' => $this->integer('piggy_bank_id'),
|
||||||
|
|
||||||
// amount for exchanged data:
|
// native amount
|
||||||
'exchanged_amount' => $this->float('exchanged_amount'),
|
'native_amount' => $this->float('native_amount'),
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ class JournalFormRequest extends Request
|
|||||||
'piggy_bank_id' => 'between:1,255',
|
'piggy_bank_id' => 'between:1,255',
|
||||||
|
|
||||||
// exchange rate data:
|
// exchange rate data:
|
||||||
'exchanged_amount' => 'numeric|required|more:0',
|
'native_amount' => 'numeric|more:0',
|
||||||
];
|
];
|
||||||
|
|
||||||
// some rules get an upgrade depending on the type of data:
|
// some rules get an upgrade depending on the type of data:
|
||||||
|
@@ -42,8 +42,8 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
private $validMetaFields
|
private $validMetaFields
|
||||||
= ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes', 'original_amount',
|
= ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes', 'foreign_amount',
|
||||||
'original_currency_id',
|
'foreign_currency_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,36 +171,10 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
/** @var TransactionType $transactionType */
|
/** @var TransactionType $transactionType */
|
||||||
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
|
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
|
||||||
$accounts = $this->storeAccounts($transactionType, $data);
|
$accounts = $this->storeAccounts($transactionType, $data);
|
||||||
|
$data = $this->verifyNativeAmount($data, $accounts);
|
||||||
$currencyId = $data['currency_id'];
|
$currencyId = $data['currency_id'];
|
||||||
$amount = strval($data['amount']);
|
$amount = strval($data['amount']);
|
||||||
// switch type to find what account to verify for currency stuff
|
$journal = new TransactionJournal(
|
||||||
switch ($transactionType->type) {
|
|
||||||
case TransactionType::WITHDRAWAL:
|
|
||||||
/*
|
|
||||||
* Overrule the currency selection and the amount:
|
|
||||||
*/
|
|
||||||
$accountCurrencyId = intval($accounts['source']->getMeta('currency_id'));
|
|
||||||
if ($accountCurrencyId !== $currencyId) {
|
|
||||||
$data['original_amount'] = $data['amount'];
|
|
||||||
$data['original_currency_id'] = $currencyId;
|
|
||||||
$currencyId = $accountCurrencyId;
|
|
||||||
$amount = strval($data['exchanged_amount']);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case TransactionType::DEPOSIT:
|
|
||||||
$accountCurrencyId = intval($accounts['destination']->getMeta('currency_id'));
|
|
||||||
if ($accountCurrencyId !== $currencyId) {
|
|
||||||
$data['original_amount'] = $data['amount'];
|
|
||||||
$data['original_currency_id'] = $currencyId;
|
|
||||||
$currencyId = $accountCurrencyId;
|
|
||||||
$amount = strval($data['exchanged_amount']);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new FireflyException(sprintf('Currency exchange routine cannot handle %s', $transactionType->type));
|
|
||||||
}
|
|
||||||
|
|
||||||
$journal = new TransactionJournal(
|
|
||||||
[
|
[
|
||||||
'user_id' => $this->user->id,
|
'user_id' => $this->user->id,
|
||||||
'transaction_type_id' => $transactionType->id,
|
'transaction_type_id' => $transactionType->id,
|
||||||
@@ -788,4 +762,47 @@ class JournalRepository implements JournalRepositoryInterface
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method checks the data array and the given accounts to verify that the native amount, currency
|
||||||
|
* and possible the foreign currency and amount are properly saved.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @param array $accounts
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function verifyNativeAmount(array $data, array $accounts): array
|
||||||
|
{
|
||||||
|
/** @var TransactionType $transactionType */
|
||||||
|
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
|
||||||
|
$submittedCurrencyId = $data['currency_id'];
|
||||||
|
$amount = strval($data['amount']);
|
||||||
|
|
||||||
|
// which account to check for what the native currency is?
|
||||||
|
$check = 'source';
|
||||||
|
if ($transactionType->type === TransactionType::DEPOSIT) {
|
||||||
|
$check = 'destination';
|
||||||
|
}
|
||||||
|
if ($transactionType->type === TransactionType::TRANSFER) {
|
||||||
|
throw new FireflyException('Cannot handle transfers in verifyNativeAmount()');
|
||||||
|
}
|
||||||
|
|
||||||
|
// continue:
|
||||||
|
$nativeCurrencyId = intval($accounts[$check]->getMeta('currency_id'));
|
||||||
|
|
||||||
|
// does not match? Then user has submitted amount in a foreign currency:
|
||||||
|
if ($nativeCurrencyId !== $submittedCurrencyId) {
|
||||||
|
// store amount and submitted currency in "foreign currency" fields:
|
||||||
|
$data['foreign_amount'] = $data['amount'];
|
||||||
|
$data['foreign_currency_id'] = $submittedCurrencyId;
|
||||||
|
|
||||||
|
// overrule the amount and currency ID fields to be the original again:
|
||||||
|
$data['amount'] = strval($data['native_amount']);
|
||||||
|
$data['currency_id'] = $nativeCurrencyId;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -11,77 +11,106 @@
|
|||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
// respond to switch buttons
|
// respond to switch buttons (first time always triggers)
|
||||||
updateButtons();
|
updateButtons();
|
||||||
updateForm();
|
updateForm();
|
||||||
updateLayout();
|
updateLayout();
|
||||||
updateDescription();
|
updateDescription();
|
||||||
|
runModernizer();
|
||||||
|
updateNativeCurrency(); // verify native currency by first account (may be different).
|
||||||
|
|
||||||
// hide exchange rate instructions:
|
// hide ALL exchange things
|
||||||
$('#exchange_rate_instruction_holder').hide();
|
$('#exchange_rate_instruction_holder').hide();
|
||||||
$('#exchanged_amount_holder').hide();
|
$('#native_amount_holder').hide();
|
||||||
|
|
||||||
if (!Modernizr.inputtypes.date) {
|
// when user changes source account, native currency may be different.
|
||||||
$('input[type="date"]').datepicker(
|
$('select[name="source_account_id"]').on('change', updateNativeCurrency);
|
||||||
{
|
|
||||||
dateFormat: 'yy-mm-dd'
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// update currency
|
// convert foreign currency to native currency.
|
||||||
$('select[name="source_account_id"]').on('change', updateCurrency);
|
$('#ffInput_amount').on('change', convertForeignToNative);
|
||||||
updateCurrency();
|
|
||||||
$('#ffInput_amount').on('change', getExchangeRate);
|
|
||||||
|
|
||||||
// respond to changes to the hidden input,
|
// when user selects different currency,
|
||||||
// so we can show the "exchange rate" thing if necessary:
|
$('.currency-option').on('click', selectsForeignCurrency);
|
||||||
$('.currency-option').on('click', triggerCurrencyChange);
|
|
||||||
|
|
||||||
// get JSON things:
|
// get JSON things:
|
||||||
getJSONautocomplete();
|
getJSONautocomplete();
|
||||||
});
|
});
|
||||||
|
|
||||||
function getExchangeRate() {
|
/**
|
||||||
|
* Converts any foreign amount to the native currency.
|
||||||
|
*/
|
||||||
|
function convertForeignToNative() {
|
||||||
var accountId = getAccountId();
|
var accountId = getAccountId();
|
||||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
var nativeCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
||||||
var selectedCurrencyCode = currencyInfo[selectedCurrencyId].code;
|
var foreignCurrencyCode = currencyInfo[foreignCurrencyId].code;
|
||||||
var accountCurrencyCode = currencyInfo[accountCurrencyId].code;
|
var nativeCurrencyCode = currencyInfo[nativeCurrencyId].code;
|
||||||
var date = $('#ffInput_date').val();
|
var date = $('#ffInput_date').val();
|
||||||
var amount = $('#ffInput_amount').val();
|
var amount = $('#ffInput_amount').val();
|
||||||
var uri = 'json/rate/' + selectedCurrencyCode + '/' + accountCurrencyCode + '/' + date + '?amount=' + amount;
|
var uri = 'json/rate/' + foreignCurrencyCode + '/' + nativeCurrencyCode + '/' + date + '?amount=' + amount;
|
||||||
console.log('Will grab ' + uri);
|
console.log('Will grab ' + uri);
|
||||||
$.get(uri).done(updateExchangedAmount);
|
$.get(uri).done(updateNativeAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateExchangedAmount(data) {
|
/**
|
||||||
|
* Once the data has been grabbed will update the field in the form.
|
||||||
|
* @param data
|
||||||
|
*/
|
||||||
|
function updateNativeAmount(data) {
|
||||||
console.log('Returned data:');
|
console.log('Returned data:');
|
||||||
console.log(data);
|
console.log(data);
|
||||||
$('#ffInput_exchanged_amount').val(data.amount);
|
$('#ffInput_native_amount').val(data.amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This function generates a small helper text to explain the user
|
||||||
|
* that they have selected a foreign currency.
|
||||||
|
* @returns {XML|string|void}
|
||||||
|
*/
|
||||||
|
function getExchangeInstructions() {
|
||||||
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||||
|
var selectedAccountId = getAccountId();
|
||||||
|
var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency);
|
||||||
|
|
||||||
function triggerCurrencyChange() {
|
var text = exchangeRateInstructions.replace('@name', accountInfo[selectedAccountId].name);
|
||||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
text = text.replace(/@native_currency/g, currencyInfo[nativeCurrencyId].name);
|
||||||
var accountId = getAccountId();
|
text = text.replace(/@foreign_currency/g, currencyInfo[foreignCurrencyId].name);
|
||||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
return text;
|
||||||
console.log('Selected currency is ' + selectedCurrencyId);
|
}
|
||||||
console.log('Account prefers ' + accountCurrencyId);
|
|
||||||
if (selectedCurrencyId !== accountCurrencyId) {
|
|
||||||
var text = exchangeRateInstructions.replace('@name', accountInfo[accountId].name);
|
|
||||||
text = text.replace(/@account_currency/g, currencyInfo[accountCurrencyId].name);
|
|
||||||
text = text.replace(/@transaction_currency/g, currencyInfo[selectedCurrencyId].name);
|
|
||||||
$('.non-selectable-currency-symbol').text(currencyInfo[accountCurrencyId].symbol);
|
|
||||||
getExchangeRate();
|
|
||||||
|
|
||||||
$('#ffInput_exchange_rate_instruction').text(text);
|
/**
|
||||||
|
* When the user changes the currency in the amount drop down, it may jump from being
|
||||||
|
* the native currency to a foreign currency. This triggers the display of several
|
||||||
|
* information things that make sure that the user always supplies the amount in the native currency.
|
||||||
|
*
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
function selectsForeignCurrency() {
|
||||||
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||||
|
var selectedAccountId = getAccountId();
|
||||||
|
var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency);
|
||||||
|
|
||||||
|
if (foreignCurrencyId !== nativeCurrencyId) {
|
||||||
|
console.log('User has selected currency #' + foreignCurrencyId + ' and this is different from native currency #' + nativeCurrencyId);
|
||||||
|
|
||||||
|
// the input where the native amount is entered gets the symbol for the native currency:
|
||||||
|
$('.non-selectable-currency-symbol').text(currencyInfo[nativeCurrencyId].symbol);
|
||||||
|
|
||||||
|
// the instructions get updated:
|
||||||
|
$('#ffInput_exchange_rate_instruction').text(getExchangeInstructions());
|
||||||
|
|
||||||
|
// both holders are shown to the user:
|
||||||
$('#exchange_rate_instruction_holder').show();
|
$('#exchange_rate_instruction_holder').show();
|
||||||
$('#exchanged_amount_holder').show();
|
$('#native_amount_holder').show();
|
||||||
|
|
||||||
|
// if possible the amount is already exchanged for the foreign currency
|
||||||
|
convertForeignToNative();
|
||||||
|
|
||||||
}
|
}
|
||||||
if (selectedCurrencyId === accountCurrencyId) {
|
if (foreignCurrencyId === nativeCurrencyId) {
|
||||||
|
console.log('User has selected currency #' + foreignCurrencyId + ' and this is equal to native currency #' + nativeCurrencyId + ' (phew).');
|
||||||
$('#exchange_rate_instruction_holder').hide();
|
$('#exchange_rate_instruction_holder').hide();
|
||||||
$('#exchanged_amount_holder').hide();
|
$('#native_amount_holder').hide();
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the value of the selected currency does not match the account's currency
|
// if the value of the selected currency does not match the account's currency
|
||||||
@@ -89,23 +118,33 @@ function triggerCurrencyChange() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* There is an input that shows the currency symbol that is native to the selected
|
||||||
|
* acccount. So when the user changes the selected account, the native currency is updated:
|
||||||
|
*/
|
||||||
|
function updateNativeCurrency() {
|
||||||
|
var newAccountId = getAccountId();
|
||||||
|
var nativeCurrencyId = accountInfo[newAccountId].preferredCurrency;
|
||||||
|
|
||||||
function updateCurrency() {
|
console.log('User selected account #' + newAccountId + '. Native currency is #' + nativeCurrencyId);
|
||||||
// get value:
|
|
||||||
var accountId = getAccountId();
|
|
||||||
var currencyPreference = accountInfo[accountId].preferredCurrency;
|
|
||||||
|
|
||||||
$('.currency-option[data-id="' + currencyPreference + '"]').click();
|
$('.currency-option[data-id="' + nativeCurrencyId + '"]').click();
|
||||||
$('[data-toggle="dropdown"]').parent().removeClass('open');
|
$('[data-toggle="dropdown"]').parent().removeClass('open');
|
||||||
$('select[name="source_account_id"]').focus();
|
$('select[name="source_account_id"]').focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function updateDescription() {
|
function updateDescription() {
|
||||||
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
||||||
$('input[name="description"]').typeahead('destroy').typeahead({source: data});
|
$('input[name="description"]').typeahead('destroy').typeahead({source: data});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function getJSONautocomplete() {
|
function getJSONautocomplete() {
|
||||||
|
|
||||||
// for withdrawals
|
// for withdrawals
|
||||||
@@ -142,6 +181,9 @@ function getJSONautocomplete() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function updateLayout() {
|
function updateLayout() {
|
||||||
"use strict";
|
"use strict";
|
||||||
$('#subTitle').text(title[what]);
|
$('#subTitle').text(title[what]);
|
||||||
@@ -150,6 +192,9 @@ function updateLayout() {
|
|||||||
$('#transaction-btn').text(button[what]);
|
$('#transaction-btn').text(button[what]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function updateForm() {
|
function updateForm() {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
@@ -221,7 +266,9 @@ function updateForm() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
function updateButtons() {
|
function updateButtons() {
|
||||||
"use strict";
|
"use strict";
|
||||||
$('.switch').each(function (i, v) {
|
$('.switch').each(function (i, v) {
|
||||||
@@ -240,6 +287,11 @@ function updateButtons() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
function clickButton(e) {
|
function clickButton(e) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var button = $(e.target);
|
var button = $(e.target);
|
||||||
@@ -258,11 +310,24 @@ function clickButton(e) {
|
|||||||
* Get accountID based on some meta info.
|
* Get accountID based on some meta info.
|
||||||
*/
|
*/
|
||||||
function getAccountId() {
|
function getAccountId() {
|
||||||
if(what === "withdrawal") {
|
if (what === "withdrawal") {
|
||||||
return $('select[name="source_account_id"]').val();
|
return $('select[name="source_account_id"]').val();
|
||||||
}
|
}
|
||||||
if(what === "deposit") {
|
if (what === "deposit") {
|
||||||
return $('select[name="destination_account_id"]').val();
|
return $('select[name="destination_account_id"]').val();
|
||||||
}
|
}
|
||||||
alert('Cannot handle ' + what);
|
alert('Cannot handle ' + what);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function runModernizer() {
|
||||||
|
if (!Modernizr.inputtypes.date) {
|
||||||
|
$('input[type="date"]').datepicker(
|
||||||
|
{
|
||||||
|
dateFormat: 'yy-mm-dd'
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
@@ -12,7 +12,115 @@
|
|||||||
|
|
||||||
$(document).ready(function () {
|
$(document).ready(function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
// give date a datepicker if not natively supported.
|
runModernizer();
|
||||||
|
setAutocompletes();
|
||||||
|
updateInitialPage();
|
||||||
|
|
||||||
|
|
||||||
|
// respond to user input:
|
||||||
|
$('.currency-option').on('click', selectsForeignCurrency);
|
||||||
|
$('#ffInput_amount').on('change', convertForeignToNative);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set some initial values for the user to see.
|
||||||
|
*/
|
||||||
|
function updateInitialPage() {
|
||||||
|
|
||||||
|
console.log('Native currency is #' + journalData.native_currency.id + ' and (foreign) currency id is #' + journalData.currency.id);
|
||||||
|
if (journalData.native_currency.id === journalData.currency.id) {
|
||||||
|
$('#exchange_rate_instruction_holder').hide();
|
||||||
|
$('#native_amount_holder').hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (journalData.native_currency.id !== journalData.currency.id) {
|
||||||
|
$('#ffInput_exchange_rate_instruction').text(getExchangeInstructions());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When the user changes the currency in the amount drop down, it may jump from being
|
||||||
|
* the native currency to a foreign currency. This triggers the display of several
|
||||||
|
* information things that make sure that the user always supplies the amount in the native currency.
|
||||||
|
*
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
function selectsForeignCurrency() {
|
||||||
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||||
|
var selectedAccountId = getAccountId();
|
||||||
|
var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency);
|
||||||
|
|
||||||
|
if (foreignCurrencyId !== nativeCurrencyId) {
|
||||||
|
console.log('User has selected currency #' + foreignCurrencyId + ' and this is different from native currency #' + nativeCurrencyId);
|
||||||
|
|
||||||
|
// the input where the native amount is entered gets the symbol for the native currency:
|
||||||
|
$('.non-selectable-currency-symbol').text(currencyInfo[nativeCurrencyId].symbol);
|
||||||
|
|
||||||
|
// the instructions get updated:
|
||||||
|
$('#ffInput_exchange_rate_instruction').text(getExchangeInstructions());
|
||||||
|
|
||||||
|
// both holders are shown to the user:
|
||||||
|
$('#exchange_rate_instruction_holder').show();
|
||||||
|
$('#native_amount_holder').show();
|
||||||
|
|
||||||
|
// if possible the amount is already exchanged for the foreign currency
|
||||||
|
convertForeignToNative();
|
||||||
|
|
||||||
|
}
|
||||||
|
if (foreignCurrencyId === nativeCurrencyId) {
|
||||||
|
console.log('User has selected currency #' + foreignCurrencyId + ' and this is equal to native currency #' + nativeCurrencyId + ' (phew).');
|
||||||
|
$('#exchange_rate_instruction_holder').hide();
|
||||||
|
$('#native_amount_holder').hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts any foreign amount to the native currency.
|
||||||
|
*/
|
||||||
|
function convertForeignToNative() {
|
||||||
|
var accountId = getAccountId();
|
||||||
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||||
|
var nativeCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
||||||
|
var foreignCurrencyCode = currencyInfo[foreignCurrencyId].code;
|
||||||
|
var nativeCurrencyCode = currencyInfo[nativeCurrencyId].code;
|
||||||
|
var date = $('#ffInput_date').val();
|
||||||
|
var amount = $('#ffInput_amount').val();
|
||||||
|
var uri = 'json/rate/' + foreignCurrencyCode + '/' + nativeCurrencyCode + '/' + date + '?amount=' + amount;
|
||||||
|
console.log('Will grab ' + uri);
|
||||||
|
$.get(uri).done(updateNativeAmount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Once the data has been grabbed will update the field in the form.
|
||||||
|
* @param data
|
||||||
|
*/
|
||||||
|
function updateNativeAmount(data) {
|
||||||
|
console.log('Returned data:');
|
||||||
|
console.log(data);
|
||||||
|
$('#ffInput_native_amount').val(data.amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get accountID based on some meta info.
|
||||||
|
*/
|
||||||
|
function getAccountId() {
|
||||||
|
if (journal.transaction_type.type === "Withdrawal") {
|
||||||
|
return $('select[name="source_account_id"]').val();
|
||||||
|
}
|
||||||
|
if (journal.transaction_type.type === "Deposit") {
|
||||||
|
return $('select[name="destination_account_id"]').val();
|
||||||
|
}
|
||||||
|
|
||||||
|
alert('Cannot handle ' + journal.transaction_type.type);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Give date a datepicker if not natively supported.
|
||||||
|
*/
|
||||||
|
function runModernizer() {
|
||||||
if (!Modernizr.inputtypes.date) {
|
if (!Modernizr.inputtypes.date) {
|
||||||
$('input[type="date"]').datepicker(
|
$('input[type="date"]').datepicker(
|
||||||
{
|
{
|
||||||
@@ -20,8 +128,12 @@ $(document).ready(function () {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// the destination account name is always an expense account name.
|
/**
|
||||||
|
* Set the auto-complete JSON things.
|
||||||
|
*/
|
||||||
|
function setAutocompletes() {
|
||||||
if ($('input[name="destination_account_name"]').length > 0) {
|
if ($('input[name="destination_account_name"]').length > 0) {
|
||||||
$.getJSON('json/expense-accounts').done(function (data) {
|
$.getJSON('json/expense-accounts').done(function (data) {
|
||||||
$('input[name="destination_account_name"]').typeahead({source: data});
|
$('input[name="destination_account_name"]').typeahead({source: data});
|
||||||
@@ -29,7 +141,6 @@ $(document).ready(function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$.getJSON('json/tags').done(function (data) {
|
$.getJSON('json/tags').done(function (data) {
|
||||||
|
|
||||||
var opt = {
|
var opt = {
|
||||||
typeahead: {
|
typeahead: {
|
||||||
source: data,
|
source: data,
|
||||||
@@ -43,7 +154,6 @@ $(document).ready(function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// the source account name is always a revenue account name.
|
|
||||||
if ($('input[name="source_account_name"]').length > 0) {
|
if ($('input[name="source_account_name"]').length > 0) {
|
||||||
$.getJSON('json/revenue-accounts').done(function (data) {
|
$.getJSON('json/revenue-accounts').done(function (data) {
|
||||||
$('input[name="source_account_name"]').typeahead({source: data});
|
$('input[name="source_account_name"]').typeahead({source: data});
|
||||||
@@ -58,87 +168,20 @@ $(document).ready(function () {
|
|||||||
$.getJSON('json/categories').done(function (data) {
|
$.getJSON('json/categories').done(function (data) {
|
||||||
$('input[name="category"]').typeahead({source: data});
|
$('input[name="category"]').typeahead({source: data});
|
||||||
});
|
});
|
||||||
|
|
||||||
$('.currency-option').on('click', triggerCurrencyChange);
|
|
||||||
$('#ffInput_amount').on('change', getExchangeRate);
|
|
||||||
|
|
||||||
// always update the exchanged_amount to match the correct currency
|
|
||||||
var journalCurrency = currencyInfo[journal.transaction_currency_id].symbol;
|
|
||||||
$('.non-selectable-currency-symbol').text(journalCurrency);
|
|
||||||
|
|
||||||
// hide the exchange amount / foreign things:
|
|
||||||
if (journal.transaction_currency_id === journalData.currency.id) {
|
|
||||||
$('#exchange_rate_instruction_holder').hide();
|
|
||||||
$('#exchanged_amount_holder').hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
// or update the related text.
|
|
||||||
if (journal.transaction_currency_id !== journalData.currency.id) {
|
|
||||||
// update info text:
|
|
||||||
var accountId = getAccountId();
|
|
||||||
var text = exchangeRateInstructions.replace('@name', accountInfo[accountId].name);
|
|
||||||
text = text.replace(/@account_currency/g, currencyInfo[journal.transaction_currency_id].name);
|
|
||||||
text = text.replace(/@transaction_currency/g, currencyInfo[journalData.currency.id].name);
|
|
||||||
$('#ffInput_exchange_rate_instruction').text(text);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function triggerCurrencyChange() {
|
|
||||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
|
||||||
var accountId = getAccountId();
|
|
||||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
|
||||||
console.log('Selected currency is ' + selectedCurrencyId);
|
|
||||||
console.log('Account prefers ' + accountCurrencyId);
|
|
||||||
if (selectedCurrencyId !== accountCurrencyId) {
|
|
||||||
var text = exchangeRateInstructions.replace('@name', accountInfo[accountId].name);
|
|
||||||
text = text.replace(/@account_currency/g, currencyInfo[accountCurrencyId].name);
|
|
||||||
text = text.replace(/@transaction_currency/g, currencyInfo[selectedCurrencyId].name);
|
|
||||||
$('.non-selectable-currency-symbol').text(currencyInfo[accountCurrencyId].symbol);
|
|
||||||
getExchangeRate();
|
|
||||||
|
|
||||||
$('#ffInput_exchange_rate_instruction').text(text);
|
|
||||||
$('#exchange_rate_instruction_holder').show();
|
|
||||||
$('#exchanged_amount_holder').show();
|
|
||||||
}
|
|
||||||
if (selectedCurrencyId === accountCurrencyId) {
|
|
||||||
$('#exchange_rate_instruction_holder').hide();
|
|
||||||
$('#exchanged_amount_holder').hide();
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the value of the selected currency does not match the account's currency
|
|
||||||
// show the exchange rate thing!
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getExchangeRate() {
|
|
||||||
var accountId = getAccountId();
|
|
||||||
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
|
||||||
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
|
|
||||||
var selectedCurrencyCode = currencyInfo[selectedCurrencyId].code;
|
|
||||||
var accountCurrencyCode = currencyInfo[accountCurrencyId].code;
|
|
||||||
var date = $('#ffInput_date').val();
|
|
||||||
var amount = $('#ffInput_amount').val();
|
|
||||||
var uri = 'json/rate/' + selectedCurrencyCode + '/' + accountCurrencyCode + '/' + date + '?amount=' + amount;
|
|
||||||
console.log('Will grab ' + uri);
|
|
||||||
$.get(uri).done(updateExchangedAmount);
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateExchangedAmount(data) {
|
|
||||||
console.log('Returned data:');
|
|
||||||
console.log(data);
|
|
||||||
$('#ffInput_exchanged_amount').val(data.amount);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get accountID based on some meta info.
|
* This function generates a small helper text to explain the user
|
||||||
|
* that they have selected a foreign currency.
|
||||||
|
* @returns {XML|string|void}
|
||||||
*/
|
*/
|
||||||
function getAccountId() {
|
function getExchangeInstructions() {
|
||||||
if(journal.transaction_type.type === "Withdrawal") {
|
var selectedAccountId = getAccountId();
|
||||||
return $('select[name="source_account_id"]').val();
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||||
}
|
var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency);
|
||||||
if(journal.transaction_type.type === "Deposit") {
|
|
||||||
return $('select[name="destination_account_id"]').val();
|
|
||||||
}
|
|
||||||
|
|
||||||
alert('Cannot handle ' + journal.transaction_type.type);
|
var text = exchangeRateInstructions.replace('@name', accountInfo[selectedAccountId].name);
|
||||||
}
|
text = text.replace(/@native_currency/g, currencyInfo[nativeCurrencyId].name);
|
||||||
|
text = text.replace(/@foreign_currency/g, currencyInfo[foreignCurrencyId].name);
|
||||||
|
return text;
|
||||||
|
}
|
@@ -135,7 +135,7 @@ return [
|
|||||||
'all_journals_for_category' => 'All transactions for category :name',
|
'all_journals_for_category' => 'All transactions for category :name',
|
||||||
'journals_in_period_for_category' => 'All transactions for category :name between :start and :end',
|
'journals_in_period_for_category' => 'All transactions for category :name between :start and :end',
|
||||||
'not_available_demo_user' => 'The feature you try to access is not available to demo users.',
|
'not_available_demo_user' => 'The feature you try to access is not available to demo users.',
|
||||||
'exchange_rate_instructions' => 'Asset account "@name" only accepts transactions in @account_currency. If you wish to use @transaction_currency instead, make sure that the amount in @account_currency is known as well:',
|
'exchange_rate_instructions' => 'Asset account "@name" only accepts transactions in @native_currency. If you wish to use @foreign_currency instead, make sure that the amount in @native_currency is known as well:',
|
||||||
|
|
||||||
// repeat frequencies:
|
// repeat frequencies:
|
||||||
'repeat_freq_yearly' => 'yearly',
|
'repeat_freq_yearly' => 'yearly',
|
||||||
|
@@ -37,11 +37,11 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>{{ 'total_amount'|_ }}</td>
|
<td>{{ 'total_amount'|_ }}</td>
|
||||||
<td>{{ journal|formatJournal }}
|
<td>{{ journal|formatJournal }}
|
||||||
{% if journal.hasMeta('original_amount') %}
|
{% if journal.hasMeta('foreign_amount') %}
|
||||||
{% if journal.transactiontype.type == 'Withdrawal' %}
|
{% if journal.transactiontype.type == 'Withdrawal' %}
|
||||||
({{ formatAnything(originalCurrency, journal.getMeta('original_amount')*-1) }})
|
({{ formatAnything(foreignCurrency, journal.getMeta('foreign_amount')*-1) }})
|
||||||
{% else %}
|
{% else %}
|
||||||
({{ formatAnything(originalCurrency, journal.getMeta('original_amount')) }})
|
({{ formatAnything(foreignCurrency, journal.getMeta('foreign_amount')) }})
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
|
@@ -50,7 +50,7 @@
|
|||||||
{# INSTRUCTIONS FOR EXCHANGE RATES #}
|
{# INSTRUCTIONS FOR EXCHANGE RATES #}
|
||||||
{{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}
|
{{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}
|
||||||
|
|
||||||
{{ ExpandedForm.nonSelectableAmount('exchanged_amount') }}
|
{{ ExpandedForm.nonSelectableAmount('native_amount') }}
|
||||||
|
|
||||||
{# ALWAYS SHOW DATE #}
|
{# ALWAYS SHOW DATE #}
|
||||||
{{ ExpandedForm.date('date', preFilled.date|default(phpdate('Y-m-d'))) }}
|
{{ ExpandedForm.date('date', preFilled.date|default(phpdate('Y-m-d'))) }}
|
||||||
|
@@ -57,12 +57,12 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{# ALWAYS SHOW AMOUNT #}
|
{# ALWAYS SHOW AMOUNT #}
|
||||||
{{ ExpandedForm.amount('amount',data.amount,{'currency' : data.currency}) }}
|
{{ ExpandedForm.amount('amount',data.amount, {'currency' : data.currency}) }}
|
||||||
|
|
||||||
{# INSTRUCTIONS FOR EXCHANGE RATES #}
|
{# INSTRUCTIONS FOR EXCHANGE RATES #}
|
||||||
{{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}
|
{{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}
|
||||||
|
|
||||||
{{ ExpandedForm.nonSelectableAmount('exchanged_amount') }}
|
{{ ExpandedForm.nonSelectableAmount('native_amount', data.native_amount, {'currency' : data.native_currency}) }}
|
||||||
|
|
||||||
{# ALWAYS SHOW DATE #}
|
{# ALWAYS SHOW DATE #}
|
||||||
{{ ExpandedForm.date('date',data['date']) }}
|
{{ ExpandedForm.date('date',data['date']) }}
|
||||||
|
Reference in New Issue
Block a user