2016-01-29 18:39:50 +01:00
|
|
|
/*
|
|
|
|
|
* create.js
|
2017-01-15 19:07:31 +01:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-12-23 07:02:45 +01:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* This file is part of Firefly III.
|
|
|
|
|
*
|
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 14:43:13 +01:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-01-29 18:39:50 +01:00
|
|
|
*/
|
|
|
|
|
|
2017-07-23 08:16:11 +02:00
|
|
|
/** global: currencyInfo, overruleCurrency,useAccountCurrency, accountInfo, what,Modernizr, title, breadcrumbs, middleCrumbName, button, piggiesLength, txt, middleCrumbUrl,exchangeRateInstructions, convertForeignToNative, convertSourceToDestination, selectsForeignCurrency, accountInfo */
|
2017-01-02 10:34:01 +01:00
|
|
|
|
2015-02-24 21:10:25 +01:00
|
|
|
$(document).ready(function () {
|
2015-05-24 20:41:14 +02:00
|
|
|
"use strict";
|
2015-03-26 22:52:49 +01:00
|
|
|
|
2018-02-24 09:17:48 +01:00
|
|
|
// hide ALL exchange things and AMOUNT fields
|
2017-04-14 22:25:48 +02:00
|
|
|
$('#exchange_rate_instruction_holder').hide();
|
|
|
|
|
$('#native_amount_holder').hide();
|
|
|
|
|
$('#amount_holder').hide();
|
|
|
|
|
$('#source_amount_holder').hide();
|
|
|
|
|
$('#destination_amount_holder').hide();
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
// respond to switch buttons (first time always triggers)
|
2017-03-03 18:19:25 +01:00
|
|
|
updateButtons();
|
|
|
|
|
updateForm();
|
|
|
|
|
updateLayout();
|
|
|
|
|
updateDescription();
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2017-04-14 10:16:52 +02:00
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// when user changes source account or destination, native currency may be different.
|
2018-07-02 15:37:56 +02:00
|
|
|
$('select[name="source_id"]').on('change', function () {
|
2018-02-24 09:17:48 +01:00
|
|
|
selectsDifferentSource();
|
|
|
|
|
// do something for transfers:
|
|
|
|
|
validateCurrencyForTransfer();
|
|
|
|
|
});
|
2018-07-02 15:37:56 +02:00
|
|
|
$('select[name="destination_id"]').on('change', function () {
|
2018-02-24 09:17:48 +01:00
|
|
|
selectsDifferentDestination();
|
|
|
|
|
// do something for transfers:
|
|
|
|
|
validateCurrencyForTransfer();
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// convert foreign currency to native currency (when input changes, exchange rate)
|
2017-04-14 15:42:54 +02:00
|
|
|
$('#ffInput_amount').on('change', convertForeignToNative);
|
2017-04-14 10:16:52 +02:00
|
|
|
|
2017-04-14 22:25:48 +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
|
|
|
// when user selects different currency,
|
|
|
|
|
$('.currency-option').on('click', selectsForeignCurrency);
|
2018-07-02 15:37:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// overrule click on currency:
|
|
|
|
|
if(useAccountCurrency === false) {
|
|
|
|
|
$('.currency-option[data-id="' + overruleCurrency + '"]').click();
|
|
|
|
|
$('[data-toggle="dropdown"]').parent().removeClass('open');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-03 10:51:02 +02:00
|
|
|
$('#ffInput_description').focus();
|
2015-06-03 17:32:50 +02:00
|
|
|
});
|
|
|
|
|
|
2018-02-24 09:17:48 +01:00
|
|
|
/**
|
|
|
|
|
* The user selects a different source account. Applies to withdrawals
|
|
|
|
|
* and transfers.
|
|
|
|
|
*/
|
|
|
|
|
function selectsDifferentSource() {
|
2018-07-02 15:37:56 +02:00
|
|
|
console.log('Now in selectsDifferentSource()');
|
2018-02-24 09:17:48 +01:00
|
|
|
if (what === "deposit") {
|
|
|
|
|
console.log('User is making a deposit. Don\'t bother with source.');
|
|
|
|
|
$('input[name="source_account_currency"]').val("0");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// store original currency ID of the selected account in a separate var:
|
2018-06-30 05:21:21 +02:00
|
|
|
var sourceId = $('select[name="source_id"]').val();
|
2018-02-24 09:17:48 +01:00
|
|
|
var sourceCurrency = accountInfo[sourceId].preferredCurrency;
|
|
|
|
|
$('input[name="source_account_currency"]').val(sourceCurrency);
|
2018-03-09 04:04:42 +01:00
|
|
|
console.log('selectsDifferenctSource(): Set source account currency to ' + sourceCurrency);
|
2018-02-24 09:17:48 +01:00
|
|
|
|
|
|
|
|
// change input thing:
|
2018-07-02 15:37:56 +02:00
|
|
|
console.log('Emulate click on .currency-option[data-id="' + sourceCurrency + '"]');
|
2018-02-24 09:17:48 +01:00
|
|
|
$('.currency-option[data-id="' + sourceCurrency + '"]').click();
|
|
|
|
|
$('[data-toggle="dropdown"]').parent().removeClass('open');
|
2018-06-30 05:21:21 +02:00
|
|
|
$('select[name="source_id"]').focus();
|
2018-02-24 09:17:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The user selects a different source account. Applies to withdrawals
|
|
|
|
|
* and transfers.
|
|
|
|
|
*/
|
|
|
|
|
function selectsDifferentDestination() {
|
|
|
|
|
if (what === "withdrawal") {
|
|
|
|
|
console.log('User is making a withdrawal. Don\'t bother with destination.');
|
|
|
|
|
$('input[name="destination_account_currency"]').val("0");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// store original currency ID of the selected account in a separate var:
|
2018-06-30 05:21:21 +02:00
|
|
|
var destinationId = $('select[name="destination_id"]').val();
|
2018-02-24 09:17:48 +01:00
|
|
|
var destinationCurrency = accountInfo[destinationId].preferredCurrency;
|
|
|
|
|
$('input[name="destination_account_currency"]').val(destinationCurrency);
|
2018-03-09 04:04:42 +01:00
|
|
|
console.log('selectsDifferentDestination(): Set destinationId account currency to ' + destinationCurrency);
|
2018-02-24 09:17:48 +01:00
|
|
|
|
|
|
|
|
// change input thing:
|
|
|
|
|
$('.currency-option[data-id="' + destinationCurrency + '"]').click();
|
|
|
|
|
$('[data-toggle="dropdown"]').parent().removeClass('open');
|
2018-06-30 05:21:21 +02:00
|
|
|
$('select[name="destination_id"]').focus();
|
2018-02-24 09:17:48 +01: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}
|
|
|
|
|
*/
|
|
|
|
|
function getExchangeInstructions() {
|
|
|
|
|
var foreignCurrencyId = parseInt($('input[name="amount_currency_id_amount"]').val());
|
|
|
|
|
var selectedAccountId = getAccountId();
|
|
|
|
|
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;
|
|
|
|
|
}
|
2017-04-14 10:16:52 +02:00
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2017-01-15 19:07:31 +01:00
|
|
|
function updateDescription() {
|
|
|
|
|
$.getJSON('json/transaction-journals/' + what).done(function (data) {
|
2018-01-22 18:37:59 +01:00
|
|
|
$('input[name="description"]').typeahead('destroy').typeahead({source: data, autoSelect: false});
|
2017-01-15 19:07:31 +01:00
|
|
|
});
|
2017-09-03 10:51:02 +02:00
|
|
|
$('#ffInput_description').focus();
|
2017-01-15 19:07:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function updateLayout() {
|
|
|
|
|
"use strict";
|
|
|
|
|
$('#subTitle').text(title[what]);
|
|
|
|
|
$('.breadcrumb .active').text(breadcrumbs[what]);
|
|
|
|
|
$('.breadcrumb li:nth-child(2)').html('<a href="' + middleCrumbUrl[what] + '">' + middleCrumbName[what] + '</a>');
|
2018-06-10 10:41:45 +02:00
|
|
|
$('.transaction-btn').text(button[what]);
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function updateForm() {
|
|
|
|
|
"use strict";
|
2018-07-02 15:37:56 +02:00
|
|
|
console.log('Now in updateForm()');
|
2015-06-03 17:32:50 +02:00
|
|
|
|
|
|
|
|
$('input[name="what"]').val(what);
|
2017-04-15 07:25:09 +02:00
|
|
|
|
2018-06-30 05:21:21 +02:00
|
|
|
var destName = $('#ffInput_destination_name');
|
|
|
|
|
var srcName = $('#ffInput_source_name');
|
2017-04-15 07:25:09 +02:00
|
|
|
|
2015-06-03 17:32:50 +02:00
|
|
|
switch (what) {
|
2017-06-05 11:12:50 +02:00
|
|
|
|
2015-06-03 17:32:50 +02:00
|
|
|
case 'withdrawal':
|
2017-04-14 22:25:48 +02:00
|
|
|
// show source_id and dest_name
|
2018-06-30 05:21:21 +02:00
|
|
|
document.getElementById('source_id_holder').style.display = 'block';
|
|
|
|
|
document.getElementById('destination_name_holder').style.display = 'block';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide others:
|
2018-06-30 05:21:21 +02:00
|
|
|
document.getElementById('source_name_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_id_holder').style.display = 'none';
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('budget_id_holder').style.display = 'block';
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2016-04-29 17:29:13 +02:00
|
|
|
// hide piggy bank:
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('piggy_bank_id_holder').style.display = 'none';
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2017-04-15 07:25:09 +02:00
|
|
|
// copy destination account name to source account name:
|
|
|
|
|
if (destName.val().length > 0) {
|
|
|
|
|
srcName.val(destName.val());
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// exchange / foreign currencies:
|
2017-04-15 07:25:09 +02:00
|
|
|
// hide explanation, hide source and destination amounts, show normal amount
|
|
|
|
|
document.getElementById('exchange_rate_instruction_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('source_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('amount_holder').style.display = 'block';
|
2015-06-03 17:32:50 +02:00
|
|
|
break;
|
|
|
|
|
case 'deposit':
|
2016-04-29 17:29:13 +02:00
|
|
|
// show source_name and dest_id:
|
2018-06-30 05:21:21 +02:00
|
|
|
document.getElementById('source_name_holder').style.display = 'block';
|
|
|
|
|
document.getElementById('destination_id_holder').style.display = 'block';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide others:
|
2018-06-30 05:21:21 +02:00
|
|
|
document.getElementById('source_id_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_name_holder').style.display = 'none';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide budget
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('budget_id_holder').style.display = 'none';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide piggy bank
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('piggy_bank_id_holder').style.display = 'none';
|
2015-06-03 17:32:50 +02:00
|
|
|
|
2017-04-15 07:25:09 +02:00
|
|
|
// copy name
|
|
|
|
|
if (srcName.val().length > 0) {
|
|
|
|
|
destName.val(srcName.val());
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 22:25:48 +02:00
|
|
|
// exchange / foreign currencies:
|
2017-04-15 07:25:09 +02:00
|
|
|
// hide explanation, hide source and destination amounts, show amount
|
|
|
|
|
document.getElementById('exchange_rate_instruction_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('source_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_amount_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('amount_holder').style.display = 'block';
|
2015-06-03 17:32:50 +02:00
|
|
|
break;
|
|
|
|
|
case 'transfer':
|
2016-04-29 17:29:13 +02:00
|
|
|
// show source_id and dest_id:
|
2018-06-30 05:21:21 +02:00
|
|
|
document.getElementById('source_id_holder').style.display = 'block';
|
|
|
|
|
document.getElementById('destination_id_holder').style.display = 'block';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide others:
|
2018-06-30 05:21:21 +02:00
|
|
|
document.getElementById('source_name_holder').style.display = 'none';
|
|
|
|
|
document.getElementById('destination_name_holder').style.display = 'none';
|
2016-04-29 17:29:13 +02:00
|
|
|
|
|
|
|
|
// hide budget
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('budget_id_holder').style.display = 'none';
|
|
|
|
|
|
|
|
|
|
// optional piggies
|
|
|
|
|
var showPiggies = 'block';
|
2018-06-17 15:14:34 +02:00
|
|
|
if ($('#ffInput_piggy_bank_id option').length === 0) {
|
2017-04-15 07:25:09 +02:00
|
|
|
showPiggies = 'none';
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
2017-04-15 07:25:09 +02:00
|
|
|
document.getElementById('piggy_bank_id_holder').style.display = showPiggies;
|
2017-01-02 12:18:29 +01:00
|
|
|
break;
|
2017-06-05 11:12:50 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
2017-07-07 17:51:14 +02:00
|
|
|
// get instructions all the time.
|
2018-02-24 09:17:48 +01:00
|
|
|
console.log('End of update form');
|
|
|
|
|
selectsDifferentSource();
|
|
|
|
|
selectsDifferentDestination();
|
2017-07-07 17:51:14 +02:00
|
|
|
selectsForeignCurrency();
|
2018-02-24 09:17:48 +01:00
|
|
|
|
|
|
|
|
// do something for transfers:
|
|
|
|
|
validateCurrencyForTransfer();
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function updateButtons() {
|
|
|
|
|
"use strict";
|
|
|
|
|
$('.switch').each(function (i, v) {
|
|
|
|
|
var button = $(v);
|
|
|
|
|
|
|
|
|
|
// remove click event:
|
|
|
|
|
button.unbind('click');
|
|
|
|
|
// new click event:
|
|
|
|
|
button.bind('click', clickButton);
|
|
|
|
|
|
2017-04-09 07:56:46 +02:00
|
|
|
if (button.data('what') === what) {
|
2015-06-03 17:32:50 +02:00
|
|
|
button.removeClass('btn-default').addClass('btn-info').html('<i class="fa fa-fw fa-check"></i> ' + txt[button.data('what')]);
|
|
|
|
|
} else {
|
|
|
|
|
button.removeClass('btn-info').addClass('btn-default').text(txt[button.data('what')]);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-14 15:42:54 +02:00
|
|
|
/**
|
2017-04-14 15:56:43 +02:00
|
|
|
*
|
2017-04-14 15:42:54 +02:00
|
|
|
* @param e
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2015-06-03 17:32:50 +02:00
|
|
|
function clickButton(e) {
|
|
|
|
|
"use strict";
|
|
|
|
|
var button = $(e.target);
|
|
|
|
|
var newWhat = button.data('what');
|
2017-04-09 07:56:46 +02:00
|
|
|
if (newWhat !== what) {
|
2015-06-03 17:32:50 +02:00
|
|
|
what = newWhat;
|
|
|
|
|
updateButtons();
|
|
|
|
|
updateForm();
|
|
|
|
|
updateLayout();
|
2017-01-15 19:07:31 +01:00
|
|
|
updateDescription();
|
2015-06-03 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
2017-04-14 14:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get accountID based on some meta info.
|
|
|
|
|
*/
|
|
|
|
|
function getAccountId() {
|
2017-04-14 15:42:54 +02:00
|
|
|
if (what === "withdrawal") {
|
2018-06-30 05:21:21 +02:00
|
|
|
return $('select[name="source_id"]').val();
|
2017-04-14 14:37:04 +02:00
|
|
|
}
|
2017-04-14 22:25:48 +02:00
|
|
|
if (what === "deposit" || what === "transfer") {
|
2018-06-30 05:21:21 +02:00
|
|
|
return $('select[name="destination_id"]').val();
|
2017-04-14 14:48:44 +02:00
|
|
|
}
|
2017-06-05 11:12:50 +02:00
|
|
|
return undefined;
|
2017-04-14 15:42:54 +02:00
|
|
|
}
|