2016-12-23 07:02:45 +01:00
|
|
|
/*
|
|
|
|
* index.js
|
2020-02-16 13:59:41 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-12-23 07:02:45 +01:00
|
|
|
*
|
2019-10-02 06:43:38 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-12-23 07:02:45 +01:00
|
|
|
*
|
2019-10-02 06:43:38 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:43:38 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:43:38 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:43:38 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-12-23 07:02:45 +01:00
|
|
|
*/
|
|
|
|
|
2017-09-29 08:52:15 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
$(function () {
|
|
|
|
"use strict";
|
|
|
|
/*
|
|
|
|
On start, fill the "spent"-bar using the content from the page.
|
|
|
|
*/
|
2019-08-31 21:47:55 +02:00
|
|
|
drawSpentBars();
|
|
|
|
drawBudgetedBars();
|
|
|
|
|
|
|
|
$('.update_ab').on('click', updateAvailableBudget);
|
2021-08-20 10:05:18 +02:00
|
|
|
$('.delete_ab').on('click', deleteAvailableBudget);
|
2019-08-31 21:47:55 +02:00
|
|
|
$('.create_ab_alt').on('click', createAltAvailableBudget);
|
|
|
|
|
|
|
|
$('.budget_amount').on('change', updateBudgetedAmount);
|
2019-09-01 10:48:18 +02:00
|
|
|
$('.create_bl').on('click', createBudgetLimit);
|
2021-08-20 09:29:00 +02:00
|
|
|
$('.delete_bl').on('click', deleteBudgetLimit);
|
2019-09-01 10:48:18 +02:00
|
|
|
|
2017-09-29 08:52:15 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
When the input changes, update the percentages for the budgeted bar:
|
|
|
|
*/
|
|
|
|
$('.selectPeriod').change(function (e) {
|
2019-08-31 09:35:35 +02:00
|
|
|
var selected = $(e.currentTarget);
|
|
|
|
if (selected.find(":selected").val() !== "x") {
|
2022-04-12 18:19:30 +02:00
|
|
|
var newUrl = budgetIndexUrl.replace("START", selected.find(":selected").data('start')).replace('END', selected.find(":selected").data('end'));
|
|
|
|
window.location.assign(newUrl);
|
2017-09-29 08:52:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-17 15:18:09 +02:00
|
|
|
// sortable!
|
|
|
|
if (typeof $(".sortable-table tbody").sortable !== "undefined") {
|
|
|
|
$(".sortable-table tbody").sortable(
|
|
|
|
{
|
|
|
|
helper: fixHelper,
|
|
|
|
items: 'tr:not(.ignore)',
|
|
|
|
stop: sortStop,
|
|
|
|
handle: '.handle',
|
|
|
|
start: function (event, ui) {
|
|
|
|
// Build a placeholder cell that spans all the cells in the row
|
|
|
|
var cellCount = 0;
|
|
|
|
$('td, th', ui.helper).each(function () {
|
|
|
|
// For each TD or TH try and get it's colspan attribute, and add that or 1 to the total
|
|
|
|
var colspan = 1;
|
|
|
|
var colspanAttr = $(this).attr('colspan');
|
|
|
|
if (colspanAttr > 1) {
|
|
|
|
colspan = colspanAttr;
|
|
|
|
}
|
|
|
|
cellCount += colspan;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add the placeholder UI - note that this is the item's content, so TD rather than TR
|
|
|
|
ui.placeholder.html('<td colspan="' + cellCount + '"> </td>');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-09-29 08:52:15 +02:00
|
|
|
});
|
|
|
|
|
2019-08-31 21:47:55 +02:00
|
|
|
function updateBudgetedAmount(e) {
|
|
|
|
var input = $(e.currentTarget);
|
|
|
|
var budgetId = parseInt(input.data('id'));
|
|
|
|
var budgetLimitId = parseInt(input.data('limit'));
|
|
|
|
var currencyId = parseInt(input.data('currency'));
|
2019-09-01 10:48:18 +02:00
|
|
|
input.prop('disabled', true);
|
2019-08-31 21:47:55 +02:00
|
|
|
if (0 === budgetLimitId) {
|
2022-04-12 18:19:30 +02:00
|
|
|
$.post(storeBudgetLimitUrl, {
|
2019-08-31 21:47:55 +02:00
|
|
|
_token: token,
|
|
|
|
budget_id: budgetId,
|
|
|
|
transaction_currency_id: currencyId,
|
|
|
|
amount: input.val(),
|
|
|
|
start: periodStart,
|
|
|
|
end: periodEnd
|
|
|
|
}).done(function (data) {
|
|
|
|
|
2019-09-01 10:48:18 +02:00
|
|
|
input.prop('disabled', false);
|
|
|
|
|
|
|
|
// update amount left.
|
|
|
|
$('.left_span[data-limit="0"][data-id="' + budgetId + '"]').html(data.left_formatted);
|
|
|
|
if (data.left_per_day > 0) {
|
|
|
|
$('.left_span[data-limit="0"][data-id="' + budgetId + '"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')');
|
|
|
|
}
|
2020-03-21 14:50:54 +01:00
|
|
|
// update budgeted amount
|
|
|
|
updateTotalBudgetedAmount(data.transaction_currency_id);
|
2019-09-01 10:48:18 +02:00
|
|
|
|
2019-08-31 21:47:55 +02:00
|
|
|
}).fail(function () {
|
2020-10-13 06:35:33 +02:00
|
|
|
console.error('I failed :(');
|
2019-08-31 21:47:55 +02:00
|
|
|
});
|
|
|
|
} else {
|
2022-04-12 18:19:30 +02:00
|
|
|
$.post(updateBudgetLimitUrl.replace('REPLACEME', budgetLimitId.toString()), {
|
2019-08-31 21:47:55 +02:00
|
|
|
_token: token,
|
|
|
|
amount: input.val(),
|
|
|
|
}).done(function (data) {
|
2019-09-01 11:13:03 +02:00
|
|
|
input.prop('disabled', false);
|
2020-03-21 14:50:54 +01:00
|
|
|
$('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted);
|
2019-09-01 11:13:03 +02:00
|
|
|
if (data.left_per_day > 0) {
|
2020-03-21 14:50:54 +01:00
|
|
|
$('.left_span[data-limit="' + budgetLimitId + '"]').html(data.left_formatted + '(' + data.left_per_day_formatted + ')');
|
2019-09-01 11:13:03 +02:00
|
|
|
}
|
2020-03-21 14:50:54 +01:00
|
|
|
updateTotalBudgetedAmount(data.transaction_currency_id);
|
|
|
|
// update budgeted amount
|
2019-09-01 11:13:03 +02:00
|
|
|
|
2019-08-31 21:47:55 +02:00
|
|
|
}).fail(function () {
|
2020-10-13 06:35:33 +02:00
|
|
|
console.error('I failed :(');
|
2019-08-31 21:47:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 14:50:54 +01:00
|
|
|
function updateTotalBudgetedAmount(currencyId) {
|
|
|
|
// fade info away:
|
|
|
|
$('span.budgeted_amount[data-currency="' + currencyId + '"]')
|
|
|
|
.fadeTo(100, 0.1, function () {
|
|
|
|
//$(this).fadeTo(500, 1.0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// get new amount:
|
2022-04-12 18:19:30 +02:00
|
|
|
$.get(totalBudgetedUrl.replace('REPLACEME', currencyId)).done(function (data) {
|
2020-03-21 15:12:23 +01:00
|
|
|
// set thing:
|
2020-03-21 14:50:54 +01:00
|
|
|
$('span.budgeted_amount[data-currency="' + currencyId + '"]')
|
|
|
|
.html(data.budgeted_formatted)
|
|
|
|
// fade back:
|
|
|
|
.fadeTo(300, 1.0);
|
2020-03-21 15:12:23 +01:00
|
|
|
|
|
|
|
// set bar:
|
|
|
|
var pct = parseFloat(data.percentage);
|
|
|
|
if (pct <= 100) {
|
|
|
|
console.log('<100 (' + pct + ')');
|
|
|
|
console.log($('div.budgeted_bar[data-currency="' + currencyId + '"]'));
|
|
|
|
// red bar to 0
|
|
|
|
$('div.budgeted_bar[data-currency="' + currencyId + '"] div.progress-bar-danger').width('0%');
|
|
|
|
// orange to 0:
|
|
|
|
$('div.budgeted_bar[data-currency="' + currencyId + '"] div.progress-bar-warning').width('0%');
|
|
|
|
// blue to the rest:
|
|
|
|
$('div.budgeted_bar[data-currency="' + currencyId + '"] div.progress-bar-info').width(pct + '%');
|
|
|
|
} else {
|
|
|
|
var newPct = (100 / pct) * 100;
|
|
|
|
// red bar to new pct
|
|
|
|
$('div.budgeted_bar[data-currency="' + currencyId + '"] div.progress-bar-danger').width(newPct + '%');
|
|
|
|
// orange to the rest:
|
|
|
|
$('div.budgeted_bar[data-currency="' + currencyId + '"] div.progress-bar-warning').width((100 - newPct) + '%');
|
|
|
|
// blue to 0:
|
|
|
|
$('div.budgeted_bar[data-currency="' + currencyId + '"] div.progress-bar-info').width('0%');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-21 14:50:54 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:18:09 +02:00
|
|
|
var fixHelper = function (e, tr) {
|
|
|
|
"use strict";
|
|
|
|
var $originals = tr.children();
|
|
|
|
var $helper = tr.clone();
|
|
|
|
$helper.children().each(function (index) {
|
|
|
|
// Set helper cell sizes to match the original sizes
|
|
|
|
$(this).width($originals.eq(index).width());
|
|
|
|
});
|
|
|
|
return $helper;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function sortStop(event, ui) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//var current = $(ui.item);
|
|
|
|
var list = $('.sortable-table tbody tr');
|
|
|
|
var submit = [];
|
|
|
|
$.each(list, function (i, v) {
|
|
|
|
var row = $(v);
|
|
|
|
var id = parseInt(row.data('id'));
|
|
|
|
if (id > 0) {
|
|
|
|
submit.push(id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var arr = {
|
|
|
|
budgetIds: submit,
|
|
|
|
_token: token
|
|
|
|
};
|
2018-12-18 07:08:35 +01:00
|
|
|
$.post('budgets/reorder', arr);
|
2018-10-17 15:18:09 +02:00
|
|
|
}
|
2019-08-31 21:47:55 +02:00
|
|
|
|
2019-09-01 10:48:18 +02:00
|
|
|
function createBudgetLimit(e) {
|
2019-08-31 09:35:35 +02:00
|
|
|
var button = $(e.currentTarget);
|
2019-09-01 10:48:18 +02:00
|
|
|
var budgetId = button.data('id');
|
2022-04-12 18:19:30 +02:00
|
|
|
$('#defaultModal').empty().load(createBudgetLimitUrl.replace('REPLACEME', budgetId.toString()), function () {
|
2019-09-01 10:48:18 +02:00
|
|
|
$('#defaultModal').modal('show');
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:29:00 +02:00
|
|
|
function deleteBudgetLimit(e) {
|
2021-08-20 09:38:52 +02:00
|
|
|
e.preventDefault();
|
2021-08-20 09:29:00 +02:00
|
|
|
var button = $(e.currentTarget);
|
|
|
|
var budgetLimitId = button.data('budget-limit-id');
|
2021-08-20 09:38:52 +02:00
|
|
|
var url = deleteBudgetLimitUrl.replace('REPLACEME', budgetLimitId.toString());
|
|
|
|
$.post(url, {_token: token}).then(function () {
|
|
|
|
$('.bl_entry[data-budget-limit-id="' + budgetLimitId + '"]').remove();
|
|
|
|
|
|
|
|
});
|
2021-08-20 09:29:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-01 10:48:18 +02:00
|
|
|
function createAltAvailableBudget(e) {
|
2022-04-12 18:19:30 +02:00
|
|
|
$('#defaultModal').empty().load(createAltAvailableBudgetUrl, function () {
|
2019-08-31 21:47:55 +02:00
|
|
|
$('#defaultModal').modal('show');
|
|
|
|
});
|
2019-08-31 09:35:35 +02:00
|
|
|
return false;
|
2015-05-24 22:54:59 +02:00
|
|
|
}
|
2019-08-31 21:47:55 +02:00
|
|
|
|
2019-08-31 09:35:35 +02:00
|
|
|
function updateAvailableBudget(e) {
|
|
|
|
var button = $(e.currentTarget);
|
|
|
|
var abId = parseInt(button.data('id'));
|
|
|
|
if (0 === abId) {
|
2022-04-12 18:19:30 +02:00
|
|
|
$('#defaultModal').empty().load(createAvailableBudgetUrl, function () {
|
2019-08-31 09:35:35 +02:00
|
|
|
$('#defaultModal').modal('show');
|
|
|
|
});
|
2015-06-02 18:13:23 +02:00
|
|
|
}
|
2019-08-31 09:35:35 +02:00
|
|
|
if (abId > 0) {
|
|
|
|
// edit URL.
|
2022-04-12 18:19:30 +02:00
|
|
|
$('#defaultModal').empty().load(editAvailableBudgetUrl.replace('REPLACEME', abId), function () {
|
2019-08-31 09:35:35 +02:00
|
|
|
$('#defaultModal').modal('show');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return false;
|
2015-02-22 09:46:21 +01:00
|
|
|
}
|
2021-08-20 10:05:18 +02:00
|
|
|
function deleteAvailableBudget(e) {
|
|
|
|
//
|
|
|
|
e.preventDefault();
|
|
|
|
var button = $(e.currentTarget);
|
|
|
|
var abId = button.data('id');
|
|
|
|
$.post(deleteABUrl, {_token: token, id: abId}).then(function () {
|
|
|
|
// lame but it works.
|
|
|
|
location.reload();
|
|
|
|
});
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-06 20:35:39 +02:00
|
|
|
|
2019-08-31 21:47:55 +02:00
|
|
|
function drawBudgetedBars() {
|
|
|
|
"use strict";
|
|
|
|
$.each($('.budgeted_bar'), function (i, v) {
|
|
|
|
var bar = $(v);
|
|
|
|
var budgeted = parseFloat(bar.data('budgeted'));
|
|
|
|
var available = parseFloat(bar.data('available'));
|
|
|
|
var budgetedTooMuch = budgeted > available;
|
|
|
|
var pct;
|
|
|
|
if (budgetedTooMuch) {
|
|
|
|
// budgeted too much.
|
|
|
|
pct = (available / budgeted) * 100;
|
2020-10-03 15:16:42 +02:00
|
|
|
bar.find('.progress-bar-danger').css('width', pct + '%');
|
|
|
|
bar.find('.progress-bar-warning').css('width', (100 - pct) + '%');
|
2019-08-31 21:47:55 +02:00
|
|
|
bar.find('.progress-bar-info').css('width', 0);
|
|
|
|
} else {
|
|
|
|
pct = (budgeted / available) * 100;
|
|
|
|
bar.find('.progress-bar-danger').css('width', 0);
|
2020-10-03 15:16:42 +02:00
|
|
|
bar.find('.progress-bar-warning').css('width', 0);
|
2019-08-31 21:47:55 +02:00
|
|
|
bar.find('.progress-bar-info').css('width', pct + '%');
|
|
|
|
}
|
|
|
|
//$('#budgetedAmount').html(currencySymbol + ' ' + budgeted.toFixed(2));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawSpentBars() {
|
|
|
|
"use strict";
|
|
|
|
$.each($('.spent_bar'), function (i, v) {
|
|
|
|
var bar = $(v);
|
|
|
|
var spent = parseFloat(bar.data('spent')) * -1;
|
|
|
|
var budgeted = parseFloat(bar.data('budgeted'));
|
|
|
|
var overspent = spent > budgeted;
|
|
|
|
var pct;
|
|
|
|
|
|
|
|
if (overspent) {
|
|
|
|
// draw overspent bar
|
|
|
|
pct = (budgeted / spent) * 100;
|
|
|
|
bar.find('.progress-bar-warning').css('width', pct + '%');
|
|
|
|
bar.find('.progress-bar-danger').css('width', (100 - pct) + '%');
|
|
|
|
} else {
|
|
|
|
// draw normal bar:
|
|
|
|
pct = (spent / budgeted) * 100;
|
|
|
|
bar.find('.progress-bar-info').css('width', pct + '%');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|