Ignore:
Timestamp:
Oct 23, 2012, 8:18:20 AM (10 years ago)
Author:
olle
Message:

Fixes #421. Bug fixes for problems with different period views.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/resources/samplereportgenerator.jsp

    r1726 r1727  
    187187    var sdString = report.beginDate;
    188188    var edString = report.endDate;
     189    var psdString = report.periodBeginDate;
    189190    var ldString = report.latestDate;
    190191    var startDate = new Date();
     
    196197    endDate.setMonth(edString.substr(4,2)-1);
    197198    endDate.setDate(edString.substr(6));
     199    var periodStartDate = new Date();
     200    periodStartDate.setYear(psdString.substr(0,4));
     201    periodStartDate.setMonth(psdString.substr(4,2)-1);
     202    periodStartDate.setDate(psdString.substr(6));
    198203    var latestDate;
    199204    if (ldString != null)
     
    214219    if (viewType == 'WEEK')
    215220    {
    216       var startWeek = getWeekByDate(startDate);
    217       var endWeek = getWeekByDate(endDate);
    218       var tempDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()-startDate.getDay()+1);
     221      var startWeek = getISOWeekNumber(periodStartDate);
     222      var endWeek = getISOWeekNumber(endDate);
     223      var tempDate = new Date(periodStartDate.getFullYear(), periodStartDate.getMonth(), periodStartDate.getDate()-periodStartDate.getDay()+1);
    219224      numCols = 0;
    220225      while (tempDate < endDate)
     
    227232    else if (viewType == 'MONTH')
    228233    {
    229       numCols = (endDate.getFullYear()-startDate.getFullYear())*12 + endDate.getMonth()-startDate.getMonth()+1;
     234      numCols = (endDate.getFullYear()-periodStartDate.getFullYear())*12 + endDate.getMonth()-periodStartDate.getMonth()+1;
    230235      headerText += 'month';
    231236    }
     
    233238    {
    234239      //First year
    235       numCols = 4 - Math.floor(startDate.getMonth()/3);
     240      numCols = 4 - Math.floor(periodStartDate.getMonth()/3);
    236241      numCols += Math.floor(endDate.getMonth()/3)+1;
    237       if((endDate.getFullYear() - startDate.getFullYear()) > 1) numCols += 4 * (endDate.getFullYear() - startDate.getFullYear()-1);
     242      if((endDate.getFullYear() - periodStartDate.getFullYear()) > 1)
     243      {
     244        numCols += 4 * (endDate.getFullYear() - periodStartDate.getFullYear()-1);
     245      }
    238246      headerText += 'quarter';
    239247    }
    240248    else
    241249    {
    242       numCols = endDate.getFullYear() - startDate.getFullYear() + 1;
     250      numCols = endDate.getFullYear() - periodStartDate.getFullYear() + 1;
    243251      headerText += 'year';
    244252    }
     
    247255    // Set table header
    248256    headerText += ' (between ' + startDate.getFullYear()+'-';
    249     if ((startDate.getMonth()+1) < 10) headerText += '0' + (startDate.getMonth()+1);
    250     else headerText += (startDate.getMonth()+1);
     257    if ((startDate.getMonth()+1) < 10)
     258    {
     259      headerText += '0' + (startDate.getMonth()+1);
     260    }
     261    else
     262    {
     263      headerText += (startDate.getMonth()+1);
     264    }
    251265    headerText += '-';
    252     if (startDate.getDate() < 10) headerText += '0' + startDate.getDate();
    253     else headerText += startDate.getDate();
     266    if (startDate.getDate() < 10)
     267    {
     268      headerText += '0' + startDate.getDate();
     269    }
     270    else
     271    {
     272      headerText += startDate.getDate();
     273    }
    254274    headerText += ' and ';
    255275    headerText += endDate.getFullYear()+'-';
    256     if ((endDate.getMonth()+1) < 10) headerText += '0' + (endDate.getMonth()+1);
    257     else headerText += (endDate.getMonth()+1);
     276    if ((endDate.getMonth()+1) < 10)
     277    {
     278      headerText += '0' + (endDate.getMonth()+1);
     279    }
     280    else
     281    {
     282      headerText += (endDate.getMonth()+1);
     283    }
    258284    headerText += '-';
    259     if (endDate.getDate() < 10) headerText += '0' + endDate.getDate();
    260     else headerText += endDate.getDate();
     285    if (endDate.getDate() < 10)
     286    {
     287      headerText += '0' + endDate.getDate();
     288    }
     289    else
     290    {
     291      headerText += endDate.getDate();
     292    }
    261293    headerText += ')';
    262294    if (latestDate != null)
     
    264296      headerText += '\nLast registration ' + latestDate.getFullYear();
    265297      headerText += '-';
    266       if ((latestDate.getMonth()+1) < 10) headerText += '0';
     298      if ((latestDate.getMonth()+1) < 10)
     299      {
     300        headerText += '0';
     301      }
    267302      headerText += (latestDate.getMonth()+1);
    268303      headerText += '-';
    269       if (latestDate.getDate()<10) headerText += '0';
     304      if (latestDate.getDate()<10)
     305      {
     306        headerText += '0';
     307      }
    270308      headerText += latestDate.getDate();
    271309    }   
     
    277315    {
    278316      subHeaderRowYear.appendChild(getTableCellElement('', 'reportsubheader', 2));
    279       addYearSubHeaders(startDate, endDate, subHeaderRowYear, viewType);     
    280       subHeaderRowYear.appendChild(getTableCellElement('', 'reportsubheader'));
     317      addYearSubHeaders(periodStartDate, endDate, subHeaderRowYear, viewType);     
     318      subHeaderRowYear.appendChild(getTableCellElement('', 'reportsubheader', 2));
    281319    }   
    282320         
     
    287325    columnHeaderRow.appendChild(startDateHeader);
    288326   
    289     if (viewType == 'MONTH') addMonthColumnHeaders(columnHeaderRow, startDate);
    290     else if (viewType == 'WEEK') addWeekColumnHeaders(columnHeaderRow, startDate);
    291     else if (viewType == 'QUARTER') addQuarterColumnHeaders(columnHeaderRow, startDate);
    292     else if (viewType == 'YEAR') addYearSubHeaders(startDate, endDate, columnHeaderRow, viewType);
     327    if (viewType == 'MONTH') addMonthColumnHeaders(columnHeaderRow, periodStartDate);
     328    else if (viewType == 'WEEK') addWeekColumnHeaders(columnHeaderRow, periodStartDate);
     329    else if (viewType == 'QUARTER') addQuarterColumnHeaders(columnHeaderRow, periodStartDate);
     330    else if (viewType == 'YEAR') addYearSubHeaders(periodStartDate, endDate, columnHeaderRow, viewType);
    293331   
    294332   
     
    313351      if (sitesCombined[key] != '') gt += sitesCombined[key];
    314352    }
    315     sitesCombinedRow.appendChild(getTableCellElement(gt, 'colsummary'));
     353    sitesCombinedRow.appendChild(getTableCellElement(gt,'colsummary'));
    316354    reportTable.appendChild(sitesCombinedRow); 
     355
    317356    return reportTable;
    318357  }
     
    339378    for (var i=0;i<numCols;i++)
    340379    {     
    341       var weekNum = getWeekByDate(currentDate);
     380      var weekNum = getISOWeekNumber(currentDate);
    342381      var columnText = weekNum;
    343382      if (weekNum < 10) columnText = '0'+columnText;     
     
    390429        else if (currentYear == startDate.getFullYear())
    391430        {
    392           var startWeek = getWeekByDate(startDate);
     431          var startWeek = getISOWeekNumber(startDate);
    393432          var lastDay = new Date(startDate.getFullYear(), 11, 31);
    394           var endWeek = getWeekByDate(lastDay);
    395           if (endWeek == 1) endWeek = getWeekByDate(lastDay.setDate(lastDay.getDate()-7));
     433          var endWeek = getISOWeekNumber(lastDay);
     434          if (endWeek == 1)
     435          {
     436            lastDay.setDate(lastDay.getDate()-7);
     437            endWeek = getISOWeekNumber(lastDay);
     438          }
    396439          colspan = endWeek - startWeek +1;
    397440        }
    398441        else if (currentYear == endDate.getFullYear())
    399442        {
    400           var endWeek = getWeekByDate(endDate);
     443          var endWeek = getISOWeekNumber(endDate);
    401444          colspan = endWeek;
    402445        }
     
    404447        {
    405448          var dateInLastWeek = new Date(currentYear, 11, 31);
    406           var endWeek = getWeekByDate(dateInLastWeek);
    407           if (endWeek == 1) endWeek = getWeekByDate(dateInLastWeek.setDate(dateInLastWeek.getDate()-7));
     449          var endWeek = getISOWeekNumber(dateInLastWeek);
     450          if (endWeek == 1)
     451          {
     452            dateInLastWeek.setDate(dateInLastWeek.getDate()-7);
     453            endWeek = getISOWeekNumber(dateInLastWeek);
     454          }
    408455          colspan = endWeek;
    409456        }               
     
    444491    var fdString = report.beginDate;
    445492    var ldString = report.endDate;
     493    var psdString = report.periodBeginDate;
    446494    var startDate = new Date();
    447495    startDate.setYear(fdString.substr(0,4));
     
    452500    endDate.setMonth(ldString.substr(4,2)-1);
    453501    endDate.setDate(ldString.substr(6));
     502    var periodStartDate = new Date();
     503    periodStartDate.setYear(psdString.substr(0,4));
     504    periodStartDate.setMonth(psdString.substr(4,2)-1);
     505    periodStartDate.setDate(psdString.substr(6));
    454506    var viewType = report.viewType;   
    455507    var sites = report.sites;   
     
    472524      tableRow.appendChild(tableCol);
    473525      tableRow.appendChild(getTableCellElement(year+'-'+(month)+'-'+date, 'reportdata'));
    474       var currentDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate());    
    475       var grandTotal = 0;
     526      var grandTotal = 0;   
     527      var currentDate = new Date(periodStartDate.getFullYear(), periodStartDate.getMonth(), periodStartDate.getDate());     
    476528      var columnCounter = 0;
    477529      do
     
    489541          }
    490542          grandTotal += data;         
    491           if ( (currentDate.getFullYear() <= siteStartDate.getFullYear()))
     543          if ( (currentDate.getFullYear() < siteStartDate.getFullYear()))
    492544          {
    493545            data = '';
     
    540592        else if (viewType == 'WEEK')
    541593        {
    542           var weekIndex = getWeekByDate(currentDate);
     594          var weekIndex = getISOWeekNumber(currentDate);
    543595          keyIndex = yearIndex + '' + weekIndex;
    544596          if (siteData != null)
     
    547599          }
    548600          grandTotal += data;         
    549           if (currentDate.getFullYear() <= siteStartDate.getFullYear() && getWeekByDate(currentDate) < getWeekByDate(siteStartDate))
     601          if (currentDate.getFullYear() <= siteStartDate.getFullYear() && getISOWeekNumber(currentDate) < getISOWeekNumber(siteStartDate))
    550602          {
    551603            data = '';
     
    559611        tableRow.appendChild(getTableCellElement(data, 'reportdata'));
    560612        columnCounter++;       
    561       }while (columnCounter< numCols)     
     613      } while (columnCounter < numCols)
    562614      tableRow.appendChild(getTableCellElement(grandTotal, 'rowsummary'));
    563615      reportTable.appendChild(tableRow);
     
    604656    return listElement;
    605657  }
     658
     659  /*
     660   * Get the ISO week number for a given date
     661   *
     662   * Based on code at http://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php
     663   */
     664  function getISOWeekNumber(date)
     665  {
     666    tmpDate = new Date(date);
     667    tmpDate.setHours(0,0,0);
     668    // Set to nearest Thursday: current date + 4 - current day number
     669    // Make Sunday day number 7
     670    tmpDate.setDate(tmpDate.getDate() + 4 - (tmpDate.getDay() || 7));
     671    // Get first day of year
     672    var yearStart = new Date(tmpDate.getFullYear(), 0, 1);
     673    // Calculate full weeks to nearast Thursday (86400000 = 24*60*60*1000 number of ms in a day)
     674    var weekNo = Math.ceil(( ( (tmpDate - yearStart) / 86400000) + 1)/7);
     675    return weekNo;
     676  }
     677
    606678  </script>
    607679 
     
    683755                </ul>                 
    684756              </td>
    685             </tr>           
     757            </tr>
    686758            </table>
    687759          </td>
Note: See TracChangeset for help on using the changeset viewer.