mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-02 10:22:09 +00:00
Clean up and consistency in foreign and native amounts.
This commit is contained in:
@@ -261,16 +261,16 @@ class SingleController extends Controller
|
||||
'notes' => $journal->getMeta('notes'),
|
||||
|
||||
// exchange rate fields
|
||||
'exchanged_amount' => $journal->amountPositive(),
|
||||
'exchanged_currency' => $journal->transactionCurrency,
|
||||
'native_amount' => $journal->amountPositive(),
|
||||
'native_currency' => $journal->transactionCurrency,
|
||||
];
|
||||
|
||||
// catch possibly exchanged currencies and what-not.
|
||||
$originalCurrencyId = intval($journal->getMeta('original_currency_id'));
|
||||
if ($originalCurrencyId > 0) {
|
||||
// if user has entered a foreign currency, update some fields
|
||||
$foreignCurrencyId = intval($journal->getMeta('foreign_currency_id'));
|
||||
if ($foreignCurrencyId > 0) {
|
||||
// update some fields in pre-filled.
|
||||
$preFilled['amount'] = $journal->getMeta('original_amount');
|
||||
$preFilled['currency'] = $this->currency->find(intval($journal->getMeta('original_currency_id')));
|
||||
$preFilled['amount'] = $journal->getMeta('foreign_amount');
|
||||
$preFilled['currency'] = $this->currency->find(intval($journal->getMeta('foreign_currency_id')));
|
||||
}
|
||||
|
||||
if ($journal->isWithdrawal() && $destinationAccounts->first()->accountType->type == AccountType::CASH) {
|
||||
|
@@ -182,15 +182,15 @@ class TransactionController extends Controller
|
||||
$transactions = $tasker->getTransactionsOverview($journal);
|
||||
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
|
||||
$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 */
|
||||
$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'),
|
||||
'piggy_bank_id' => $this->integer('piggy_bank_id'),
|
||||
|
||||
// amount for exchanged data:
|
||||
'exchanged_amount' => $this->float('exchanged_amount'),
|
||||
// native amount
|
||||
'native_amount' => $this->float('native_amount'),
|
||||
|
||||
];
|
||||
|
||||
@@ -106,7 +106,7 @@ class JournalFormRequest extends Request
|
||||
'piggy_bank_id' => 'between:1,255',
|
||||
|
||||
// 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:
|
||||
|
@@ -42,8 +42,8 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
|
||||
/** @var array */
|
||||
private $validMetaFields
|
||||
= ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes', 'original_amount',
|
||||
'original_currency_id',
|
||||
= ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date', 'invoice_date', 'internal_reference', 'notes', 'foreign_amount',
|
||||
'foreign_currency_id',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -171,35 +171,9 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
/** @var TransactionType $transactionType */
|
||||
$transactionType = TransactionType::where('type', ucfirst($data['what']))->first();
|
||||
$accounts = $this->storeAccounts($transactionType, $data);
|
||||
$data = $this->verifyNativeAmount($data, $accounts);
|
||||
$currencyId = $data['currency_id'];
|
||||
$amount = strval($data['amount']);
|
||||
// switch type to find what account to verify for currency stuff
|
||||
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,
|
||||
@@ -788,4 +762,47 @@ class JournalRepository implements JournalRepositoryInterface
|
||||
|
||||
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 () {
|
||||
"use strict";
|
||||
|
||||
// respond to switch buttons
|
||||
// respond to switch buttons (first time always triggers)
|
||||
updateButtons();
|
||||
updateForm();
|
||||
updateLayout();
|
||||
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();
|
||||
$('#exchanged_amount_holder').hide();
|
||||
$('#native_amount_holder').hide();
|
||||
|
||||
if (!Modernizr.inputtypes.date) {
|
||||
$('input[type="date"]').datepicker(
|
||||
{
|
||||
dateFormat: 'yy-mm-dd'
|
||||
}
|
||||
);
|
||||
}
|
||||
// when user changes source account, native currency may be different.
|
||||
$('select[name="source_account_id"]').on('change', updateNativeCurrency);
|
||||
|
||||
// update currency
|
||||
$('select[name="source_account_id"]').on('change', updateCurrency);
|
||||
updateCurrency();
|
||||
$('#ffInput_amount').on('change', getExchangeRate);
|
||||
// convert foreign currency to native currency.
|
||||
$('#ffInput_amount').on('change', convertForeignToNative);
|
||||
|
||||
// respond to changes to the hidden input,
|
||||
// so we can show the "exchange rate" thing if necessary:
|
||||
$('.currency-option').on('click', triggerCurrencyChange);
|
||||
// when user selects different currency,
|
||||
$('.currency-option').on('click', selectsForeignCurrency);
|
||||
|
||||
// get JSON things:
|
||||
getJSONautocomplete();
|
||||
});
|
||||
|
||||
function getExchangeRate() {
|
||||
/**
|
||||
* Converts any foreign amount to the native currency.
|
||||
*/
|
||||
function convertForeignToNative() {
|
||||
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 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/' + selectedCurrencyCode + '/' + accountCurrencyCode + '/' + date + '?amount=' + amount;
|
||||
var uri = 'json/rate/' + foreignCurrencyCode + '/' + nativeCurrencyCode + '/' + date + '?amount=' + amount;
|
||||
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(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 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();
|
||||
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;
|
||||
}
|
||||
|
||||
$('#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();
|
||||
$('#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();
|
||||
$('#exchanged_amount_holder').hide();
|
||||
$('#native_amount_holder').hide();
|
||||
}
|
||||
|
||||
// if the value of the selected currency does not match the account's currency
|
||||
@@ -89,23 +118,33 @@ function triggerCurrencyChange() {
|
||||
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() {
|
||||
// get value:
|
||||
var accountId = getAccountId();
|
||||
var currencyPreference = accountInfo[accountId].preferredCurrency;
|
||||
console.log('User selected account #' + newAccountId + '. Native currency is #' + nativeCurrencyId);
|
||||
|
||||
$('.currency-option[data-id="' + currencyPreference + '"]').click();
|
||||
$('.currency-option[data-id="' + nativeCurrencyId + '"]').click();
|
||||
$('[data-toggle="dropdown"]').parent().removeClass('open');
|
||||
$('select[name="source_account_id"]').focus();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function updateDescription() {
|
||||
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
||||
$('input[name="description"]').typeahead('destroy').typeahead({source: data});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function getJSONautocomplete() {
|
||||
|
||||
// for withdrawals
|
||||
@@ -142,6 +181,9 @@ function getJSONautocomplete() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function updateLayout() {
|
||||
"use strict";
|
||||
$('#subTitle').text(title[what]);
|
||||
@@ -150,6 +192,9 @@ function updateLayout() {
|
||||
$('#transaction-btn').text(button[what]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function updateForm() {
|
||||
"use strict";
|
||||
|
||||
@@ -221,7 +266,9 @@ function updateForm() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function updateButtons() {
|
||||
"use strict";
|
||||
$('.switch').each(function (i, v) {
|
||||
@@ -240,6 +287,11 @@ function updateButtons() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param e
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function clickButton(e) {
|
||||
"use strict";
|
||||
var button = $(e.target);
|
||||
@@ -266,3 +318,16 @@ function getAccountId() {
|
||||
}
|
||||
alert('Cannot handle ' + what);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function runModernizer() {
|
||||
if (!Modernizr.inputtypes.date) {
|
||||
$('input[type="date"]').datepicker(
|
||||
{
|
||||
dateFormat: 'yy-mm-dd'
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@@ -12,121 +12,94 @@
|
||||
|
||||
$(document).ready(function () {
|
||||
"use strict";
|
||||
// give date a datepicker if not natively supported.
|
||||
if (!Modernizr.inputtypes.date) {
|
||||
$('input[type="date"]').datepicker(
|
||||
{
|
||||
dateFormat: 'yy-mm-dd'
|
||||
}
|
||||
);
|
||||
}
|
||||
runModernizer();
|
||||
setAutocompletes();
|
||||
updateInitialPage();
|
||||
|
||||
// the destination account name is always an expense account name.
|
||||
if ($('input[name="destination_account_name"]').length > 0) {
|
||||
$.getJSON('json/expense-accounts').done(function (data) {
|
||||
$('input[name="destination_account_name"]').typeahead({source: data});
|
||||
});
|
||||
}
|
||||
|
||||
$.getJSON('json/tags').done(function (data) {
|
||||
|
||||
var opt = {
|
||||
typeahead: {
|
||||
source: data,
|
||||
afterSelect: function () {
|
||||
this.$element.val("");
|
||||
}
|
||||
}
|
||||
};
|
||||
$('input[name="tags"]').tagsinput(
|
||||
opt
|
||||
);
|
||||
// respond to user input:
|
||||
$('.currency-option').on('click', selectsForeignCurrency);
|
||||
$('#ffInput_amount').on('change', convertForeignToNative);
|
||||
});
|
||||
|
||||
// the source account name is always a revenue account name.
|
||||
if ($('input[name="source_account_name"]').length > 0) {
|
||||
$.getJSON('json/revenue-accounts').done(function (data) {
|
||||
$('input[name="source_account_name"]').typeahead({source: data});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Set some initial values for the user to see.
|
||||
*/
|
||||
function updateInitialPage() {
|
||||
|
||||
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
||||
$('input[name="description"]').typeahead({source: data});
|
||||
});
|
||||
|
||||
|
||||
$.getJSON('json/categories').done(function (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) {
|
||||
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();
|
||||
$('#exchanged_amount_holder').hide();
|
||||
$('#native_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);
|
||||
if (journalData.native_currency.id !== journalData.currency.id) {
|
||||
$('#ffInput_exchange_rate_instruction').text(getExchangeInstructions());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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();
|
||||
/**
|
||||
* 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);
|
||||
|
||||
$('#ffInput_exchange_rate_instruction').text(text);
|
||||
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();
|
||||
$('#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();
|
||||
$('#exchanged_amount_holder').hide();
|
||||
$('#native_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() {
|
||||
/**
|
||||
* Converts any foreign amount to the native currency.
|
||||
*/
|
||||
function convertForeignToNative() {
|
||||
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 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/' + selectedCurrencyCode + '/' + accountCurrencyCode + '/' + date + '?amount=' + amount;
|
||||
var uri = 'json/rate/' + foreignCurrencyCode + '/' + nativeCurrencyCode + '/' + date + '?amount=' + amount;
|
||||
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(data);
|
||||
$('#ffInput_exchanged_amount').val(data.amount);
|
||||
$('#ffInput_native_amount').val(data.amount);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,3 +115,73 @@ function getAccountId() {
|
||||
|
||||
alert('Cannot handle ' + journal.transaction_type.type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Give date a datepicker if not natively supported.
|
||||
*/
|
||||
function runModernizer() {
|
||||
if (!Modernizr.inputtypes.date) {
|
||||
$('input[type="date"]').datepicker(
|
||||
{
|
||||
dateFormat: 'yy-mm-dd'
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the auto-complete JSON things.
|
||||
*/
|
||||
function setAutocompletes() {
|
||||
if ($('input[name="destination_account_name"]').length > 0) {
|
||||
$.getJSON('json/expense-accounts').done(function (data) {
|
||||
$('input[name="destination_account_name"]').typeahead({source: data});
|
||||
});
|
||||
}
|
||||
|
||||
$.getJSON('json/tags').done(function (data) {
|
||||
var opt = {
|
||||
typeahead: {
|
||||
source: data,
|
||||
afterSelect: function () {
|
||||
this.$element.val("");
|
||||
}
|
||||
}
|
||||
};
|
||||
$('input[name="tags"]').tagsinput(
|
||||
opt
|
||||
);
|
||||
});
|
||||
|
||||
if ($('input[name="source_account_name"]').length > 0) {
|
||||
$.getJSON('json/revenue-accounts').done(function (data) {
|
||||
$('input[name="source_account_name"]').typeahead({source: data});
|
||||
});
|
||||
}
|
||||
|
||||
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
||||
$('input[name="description"]').typeahead({source: data});
|
||||
});
|
||||
|
||||
|
||||
$.getJSON('json/categories').done(function (data) {
|
||||
$('input[name="category"]').typeahead({source: data});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 selectedAccountId = getAccountId();
|
||||
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
||||
var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency);
|
||||
|
||||
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',
|
||||
'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.',
|
||||
'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_freq_yearly' => 'yearly',
|
||||
|
@@ -37,11 +37,11 @@
|
||||
<tr>
|
||||
<td>{{ 'total_amount'|_ }}</td>
|
||||
<td>{{ journal|formatJournal }}
|
||||
{% if journal.hasMeta('original_amount') %}
|
||||
{% if journal.hasMeta('foreign_amount') %}
|
||||
{% if journal.transactiontype.type == 'Withdrawal' %}
|
||||
({{ formatAnything(originalCurrency, journal.getMeta('original_amount')*-1) }})
|
||||
({{ formatAnything(foreignCurrency, journal.getMeta('foreign_amount')*-1) }})
|
||||
{% else %}
|
||||
({{ formatAnything(originalCurrency, journal.getMeta('original_amount')) }})
|
||||
({{ formatAnything(foreignCurrency, journal.getMeta('foreign_amount')) }})
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</td>
|
||||
|
@@ -50,7 +50,7 @@
|
||||
{# INSTRUCTIONS FOR EXCHANGE RATES #}
|
||||
{{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}
|
||||
|
||||
{{ ExpandedForm.nonSelectableAmount('exchanged_amount') }}
|
||||
{{ ExpandedForm.nonSelectableAmount('native_amount') }}
|
||||
|
||||
{# ALWAYS SHOW DATE #}
|
||||
{{ ExpandedForm.date('date', preFilled.date|default(phpdate('Y-m-d'))) }}
|
||||
|
@@ -62,7 +62,7 @@
|
||||
{# INSTRUCTIONS FOR EXCHANGE RATES #}
|
||||
{{ 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 #}
|
||||
{{ ExpandedForm.date('date',data['date']) }}
|
||||
|
Reference in New Issue
Block a user