2016-04-29 20:59:28 +02:00
|
|
|
/*
|
2017-01-15 19:16:46 +01:00
|
|
|
* edit.js
|
|
|
|
* 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-04-29 20:59:28 +02:00
|
|
|
*/
|
|
|
|
|
2017-01-15 19:16:46 +01:00
|
|
|
|
2018-04-22 09:16:51 +02:00
|
|
|
/** global: originalSum,originalForeignSum, accounting, what, Modernizr, currencySymbol, foreignCurrencySymbol */
|
2017-01-02 10:34:01 +01:00
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
var destNames;
|
|
|
|
var sourceNames;
|
|
|
|
var categories;
|
|
|
|
var journalNames;
|
2017-01-15 19:16:46 +01:00
|
|
|
|
|
|
|
$(document).ready(function () {
|
2016-04-29 20:59:28 +02:00
|
|
|
"use strict";
|
2017-07-02 09:22:56 +02:00
|
|
|
$('.btn-do-split').click(cloneDivRow);
|
|
|
|
$('.remove-current-split').click(removeDivRow);
|
2016-07-26 20:40:46 +02:00
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
// auto complete destination name (expense accounts):
|
|
|
|
destNames = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
prefetch: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/expense-accounts?uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/expense-accounts?search=%QUERY&uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
wildcard: '%QUERY',
|
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
destNames.initialize();
|
|
|
|
$('input[name$="destination_name]"]').typeahead({hint: true, highlight: true,}, {source: destNames, displayKey: 'name', autoSelect: false});
|
2016-07-26 20:40:46 +02:00
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
// auto complete source name (revenue accounts):
|
|
|
|
sourceNames = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
prefetch: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/revenue-accounts?uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/revenue-accounts?search=%QUERY&uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
wildcard: '%QUERY',
|
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sourceNames.initialize();
|
|
|
|
$('input[name$="source_name]"]').typeahead({hint: true, highlight: true,}, {source: sourceNames, displayKey: 'name', autoSelect: false});
|
2016-07-26 20:40:46 +02:00
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
// auto complete category fields:
|
|
|
|
categories = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
prefetch: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/categories?uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/categories?search=%QUERY&uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
wildcard: '%QUERY',
|
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
categories.initialize();
|
|
|
|
$('input[name$="category_name]"]').typeahead({hint: true, highlight: true,}, {source: categories, displayKey: 'name', autoSelect: false});
|
2016-10-21 21:41:31 +02:00
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
// get transaction journal name things:
|
|
|
|
journalNames = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
prefetch: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/transaction-journals/' + what + '?uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/transaction-journals/' + what + '?search=%QUERY&uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
wildcard: '%QUERY',
|
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
journalNames.initialize();
|
2017-01-15 19:16:46 +01:00
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
$('input[name="journal_description"]').typeahead({hint: true, highlight: true,}, {source: journalNames, displayKey: 'name', autoSelect: false});
|
|
|
|
$('input[name$="transaction_description]"]').typeahead({hint: true, highlight: true,}, {source: journalNames, displayKey: 'name', autoSelect: false});
|
2016-10-21 21:41:31 +02:00
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
// get tags:
|
|
|
|
console.log('initTagsAC()');
|
|
|
|
var tagTags = new Bloodhound({
|
|
|
|
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
|
|
|
|
queryTokenizer: Bloodhound.tokenizers.whitespace,
|
|
|
|
prefetch: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/tags?uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (tagTag) {
|
|
|
|
return {name: tagTag};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
remote: {
|
2018-10-28 19:17:33 +01:00
|
|
|
url: 'json/tags?search=%QUERY&uid=' + uid,
|
2018-09-23 07:39:04 +02:00
|
|
|
wildcard: '%QUERY',
|
|
|
|
filter: function (list) {
|
|
|
|
return $.map(list, function (name) {
|
|
|
|
return {name: name};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
tagTags.initialize();
|
|
|
|
$('input[name="tags"]').tagsinput({
|
|
|
|
typeaheadjs: {
|
|
|
|
hint: true,
|
|
|
|
highlight: true,
|
|
|
|
name: 'tags',
|
|
|
|
displayKey: 'name',
|
|
|
|
valueKey: 'name',
|
|
|
|
source: tagTags.ttAdapter()
|
|
|
|
}
|
|
|
|
});
|
2016-10-21 21:41:31 +02:00
|
|
|
|
2018-04-22 09:16:51 +02:00
|
|
|
$('input[name$="][amount]"]').on('change', calculateBothSums);
|
|
|
|
$('input[name$="][foreign_amount]"]').on('change', calculateBothSums);
|
2017-01-20 08:18:52 +01:00
|
|
|
|
|
|
|
if (!Modernizr.inputtypes.date) {
|
|
|
|
$('input[type="date"]').datepicker(
|
|
|
|
{
|
|
|
|
dateFormat: 'yy-mm-dd'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-10-22 07:28:31 +02:00
|
|
|
});
|
2016-10-21 21:41:31 +02:00
|
|
|
|
2018-04-22 09:16:51 +02:00
|
|
|
function calculateBothSums() {
|
|
|
|
console.log("Now in calculateBothSums()");
|
|
|
|
calculateSum();
|
|
|
|
calculateForeignSum();
|
|
|
|
}
|
|
|
|
|
2017-07-02 09:22:56 +02:00
|
|
|
/**
|
|
|
|
* New and cool
|
|
|
|
* @param e
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function removeDivRow(e) {
|
|
|
|
"use strict";
|
|
|
|
var rows = $('div.split_row');
|
|
|
|
if (rows.length === 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var row = $(e.target);
|
|
|
|
var index = row.data('split');
|
2018-06-24 15:43:05 +02:00
|
|
|
if (typeof index === 'undefined') {
|
|
|
|
var parent = row.parent();
|
|
|
|
index = parent.data('split');
|
|
|
|
console.log('Parent. ' + parent.className);
|
|
|
|
}
|
|
|
|
console.log('Split index is "' + index + '"');
|
2017-07-02 09:22:56 +02:00
|
|
|
$('div.split_row[data-split="' + index + '"]').remove();
|
|
|
|
|
2017-01-15 19:16:46 +01:00
|
|
|
|
2017-07-02 09:22:56 +02:00
|
|
|
resetDivSplits();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* New and cool
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function cloneDivRow() {
|
|
|
|
"use strict";
|
|
|
|
var source = $('div.split_row').last().clone();
|
|
|
|
var count = $('div.split_row').length + 1;
|
|
|
|
source.removeClass('initial-row');
|
|
|
|
source.find('.count').text('#' + count);
|
|
|
|
|
2018-04-22 09:16:51 +02:00
|
|
|
source.find('input[name$="][amount]"]').val("").on('change', calculateBothSums);
|
|
|
|
source.find('input[name$="][foreign_amount]"]').val("").on('change', calculateBothSums);
|
2018-09-23 07:39:04 +02:00
|
|
|
if (destNames) {
|
|
|
|
source.find('input[name$="destination_name]"]').typeahead({hint: true, highlight: true,}, {source: destNames, displayKey: 'name', autoSelect: false});
|
2017-07-02 09:22:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 07:39:04 +02:00
|
|
|
if (sourceNames) {
|
|
|
|
source.find('input[name$="source_name]"]').typeahead({hint: true, highlight: true,}, {source: sourceNames, displayKey: 'name', autoSelect: false});
|
2017-07-02 09:22:56 +02:00
|
|
|
}
|
2018-09-23 07:39:04 +02:00
|
|
|
if (categories) {
|
|
|
|
source.find('input[name$="category_name]"]').typeahead({hint: true, highlight: true,}, {source: categories, displayKey: 'name', autoSelect: false});
|
2017-07-02 09:22:56 +02:00
|
|
|
}
|
2018-09-23 07:39:04 +02:00
|
|
|
if (journalNames) {
|
|
|
|
source.find('input[name$="transaction_description]"]').typeahead({hint: true, highlight: true,}, {source: journalNames, displayKey: 'name', autoSelect: false});
|
2017-07-02 09:22:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$('div.split_row_holder').append(source);
|
|
|
|
|
|
|
|
// remove original click things, add them again:
|
2017-07-02 09:24:00 +02:00
|
|
|
$('.remove-current-split').unbind('click').click(removeDivRow);
|
2017-07-02 09:22:56 +02:00
|
|
|
|
2018-04-22 09:16:51 +02:00
|
|
|
calculateBothSums();
|
2017-07-02 09:22:56 +02:00
|
|
|
resetDivSplits();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* New and hip
|
|
|
|
*/
|
|
|
|
function resetDivSplits() {
|
|
|
|
"use strict";
|
|
|
|
// loop rows, reset numbers:
|
|
|
|
|
|
|
|
// update the row split number:
|
|
|
|
$.each($('div.split_row'), function (i, v) {
|
|
|
|
var row = $(v);
|
|
|
|
row.attr('data-split', i);
|
|
|
|
|
|
|
|
// add or remove class with bg thing
|
2017-07-08 06:28:44 +02:00
|
|
|
if (i % 2 === 0) {
|
2017-07-02 09:22:56 +02:00
|
|
|
row.removeClass('bg-gray-light');
|
|
|
|
}
|
2017-07-08 06:28:44 +02:00
|
|
|
if (i % 2 === 1) {
|
2017-07-02 09:22:56 +02:00
|
|
|
row.addClass('bg-gray-light');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// loop each remove button, update the index
|
|
|
|
$.each($('.remove-current-split'), function (i, v) {
|
|
|
|
var button = $(v);
|
|
|
|
button.attr('data-split', i);
|
2018-04-22 09:16:51 +02:00
|
|
|
button.find('span').text(' #' + (i + 1));
|
2017-07-02 09:22:56 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// loop each possible field.
|
|
|
|
|
|
|
|
// ends with ][description]
|
2018-04-04 19:14:47 +02:00
|
|
|
$.each($('input[name$="][transaction_description]"]'), function (i, v) {
|
2017-07-02 09:22:56 +02:00
|
|
|
var input = $(v);
|
2018-04-04 19:14:47 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][transaction_description]');
|
2017-07-02 09:22:56 +02:00
|
|
|
});
|
2018-06-30 05:21:21 +02:00
|
|
|
// ends with ][destination_name]
|
2018-05-31 21:48:09 +02:00
|
|
|
$.each($('input[name$="][destination_name]"]'), function (i, v) {
|
2017-07-02 09:22:56 +02:00
|
|
|
var input = $(v);
|
2018-06-24 13:46:34 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][destination_name]');
|
2017-07-02 09:22:56 +02:00
|
|
|
});
|
2018-06-30 05:21:21 +02:00
|
|
|
// ends with ][source_name]
|
2018-05-31 21:48:09 +02:00
|
|
|
$.each($('input[name$="][source_name]"]'), function (i, v) {
|
2017-07-02 09:22:56 +02:00
|
|
|
var input = $(v);
|
2018-06-24 13:46:34 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][source_name]');
|
2017-07-02 09:22:56 +02:00
|
|
|
});
|
|
|
|
// ends with ][amount]
|
|
|
|
$.each($('input[name$="][amount]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
|
|
|
input.attr('name', 'transactions[' + i + '][amount]');
|
|
|
|
});
|
|
|
|
|
|
|
|
// ends with ][foreign_amount]
|
|
|
|
$.each($('input[name$="][foreign_amount]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
|
|
|
input.attr('name', 'transactions[' + i + '][foreign_amount]');
|
|
|
|
});
|
|
|
|
|
2018-12-23 07:34:51 +01:00
|
|
|
// ends with ][currency_id]
|
|
|
|
$.each($('input[name$="][currency_id]"]'), function (i, v) {
|
2017-07-02 09:22:56 +02:00
|
|
|
var input = $(v);
|
2018-12-23 07:34:51 +01:00
|
|
|
input.attr('name', 'transactions[' + i + '][currency_id]');
|
2017-07-02 09:22:56 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// ends with ][foreign_currency_id]
|
|
|
|
$.each($('input[name$="][foreign_currency_id]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
|
|
|
input.attr('name', 'transactions[' + i + '][foreign_currency_id]');
|
|
|
|
});
|
|
|
|
|
|
|
|
// ends with ][budget_id]
|
|
|
|
$.each($('select[name$="][budget_id]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
|
|
|
input.attr('name', 'transactions[' + i + '][budget_id]');
|
|
|
|
});
|
|
|
|
|
|
|
|
// ends with ][category]
|
2018-04-04 19:14:47 +02:00
|
|
|
$.each($('input[name$="][category_name]"]'), function (i, v) {
|
2017-07-02 09:22:56 +02:00
|
|
|
var input = $(v);
|
2018-04-04 19:14:47 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][category_name]');
|
2017-07-02 09:22:56 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-22 07:28:31 +02:00
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
function calculateSum() {
|
|
|
|
"use strict";
|
2018-04-22 09:16:51 +02:00
|
|
|
console.log("Now in calculateSum()");
|
2017-08-12 06:55:46 +02:00
|
|
|
var left = originalSum * -1;
|
2016-04-29 20:59:28 +02:00
|
|
|
var sum = 0;
|
2016-10-21 21:41:31 +02:00
|
|
|
var set = $('input[name$="][amount]"]');
|
2016-04-29 20:59:28 +02:00
|
|
|
for (var i = 0; i < set.length; i++) {
|
|
|
|
var current = $(set[i]);
|
2017-04-09 07:56:46 +02:00
|
|
|
sum += (current.val() === "" ? 0 : parseFloat(current.val()));
|
2017-08-12 06:55:46 +02:00
|
|
|
left += (current.val() === "" ? 0 : parseFloat(current.val()));
|
2016-04-29 20:59:28 +02:00
|
|
|
}
|
2016-12-24 14:43:42 +01:00
|
|
|
sum = Math.round(sum * 100) / 100;
|
2017-08-12 06:55:46 +02:00
|
|
|
left = Math.round(left * 100) / 100;
|
2016-12-24 14:43:42 +01:00
|
|
|
|
2018-04-22 09:16:51 +02:00
|
|
|
console.log("Sum is " + sum + ", left is " + left);
|
2017-08-19 21:59:13 +02:00
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
$('.amount-warning').remove();
|
2017-04-09 07:56:46 +02:00
|
|
|
if (sum !== originalSum) {
|
2018-04-22 09:16:51 +02:00
|
|
|
console.log("Is different from original sum " + originalSum);
|
|
|
|
var paragraph = $('#journal_amount_holder').find('p.form-control-static');
|
|
|
|
|
|
|
|
$('<span>').text(' (' + accounting.formatMoney(sum, currencySymbol) + ')').addClass('text-danger amount-warning').appendTo(paragraph);
|
|
|
|
|
|
|
|
// also add what's left to divide (or vice versa)
|
|
|
|
$('<span>').text(' (' + accounting.formatMoney(left, currencySymbol) + ')').addClass('text-danger amount-warning').appendTo(paragraph);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function calculateForeignSum() {
|
|
|
|
// "use strict";
|
|
|
|
var left = originalForeignSum * -1;
|
|
|
|
var sum = 0;
|
|
|
|
var set = $('input[name$="][foreign_amount]"]');
|
|
|
|
for (var i = 0; i < set.length; i++) {
|
|
|
|
var current = $(set[i]);
|
|
|
|
sum += (current.val() === "" ? 0 : parseFloat(current.val()));
|
|
|
|
left += (current.val() === "" ? 0 : parseFloat(current.val()));
|
|
|
|
}
|
|
|
|
sum = Math.round(sum * 100) / 100;
|
|
|
|
left = Math.round(left * 100) / 100;
|
|
|
|
|
|
|
|
|
|
|
|
$('.amount-warning-foreign').remove();
|
|
|
|
if (sum !== originalForeignSum) {
|
|
|
|
var paragraph = $('#journal_foreign_amount_holder').find('p.form-control-static');
|
|
|
|
$('<span>').text(' (' + accounting.formatMoney(sum, foreignCurrencySymbol) + ')').addClass('text-danger amount-warning-foreign').appendTo(paragraph);
|
|
|
|
|
2017-08-12 06:55:46 +02:00
|
|
|
// also add what's left to divide (or vice versa)
|
2018-04-22 09:16:51 +02:00
|
|
|
$('<span>').text(' (' + accounting.formatMoney(left, foreignCurrencySymbol) + ')').addClass('text-danger amount-warning-foreign').appendTo(paragraph);
|
2016-04-29 20:59:28 +02:00
|
|
|
}
|
2017-08-12 06:55:46 +02:00
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
}
|