2016-04-29 20:59:28 +02:00
|
|
|
/*
|
|
|
|
* from-store.js
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* globals globalSum */
|
|
|
|
|
2016-07-26 20:40:46 +02:00
|
|
|
var destAccounts = {};
|
|
|
|
var srcAccounts = {};
|
|
|
|
var categories = {};
|
2016-04-29 20:59:28 +02:00
|
|
|
$(function () {
|
|
|
|
"use strict";
|
|
|
|
$('.btn-do-split').click(cloneRow);
|
2016-10-22 07:28:31 +02:00
|
|
|
$('.remove-current-split').click(removeRow);
|
2016-07-26 20:40:46 +02:00
|
|
|
|
|
|
|
$.getJSON('json/expense-accounts').done(function (data) {
|
|
|
|
destAccounts = data;
|
|
|
|
console.log('destAccounts length is now ' + destAccounts.length);
|
2016-10-21 21:41:31 +02:00
|
|
|
$('input[name$="destination_account_name]"]').typeahead({source: destAccounts});
|
2016-07-26 20:40:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$.getJSON('json/revenue-accounts').done(function (data) {
|
|
|
|
srcAccounts = data;
|
|
|
|
console.log('srcAccounts length is now ' + srcAccounts.length);
|
2016-10-21 21:41:31 +02:00
|
|
|
$('input[name$="source_account_name]"]').typeahead({source: srcAccounts});
|
2016-07-26 20:40:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
$.getJSON('json/categories').done(function (data) {
|
|
|
|
categories = data;
|
|
|
|
console.log('categories length is now ' + categories.length);
|
2016-10-21 21:41:31 +02:00
|
|
|
$('input[name$="category]"]').typeahead({source: categories});
|
2016-07-26 20:40:46 +02:00
|
|
|
});
|
|
|
|
|
2016-10-21 21:41:31 +02:00
|
|
|
$('input[name$="][amount]"]').on('input', calculateSum);
|
|
|
|
|
|
|
|
// add auto complete:
|
|
|
|
|
|
|
|
|
2016-10-22 07:28:31 +02:00
|
|
|
});
|
2016-10-21 21:41:31 +02:00
|
|
|
|
2016-10-22 07:28:31 +02:00
|
|
|
function removeRow(e) {
|
|
|
|
"use strict";
|
|
|
|
var rows = $('table.split-table tbody tr');
|
|
|
|
if (rows.length === 1) {
|
|
|
|
console.log('Will not remove last split');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var row = $(e.target);
|
|
|
|
var index = row.data('split');
|
|
|
|
console.log('Trying to remove row with split ' + index);
|
|
|
|
$('table.split-table tbody tr[data-split="' + index + '"]').remove();
|
2016-10-21 21:41:31 +02:00
|
|
|
|
2016-10-22 07:28:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
resetSplits();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2016-04-29 20:59:28 +02:00
|
|
|
|
|
|
|
function cloneRow() {
|
|
|
|
"use strict";
|
2016-10-22 07:28:31 +02:00
|
|
|
var source = $('.table.split-table tbody tr').last().clone();
|
2016-04-29 20:59:28 +02:00
|
|
|
var count = $('.split-table tbody tr').length + 1;
|
2016-10-21 21:41:31 +02:00
|
|
|
var index = count - 1;
|
2016-04-29 20:59:28 +02:00
|
|
|
source.removeClass('initial-row');
|
|
|
|
source.find('.count').text('#' + count);
|
2016-10-21 21:41:31 +02:00
|
|
|
|
2016-10-22 07:28:31 +02:00
|
|
|
// // get each input, change the name?
|
|
|
|
// $.each(source.find('input, select'), function (i, v) {
|
|
|
|
// var obj = $(v);
|
|
|
|
// var name = obj.attr('name').replace('[0]', '[' + index + ']');
|
|
|
|
// obj.attr('name', name);
|
|
|
|
// });
|
2016-10-21 21:41:31 +02:00
|
|
|
|
|
|
|
source.find('input[name$="][amount]"]').val("").on('input', calculateSum);
|
2016-07-26 20:40:46 +02:00
|
|
|
if (destAccounts.length > 0) {
|
|
|
|
console.log('Will be able to extend dest-accounts.');
|
2016-10-21 21:41:31 +02:00
|
|
|
source.find('input[name$="destination_account_name]"]').typeahead({source: destAccounts});
|
2016-07-26 20:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (destAccounts.length > 0) {
|
|
|
|
console.log('Will be able to extend src-accounts.');
|
2016-10-21 21:41:31 +02:00
|
|
|
source.find('input[name$="source_account_name]"]').typeahead({source: srcAccounts});
|
2016-07-26 20:40:46 +02:00
|
|
|
}
|
2016-10-21 21:41:31 +02:00
|
|
|
if (categories.length > 0) {
|
2016-07-26 20:40:46 +02:00
|
|
|
console.log('Will be able to extend categories.');
|
2016-10-21 21:41:31 +02:00
|
|
|
source.find('input[name$="category]"]').typeahead({source: categories});
|
2016-07-26 20:40:46 +02:00
|
|
|
}
|
2016-04-29 20:59:28 +02:00
|
|
|
|
|
|
|
$('.split-table tbody').append(source);
|
|
|
|
|
2016-10-22 07:28:31 +02:00
|
|
|
// remove original click things, add them again:
|
|
|
|
$('.remove-current-split').unbind('click').click(removeRow);
|
|
|
|
|
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
calculateSum();
|
2016-10-22 07:28:31 +02:00
|
|
|
resetSplits();
|
2016-04-29 20:59:28 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-22 07:28:31 +02:00
|
|
|
function resetSplits() {
|
|
|
|
"use strict";
|
|
|
|
// loop rows, reset numbers:
|
|
|
|
|
|
|
|
// update the row split number:
|
|
|
|
$.each($('table.split-table tbody tr'), function (i, v) {
|
|
|
|
var row = $(v);
|
|
|
|
row.attr('data-split', i);
|
|
|
|
console.log('Row is now ' + row.data('split'));
|
|
|
|
});
|
|
|
|
|
|
|
|
// loop each remove button, update the index
|
|
|
|
$.each($('.remove-current-split'), function (i, v) {
|
|
|
|
var button = $(v);
|
|
|
|
button.attr('data-split', i);
|
|
|
|
button.find('i').attr('data-split', i);
|
|
|
|
console.log('Remove button index is now ' + button.data('split'));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// loop each indicator (#) and update it:
|
|
|
|
$.each($('td.count'), function (i, v) {
|
|
|
|
var cell = $(v);
|
|
|
|
var index = i + 1;
|
|
|
|
cell.text('#' + index);
|
|
|
|
console.log('Cell is now ' + cell.text());
|
|
|
|
});
|
|
|
|
|
|
|
|
// loop each possible field.
|
|
|
|
|
|
|
|
// ends with ][description]
|
|
|
|
$.each($('input[name$="][description]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
2016-10-23 12:37:12 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][description]');
|
2016-10-22 07:28:31 +02:00
|
|
|
console.log('description is now ' + input.attr('name'));
|
|
|
|
});
|
|
|
|
// ends with ][destination_account_name]
|
|
|
|
$.each($('input[name$="][destination_account_name]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
2016-10-23 12:37:12 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][destination_account_name]');
|
2016-10-22 07:28:31 +02:00
|
|
|
console.log('destination_account_name is now ' + input.attr('name'));
|
|
|
|
});
|
|
|
|
// ends with ][source_account_name]
|
|
|
|
$.each($('input[name$="][source_account_name]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
2016-11-18 18:58:48 +01:00
|
|
|
input.attr('name', 'transactions[' + i + '][source_account_name]');
|
2016-10-22 07:28:31 +02:00
|
|
|
console.log('source_account_name is now ' + input.attr('name'));
|
|
|
|
});
|
|
|
|
// ends with ][amount]
|
|
|
|
$.each($('input[name$="][amount]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
2016-10-23 12:37:12 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][amount]');
|
2016-10-22 07:28:31 +02:00
|
|
|
console.log('amount is now ' + input.attr('name'));
|
|
|
|
});
|
|
|
|
// ends with ][budget_id]
|
|
|
|
$.each($('input[name$="][budget_id]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
2016-10-23 12:37:12 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][budget_id]');
|
2016-10-22 07:28:31 +02:00
|
|
|
console.log('budget_id is now ' + input.attr('name'));
|
|
|
|
});
|
|
|
|
// ends with ][category]
|
|
|
|
$.each($('input[name$="][category]"]'), function (i, v) {
|
|
|
|
var input = $(v);
|
2016-10-23 12:37:12 +02:00
|
|
|
input.attr('name', 'transactions[' + i + '][category]');
|
2016-10-22 07:28:31 +02:00
|
|
|
console.log('category is now ' + input.attr('name'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-29 20:59:28 +02:00
|
|
|
function calculateSum() {
|
|
|
|
"use strict";
|
|
|
|
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]);
|
|
|
|
sum += (current.val() == "" ? 0 : parseFloat(current.val()));
|
|
|
|
|
|
|
|
}
|
|
|
|
console.log("Sum is now " + sum);
|
|
|
|
$('.amount-warning').remove();
|
|
|
|
if (sum != originalSum) {
|
|
|
|
console.log(sum + ' does not match ' + originalSum);
|
2016-05-05 07:45:40 +02:00
|
|
|
var holder = $('#journal_amount_holder');
|
2016-04-29 20:59:28 +02:00
|
|
|
var par = holder.find('p.form-control-static');
|
|
|
|
var amount = $('<span>').text(' (' + accounting.formatMoney(sum) + ')').addClass('text-danger amount-warning').appendTo(par);
|
|
|
|
}
|
|
|
|
}
|