2017-11-10 18:25:11 +01:00
|
|
|
/*
|
|
|
|
* reconcile.js
|
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* 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/>.
|
2017-11-10 18:25:11 +01:00
|
|
|
*/
|
|
|
|
|
2017-11-25 08:54:52 +01:00
|
|
|
/** global: overviewUri, transactionsUri, indexUri,accounting */
|
|
|
|
|
2017-11-15 06:29:49 +01:00
|
|
|
var balanceDifference = 0;
|
|
|
|
var difference = 0;
|
|
|
|
var selectedAmount = 0;
|
|
|
|
var reconcileStarted = false;
|
2017-11-24 18:05:58 +01:00
|
|
|
var changedBalances = false;
|
2017-11-10 18:25:11 +01:00
|
|
|
|
2017-11-15 06:29:49 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2017-11-10 18:25:11 +01:00
|
|
|
$(function () {
|
|
|
|
"use strict";
|
2017-11-15 06:29:49 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Respond to changes in balance statements.
|
|
|
|
*/
|
|
|
|
$('input[type="number"]').on('change', function () {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('On type=number change.');
|
2017-11-15 06:29:49 +01:00
|
|
|
if (reconcileStarted) {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('Reconcile has started.');
|
2017-11-15 06:29:49 +01:00
|
|
|
calculateBalanceDifference();
|
|
|
|
difference = balanceDifference - selectedAmount;
|
|
|
|
updateDifference();
|
2017-12-09 19:19:15 +01:00
|
|
|
|
2017-11-15 06:29:49 +01:00
|
|
|
}
|
2017-11-24 18:05:58 +01:00
|
|
|
changedBalances = true;
|
2017-11-15 06:29:49 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
|
|
Respond to changes in the date range.
|
|
|
|
*/
|
|
|
|
$('input[type="date"]').on('change', function () {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('On type=date change.');
|
2017-11-15 06:29:49 +01:00
|
|
|
if (reconcileStarted) {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('Reconcile has started.');
|
2017-11-15 06:29:49 +01:00
|
|
|
// hide original instructions.
|
|
|
|
$('.select_transactions_instruction').hide();
|
|
|
|
|
|
|
|
// show date-change warning
|
|
|
|
$('.date_change_warning').show();
|
|
|
|
|
|
|
|
// show update button
|
|
|
|
$('.change_date_button').show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.change_date_button').click(startReconcile);
|
|
|
|
$('.start_reconcile').click(startReconcile);
|
|
|
|
$('.store_reconcile').click(storeReconcile);
|
|
|
|
|
2017-11-10 18:25:11 +01:00
|
|
|
});
|
|
|
|
|
2017-11-15 06:29:49 +01:00
|
|
|
function storeReconcile() {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in storeReconcile()');
|
2017-11-15 06:29:49 +01:00
|
|
|
// get modal HTML:
|
|
|
|
var ids = [];
|
|
|
|
$.each($('.reconcile_checkbox:checked'), function (i, v) {
|
2018-09-09 21:23:04 +02:00
|
|
|
var obj = $(v);
|
2019-06-22 13:09:11 +02:00
|
|
|
if (obj.data('inrange') === true) {
|
|
|
|
console.log('Added item with amount to list of checked ' + obj.val());
|
|
|
|
ids.push(obj.data('id'));
|
2018-09-09 21:23:04 +02:00
|
|
|
} else {
|
|
|
|
console.log('Ignored item with amount because is not in range ' + obj.val());
|
|
|
|
}
|
2017-11-15 06:29:49 +01:00
|
|
|
});
|
2017-11-22 16:54:49 +01:00
|
|
|
var cleared = [];
|
|
|
|
$.each($('input[class="cleared"]'), function (i, v) {
|
|
|
|
var obj = $(v);
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('Added item with amount to list of cleared ' + obj.val());
|
|
|
|
// todo here we need to check previous transactions etc.
|
|
|
|
cleared.push(obj.data('id'));
|
2017-11-22 16:54:49 +01:00
|
|
|
});
|
|
|
|
|
2017-11-15 06:29:49 +01:00
|
|
|
var variables = {
|
|
|
|
startBalance: parseFloat($('input[name="start_balance"]').val()),
|
|
|
|
endBalance: parseFloat($('input[name="end_balance"]').val()),
|
|
|
|
startDate: $('input[name="start_date"]').val(),
|
|
|
|
startEnd: $('input[name="end_date"]').val(),
|
2019-06-22 13:09:11 +02:00
|
|
|
journals: ids,
|
2017-11-22 16:54:49 +01:00
|
|
|
cleared: cleared,
|
2017-11-15 06:29:49 +01:00
|
|
|
};
|
|
|
|
var uri = overviewUri.replace('%start%', $('input[name="start_date"]').val()).replace('%end%', $('input[name="end_date"]').val());
|
|
|
|
|
|
|
|
|
|
|
|
$.getJSON(uri, variables).done(function (data) {
|
2017-12-09 19:19:15 +01:00
|
|
|
$('#defaultModal').empty().html(data.html).modal('show');
|
2017-11-15 06:29:49 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What happens when you check a checkbox:
|
|
|
|
* @param e
|
|
|
|
*/
|
|
|
|
function checkReconciledBox(e) {
|
2018-09-09 21:23:04 +02:00
|
|
|
|
2017-11-15 06:29:49 +01:00
|
|
|
var el = $(e.target);
|
|
|
|
var amount = parseFloat(el.val());
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in checkReconciledBox() with amount ' + amount + ' and selected amount ' + selectedAmount);
|
2017-11-15 06:29:49 +01:00
|
|
|
// if checked, add to selected amount
|
|
|
|
if (el.prop('checked') === true && el.data('younger') === false) {
|
|
|
|
selectedAmount = selectedAmount - amount;
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('checked = true and younger = false so selected amount = ' + selectedAmount);
|
2017-11-15 06:29:49 +01:00
|
|
|
}
|
|
|
|
if (el.prop('checked') === false && el.data('younger') === false) {
|
|
|
|
selectedAmount = selectedAmount + amount;
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('checked = false and younger = false so selected amount = ' + selectedAmount);
|
2017-11-15 06:29:49 +01:00
|
|
|
}
|
|
|
|
difference = balanceDifference - selectedAmount;
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('Difference is now ' + difference);
|
2017-11-15 06:29:49 +01:00
|
|
|
updateDifference();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the difference between given start and end balance
|
|
|
|
* and put it in balanceDifference.
|
|
|
|
*/
|
|
|
|
function calculateBalanceDifference() {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in calculateBalanceDifference()');
|
2017-11-15 06:29:49 +01:00
|
|
|
var startBalance = parseFloat($('input[name="start_balance"]').val());
|
|
|
|
var endBalance = parseFloat($('input[name="end_balance"]').val());
|
|
|
|
balanceDifference = startBalance - endBalance;
|
|
|
|
}
|
|
|
|
|
2017-11-22 16:54:49 +01:00
|
|
|
/**
|
|
|
|
* Grab all transactions, update the URL and place the set of transactions in the box.
|
|
|
|
* This more or less resets the reconciliation.
|
|
|
|
*/
|
2017-11-15 06:29:49 +01:00
|
|
|
function getTransactionsForRange() {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in getTransactionsForRange()');
|
2017-11-15 06:29:49 +01:00
|
|
|
// clear out the box:
|
|
|
|
$('#transactions_holder').empty().append($('<p>').addClass('text-center').html('<i class="fa fa-fw fa-spin fa-spinner"></i>'));
|
|
|
|
var uri = transactionsUri.replace('%start%', $('input[name="start_date"]').val()).replace('%end%', $('input[name="end_date"]').val());
|
|
|
|
var index = indexUri.replace('%start%', $('input[name="start_date"]').val()).replace('%end%', $('input[name="end_date"]').val());
|
|
|
|
window.history.pushState('object or string', "Reconcile account", index);
|
|
|
|
|
|
|
|
$.getJSON(uri).done(placeTransactions);
|
|
|
|
}
|
|
|
|
|
2018-09-09 21:23:04 +02:00
|
|
|
// /**
|
|
|
|
// * Loop over all transactions that have already been cleared (in the range)
|
|
|
|
// * and add this to the selectedAmount.
|
|
|
|
// *
|
|
|
|
// */
|
|
|
|
// function includeClearedTransactions() {
|
|
|
|
// $.each($('input[class="cleared"]'), function (i, v) {
|
|
|
|
// var obj = $(v);
|
|
|
|
// if (obj.data('younger') === false) {
|
|
|
|
// //selectedAmount = selectedAmount - parseFloat(obj.val());
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
2017-11-22 16:54:49 +01:00
|
|
|
/**
|
2018-08-31 19:51:18 +02:00
|
|
|
* Loop over all transactions that have already been cleared (in the range)
|
|
|
|
* and add this to the selectedAmount.
|
2017-11-22 16:54:49 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function includeClearedTransactions() {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in includeClearedTransactions()');
|
2017-11-22 16:54:49 +01:00
|
|
|
$.each($('input[class="cleared"]'), function (i, v) {
|
|
|
|
var obj = $(v);
|
2018-09-09 21:23:04 +02:00
|
|
|
var amount = parseFloat(obj.val());
|
|
|
|
if (obj.data('inrange') === true) {
|
|
|
|
console.log('Amount is ' + amount + ' and inrange = true');
|
|
|
|
selectedAmount = selectedAmount - amount;
|
|
|
|
} else {
|
|
|
|
console.log('Amount is ' + amount + ' but inrange = FALSE so ignore.');
|
2017-11-22 20:20:44 +01:00
|
|
|
}
|
2017-11-22 16:54:49 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place the HTML for the transactions within the date range and update the balance difference.
|
|
|
|
* @param data
|
|
|
|
*/
|
2017-11-15 06:29:49 +01:00
|
|
|
function placeTransactions(data) {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in placeTransactions()');
|
2017-11-15 06:29:49 +01:00
|
|
|
$('#transactions_holder').empty().html(data.html);
|
2017-11-24 18:05:58 +01:00
|
|
|
selectedAmount = 0;
|
|
|
|
// update start + end balance when user has not touched them.
|
|
|
|
if (!changedBalances) {
|
|
|
|
$('input[name="start_balance"]').val(data.startBalance);
|
|
|
|
$('input[name="end_balance"]').val(data.endBalance);
|
|
|
|
}
|
2017-11-15 06:29:49 +01:00
|
|
|
|
|
|
|
// as long as the dates are equal, changing the balance does not matter.
|
|
|
|
calculateBalanceDifference();
|
2017-11-22 16:54:49 +01:00
|
|
|
|
|
|
|
// any already cleared transactions must be added to / removed from selectedAmount.
|
|
|
|
includeClearedTransactions();
|
|
|
|
|
2017-12-23 22:02:42 +01:00
|
|
|
difference = balanceDifference - selectedAmount;
|
2017-11-15 06:29:49 +01:00
|
|
|
updateDifference();
|
|
|
|
|
|
|
|
// enable the check buttons:
|
|
|
|
$('.reconcile_checkbox').prop('disabled', false).unbind('change').change(checkReconciledBox);
|
|
|
|
|
|
|
|
// show the other instruction:
|
|
|
|
$('.select_transactions_instruction').show();
|
2017-11-24 18:05:58 +01:00
|
|
|
|
|
|
|
$('.store_reconcile').prop('disabled', false);
|
2017-11-10 18:25:11 +01:00
|
|
|
}
|
|
|
|
|
2017-11-15 06:29:49 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function startReconcile() {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in startReconcile()');
|
2017-11-15 06:29:49 +01:00
|
|
|
reconcileStarted = true;
|
|
|
|
|
|
|
|
// hide the start button.
|
|
|
|
$('.start_reconcile').hide();
|
|
|
|
|
|
|
|
// hide the start instructions:
|
|
|
|
$('.update_balance_instruction').hide();
|
|
|
|
|
|
|
|
// hide date-change warning
|
|
|
|
$('.date_change_warning').hide();
|
|
|
|
|
|
|
|
// hide update button
|
|
|
|
$('.change_date_button').hide();
|
|
|
|
|
|
|
|
getTransactionsForRange();
|
|
|
|
|
|
|
|
|
2017-11-10 18:25:11 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-15 06:29:49 +01:00
|
|
|
|
|
|
|
function updateDifference() {
|
2018-09-09 21:23:04 +02:00
|
|
|
console.log('in updateDifference()');
|
2017-11-15 06:29:49 +01:00
|
|
|
var addClass = 'text-info';
|
|
|
|
if (difference > 0) {
|
|
|
|
addClass = 'text-success';
|
|
|
|
}
|
|
|
|
if (difference < 0) {
|
|
|
|
addClass = 'text-danger';
|
|
|
|
}
|
|
|
|
$('#difference').addClass(addClass).text(accounting.formatMoney(difference));
|
|
|
|
}
|