Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).on("ready", function () {
- var d = new Date();
- var month = new Array(
- 'January',
- 'February',
- 'March',
- 'April',
- 'May',
- 'June',
- 'July',
- 'August',
- 'September',
- 'October',
- 'November',
- 'December');
- for (var i = -1; i < 2; i++) {
- var tmp = d.getFullYear() + i;
- $("#year").append("<option value='" + tmp + "'>" + tmp + "</option>");
- if (d.getFullYear() === tmp) {
- $("#year option").last().prop("selected", "selected");
- }
- }
- for (var i = 0; i < 12; i++) {
- $("#month").append("<option value='" + (i + 1) + "'>" + month[i] + "</option>");
- if (d.getMonth() === i) {
- $("#month option").last().prop("selected", "selected");
- }
- }
- $("#month").change();
- Handsontable.renderers.registerRenderer('cellRenderer', cellRenderer); //maps function to lookup string
- });
- $(function () {
- $("#year").change(function () {
- $("#month").change();
- });
- $("#month").change(function () {
- refreshTableData();
- updateNavigationButtonStates();
- });
- });
- $("#previousMonth").click(function () {
- var yearToDisplay = $("#year").val();
- var monthToDisplay = $("#month").val();
- if (monthToDisplay > 1 && monthToDisplay <= 12) {
- $("#month").val(parseInt(monthToDisplay) - 1);
- }
- else if (monthToDisplay == 1) {
- $("#month").val(12);
- $("#year").val(parseInt(yearToDisplay) - 1);
- }
- updateNavigationButtonStates();
- refreshTableData();
- });
- $("#nextMonth").click(function () {
- var yearToDisplay = $("#year").val();
- var monthToDisplay = $("#month").val();
- if (monthToDisplay >= 1 && monthToDisplay < 12) {
- $("#month").val(parseInt(monthToDisplay) + 1);
- }
- else if (monthToDisplay == 12) {
- $("#month").val(1);
- $("#year").val(parseInt(yearToDisplay) + 1);
- }
- updateNavigationButtonStates();
- refreshTableData();
- });
- function updateNavigationButtonStates() {
- if ($("#month").val() == 12 && $("#year").val() == new Date().getFullYear() + 1)
- $("#nextMonth").prop('disabled', true);
- else if ($("#month").val() == 1 && $("#year").val() == new Date().getFullYear() - 1)
- $("#previousMonth").prop('disabled', true);
- else
- {
- $("#nextMonth").prop('disabled', false);
- $("#previousMonth").prop('disabled', false);
- }
- }
- function getPhpStyleMonth(jsMonth)
- {
- if (jsMonth < 10) {
- jsMonth = "0" + (jsMonth + 1);
- return jsMonth;
- }
- if (jsMonth >= 10)
- return jsMonth + 1;
- }
- function getPhpStyleDay(jsDay) {
- if (jsDay < 10)
- return "0" + jsDay;
- else
- return jsDay;
- }
- function refreshTableData() {
- var date = {};
- date.year = $("#year").val();
- date.month = $("#month").val();
- $.ajax({
- type: "POST",
- url: "plugin.php?page=LDCTimeReport/ajax_get_tr_data",
- data: JSON.stringify(date),
- async: true,
- success: function (data) {
- localStorage['public_holidays'] = JSON.stringify(data.public_holidays);
- localStorage['daysInMonth'] = JSON.stringify(data.daysInMonth);
- // localStorage['tasks'] = JSON.stringify(data.tasks);
- localStorage['bugSummaries'] = JSON.stringify(data.bugSummaries);
- localStorage['uniqueBugSummaries'] = JSON.stringify(data.uniqueBugSummaries);
- renderTable(data.daysInMonth, data.bugSummaries, data.uniqueBugSummaries, data.tasks, data.projectName, data.public_holidays);
- },
- error: function () {
- }
- });
- }
- ;
- function cellRenderer(instance, td, row, col, prop, value, cellProperties) {
- var publicHolidays = JSON.parse(localStorage['public_holidays']);
- var daysInMonth = JSON.parse(localStorage['daysInMonth']);
- var uniqueBugSummaries = JSON.parse(localStorage['uniqueBugSummaries']);
- // var bugSummaries = JSON.parse(localStorage['uniqueBugSummaries']);
- // var tasks = JSON.parse(localStorage['tasks']);
- Handsontable.renderers.TextRenderer.apply(this, arguments);
- if (0 === row || 0 === col) {
- td.style.background = '#EEE';
- td.style.color = '#000000';
- td.style.fontSize = '10px';
- }
- if (1 === row && col === 0)
- {
- td.style.background = 'white';
- }
- if (row > uniqueBugSummaries.length + 1 && col > 0) // kolor dla podsumowań na dole
- {
- td.style.background = '#EEE';
- td.style.color = 'gray';
- }
- if (col > 0 && col <= daysInMonth & row !== 0) {
- var date = new Date($("#year").val(), $("#month").val() - 1, col);
- var currentDate = new Date($.now());
- //zaznacza weekendy
- if (0 === date.getDay() || 6 === date.getDay()) {
- td.style.background = '#BFECBF';
- cellProperties.width = 15;
- }
- //zaznacza aktualna date
- if (date.getDate() === currentDate.getDate() & date.getMonth() === currentDate.getMonth() & date.getFullYear() === currentDate.getFullYear())
- {
- td.style.background = '#AFEEEE';
- }
- // zaznacza PH
- for (var i = 0; i < publicHolidays.length; i++) {
- if (publicHolidays[i].date === date.getFullYear() + '-' + getPhpStyleMonth(date.getMonth()) + '-' + getPhpStyleDay(col))
- {
- td.style.background = '#fffacd';
- }
- }
- }
- else if (col === daysInMonth + 1 && row > 0) // kolor dla kolumny total
- {
- td.style.background = '#EEE';
- td.style.color = 'gray';
- }
- else if (col === 0 && row > uniqueBugSummaries.length + 1 || (col === 0 && row === 0))
- {
- td.style.fontWeight = 'bold';
- td.style.fontSize = '11px';
- }
- td.className = "htMiddle";
- };
- function renderTable(daysInMonth, bugSummaries, uniqueBugSummaries, tasks, projectName, publicHolidays) {
- var data = new Array(uniqueBugSummaries.length + 5); // tyle jest wierszy
- for (var i = 0; i < (uniqueBugSummaries.length + 5); i++) {
- data[i] = new Array(daysInMonth + 1); // tyle kolumn
- }
- //kolumny i wiersze, ktore sa zawsze
- data[0][0] = projectName;
- data[0][daysInMonth + 1] = "total";
- data[uniqueBugSummaries.length + 2][0] = "Total hours";
- data[uniqueBugSummaries.length + 3][0] = "Overtime";
- data[uniqueBugSummaries.length + 4][0] = "Schedule hours to complete";
- //dni miesiaca + skroty
- for (var i = 1; i <= daysInMonth; i++) {
- data[0][i] = i + "\n";
- var date = new Date($("#year").val(), $("#month").val() - 1, i);
- var weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
- data[0][i] += weekDays[date.getDay()];
- }
- for (var i = 0; i < uniqueBugSummaries.length; i++) // pierwsza kolumna z taskami
- {
- data[i + 2][0] = uniqueBugSummaries[i];
- }
- for (var row = 0; row < uniqueBugSummaries.length; row++)// wypelnianie tabeli godzinami
- {
- for (var day = 1; day <= daysInMonth; day++)
- {
- for (var d = 0; d < bugSummaries.length; d++)
- {
- if (parseInt(day) === parseInt(bugSummaries[d].date) && (bugSummaries[d].desc === uniqueBugSummaries[row]))
- data[row + 2][day] = parseFloat(bugSummaries[d].workhour).toFixed(1);
- }
- }
- }
- //totale na dole strony
- for (var day = 1; day <= daysInMonth; day++)
- {
- var scheduleHoursToComplete = 8;
- var totalHours = 0;
- var overtime = 0;
- for (var row = 0; row < uniqueBugSummaries.length; row++)
- {
- if (!isNaN(data[row + 2][day]))
- totalHours += parseFloat(data[row + 2][day]);
- }
- if (totalHours > 0)
- data[row + 2][day] = parseFloat(totalHours);
- if (!(data[0][day].indexOf("Sun") >= 0) && !(data[0][day].indexOf("Sat") >= 0))
- overtime = totalHours - scheduleHoursToComplete;
- else
- overtime = totalHours;
- if (overtime > 0)
- data[row + 3][day] = parseFloat(overtime.toFixed(2));
- scheduleHoursToComplete -= totalHours;
- if (!(data[0][day].indexOf("Sun") >= 0) && !(data[0][day].indexOf("Sat") >= 0) && scheduleHoursToComplete > 0)
- data[row + 4][day] = parseFloat(scheduleHoursToComplete);
- for (var i = 0; i < publicHolidays.length; i++) {
- if (publicHolidays[i].date === date.getFullYear() + '-' + getPhpStyleMonth(date.getMonth()) + '-' + getPhpStyleDay(day))
- data[row + 4][day] = null;
- }
- }
- //liczenie "total" ostatnia kolumna
- for (var row = 0; row < uniqueBugSummaries.length + 3; row++)
- {
- var totalCount = 0;
- for (var day = 1; day <= daysInMonth; day++)
- {
- if (!isNaN(data[row + 2][day]) && (data[row + 2][day]) !== "" && (data[row + 2][day]) !== null)
- totalCount += parseFloat(data[row + 2][day]);
- }
- data[row + 2][daysInMonth + 1] = totalCount;
- }
- // Col widths
- var colWidths = new Array(daysInMonth + 1);
- colWidths[0] = 400; // pierwsza kolumna
- colWidths[daysInMonth + 1] = 45; // ostatnia kolumna
- for (var i = 1; i <= daysInMonth; i++) {
- colWidths[i] = 35;
- // colWidths[i] = ( $("body").width() - 400 ) / (daysInMonth + 1); // odkomentowac jesli chceby aby timesheet dopasowywal sie do okna
- }
- $('#timesheet').handsontable({
- data: data,
- maxCols: daysInMonth + 1,
- maxRows: uniqueBugSummaries.length + 5,
- colWidths: colWidths,
- rowHeaders: false,
- colHeaders: false,
- fixedColumnsLeft: 1,
- minSpareRows: 0,
- contextMenu: false,
- fillHandle: false,
- cells: function (row, col, prop) {
- var cellProp = {};
- if (0 === row || 0 === col || (daysInMonth + 1) === col) {
- cellProp.readOnly = true;
- }
- if (0 === col & 1 === row)
- {
- //wypelnianie dropdownu
- var select = new Array();
- select[0] = "";
- for (var i = 0; i < tasks.length; i++) {
- select[i + 1] = tasks[i].description;
- }
- cellProp.readOnly = false;
- cellProp.type = 'dropdown';
- cellProp.source = select;
- }
- cellProp.renderer = "cellRenderer";
- return cellProp;
- }
- });
- }
- $("#saveTR").on("click", function () {
- //if ( $("#saveVP").prop("disabled") == false ){
- //blokuj mozliwosc ponownego wcisniecia buttonu
- $("#saveTR").prop("disabled", true);
- var jsonData = {};
- //nanies na date weekendy
- var MONTH = $("#month").val();
- var YEAR = $("#year").val()
- jsonData.month = MONTH; // baza miesiace trzyma liczac od 1
- jsonData.year = YEAR;
- //-------------------------------------------------------------------
- // SAVE USERS
- //-------------------------------------------------------------------
- var tableData = $("#timesheet").handsontable('getInstance');
- var rows = tableData.countRows() - 3;
- var cols = tableData.countCols() - 1;
- var recordsCount = 0;
- var data = tableData.getData();
- //dostane informacje ile rowsów bede odsylal
- for (var r = 2; r < rows; r++) {
- for (var c = 1; c < cols; c++) {
- var date = new Date(YEAR, MONTH - 1, c);
- if (undefined !== data[r][c] && !isNaN(data[r][c])) {
- recordsCount++;
- }
- }
- }
- var users1 = new Array(recordsCount);
- var users1_index = 0;
- for (var r = 1; r < rows; r++) {
- for (var c = 1; c < cols; c++) {
- var date = new Date(YEAR, MONTH - 1, c);
- if (undefined !== data[r][c] && !isNaN(data[r][c])) {
- var obj = {};
- obj.bugid = data[r][0];
- obj.date = "\"" + YEAR + "-" + MONTH + "-" + c + "\"";
- obj.workhour = data[r][c];
- users1[users1_index] = obj;
- users1_index++;
- }
- }
- }
- jsonData.users = users1;
- //-------------------------------------------------------------------
- // END SAVE USERS
- //-------------------------------------------------------------------
- //clearMsgBox();
- $.ajax({
- type: "POST",
- url: "plugin.php?page=LDCTimeReport/ajax_save_tr_data",
- data: JSON.stringify(jsonData),
- async: false,
- success: function (data) {
- if (data.message) {
- $("#messageBox").html(data.message);
- }
- unsavedChanges = false;
- refreshTableData(); // reload table data
- },
- error: function () {
- }
- });
- //odblokuj mozliwosc ponownego wcisniecia buttonu
- $("#saveVP").prop("disabled", false);
- //}
- });
- $(window).on("resize", function () {
- refreshTableData();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement