From ea3fdb0668401075a345789c7c60f8952c4e668a Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 14 Apr 2017 15:42:54 +0200 Subject: [PATCH] Clean up and consistency in foreign and native amounts. --- .../Transaction/SingleController.php | 18 +- .../Controllers/TransactionController.php | 10 +- app/Http/Requests/JournalFormRequest.php | 6 +- .../Journal/JournalRepository.php | 77 ++++--- public/js/ff/transactions/single/create.js | 165 +++++++++----- public/js/ff/transactions/single/edit.js | 209 +++++++++++------- resources/lang/en_US/firefly.php | 2 +- resources/views/transactions/show.twig | 6 +- .../views/transactions/single/create.twig | 2 +- resources/views/transactions/single/edit.twig | 4 +- 10 files changed, 312 insertions(+), 187 deletions(-) diff --git a/app/Http/Controllers/Transaction/SingleController.php b/app/Http/Controllers/Transaction/SingleController.php index 704a7007d4..2ba2f23c5b 100644 --- a/app/Http/Controllers/Transaction/SingleController.php +++ b/app/Http/Controllers/Transaction/SingleController.php @@ -250,8 +250,8 @@ class SingleController extends Controller 'source_account_name' => $sourceAccounts->first()->edit_name, 'destination_account_id' => $destinationAccounts->first()->id, 'destination_account_name' => $destinationAccounts->first()->edit_name, - 'amount' => $journal->amountPositive(), - 'currency' => $journal->transactionCurrency, + 'amount' => $journal->amountPositive(), + 'currency' => $journal->transactionCurrency, // new custom fields: 'due_date' => $journal->dateAsString('due_date'), @@ -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) { diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 34691e4129..cbb8e241f7 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -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'))); + $repository = app(CurrencyRepositoryInterface::class); + $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')); } diff --git a/app/Http/Requests/JournalFormRequest.php b/app/Http/Requests/JournalFormRequest.php index 3110d75559..45c387bc4e 100644 --- a/app/Http/Requests/JournalFormRequest.php +++ b/app/Http/Requests/JournalFormRequest.php @@ -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: diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 340670307b..d35b0d6ae9 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -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,36 +171,10 @@ 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( + $journal = new TransactionJournal( [ 'user_id' => $this->user->id, 'transaction_type_id' => $transactionType->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; + } } diff --git a/public/js/ff/transactions/single/create.js b/public/js/ff/transactions/single/create.js index a72fcf1370..ea3cced69c 100644 --- a/public/js/ff/transactions/single/create.js +++ b/public/js/ff/transactions/single/create.js @@ -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); @@ -258,11 +310,24 @@ function clickButton(e) { * Get accountID based on some meta info. */ function getAccountId() { - if(what === "withdrawal") { + if (what === "withdrawal") { return $('select[name="source_account_id"]').val(); } - if(what === "deposit") { + if (what === "deposit") { return $('select[name="destination_account_id"]').val(); } alert('Cannot handle ' + what); +} + +/** + * + */ +function runModernizer() { + if (!Modernizr.inputtypes.date) { + $('input[type="date"]').datepicker( + { + dateFormat: 'yy-mm-dd' + } + ); + } } \ No newline at end of file diff --git a/public/js/ff/transactions/single/edit.js b/public/js/ff/transactions/single/edit.js index 05cddafaf6..d722c3adb8 100644 --- a/public/js/ff/transactions/single/edit.js +++ b/public/js/ff/transactions/single/edit.js @@ -12,7 +12,115 @@ $(document).ready(function () { "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) { $('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) { $.getJSON('json/expense-accounts').done(function (data) { $('input[name="destination_account_name"]').typeahead({source: data}); @@ -29,7 +141,6 @@ $(document).ready(function () { } $.getJSON('json/tags').done(function (data) { - var opt = { typeahead: { 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) { $.getJSON('json/revenue-accounts').done(function (data) { $('input[name="source_account_name"]').typeahead({source: data}); @@ -58,87 +168,20 @@ $(document).ready(function () { $.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) { - $('#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() { - 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(); - } +function getExchangeInstructions() { + var selectedAccountId = getAccountId(); + var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val()); + var nativeCurrencyId = parseInt(accountInfo[selectedAccountId].preferredCurrency); - 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; +} \ No newline at end of file diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 5944548a90..79a1cec347 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -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', diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig index 69d7fef5d5..3c086d1899 100644 --- a/resources/views/transactions/show.twig +++ b/resources/views/transactions/show.twig @@ -37,11 +37,11 @@ {{ 'total_amount'|_ }} {{ 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 %} diff --git a/resources/views/transactions/single/create.twig b/resources/views/transactions/single/create.twig index 97fcf092e6..c64b46d047 100644 --- a/resources/views/transactions/single/create.twig +++ b/resources/views/transactions/single/create.twig @@ -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'))) }} diff --git a/resources/views/transactions/single/edit.twig b/resources/views/transactions/single/edit.twig index ee02df0f71..53ff1b9ea5 100644 --- a/resources/views/transactions/single/edit.twig +++ b/resources/views/transactions/single/edit.twig @@ -57,12 +57,12 @@ {% endif %} {# ALWAYS SHOW AMOUNT #} - {{ ExpandedForm.amount('amount',data.amount,{'currency' : data.currency}) }} + {{ ExpandedForm.amount('amount',data.amount, {'currency' : data.currency}) }} {# 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']) }}