2017-01-15 19:00:06 +01:00
|
|
|
/*
|
|
|
|
|
* edit.js
|
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
|
*
|
|
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
|
*
|
|
|
|
|
* See the LICENSE file for details.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-06-05 11:12:50 +02:00
|
|
|
/** global: what, Modernizr, selectsForeignCurrency, convertForeignToNative, validateCurrencyForTransfer, convertSourceToDestination, journalData, journal, accountInfo, exchangeRateInstructions, currencyInfo */
|
2017-01-15 20:18:32 +01:00
|
|
|
|
2017-01-15 19:00:06 +01:00
|
|
|
$(document).ready(function () {
|
|
|
|
|
"use strict";
|
2017-04-14 15:42:54 +02:00
|
|
|
setAutocompletes();
|
|
|
|
|
updateInitialPage();
|
|
|
|
|
|
|
|
|
|
// respond to user input:
|
|
|
|
|
$('.currency-option').on('click', selectsForeignCurrency);
|
|
|
|
|
$('#ffInput_amount').on('change', convertForeignToNative);
|
2017-04-14 22:49:12 +02:00
|
|
|
|
|
|
|
|
// respond to transfer changes:
|
2017-07-08 06:28:44 +02:00
|
|
|
$('#ffInput_source_account_id').on('change', validateCurrencyForTransfer);
|
|
|
|
|
$('#ffInput_destination_account_id').on('change', validateCurrencyForTransfer);
|
2017-04-14 22:49:12 +02:00
|
|
|
|
|
|
|
|
// convert source currency to destination currency (slightly different routine for transfers)
|
|
|
|
|
$('#ffInput_source_amount').on('change', convertSourceToDestination);
|
2017-04-14 15:42:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set some initial values for the user to see.
|
|
|
|
|
*/
|
|
|
|
|
function updateInitialPage() {
|
|
|
|
|
|
2017-04-14 22:49:12 +02:00
|
|
|
if (journal.transaction_type.type === "Transfer") {
|
|
|
|
|
$('#native_amount_holder').hide();
|
|
|
|
|
$('#amount_holder').hide();
|
|
|
|
|
|
2017-06-04 13:39:16 +02:00
|
|
|
|
|
|
|
|
if (journalData.native_currency.id === journalData.destination_currency.id) {
|
2017-04-14 22:49:12 +02:00
|
|
|
$('#exchange_rate_instruction_holder').hide();
|
|
|
|
|
$('#destination_amount_holder').hide();
|
|
|
|
|
}
|
2017-06-04 13:39:16 +02:00
|
|
|
if (journalData.native_currency.id !== journalData.destination_currency.id) {
|
2017-04-14 22:49:12 +02:00
|
|
|
$('#exchange_rate_instruction_holder').show().find('p').text(getTransferExchangeInstructions());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-04-15 07:25:09 +02:00
|
|
|
$('#source_amount_holder').hide();
|
|
|
|
|
$('#destination_amount_holder').hide();
|
2017-04-14 22:49:12 +02:00
|
|
|
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
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());
|
|
|
|
|
}
|
2017-04-14 22:49:12 +02:00
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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);
|
2017-06-05 11:12:50 +02:00
|
|
|
return undefined;
|
2017-04-14 15:42:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the auto-complete JSON things.
|
|
|
|
|
*/
|
|
|
|
|
function setAutocompletes() {
|
2017-01-15 20:10:34 +01:00
|
|
|
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
|
|
|
|
$('input[name="description"]').typeahead({source: data});
|
|
|
|
|
});
|
2017-04-14 14:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-04-14 15:42:54 +02:00
|
|
|
* This function generates a small helper text to explain the user
|
|
|
|
|
* that they have selected a foreign currency.
|
|
|
|
|
* @returns {XML|string|void}
|
2017-04-14 14:37:04 +02:00
|
|
|
*/
|
2017-04-14 15:42:54 +02:00
|
|
|
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;
|
|
|
|
|
}
|