Can now also edit transfers.

This commit is contained in:
James Cole 2017-04-14 22:49:12 +02:00
parent a27f5d2474
commit 32b6e030ef
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
4 changed files with 115 additions and 95 deletions

View File

@ -123,3 +123,84 @@ function updateNativeAmount(data) {
console.log(data); console.log(data);
$('#ffInput_native_amount').val(data.amount); $('#ffInput_native_amount').val(data.amount);
} }
/**
* Instructions for transfers
*/
function getTransferExchangeInstructions() {
var sourceAccount = $('select[name="source_account_id"]').val();
var destAccount = $('select[name="destination_account_id"]').val();
var sourceCurrency = accountInfo[sourceAccount].preferredCurrency;
var destinationCurrency = accountInfo[destAccount].preferredCurrency;
return transferInstructions.replace('@source_name', accountInfo[sourceAccount].name)
.replace('@dest_name', accountInfo[destAccount].name)
.replace(/@source_currency/g, currencyInfo[sourceCurrency].name)
.replace(/@dest_currency/g, currencyInfo[destinationCurrency].name);
}
/**
* When the transaction to create is a transfer some more checks are necessary.
*/
function validateCurrencyForTransfer() {
if (what !== "transfer") {
return;
}
$('#source_amount_holder').show();
var sourceAccount = $('select[name="source_account_id"]').val();
var destAccount = $('select[name="destination_account_id"]').val();
var sourceCurrency = accountInfo[sourceAccount].preferredCurrency;
var sourceSymbol = currencyInfo[sourceCurrency].symbol;
var destinationCurrency = accountInfo[destAccount].preferredCurrency;
var destinationSymbol = currencyInfo[destinationCurrency].symbol;
$('#source_amount_holder').show().find('.non-selectable-currency-symbol').text(sourceSymbol);
if (sourceCurrency === destinationCurrency) {
console.log('Both accounts accept ' + sourceCurrency);
$('#destination_amount_holder').hide();
$('#amount_holder').hide();
return;
}
console.log('Source accepts #' + sourceCurrency + ', destination #' + destinationCurrency);
$('#ffInput_exchange_rate_instruction').text(getTransferExchangeInstructions());
$('#exchange_rate_instruction_holder').show();
$('input[name="source_amount"]').val($('input[name="amount"]').val());
convertSourceToDestination();
$('#destination_amount_holder').show().find('.non-selectable-currency-symbol').text(destinationSymbol);
$('#amount_holder').hide();
}
/**
* Convert from source amount currency to destination currency for transfers.
*
*/
function convertSourceToDestination() {
var sourceAccount = $('select[name="source_account_id"]').val();
var destAccount = $('select[name="destination_account_id"]').val();
var sourceCurrency = accountInfo[sourceAccount].preferredCurrency;
var destinationCurrency = accountInfo[destAccount].preferredCurrency;
var sourceCurrencyCode = currencyInfo[sourceCurrency].code;
var destinationCurrencyCode = currencyInfo[destinationCurrency].code;
var date = $('#ffInput_date').val();
var amount = $('#ffInput_source_amount').val();
$('#ffInput_amount').val(amount);
var uri = 'json/rate/' + sourceCurrencyCode + '/' + destinationCurrencyCode + '/' + date + '?amount=' + amount;
console.log('Will grab ' + uri);
$.get(uri).done(updateDestinationAmount);
}
/**
* Once the data has been grabbed will update the field (for transfers)
* @param data
*/
function updateDestinationAmount(data) {
console.log('Returned data:');
console.log(data);
$('#ffInput_destination_amount').val(data.amount);
}

View File

@ -41,38 +41,6 @@ $(document).ready(function () {
$('.currency-option').on('click', selectsForeignCurrency); $('.currency-option').on('click', selectsForeignCurrency);
}); });
/**
* Convert from source amount currency to destination currency for transfers.
*
*/
function convertSourceToDestination() {
var sourceAccount = $('select[name="source_account_id"]').val();
var destAccount = $('select[name="destination_account_id"]').val();
var sourceCurrency = accountInfo[sourceAccount].preferredCurrency;
var destinationCurrency = accountInfo[destAccount].preferredCurrency;
var sourceCurrencyCode = currencyInfo[sourceCurrency].code;
var destinationCurrencyCode = currencyInfo[destinationCurrency].code;
var date = $('#ffInput_date').val();
var amount = $('#ffInput_source_amount').val();
$('#ffInput_amount').val(amount);
var uri = 'json/rate/' + sourceCurrencyCode + '/' + destinationCurrencyCode + '/' + date + '?amount=' + amount;
console.log('Will grab ' + uri);
$.get(uri).done(updateDestinationAmount);
}
/**
* Once the data has been grabbed will update the field (for transfers)
* @param data
*/
function updateDestinationAmount(data) {
console.log('Returned data:');
console.log(data);
$('#ffInput_destination_amount').val(data.amount);
}
/** /**
* This function generates a small helper text to explain the user * This function generates a small helper text to explain the user
* that they have selected a foreign currency. * that they have selected a foreign currency.
@ -89,22 +57,6 @@ function getExchangeInstructions() {
return text; return text;
} }
/**
* Same as above but for transfers
*/
function getTransferExchangeInstructions() {
var sourceAccount = $('select[name="source_account_id"]').val();
var destAccount = $('select[name="destination_account_id"]').val();
var sourceCurrency = accountInfo[sourceAccount].preferredCurrency;
var destinationCurrency = accountInfo[destAccount].preferredCurrency;
return transferInstructions.replace('@source_name', accountInfo[sourceAccount].name)
.replace('@dest_name', accountInfo[destAccount].name)
.replace(/@source_currency/g, currencyInfo[sourceCurrency].name)
.replace(/@dest_currency/g, currencyInfo[destinationCurrency].name);
}
/** /**
* There is an input that shows the currency symbol that is native to the selected * 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: * acccount. So when the user changes the selected account, the native currency is updated:
@ -122,39 +74,6 @@ function updateNativeCurrency() {
validateCurrencyForTransfer(); validateCurrencyForTransfer();
} }
/**
* When the transaction to create is a transfer some more checks are necessary.
*/
function validateCurrencyForTransfer() {
if (what !== "transfer") {
return;
}
$('#source_amount_holder').show();
var sourceAccount = $('select[name="source_account_id"]').val();
var destAccount = $('select[name="destination_account_id"]').val();
var sourceCurrency = accountInfo[sourceAccount].preferredCurrency;
var sourceSymbol = currencyInfo[sourceCurrency].symbol;
var destinationCurrency = accountInfo[destAccount].preferredCurrency;
var destinationSymbol = currencyInfo[destinationCurrency].symbol;
$('#source_amount_holder').show().find('.non-selectable-currency-symbol').text(sourceSymbol);
if (sourceCurrency === destinationCurrency) {
console.log('Both accounts accept ' + sourceCurrency);
$('#destination_amount_holder').hide();
$('#amount_holder').hide();
return;
}
console.log('Source accepts #' + sourceCurrency + ', destination #' + destinationCurrency);
$('#ffInput_exchange_rate_instruction').text(getTransferExchangeInstructions());
$('#exchange_rate_instruction_holder').show();
$('input[name="source_amount"]').val($('input[name="amount"]').val());
convertSourceToDestination();
$('#destination_amount_holder').show().find('.non-selectable-currency-symbol').text(destinationSymbol);
$('#amount_holder').hide();
}
/** /**
* *
*/ */
@ -211,10 +130,6 @@ function updateForm() {
$('#destination_amount_holder').hide(); $('#destination_amount_holder').hide();
// show normal amount: // show normal amount:
$('#amount_holder').show(); $('#amount_holder').show();
// update the amount thing:
updateNativeCurrency();
break; break;
case 'deposit': case 'deposit':
// show source_name and dest_id: // show source_name and dest_id:
@ -242,10 +157,6 @@ function updateForm() {
$('#destination_amount_holder').hide(); $('#destination_amount_holder').hide();
// show normal amount: // show normal amount:
$('#amount_holder').show(); $('#amount_holder').show();
// update the amount thing:
updateNativeCurrency();
break; break;
case 'transfer': case 'transfer':
// show source_id and dest_id: // show source_id and dest_id:
@ -263,15 +174,13 @@ function updateForm() {
} else { } else {
$('#piggy_bank_id_holder').show(); $('#piggy_bank_id_holder').show();
} }
// update the amount thing:
updateNativeCurrency();
break; break;
default: default:
// no action. // no action.
break; break;
} }
// update the amount thing:
updateNativeCurrency();
} }
/** /**

View File

@ -15,10 +15,16 @@ $(document).ready(function () {
setAutocompletes(); setAutocompletes();
updateInitialPage(); updateInitialPage();
// respond to user input: // respond to user input:
$('.currency-option').on('click', selectsForeignCurrency); $('.currency-option').on('click', selectsForeignCurrency);
$('#ffInput_amount').on('change', convertForeignToNative); $('#ffInput_amount').on('change', convertForeignToNative);
// respond to transfer changes:
$('#ffInput_source_account_id').on('change',validateCurrencyForTransfer);
$('#ffInput_destination_account_id').on('change',validateCurrencyForTransfer);
// convert source currency to destination currency (slightly different routine for transfers)
$('#ffInput_source_amount').on('change', convertSourceToDestination);
}); });
/** /**
@ -27,6 +33,24 @@ $(document).ready(function () {
function updateInitialPage() { function updateInitialPage() {
console.log('Native currency is #' + journalData.native_currency.id + ' and (foreign) currency id is #' + journalData.currency.id); console.log('Native currency is #' + journalData.native_currency.id + ' and (foreign) currency id is #' + journalData.currency.id);
if (journal.transaction_type.type === "Transfer") {
$('#native_amount_holder').hide();
$('#amount_holder').hide();
if (journalData.native_currency.id === journalData.currency.id) {
$('#exchange_rate_instruction_holder').hide();
$('#destination_amount_holder').hide();
}
if (journalData.native_currency.id !== journalData.currency.id) {
$('#exchange_rate_instruction_holder').show().find('p').text(getTransferExchangeInstructions());
}
return;
}
if (journalData.native_currency.id === journalData.currency.id) { if (journalData.native_currency.id === journalData.currency.id) {
$('#exchange_rate_instruction_holder').hide(); $('#exchange_rate_instruction_holder').hide();
$('#native_amount_holder').hide(); $('#native_amount_holder').hide();
@ -35,6 +59,7 @@ function updateInitialPage() {
if (journalData.native_currency.id !== journalData.currency.id) { if (journalData.native_currency.id !== journalData.currency.id) {
$('#ffInput_exchange_rate_instruction').text(getExchangeInstructions()); $('#ffInput_exchange_rate_instruction').text(getExchangeInstructions());
} }
} }

View File

@ -62,7 +62,11 @@
{# INSTRUCTIONS FOR EXCHANGE RATES #} {# INSTRUCTIONS FOR EXCHANGE RATES #}
{{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }} {{ ExpandedForm.staticText('exchange_rate_instruction','(here be text)') }}
{{ ExpandedForm.nonSelectableAmount('native_amount', data.native_amount, {'currency' : data.native_currency}) }} {{ ExpandedForm.nonSelectableAmount('native_amount', data.native_amount, {currency: data.native_currency}) }}
{{ ExpandedForm.nonSelectableAmount('source_amount', data.native_amount, {currency: data.native_currency }) }}
{{ ExpandedForm.nonSelectableAmount('destination_amount', data.amount, {currency: data.currency }) }}
{# ALWAYS SHOW DATE #} {# ALWAYS SHOW DATE #}
{{ ExpandedForm.date('date',data['date']) }} {{ ExpandedForm.date('date',data['date']) }}
@ -245,6 +249,7 @@
var journal = {{ journal.toArray()|json_encode|raw }}; var journal = {{ journal.toArray()|json_encode|raw }};
var journalData = {{ data|json_encode|raw }}; var journalData = {{ data|json_encode|raw }};
var exchangeRateInstructions = "{{ 'exchange_rate_instructions'|_|escape('js') }}"; var exchangeRateInstructions = "{{ 'exchange_rate_instructions'|_|escape('js') }}";
var transferInstructions = "{{ 'transfer_exchange_rate_instructions'|_|escape('js') }}";
</script> </script>
<script type="text/javascript" src="js/ff/transactions/single/common.js"></script> <script type="text/javascript" src="js/ff/transactions/single/common.js"></script>
<script type="text/javascript" src="js/ff/transactions/single/edit.js"></script> <script type="text/javascript" src="js/ff/transactions/single/edit.js"></script>