2016-12-23 07:02:45 +01:00
|
|
|
/*
|
|
|
|
* index.js
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
|
|
|
*/
|
2015-06-27 16:00:50 +02:00
|
|
|
|
|
|
|
$(function () {
|
|
|
|
"use strict";
|
2015-12-03 14:52:10 +01:00
|
|
|
|
2015-12-11 18:32:57 +01:00
|
|
|
if ($('#inputDateRange').length > 0) {
|
|
|
|
|
|
|
|
picker = $('#inputDateRange').daterangepicker(
|
|
|
|
{
|
|
|
|
locale: {
|
|
|
|
format: 'YYYY-MM-DD',
|
|
|
|
firstDay: 1,
|
|
|
|
},
|
|
|
|
minDate: minDate,
|
|
|
|
drops: 'up',
|
|
|
|
}
|
|
|
|
);
|
2015-12-03 14:52:10 +01:00
|
|
|
|
2015-12-11 18:33:47 +01:00
|
|
|
// set values from cookies, if any:
|
2016-11-16 20:35:25 +01:00
|
|
|
if (!(readCookie('report-type') === null)) {
|
2015-12-11 18:33:47 +01:00
|
|
|
$('select[name="report_type"]').val(readCookie('report-type'));
|
|
|
|
}
|
2015-12-06 08:42:04 +01:00
|
|
|
|
2015-12-11 18:33:47 +01:00
|
|
|
if ((readCookie('report-accounts') !== null)) {
|
|
|
|
var arr = readCookie('report-accounts').split(',');
|
|
|
|
arr.forEach(function (val) {
|
2016-11-12 12:12:11 +01:00
|
|
|
$('input[class="account-checkbox"][type="checkbox"][value="' + val + '"]').prop('checked', true);
|
2015-12-11 18:33:47 +01:00
|
|
|
});
|
|
|
|
}
|
2015-12-06 08:42:04 +01:00
|
|
|
|
2015-12-11 18:33:47 +01:00
|
|
|
// set date:
|
|
|
|
var startStr = readCookie('report-start');
|
|
|
|
var endStr = readCookie('report-end');
|
|
|
|
if (startStr !== null && endStr !== null && startStr.length == 8 && endStr.length == 8) {
|
2016-02-06 04:32:30 +01:00
|
|
|
var startDate = moment(startStr, "YYYY-MM-DD");
|
|
|
|
var endDate = moment(endStr, "YYYY-MM-DD");
|
2015-12-11 18:33:47 +01:00
|
|
|
var datePicker = $('#inputDateRange').data('daterangepicker');
|
|
|
|
datePicker.setStartDate(startDate);
|
|
|
|
datePicker.setEndDate(endDate);
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 18:35:49 +01:00
|
|
|
|
2015-12-06 08:42:04 +01:00
|
|
|
$('.date-select').on('click', preSelectDate);
|
|
|
|
$('#report-form').on('submit', catchSubmit);
|
2016-11-09 19:25:09 +01:00
|
|
|
$('select[name="report_type"]').on('change', getReportOptions);
|
|
|
|
getReportOptions();
|
2015-12-04 06:56:59 +01:00
|
|
|
|
2015-06-27 16:00:50 +02:00
|
|
|
});
|
2015-03-04 20:47:00 +01:00
|
|
|
|
2016-11-09 19:25:09 +01:00
|
|
|
function getReportOptions() {
|
|
|
|
"use strict";
|
|
|
|
var reportType = $('select[name="report_type"]').val();
|
|
|
|
$('#extra-options').empty();
|
|
|
|
$('#extra-options').addClass('loading');
|
2016-11-12 12:12:11 +01:00
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
$.getJSON('reports/options/' + reportType, function (data) {
|
2016-11-09 19:25:09 +01:00
|
|
|
$('#extra-options').removeClass('loading').html(data.html);
|
2016-11-12 12:12:11 +01:00
|
|
|
setOptionalFromCookies();
|
2016-11-09 21:36:54 +01:00
|
|
|
}).fail(function () {
|
2016-11-09 19:25:09 +01:00
|
|
|
$('#extra-options').removeClass('loading').addClass('error');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-12 12:12:11 +01:00
|
|
|
function setOptionalFromCookies() {
|
2016-12-08 21:50:20 +01:00
|
|
|
var arr;
|
|
|
|
// categories
|
2016-11-12 12:12:11 +01:00
|
|
|
if ((readCookie('report-categories') !== null)) {
|
2016-12-08 21:50:20 +01:00
|
|
|
arr = readCookie('report-categories').split(',');
|
2016-11-12 12:12:11 +01:00
|
|
|
arr.forEach(function (val) {
|
|
|
|
$('input[class="category-checkbox"][type="checkbox"][value="' + val + '"]').prop('checked', true);
|
|
|
|
});
|
|
|
|
}
|
2016-12-08 21:50:20 +01:00
|
|
|
|
|
|
|
// and budgets!
|
|
|
|
if ((readCookie('report-budgets') !== null)) {
|
|
|
|
arr = readCookie('report-budgets').split(',');
|
|
|
|
arr.forEach(function (val) {
|
|
|
|
$('input[class="budget-checkbox"][type="checkbox"][value="' + val + '"]').prop('checked', true);
|
|
|
|
});
|
|
|
|
}
|
2016-11-12 12:12:11 +01:00
|
|
|
}
|
|
|
|
|
2015-12-06 08:42:04 +01:00
|
|
|
function catchSubmit() {
|
|
|
|
"use strict";
|
|
|
|
// date, processed:
|
|
|
|
var picker = $('#inputDateRange').data('daterangepicker');
|
|
|
|
|
|
|
|
// all account ids:
|
|
|
|
var count = 0;
|
|
|
|
var accounts = [];
|
|
|
|
$.each($('.account-checkbox'), function (i, v) {
|
|
|
|
var c = $(v);
|
|
|
|
if (c.prop('checked')) {
|
|
|
|
accounts.push(c.val());
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
});
|
2016-11-09 21:36:54 +01:00
|
|
|
|
2016-11-12 12:12:11 +01:00
|
|
|
// all category ids:
|
|
|
|
var categories = [];
|
|
|
|
$.each($('.category-checkbox'), function (i, v) {
|
|
|
|
var c = $(v);
|
|
|
|
if (c.prop('checked')) {
|
|
|
|
categories.push(c.val());
|
|
|
|
}
|
|
|
|
});
|
2016-11-09 21:36:54 +01:00
|
|
|
|
2016-12-08 21:50:20 +01:00
|
|
|
// all budget ids:
|
|
|
|
var budgets = [];
|
|
|
|
$.each($('.budget-checkbox'), function (i, v) {
|
|
|
|
var c = $(v);
|
|
|
|
if (c.prop('checked')) {
|
|
|
|
budgets.push(c.val());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
|
|
|
|
// remember all
|
2015-12-06 08:42:04 +01:00
|
|
|
if (count > 0) {
|
|
|
|
// set cookie to remember choices.
|
|
|
|
createCookie('report-type', $('select[name="report_type"]').val(), 365);
|
|
|
|
createCookie('report-accounts', accounts, 365);
|
2016-11-12 12:12:11 +01:00
|
|
|
createCookie('report-categories', categories, 365);
|
2016-12-08 21:50:20 +01:00
|
|
|
createCookie('report-budgets', budgets, 365);
|
2015-12-06 08:42:04 +01:00
|
|
|
createCookie('report-start', moment(picker.startDate).format("YYYYMMDD"), 365);
|
|
|
|
createCookie('report-end', moment(picker.endDate).format("YYYYMMDD"), 365);
|
|
|
|
}
|
|
|
|
|
2016-11-09 21:36:54 +01:00
|
|
|
return true;
|
2015-12-06 08:42:04 +01:00
|
|
|
}
|
|
|
|
|
2015-12-04 06:56:59 +01:00
|
|
|
function preSelectDate(e) {
|
|
|
|
"use strict";
|
|
|
|
var link = $(e.target);
|
|
|
|
var picker = $('#inputDateRange').data('daterangepicker');
|
2016-02-06 04:32:30 +01:00
|
|
|
picker.setStartDate(moment(link.data('start'), "YYYY-MM-DD"));
|
|
|
|
picker.setEndDate(moment(link.data('end'), "YYYY-MM-DD"));
|
2015-12-04 06:56:59 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2015-03-04 20:47:00 +01:00
|
|
|
|
2015-12-06 08:42:04 +01:00
|
|
|
|
|
|
|
function createCookie(name, value, days) {
|
2015-12-14 20:33:10 +01:00
|
|
|
"use strict";
|
2015-12-06 08:42:04 +01:00
|
|
|
var expires;
|
|
|
|
|
|
|
|
if (days) {
|
|
|
|
var date = new Date();
|
|
|
|
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
|
|
|
expires = "; expires=" + date.toGMTString();
|
|
|
|
} else {
|
|
|
|
expires = "";
|
|
|
|
}
|
|
|
|
document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
|
|
|
|
}
|
|
|
|
|
|
|
|
function readCookie(name) {
|
2015-12-14 20:33:10 +01:00
|
|
|
"use strict";
|
2015-12-06 08:42:04 +01:00
|
|
|
var nameEQ = encodeURIComponent(name) + "=";
|
|
|
|
var ca = document.cookie.split(';');
|
|
|
|
for (var i = 0; i < ca.length; i++) {
|
|
|
|
var c = ca[i];
|
|
|
|
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
|
|
|
|
if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
|
|
|
|
}
|
|
|
|
return null;
|
2015-12-16 16:19:15 +01:00
|
|
|
}
|
|
|
|
|