Clean up and consistency in foreign and native amounts.

This commit is contained in:
James Cole
2017-04-14 15:56:43 +02:00
parent ea3fdb0668
commit e2fe8cfb75
5 changed files with 128 additions and 229 deletions

View File

@@ -12,7 +12,6 @@
$(document).ready(function () {
"use strict";
runModernizer();
setAutocompletes();
updateInitialPage();
@@ -38,69 +37,7 @@ function updateInitialPage() {
}
}
/**
* 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.
@@ -116,58 +53,13 @@ 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});
});
}
/**