2016-12-23 07:02:45 +01:00
|
|
|
/*
|
|
|
|
* index.js
|
2017-10-21 08:40:00 +02: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.
|
2016-12-23 07:02:45 +01:00
|
|
|
*
|
2017-10-21 08:40:00 +02:00
|
|
|
* 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
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-23 07:02:45 +01:00
|
|
|
*/
|
|
|
|
|
2017-09-16 07:17:58 +02:00
|
|
|
/** global: spent, budgeted, available, currencySymbol, budgetIndexUri, updateIncomeUri, periodStart, periodEnd, budgetAmountUri, accounting */
|
2017-01-02 08:30:20 +01:00
|
|
|
|
2017-09-29 08:52:15 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
$(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
$('.updateIncome').on('click', updateIncome);
|
|
|
|
$('.infoIncome').on('click', infoIncome);
|
|
|
|
|
|
|
|
/*
|
|
|
|
On start, fill the "spent"-bar using the content from the page.
|
|
|
|
*/
|
|
|
|
drawSpentBar();
|
|
|
|
drawBudgetedBar();
|
|
|
|
|
|
|
|
/*
|
|
|
|
When the input changes, update the percentages for the budgeted bar:
|
|
|
|
*/
|
|
|
|
$('input[type="number"]').on('input', updateBudgetedAmounts);
|
|
|
|
|
|
|
|
//
|
|
|
|
$('.selectPeriod').change(function (e) {
|
|
|
|
var sel = $(e.target).val();
|
|
|
|
if (sel !== "x") {
|
|
|
|
var newUri = budgetIndexUri.replace("REPLACE", sel);
|
|
|
|
window.location.assign(newUri);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2015-05-24 22:54:59 +02:00
|
|
|
function drawSpentBar() {
|
2015-05-24 18:22:41 +02:00
|
|
|
"use strict";
|
2015-06-02 18:13:23 +02:00
|
|
|
if ($('.spentBar').length > 0) {
|
|
|
|
var overspent = spent > budgeted;
|
|
|
|
var pct;
|
|
|
|
|
|
|
|
if (overspent) {
|
|
|
|
// draw overspent bar
|
|
|
|
pct = (budgeted / spent) * 100;
|
|
|
|
$('.spentBar .progress-bar-warning').css('width', pct + '%');
|
|
|
|
$('.spentBar .progress-bar-danger').css('width', (100 - pct) + '%');
|
|
|
|
} else {
|
|
|
|
// draw normal bar:
|
|
|
|
pct = (spent / budgeted) * 100;
|
|
|
|
$('.spentBar .progress-bar-info').css('width', pct + '%');
|
|
|
|
}
|
2015-02-22 09:46:21 +01:00
|
|
|
}
|
2015-05-24 22:54:59 +02:00
|
|
|
}
|
2015-02-22 09:46:21 +01:00
|
|
|
|
2015-05-24 22:54:59 +02:00
|
|
|
function drawBudgetedBar() {
|
2015-05-24 18:22:41 +02:00
|
|
|
"use strict";
|
2015-02-22 09:46:21 +01:00
|
|
|
|
2015-06-02 18:13:23 +02:00
|
|
|
if ($('.budgetedBar').length > 0) {
|
2016-12-22 16:36:56 +01:00
|
|
|
var budgetedMuch = budgeted > available;
|
2015-06-02 18:13:23 +02:00
|
|
|
|
|
|
|
// recalculate percentage:
|
|
|
|
|
|
|
|
var pct;
|
|
|
|
if (budgetedMuch) {
|
|
|
|
// budgeted too much.
|
2016-12-22 16:36:56 +01:00
|
|
|
pct = (available / budgeted) * 100;
|
2015-06-02 18:13:23 +02:00
|
|
|
$('.budgetedBar .progress-bar-warning').css('width', pct + '%');
|
|
|
|
$('.budgetedBar .progress-bar-danger').css('width', (100 - pct) + '%');
|
|
|
|
$('.budgetedBar .progress-bar-info').css('width', 0);
|
|
|
|
} else {
|
2016-12-22 16:36:56 +01:00
|
|
|
pct = (budgeted / available) * 100;
|
2015-06-02 18:13:23 +02:00
|
|
|
$('.budgetedBar .progress-bar-warning').css('width', 0);
|
|
|
|
$('.budgetedBar .progress-bar-danger').css('width', 0);
|
|
|
|
$('.budgetedBar .progress-bar-info').css('width', pct + '%');
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#budgetedAmount').html(currencySymbol + ' ' + budgeted.toFixed(2));
|
|
|
|
}
|
2015-02-22 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
2017-09-29 08:52:15 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param e
|
|
|
|
*/
|
2015-05-24 22:54:59 +02:00
|
|
|
function updateBudgetedAmounts(e) {
|
2015-05-24 18:22:41 +02:00
|
|
|
"use strict";
|
2015-05-24 22:54:59 +02:00
|
|
|
var target = $(e.target);
|
|
|
|
var id = target.data('id');
|
2017-06-07 11:58:04 +02:00
|
|
|
|
2015-05-24 22:54:59 +02:00
|
|
|
var value = target.val();
|
|
|
|
var original = target.data('original');
|
|
|
|
var difference = value - original;
|
2017-06-07 11:58:04 +02:00
|
|
|
|
|
|
|
var spentCell = $('td[class="spent"][data-id="' + id + '"]');
|
|
|
|
var leftCell = $('td[class="left"][data-id="' + id + '"]');
|
|
|
|
var spentAmount = parseFloat(spentCell.data('spent'));
|
|
|
|
var newAmountLeft = spentAmount + parseFloat(value);
|
|
|
|
var amountLeftString = accounting.formatMoney(newAmountLeft);
|
2017-07-08 06:28:44 +02:00
|
|
|
if (newAmountLeft < 0) {
|
2017-06-07 11:58:04 +02:00
|
|
|
leftCell.html('<span class="text-danger">' + amountLeftString + '</span>');
|
|
|
|
}
|
2017-07-08 06:28:44 +02:00
|
|
|
if (newAmountLeft > 0) {
|
2017-06-07 11:58:04 +02:00
|
|
|
leftCell.html('<span class="text-success">' + amountLeftString + '</span>');
|
|
|
|
}
|
2017-07-08 06:28:44 +02:00
|
|
|
if (newAmountLeft === 0.0) {
|
2017-06-07 11:58:04 +02:00
|
|
|
leftCell.html('<span style="color:#999">' + amountLeftString + '</span>');
|
|
|
|
}
|
|
|
|
|
2015-05-24 22:54:59 +02:00
|
|
|
if (difference !== 0) {
|
|
|
|
// add difference to 'budgeted' var
|
|
|
|
budgeted = budgeted + difference;
|
|
|
|
|
|
|
|
// update original:
|
|
|
|
target.data('original', value);
|
|
|
|
// run drawBudgetedBar() again:
|
|
|
|
drawBudgetedBar();
|
2015-02-22 09:46:21 +01:00
|
|
|
|
2015-05-24 22:54:59 +02:00
|
|
|
// send a post to Firefly to update the amount:
|
2017-08-26 14:30:41 +02:00
|
|
|
var newUri = budgetAmountUri.replace("REPLACE", id);
|
2017-09-29 08:52:15 +02:00
|
|
|
$.post(newUri, {amount: value, start: periodStart, end: periodEnd}).done(function (data) {
|
2015-05-24 22:54:59 +02:00
|
|
|
// update the link if relevant:
|
|
|
|
if (data.repetition > 0) {
|
|
|
|
$('.budget-link[data-id="' + id + '"]').attr('href', 'budgets/show/' + id + '/' + data.repetition);
|
|
|
|
} else {
|
|
|
|
$('.budget-link[data-id="' + id + '"]').attr('href', 'budgets/show/' + id);
|
|
|
|
}
|
|
|
|
});
|
2015-02-22 09:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-29 08:52:15 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
function updateIncome() {
|
2015-05-24 18:22:41 +02:00
|
|
|
"use strict";
|
2017-09-29 08:52:15 +02:00
|
|
|
$('#defaultModal').empty().load(updateIncomeUri, function () {
|
|
|
|
$('#defaultModal').modal('show');
|
2017-06-06 20:35:39 +02:00
|
|
|
});
|
|
|
|
|
2017-09-29 08:52:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-22 09:46:21 +01:00
|
|
|
|
2017-09-29 08:52:15 +02:00
|
|
|
function infoIncome() {
|
|
|
|
$('#defaultModal').empty().load(infoIncomeUri, function () {
|
2015-05-31 20:23:49 +02:00
|
|
|
$('#defaultModal').modal('show');
|
2015-05-24 22:54:59 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|