Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).on("ready", function () {
  2.  
  3.     var d = new Date();
  4.     var month = new Array(
  5.             'January',
  6.             'February',
  7.             'March',
  8.             'April',
  9.             'May',
  10.             'June',
  11.             'July',
  12.             'August',
  13.             'September',
  14.             'October',
  15.             'November',
  16.             'December');
  17.  
  18.     for (var i = -1; i < 2; i++) {
  19.         var tmp = d.getFullYear() + i;
  20.         $("#year").append("<option value='" + tmp + "'>" + tmp + "</option>");
  21.  
  22.         if (d.getFullYear() === tmp) {
  23.             $("#year option").last().prop("selected", "selected");
  24.         }
  25.     }
  26.  
  27.     for (var i = 0; i < 12; i++) {
  28.         $("#month").append("<option value='" + (i + 1) + "'>" + month[i] + "</option>");
  29.  
  30.         if (d.getMonth() === i) {
  31.             $("#month option").last().prop("selected", "selected");
  32.         }
  33.     }
  34.  
  35.     $("#month").change();
  36.     Handsontable.renderers.registerRenderer('cellRenderer', cellRenderer); //maps function to lookup string
  37.  
  38. });
  39.  
  40. $(function () {
  41.  
  42.     $("#year").change(function () {
  43.  
  44.         $("#month").change();
  45.  
  46.     });
  47.  
  48.     $("#month").change(function () {
  49.  
  50.         refreshTableData();
  51.         updateNavigationButtonStates();
  52.  
  53.     });
  54. });
  55.  
  56.  
  57. $("#previousMonth").click(function () {
  58.  
  59.     var yearToDisplay = $("#year").val();
  60.     var monthToDisplay = $("#month").val();
  61.  
  62.     if (monthToDisplay > 1 && monthToDisplay <= 12) {
  63.         $("#month").val(parseInt(monthToDisplay) - 1);
  64.     }
  65.     else if (monthToDisplay == 1) {
  66.         $("#month").val(12);
  67.         $("#year").val(parseInt(yearToDisplay) - 1);
  68.     }
  69.  
  70.     updateNavigationButtonStates();
  71.     refreshTableData();
  72.  
  73. });
  74.  
  75. $("#nextMonth").click(function () {
  76.  
  77.     var yearToDisplay = $("#year").val();
  78.     var monthToDisplay = $("#month").val();
  79.  
  80.     if (monthToDisplay >= 1 && monthToDisplay < 12) {
  81.         $("#month").val(parseInt(monthToDisplay) + 1);
  82.     }
  83.  
  84.     else if (monthToDisplay == 12) {
  85.         $("#month").val(1);
  86.         $("#year").val(parseInt(yearToDisplay) + 1);
  87.     }
  88.  
  89.     updateNavigationButtonStates();
  90.     refreshTableData();
  91. });
  92.  
  93. function updateNavigationButtonStates() {
  94.  
  95.     if ($("#month").val() == 12 && $("#year").val() == new Date().getFullYear() + 1)
  96.         $("#nextMonth").prop('disabled', true);
  97.  
  98.  
  99.     else if ($("#month").val() == 1 && $("#year").val() == new Date().getFullYear() - 1)
  100.         $("#previousMonth").prop('disabled', true);
  101.  
  102.     else
  103.     {
  104.         $("#nextMonth").prop('disabled', false);
  105.         $("#previousMonth").prop('disabled', false);
  106.     }
  107. }
  108.  
  109. function getPhpStyleMonth(jsMonth)
  110. {
  111.     if (jsMonth < 10) {
  112.         jsMonth = "0" + (jsMonth + 1);
  113.         return jsMonth;
  114.     }
  115.  
  116.     if (jsMonth >= 10)
  117.         return jsMonth + 1;
  118. }
  119.  
  120. function getPhpStyleDay(jsDay) {
  121.     if (jsDay < 10)
  122.         return "0" + jsDay;
  123.     else
  124.         return jsDay;
  125. }
  126.  
  127. function refreshTableData() {
  128.     var date = {};
  129.     date.year = $("#year").val();
  130.     date.month = $("#month").val();
  131.  
  132.     $.ajax({
  133.         type: "POST",
  134.         url: "plugin.php?page=LDCTimeReport/ajax_get_tr_data",
  135.         data: JSON.stringify(date),
  136.         async: true,
  137.         success: function (data) {
  138.             localStorage['public_holidays'] = JSON.stringify(data.public_holidays);
  139.             localStorage['daysInMonth'] = JSON.stringify(data.daysInMonth);
  140.             //  localStorage['tasks'] = JSON.stringify(data.tasks);
  141.             localStorage['bugSummaries'] = JSON.stringify(data.bugSummaries);
  142.             localStorage['uniqueBugSummaries'] = JSON.stringify(data.uniqueBugSummaries);
  143.  
  144.             renderTable(data.daysInMonth, data.bugSummaries, data.uniqueBugSummaries, data.tasks, data.projectName, data.public_holidays);
  145.         },
  146.         error: function () {
  147.         }
  148.     });
  149. }
  150. ;
  151.  
  152. function cellRenderer(instance, td, row, col, prop, value, cellProperties) {
  153.  
  154.     var publicHolidays = JSON.parse(localStorage['public_holidays']);
  155.     var daysInMonth = JSON.parse(localStorage['daysInMonth']);
  156.     var uniqueBugSummaries = JSON.parse(localStorage['uniqueBugSummaries']);
  157.     // var bugSummaries = JSON.parse(localStorage['uniqueBugSummaries']);
  158.  
  159.     // var tasks = JSON.parse(localStorage['tasks']);
  160.  
  161.  
  162.     Handsontable.renderers.TextRenderer.apply(this, arguments);
  163.  
  164.     if (0 === row || 0 === col) {
  165.         td.style.background = '#EEE';
  166.         td.style.color = '#000000';
  167.         td.style.fontSize = '10px';
  168.     }
  169.  
  170.     if (1 === row && col === 0)
  171.     {
  172.         td.style.background = 'white';
  173.     }
  174.  
  175.     if (row > uniqueBugSummaries.length + 1 && col > 0) // kolor dla podsumowań na dole
  176.     {
  177.         td.style.background = '#EEE';
  178.         td.style.color = 'gray';
  179.     }
  180.  
  181.     if (col > 0 && col <= daysInMonth & row !== 0) {
  182.         var date = new Date($("#year").val(), $("#month").val() - 1, col);
  183.         var currentDate = new Date($.now());
  184.  
  185.         //zaznacza weekendy
  186.         if (0 === date.getDay() || 6 === date.getDay()) {
  187.             td.style.background = '#BFECBF';
  188.             cellProperties.width = 15;
  189.         }
  190.  
  191.         //zaznacza aktualna date
  192.         if (date.getDate() === currentDate.getDate() & date.getMonth() === currentDate.getMonth() & date.getFullYear() === currentDate.getFullYear())
  193.         {
  194.             td.style.background = '#AFEEEE';
  195.         }
  196.         // zaznacza PH
  197.         for (var i = 0; i < publicHolidays.length; i++) {
  198.             if (publicHolidays[i].date === date.getFullYear() + '-' + getPhpStyleMonth(date.getMonth()) + '-' + getPhpStyleDay(col))
  199.             {
  200.                 td.style.background = '#fffacd';
  201.  
  202.             }
  203.         }
  204.     }
  205.     else if (col === daysInMonth + 1 && row > 0) // kolor dla kolumny total
  206.     {
  207.         td.style.background = '#EEE';
  208.         td.style.color = 'gray';
  209.     }
  210.  
  211.     else if (col === 0 && row > uniqueBugSummaries.length + 1 || (col === 0 && row === 0))
  212.     {
  213.         td.style.fontWeight = 'bold';
  214.         td.style.fontSize = '11px';
  215.     }
  216.     td.className = "htMiddle";
  217.  
  218. };
  219.  
  220. function renderTable(daysInMonth, bugSummaries, uniqueBugSummaries, tasks, projectName, publicHolidays) {
  221.  
  222.  
  223.     var data = new Array(uniqueBugSummaries.length + 5); // tyle jest wierszy
  224.     for (var i = 0; i < (uniqueBugSummaries.length + 5); i++) {
  225.         data[i] = new Array(daysInMonth + 1); // tyle kolumn
  226.     }
  227.  
  228.     //kolumny i wiersze, ktore sa zawsze
  229.     data[0][0] = projectName;
  230.     data[0][daysInMonth + 1] = "total";
  231.     data[uniqueBugSummaries.length + 2][0] = "Total hours";
  232.     data[uniqueBugSummaries.length + 3][0] = "Overtime";
  233.     data[uniqueBugSummaries.length + 4][0] = "Schedule hours to complete";
  234.  
  235.     //dni miesiaca + skroty
  236.     for (var i = 1; i <= daysInMonth; i++) {
  237.         data[0][i] = i + "\n";
  238.  
  239.         var date = new Date($("#year").val(), $("#month").val() - 1, i);
  240.         var weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  241.         data[0][i] += weekDays[date.getDay()];
  242.     }
  243.  
  244.     for (var i = 0; i < uniqueBugSummaries.length; i++) // pierwsza kolumna z taskami
  245.     {
  246.         data[i + 2][0] = uniqueBugSummaries[i];
  247.     }
  248.  
  249.     for (var row = 0; row < uniqueBugSummaries.length; row++)// wypelnianie tabeli godzinami
  250.     {
  251.         for (var day = 1; day <= daysInMonth; day++)
  252.         {
  253.             for (var d = 0; d < bugSummaries.length; d++)
  254.             {
  255.                 if (parseInt(day) === parseInt(bugSummaries[d].date) && (bugSummaries[d].desc === uniqueBugSummaries[row]))
  256.                     data[row + 2][day] = parseFloat(bugSummaries[d].workhour).toFixed(1);
  257.             }
  258.         }
  259.     }
  260.  
  261. //totale na dole strony
  262.     for (var day = 1; day <= daysInMonth; day++)
  263.     {
  264.         var scheduleHoursToComplete = 8;
  265.         var totalHours = 0;
  266.         var overtime = 0;
  267.  
  268.         for (var row = 0; row < uniqueBugSummaries.length; row++)
  269.         {
  270.             if (!isNaN(data[row + 2][day]))
  271.                 totalHours += parseFloat(data[row + 2][day]);
  272.         }
  273.  
  274.         if (totalHours > 0)
  275.             data[row + 2][day] = parseFloat(totalHours);
  276.        
  277.         if (!(data[0][day].indexOf("Sun") >= 0) && !(data[0][day].indexOf("Sat") >= 0))
  278.             overtime = totalHours - scheduleHoursToComplete;
  279.         else
  280.             overtime = totalHours;
  281.        
  282.         if (overtime > 0)
  283.             data[row + 3][day] = parseFloat(overtime.toFixed(2));
  284.  
  285.         scheduleHoursToComplete -= totalHours;
  286.         if (!(data[0][day].indexOf("Sun") >= 0) && !(data[0][day].indexOf("Sat") >= 0) && scheduleHoursToComplete > 0)
  287.             data[row + 4][day] = parseFloat(scheduleHoursToComplete);
  288.  
  289.         for (var i = 0; i < publicHolidays.length; i++) {
  290.             if (publicHolidays[i].date === date.getFullYear() + '-' + getPhpStyleMonth(date.getMonth()) + '-' + getPhpStyleDay(day))
  291.                 data[row + 4][day] = null;
  292.         }
  293.     }
  294. //liczenie "total" ostatnia kolumna
  295.     for (var row = 0; row < uniqueBugSummaries.length + 3; row++)
  296.     {
  297.         var totalCount = 0;
  298.         for (var day = 1; day <= daysInMonth; day++)
  299.         {
  300.             if (!isNaN(data[row + 2][day]) && (data[row + 2][day]) !== "" && (data[row + 2][day]) !== null)
  301.                 totalCount += parseFloat(data[row + 2][day]);
  302.         }
  303.         data[row + 2][daysInMonth + 1] = totalCount;
  304.     }
  305.  
  306.     // Col widths
  307.     var colWidths = new Array(daysInMonth + 1);
  308.     colWidths[0] = 400; // pierwsza kolumna
  309.     colWidths[daysInMonth + 1] = 45; // ostatnia kolumna
  310.     for (var i = 1; i <= daysInMonth; i++) {
  311.         colWidths[i] = 35;
  312.  
  313.         // colWidths[i] = ( $("body").width() - 400 ) / (daysInMonth + 1); // odkomentowac jesli chceby aby timesheet dopasowywal sie do okna
  314.     }
  315.  
  316.     $('#timesheet').handsontable({
  317.         data: data,
  318.         maxCols: daysInMonth + 1,
  319.         maxRows: uniqueBugSummaries.length + 5,
  320.         colWidths: colWidths,
  321.         rowHeaders: false,
  322.         colHeaders: false,
  323.         fixedColumnsLeft: 1,
  324.         minSpareRows: 0,
  325.         contextMenu: false,
  326.         fillHandle: false,
  327.  
  328.         cells: function (row, col, prop) {
  329.             var cellProp = {};
  330.             if (0 === row || 0 === col || (daysInMonth + 1) === col) {
  331.                 cellProp.readOnly = true;
  332.             }
  333.             if (0 === col & 1 === row)
  334.             {
  335.  
  336.                 //wypelnianie dropdownu
  337.  
  338.                 var select = new Array();
  339.                 select[0] = "";
  340.                 for (var i = 0; i < tasks.length; i++) {
  341.                     select[i + 1] = tasks[i].description;
  342.                 }
  343.  
  344.                 cellProp.readOnly = false;
  345.                 cellProp.type = 'dropdown';
  346.                 cellProp.source = select;
  347.             }
  348.             cellProp.renderer = "cellRenderer";
  349.             return cellProp;
  350.         }
  351.  
  352.     });
  353. }
  354.  
  355. $("#saveTR").on("click", function () {
  356.     //if ( $("#saveVP").prop("disabled") == false ){
  357.     //blokuj mozliwosc ponownego wcisniecia buttonu
  358.     $("#saveTR").prop("disabled", true);
  359.  
  360.     var jsonData = {};
  361.  
  362.     //nanies na date weekendy
  363.     var MONTH = $("#month").val();
  364.     var YEAR = $("#year").val()
  365.    
  366.  
  367.     jsonData.month = MONTH; // baza miesiace trzyma liczac od 1
  368.     jsonData.year = YEAR;
  369.  
  370.  
  371.     //-------------------------------------------------------------------
  372.     //                          SAVE USERS
  373.     //-------------------------------------------------------------------
  374.     var tableData = $("#timesheet").handsontable('getInstance');
  375.  
  376.  
  377.     var rows = tableData.countRows() - 3;
  378.     var cols = tableData.countCols() - 1;
  379.  
  380.     var recordsCount = 0;
  381.  
  382.     var data = tableData.getData();
  383.  
  384.     //dostane informacje ile rowsów bede odsylal
  385.     for (var r = 2; r < rows; r++) {
  386.         for (var c = 1; c < cols; c++) {
  387.             var date = new Date(YEAR, MONTH - 1, c);
  388.  
  389.             if (undefined !== data[r][c] && !isNaN(data[r][c])) {
  390.                 recordsCount++;
  391.             }
  392.  
  393.         }
  394.     }
  395.  
  396.     var users1 = new Array(recordsCount);
  397.     var users1_index = 0;
  398.  
  399.     for (var r = 1; r < rows; r++) {
  400.         for (var c = 1; c < cols; c++) {
  401.             var date = new Date(YEAR, MONTH - 1, c);
  402.             if (undefined !== data[r][c] && !isNaN(data[r][c])) {
  403.                 var obj = {};
  404.                 obj.bugid = data[r][0];
  405.                 obj.date = "\"" + YEAR + "-" + MONTH + "-" + c + "\"";
  406.                 obj.workhour = data[r][c];
  407.  
  408.                 users1[users1_index] = obj;
  409.                 users1_index++;
  410.             }
  411.  
  412.         }
  413.     }
  414.  
  415.     jsonData.users = users1;
  416.  
  417.     //-------------------------------------------------------------------
  418.     //                          END SAVE USERS
  419.     //-------------------------------------------------------------------
  420.  
  421.     //clearMsgBox();
  422.     $.ajax({
  423.         type: "POST",
  424.         url: "plugin.php?page=LDCTimeReport/ajax_save_tr_data",
  425.         data: JSON.stringify(jsonData),
  426.         async: false,
  427.         success: function (data) {
  428.             if (data.message) {
  429.                 $("#messageBox").html(data.message);
  430.             }
  431.             unsavedChanges = false;
  432.             refreshTableData(); // reload table data
  433.         },
  434.         error: function () {
  435.         }
  436.     });
  437.  
  438.     //odblokuj mozliwosc ponownego wcisniecia buttonu
  439.     $("#saveVP").prop("disabled", false);
  440.     //}
  441. });
  442.  
  443. $(window).on("resize", function () {
  444.     refreshTableData();
  445. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement