Can now handle withdrawals in foreign currency.

This commit is contained in:
James Cole
2017-04-14 14:37:04 +02:00
parent 7e31a29b12
commit c33dd1ecee
9 changed files with 171 additions and 27 deletions

View File

@@ -43,7 +43,7 @@ $(document).ready(function () {
});
function getExchangeRate() {
var accountId = $('select[name="source_account_id"]').val();
var accountId = getAccountId();
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
var selectedCurrencyCode = currencyInfo[selectedCurrencyId].code;
@@ -53,7 +53,6 @@ function getExchangeRate() {
var uri = 'json/rate/' + selectedCurrencyCode + '/' + accountCurrencyCode + '/' + date + '?amount=' + amount;
console.log('Will grab ' + uri);
$.get(uri).done(updateExchangedAmount);
}
function updateExchangedAmount(data) {
@@ -65,7 +64,7 @@ function updateExchangedAmount(data) {
function triggerCurrencyChange() {
var selectedCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
var accountId = $('select[name="source_account_id"]').val();
var accountId = getAccountId();
var accountCurrencyId = parseInt(accountInfo[accountId].preferredCurrency);
console.log('Selected currency is ' + selectedCurrencyId);
console.log('Account prefers ' + accountCurrencyId);
@@ -93,7 +92,7 @@ function triggerCurrencyChange() {
function updateCurrency() {
// get value:
var accountId = $('select[name="source_account_id"]').val();
var accountId = getAccountId();
var currencyPreference = accountInfo[accountId].preferredCurrency;
$('.currency-option[data-id="' + currencyPreference + '"]').click();
@@ -253,4 +252,14 @@ function clickButton(e) {
updateDescription();
}
return false;
}
/**
* Get accountID based on some meta info.
*/
function getAccountId() {
if(what === "withdrawal") {
return $('select[name="source_account_id"]').val();
}
alert('Cannot handle ' + what);
}

View File

@@ -59,4 +59,81 @@ $(document).ready(function () {
$('input[name="category"]').typeahead({source: data});
});
$('.currency-option').on('click', triggerCurrencyChange);
// 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.
*/
function getAccountId() {
if(journal.transaction_type.type === "Withdrawal") {
return $('select[name="source_account_id"]').val();
}
alert('Cannot handle ' + journal.transaction_type.type);
}