[Vtigercrm-developers] Bug in Current/Previous Week options in Reports?

Alan Lord alanslists at gmail.com
Tue Apr 28 08:34:13 GMT 2015


On 27/04/15 16:02, Alan Lord wrote:
> Our customer has created a report in which one of the conditions is
> using the Previous Week meta value on a date field.
>
> Her user preferences say a week starts on Monday.
>
> The report always shows results starting on the Sunday...
>
> Is this hardcoded somewhere? Should it use the user's preferences? Can I
> change it start from a Monday?

Oh Dear...

Looking through the code for the Report module in 
modules/Reports/Reports.php in the function getCriteriaJS() it says:

> $lastweek0 = date("Y-m-d",strtotime("-2 week Monday"));
> $lastWeekStartDateTime = new DateTimeField($lastweek0.' '. date('H:i:s'));
> $lastweek1 = date("Y-m-d",strtotime("-1 week Sunday"));
> $lastWeekEndDateTime = new DateTimeField($lastweek1.' '. date('H:i:s'));

Note that the startdate is supposed to start on a Monday and also note 
that it is hardcoded. Yet my customer is seeing the Previous Week select 
option generating a date that starts on a Sunday.

So I grepped the code:
>
> alanlord at dev1:/var/www/test$ grep -rn "lastWeekStartDateTime" *
> modules/Reports/Reports.php:968:    $lastWeekStartDateTime = new DateTimeField($lastweek0.' '. date('H:i:s'));
> modules/Reports/Reports.php:1124:      document.NewReport.startdate.value = "'.$lastWeekStartDateTime->getDisplayDate().'";
> modules/CustomView/CustomView.php:584:   $lastWeekStartDateTime = new DateTimeField($lastweek0 . ' ' . date('H:i:s'));
> modules/CustomView/CustomView.php:737:  document.CustomView.startdate.value = "' . $lastWeekStartDateTime->getDisplayDate() . '";

So I looked in modules/CustomView/CustomView.php in a function called 
getCriteriaJS() - Note the same name as above!!!

> $lastweek0 = date("Y-m-d", strtotime("-2 week Sunday"));
> $lastWeekStartDateTime = new DateTimeField($lastweek0 . ' ' . date('H:i:s'));
> $lastweek1 = date("Y-m-d", strtotime("-1 week Saturday"));
> $lastWeekEndDateTime = new DateTimeField($lastweek1 . ' ' . date('H:i:s'));

Note that this one says "Sunday" for the start date... Note that it is 
also hardcoded.

Changing this made no difference :-(

So then I decided to grep the tree for "lastweek" to see if was set 
somewhere else too...

> modules/Vtiger/models/Field.php:555:            $lastweek0 = date("Y-m-d",strtotime("-1 week Sunday"));
> modules/Vtiger/models/Field.php:557:            $lastweek0 = date("Y-m-d", strtotime("-2 week Sunday"));
> modules/Vtiger/models/Field.php:558:            $lastweek1 = date("Y-m-d", strtotime("-1 week Saturday"));
> modules/Vtiger/models/Field.php:636:            } elseif ($type == "lastweek") {
> modules/Vtiger/models/Field.php:637:                    $dateValues[0] = $lastweek0;
> modules/Vtiger/models/Field.php:638:                    $dateValues[1] = $lastweek1;
> modules/Vtiger/models/Field.php:722:           'lastweek' => array('label' => 'LBL_LAST_WEEK'),
> modules/Reports/ReportRun.php:1584:            $lastweek0 = date("Y-m-d",strtotime("-1 week Sunday"));
> modules/Reports/ReportRun.php:1586:            $lastweek0 = date("Y-m-d",strtotime("-2 week Sunday"));
> modules/Reports/ReportRun.php:1587:             $lastweek1 = date("Y-m-d",strtotime("-1 week Saturday"));
> modules/Reports/ReportRun.php:1683:             elseif($type == "lastweek" )
> modules/Reports/ReportRun.php:1686:                     $datevalue[0] = $lastweek0;
> modules/Reports/ReportRun.php:1687:                     $datevalue[1] = $lastweek1;
> modules/Reports/Reports.php:852:                "yesterday","today","tomorrow","lastweek","thisweek","nextweek","lastmonth","thismonth",
> modules/Reports/Reports.php:967:                $lastweek0 = date("Y-m-d",strtotime("-2 week Monday"));
> modules/Reports/Reports.php:968:                $lastWeekStartDateTime = new DateTimeField($lastweek0.' '. date('H:i:s'));
> modules/Reports/Reports.php:969:                $lastweek1 = date("Y-m-d",strtotime("-1 week Sunday"));
> modules/Reports/Reports.php:970:                $lastWeekEndDateTime = new DateTimeField($lastweek1.' '. date('H:i:s'));
> modules/Reports/Reports.php:1123:                } else if( type == "lastweek" ) {
> modules/CustomView/CustomView.php:493:                  "lastweek","thisweek","nextweek","lastmonth","thismonth",
> modules/CustomView/CustomView.php:521:                  "lastweek" => "" . $mod_strings['Last Week'] . "",
> modules/CustomView/CustomView.php:583:          $lastweek0 = date("Y-m-d", strtotime("-2 week Monday"));
> modules/CustomView/CustomView.php:584:          $lastWeekStartDateTime = new DateTimeField($lastweek0 . ' ' . date('H:i:s'));
> modules/CustomView/CustomView.php:585:          $lastweek1 = date("Y-m-d", strtotime("-1 week Sunday"));
> modules/CustomView/CustomView.php:586:          $lastWeekEndDateTime = new DateTimeField($lastweek1 . ' ' . date('H:i:s'));
> modules/CustomView/CustomView.php:736:                          } else if( type == "lastweek" ) {
> modules/CustomView/CustomView.php:1537:            $lastweek0 = date("Y-m-d",strtotime("-1 week Sunday"));
> modules/CustomView/CustomView.php:1539:                 $lastweek0 = date("Y-m-d", strtotime("-2 week Sunday"));
> modules/CustomView/CustomView.php:1540:         $lastweek1 = date("Y-m-d", strtotime("-1 week Saturday"));
> modules/CustomView/CustomView.php:1614:         } elseif ($type == "lastweek") {
> modules/CustomView/CustomView.php:1616:                 $datevalue[0] = $lastweek0;
> modules/CustomView/CustomView.php:1617:                 $datevalue[1] = $lastweek1;
> modules/CustomView/models/Record.php:655:               $lastweek0 = date("Y-m-d", strtotime("-2 week Sunday"));
> modules/CustomView/models/Record.php:656:               $lastweek1 = date("Y-m-d", strtotime("-1 week Saturday"));
> modules/CustomView/models/Record.php:719:               } elseif ($type == "lastweek") {
> modules/CustomView/models/Record.php:720:                       $dateValues[0] = $lastweek0;
> modules/CustomView/models/Record.php:721:                       $dateValues[1] = $lastweek1;
> modules/CustomView/models/Record.php:805:             'lastweek' => array('label' => 'LBL_LAST_WEEK'),

So the hardcoding that affects the population of the dates in the 
Conditions fields when you change the Select is in 
modules/Vtiger/models/Field.php Changing this to a Monday works in that 
is changes the date you *see*.

>           // (Last Week) If Today is "Sunday" then "-2 week Sunday" will give before last week Sunday date
>         if($todayName == "Monday")
>             $lastweek0 = date("Y-m-d",strtotime("-1 week Monday"));
>         else
>             $lastweek0 = date("Y-m-d", strtotime("-2 week Monday"));
>                 $lastweek1 = date("Y-m-d", strtotime("-1 week Sunday"));

But this didn't affect the output of the report at all... So I took a 
look in modules/Reports/ReportRun.php and found the *exact* same code... 
Changing this to Monday now produces the output I desire.

>         // (Last Week) If Today is "Sunday" then "-2 week Sunday" will give before last week Sunday date
>         if($todayName == "Monday")
>             $lastweek0 = date("Y-m-d",strtotime("-1 week Monday"));
>         else
>             $lastweek0 = date("Y-m-d",strtotime("-2 week Monday"));
>                 $lastweek1 = date("Y-m-d",strtotime("-1 week Sunday"));

There are several issues with all this...

1. There is tons of duplication of code
2. The code is inconsistent. Some bits use Monday, some use Sunday
3. It's hardcoded. Islamic countries consider that Saturday is the start 
of the week and certain businesses may well want to use any random day 
as the start of _their_ week
4. It's a mess.

Al




More information about the vtigercrm-developers mailing list