From 4874cfe234434560835965c639b5d9638567ba56 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 4 Nov 2022 17:44:02 +0100 Subject: [PATCH] #46 [TimeSpent] fix: rework time spent month with generic functions --- lib/dolisirh_function.lib.php | 4 +- view/timespent_month.php | 163 ++++++++-------------------------- 2 files changed, 40 insertions(+), 127 deletions(-) diff --git a/lib/dolisirh_function.lib.php b/lib/dolisirh_function.lib.php index 776e7f7..d494d0f 100644 --- a/lib/dolisirh_function.lib.php +++ b/lib/dolisirh_function.lib.php @@ -402,7 +402,7 @@ function loadTimeToSpendWithinRange($datestart, $dateend, $taskid = 0, $userid = $workinghours = new Workinghours($db); $holiday = new Holiday($db); - $daysInRange = num_between_day($datestart, $dateend, 1); + $daysInRange = num_between_day($datestart, $dateend); $workinghoursArray = $workinghours->fetchCurrentWorkingHours($userid, 'user'); $time_to_spend = array( @@ -448,7 +448,7 @@ function loadPassedTimeWithinRange($datestart, $dateend, $taskid = 0, $userid = $workinghours = new Workinghours($db); - $daysInRange = num_between_day($datestart, $dateend, 1); + $daysInRange = num_between_day($datestart, $dateend); $workinghoursArray = $workinghours->fetchCurrentWorkingHours($userid, 'user'); $passed_working_time = array( diff --git a/view/timespent_month.php b/view/timespent_month.php index 9283980..3030c71 100644 --- a/view/timespent_month.php +++ b/view/timespent_month.php @@ -491,17 +491,11 @@ } for ($idw = 0; $idw < $dayInMonth; $idw++) { - $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); // $firstdaytoshow is a date with hours = 0 - $dayinloopfromfirstdaytoshowgmt = dol_time_plus_duree($firstdaytoshowgmt, $idw, 'd'); // $firstdaytoshow is a date with hours = 0 - - $statusofholidaytocheck = Holiday::STATUS_APPROVED; - - $isavailablefordayanduser = $holiday->verifDateHolidayForTimestamp($usertoprocess->id, $dayinloopfromfirstdaytoshow, $statusofholidaytocheck); - $isavailable[$dayinloopfromfirstdaytoshow] = $isavailablefordayanduser; // in projectLinesPerWeek later, we are using $firstdaytoshow and dol_time_plus_duree to loop on each day - - $test = num_public_holiday($dayinloopfromfirstdaytoshowgmt, $dayinloopfromfirstdaytoshowgmt + 86400, $mysoc->country_code); - if ($test) { - $isavailable[$dayinloopfromfirstdaytoshow] = array('morning'=>false, 'afternoon'=>false, 'morning_reason'=>'public_holiday', 'afternoon_reason'=>'public_holiday'); + $dayInLoop = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); + if (isDayAvailable($dayInLoop, $user->id)) { + $isavailable[$dayInLoop] = array('morning'=>1, 'afternoon'=>1); + } else { + $isavailable[$dayInLoop] = array('morning'=>false, 'afternoon'=>false, 'morning_reason'=>'public_holiday', 'afternoon_reason'=>'public_holiday'); } } @@ -591,9 +585,6 @@ $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); // $firstdaytoshow is a date with hours = 0 $cssweekend = ''; - if ((($idw + 1) < $numstartworkingday) || (($idw + 1) > $numendworkingday)) { // This is a day is not inside the setup of working days, so we use a week-end css. - //$cssweekend = 'weekend'; - } $tmpday = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); @@ -615,63 +606,30 @@ print ' '; print '
'.dol_print_date($dayinloopfromfirstdaytoshow, '%d/%m').''; } -//print ''; print_liste_field_titre('', $_SERVER["PHP_SELF"], "", '', '', '', $sortfield, $sortorder, 'center maxwidthsearch '); - print "\n"; $colspan = 1 + (empty($conf->global->PROJECT_TIMESHEET_DISABLEBREAK_ON_PROJECT) ? 0 : 2); $workinghours = new Workinghours($db); $workinghoursArray = $workinghours->fetchCurrentWorkingHours($usertoprocess->id, 'user'); +$planned_working_time = loadTimeToSpendWithinRange($firstdaytoshow, dol_time_plus_duree($firstdaytoshow, $dayInMonth, 'd'), 0, $user->id); + $workinghoursMonth = 0; if ($conf->use_javascript_ajax) { print ''; print ''; print $langs->trans("Total"); - for ($idw = 0; $idw < $dayInMonth; $idw++) { - $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); - if ($isavailable[$dayinloopfromfirstdaytoshow]['morning'] && $isavailable[$dayinloopfromfirstdaytoshow]['afternoon']) { - $currentDay = date('l', $dayinloopfromfirstdaytoshow); - $currentDay = 'workinghours_' . strtolower($currentDay); - $workinghoursMonth += $workinghoursArray->{$currentDay} * 60; - } - } - print ' - '.$langs->trans("ExpectedWorkedHoursMonth", dol_print_date(dol_mktime(0, 0, 0, $month, $day, $year), "%B %Y")).' : '.(($workinghoursMonth != 0) ? convertSecondToTime($workinghoursMonth, 'allhourmin') : '00:00').''; + print ' - '; + print $langs->trans("ExpectedWorkedHoursMonth", dol_print_date(dol_mktime(0, 0, 0, $month, $day, $year), "%B %Y")); + print ' : '; + print (($planned_working_time['minutes'] != 0) ? convertSecondToTime($planned_working_time['minutes'] * 60, 'allhourmin') : '00:00').''; print ''; if (!empty($arrayfields['timeconsumed']['checked'])) { print ''; } - for ($idw = 0; $idw < $dayInMonth; $idw++) { - $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); - if ($isavailable[$dayinloopfromfirstdaytoshow]['morning'] && $isavailable[$dayinloopfromfirstdaytoshow]['afternoon']) { - $currentDay = date('l', $dayinloopfromfirstdaytoshow); - $currentDay = 'workinghours_' . strtolower($currentDay); - $workinghoursMonth = $workinghoursArray->{$currentDay} * 60; - } else { - $workinghoursMonth = 0; - } - - $cssweekend = ''; - if ((($idw + 1) < $numstartworkingday) || (($idw + 1) > $numendworkingday)) { // This is a day is not inside the setup of working days, so we use a week-end css. - //$cssweekend = 'weekend'; - } - - $tmpday = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); - - $cssonholiday = ''; - if (!$isavailable[$tmpday]['morning'] && !$isavailable[$tmpday]['afternoon']) { - $cssonholiday .= 'onholidayallday '; - } elseif (!$isavailable[$tmpday]['morning']) { - $cssonholiday .= 'onholidaymorning '; - } elseif (!$isavailable[$tmpday]['afternoon']) { - $cssonholiday .= 'onholidayafternoon '; - } - - print '
'.(($workinghoursMonth != 0) ? convertSecondToTime($workinghoursMonth, 'allhourmin') : '00:00').'
'; - } print ''; print ''; } @@ -679,9 +637,6 @@ // By default, we can edit only tasks we are assigned to $restrictviewformytask = ((!isset($conf->global->PROJECT_TIME_SHOW_TASK_NOT_ASSIGNED)) ? 2 : $conf->global->PROJECT_TIME_SHOW_TASK_NOT_ASSIGNED); if (count($tasksarray) > 0) { - //var_dump($tasksarray); // contains only selected tasks - //var_dump($tasksarraywithoutfilter); // contains all tasks (if there is a filter, not defined if no filter) - //var_dump($tasksrole); $j = 0; $level = 0; @@ -690,51 +645,28 @@ doliSirhLinesPerMonth($j, $firstdaytoshow, $lastdaytoshow, $usertoprocess, 0, $tasksarray, $level, $projectsrole, $tasksrole, $mine, $restrictviewformytask, $isavailable, 0, $arrayfields, $extrafields, $dayInMonth); if ($conf->use_javascript_ajax) { - $workinghoursMonth = 0; - //Planned working hours within start date & end date + //Passed working hours + $passed_working_time = loadPassedTimeWithinRange($firstdaytoshow, dol_time_plus_duree($lastdaytoshow, 1, 'd'), 0, $user->id); + print ''; print ''; print $langs->trans("Total"); - $currentDayCurrent = date('d', $now); - $currentMonth = date('m', $now); - - if ($currentMonth == $month) { - $dayInMonthCurrent = $currentDayCurrent; - } else { - $dayInMonthCurrent = $dayInMonth; - } + $daysInRange = num_between_day($firstdaytoshow, $lastdaytoshow, 1); - for ($idw = 0; $idw < $dayInMonthCurrent; $idw++) { - $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); - if ($isavailable[$dayinloopfromfirstdaytoshow]['morning'] && $isavailable[$dayinloopfromfirstdaytoshow]['afternoon']) { - $currentDay = date('l', $dayinloopfromfirstdaytoshow); - $currentDay = 'workinghours_' . strtolower($currentDay); - $workinghoursMonth += $workinghoursArray->{$currentDay} * 60; - } - } - $totalspenttime = $workinghoursMonth/3600; - print ' - '.$langs->trans("SpentWorkedHoursMonth", dol_print_date($firstdaytoshow, "dayreduceformat"), (($dayInMonth == $dayInMonthCurrent) ? dol_print_date($lastdaytoshow, "dayreduceformat") : dol_print_date($now, "dayreduceformat"))).' : '.(($workinghoursMonth != 0) ? convertSecondToTime($workinghoursMonth, 'allhourmin') : '00:00').''; + print ' - '; + print $langs->trans("SpentWorkedHoursMonth", dol_print_date($firstdaytoshow, "dayreduceformat"), dol_print_date($lastdaytoshow, "dayreduceformat")); + print ' : '.(($passed_working_time['minutes'] != 0) ? convertSecondToTime($passed_working_time['minutes'] * 60, 'allhourmin') : '00:00').''; print ''; if (!empty($arrayfields['timeconsumed']['checked'])) { print ''; } - for ($idw = 0; $idw < $dayInMonthCurrent; $idw++) { - $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); - if ($isavailable[$dayinloopfromfirstdaytoshow]['morning'] && $isavailable[$dayinloopfromfirstdaytoshow]['afternoon']) { - $currentDay = date('l', $dayinloopfromfirstdaytoshow); - $currentDay = 'workinghours_' . strtolower($currentDay); - $workinghoursMonth = $workinghoursArray->{$currentDay} * 60; - } else { - $workinghoursMonth = 0; - } - + //Fill days data + for ($idw = 0; $idw < $daysInRange; $idw++) { + $passed_hours_on_day = loadPassedTimeWithinRange(dol_time_plus_duree($firstdaytoshow, $idw, 'd'),dol_time_plus_duree($firstdaytoshow, $idw + 1, 'd'), 0, $user->id); $cssweekend = ''; - if ((($idw + 1) < $numstartworkingday) || (($idw + 1) > $numendworkingday)) { // This is a day is not inside the setup of working days, so we use a week-end css. - //$cssweekend = 'weekend'; - } $tmpday = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); @@ -747,10 +679,9 @@ $cssonholiday .= 'onholidayafternoon '; } - print '
'.(($workinghoursMonth != 0) ? convertSecondToTime($workinghoursMonth, 'allhourmin') : '00:00').'
'; - } - if ($dayInMonth == $dayInMonthCurrent) { - print ''; + print '
'; + print (($passed_hours_on_day['minutes'] != 0) ? convertSecondToTime($passed_hours_on_day['minutes'] * 60, 'allhourmin') : '00:00').'
'; } print ''; @@ -767,7 +698,7 @@ print ''.convertSecondToTime($totalconsumedtime, 'allhourmin').''; } - for ($idw = 0; $idw < $dayInMonthCurrent; $idw++) { + for ($idw = 0; $idw < $daysInRange; $idw++) { $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); if ($isavailable[$dayinloopfromfirstdaytoshow]['morning'] && $isavailable[$dayinloopfromfirstdaytoshow]['afternoon']) { $currentDay = date('l', $dayinloopfromfirstdaytoshow); @@ -778,9 +709,6 @@ } $cssweekend = ''; - if ((($idw + 1) < $numstartworkingday) || (($idw + 1) > $numendworkingday)) { // This is a day is not inside the setup of working days, so we use a week-end css. - //$cssweekend = 'weekend'; - } $tmpday = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); @@ -793,18 +721,18 @@ $cssonholiday .= 'onholidayafternoon '; } - print '
'.dol_print_date($workinghoursMonth, 'hour').'
'; - } - if ($dayInMonth == $dayInMonthCurrent) { - print ''; + print '
'.dol_print_date($workinghoursMonth, 'hour').'
'; } print ''; //Difference between planned & worked hours + $timeSpentDiff = loadDifferenceBetweenPassedAndSpentTimeWithinRange($firstdaytoshow, dol_time_plus_duree($lastdaytoshow, 1, 'd'), 0, $user->id); + print ''; print ''; print $langs->trans("Total"); - $difftotaltime = $totalspenttime * 3600 - $totalconsumedtime; + $difftotaltime = $timeSpentDiff * 60; if ($difftotaltime < 0) { $morecss = colorStringToArray($conf->global->DOLISIRH_EXCEEDED_TIME_SPENT_COLOR); } elseif ($difftotaltime > 0) { @@ -818,31 +746,17 @@ print ''.(($difftotaltime != 0) ? convertSecondToTime(abs($difftotaltime), 'allhourmin') : '00:00').''; } - for ($idw = 0; $idw < $dayInMonthCurrent; $idw++) { - $dayinloopfromfirstdaytoshow = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); - if ($isavailable[$dayinloopfromfirstdaytoshow]['morning'] && $isavailable[$dayinloopfromfirstdaytoshow]['afternoon']) { - $currentDay = date('l', $dayinloopfromfirstdaytoshow); - $currentDay = 'workinghours_' . strtolower($currentDay); - $workinghoursMonth = $workinghoursArray->{$currentDay} * 60; - } else { - $workinghoursMonth = 0; - } - + for ($idw = 0; $idw < $daysInRange; $idw++) { + $timeSpentDiffThisDay = loadDifferenceBetweenPassedAndSpentTimeWithinRange(dol_time_plus_duree($firstdaytoshow, $idw, 'd'), dol_time_plus_duree($firstdaytoshow, $idw + 1, 'd'), 0, $user->id); - $timeSpentThisDay = loadTimeSpentWithinRange($dayinloopfromfirstdaytoshow, dol_time_plus_duree($dayinloopfromfirstdaytoshow, 1, 'd'), 0, $user->id); - - $difftime = $workinghoursMonth - $timeSpentThisDay['total']; - if ($difftime < 0) { + if ($timeSpentDiffThisDay < 0) { $morecss = colorStringToArray($conf->global->DOLISIRH_EXCEEDED_TIME_SPENT_COLOR); - } elseif ($difftime > 0) { + } elseif ($timeSpentDiffThisDay > 0) { $morecss = colorStringToArray($conf->global->DOLISIRH_NOT_EXCEEDED_TIME_SPENT_COLOR); - } elseif ($difftime == 0) { + } elseif ($timeSpentDiffThisDay == 0) { $morecss = colorStringToArray($conf->global->DOLISIRH_PERFECT_TIME_SPENT_COLOR); } $cssweekend = ''; - if ((($idw + 1) < $numstartworkingday) || (($idw + 1) > $numendworkingday)) { // This is a day is not inside the setup of working days, so we use a week-end css. - //$cssweekend = 'weekend'; - } $tmpday = dol_time_plus_duree($firstdaytoshow, $idw, 'd'); @@ -855,10 +769,9 @@ $cssonholiday .= 'onholidayafternoon '; } - print '
'.(($difftime != 0) ? convertSecondToTime(abs($difftime), 'allhourmin') : '00:00').'
'; - } - if ($dayInMonth == $dayInMonthCurrent) { - print ''; + print '
'; + print (($timeSpentDiffThisDay != 0) ? convertSecondToTime(abs($timeSpentDiffThisDay*60), 'allhourmin') : '00:00').'
'; } print ''; }