diff --git a/include/class.businesshours.php b/include/class.businesshours.php index 965d6809b9..0df60bf149 100644 --- a/include/class.businesshours.php +++ b/include/class.businesshours.php @@ -52,25 +52,30 @@ public function getSchedule() { } // Intitialize occurrenses buckets - private function initOccurrences(Datetime $date) { + private function initOccurrences(Datetime $date, $grace_period_hrs=72) { $dt = clone $date; // Start from next day if current date is already processed - if ($this->workhours[$dt->format('Y-m-d')]) + if (isset($this->workhours[$dt->format('Y-m-d')])) $dt->modify('+1 day'); + // Apply grace period + $period = clone $dt; + $period->modify("+$grace_period_hrs hour"); + // Reset occurrense buckets $this->workhours = $this->holidays = array(); // Init workhours foreach ($this->getSchedule()->getEntries() as $entry) - $this->workhours += $entry->getOccurrences($dt, null, 4); + $this->workhours += $entry->getOccurrences($dt, + $period->format('Y-m-d')); ksort($this->workhours); // Init holidays taking into account end date of the workhours in // the current scope. $enddate = array_pop(array_keys($this->workhours)); foreach ($this->getSchedule()->getHolidaysSchedules() as $schedule) { foreach ($schedule->getEntries() as $entry) - $this->holidays += $entry->getOccurrences($dt, $enddate, 5); + $this->holidays += $entry->getOccurrences($dt, $enddate); } ksort($this->holidays); @@ -131,10 +136,11 @@ public function addWorkingHours(Datetime $date, $hours, &$auditlog=array()) { $holiday->getHours(), $holiday->getSchedule()->getName(), $holiday->getDesc())); + + //Move the date to end of the day of the holiday + $date->modify("$d ".$holiday->getEndsTime()); // If the holiday is a full day then assume the day is a goner if ($holiday->isFullDay()) continue; - //Move the date to end of the partial day of the holiday - $date->modify("$d ".$holiday->getEndsTime()); $partial = true; // See if we need to recover any time prior to start of // holiday e.g if the day starts at 8am but the holiday diff --git a/include/class.schedule.php b/include/class.schedule.php index dd06bce2f5..a5dc3a8794 100644 --- a/include/class.schedule.php +++ b/include/class.schedule.php @@ -846,16 +846,18 @@ function next() { return $current; } - function getOccurrences($start=null, $end=null, $num=2) { + function getOccurrences($start=null, $end=null, $num=5) { $occurrences = array(); if (($current = $this->getCurrent($start))) { - $occurrences[$current->format('Y-m-d')] = $this; + $start = $start ?: $current; while (count($occurrences) < $num) { - if (!($next=$this->next())) - break; $date = $current->format('Y-m-d'); - $occurrences[$date] = $this; - if ($end && strtotime($date) >= strtotime($end)) + if ($end && strtotime($date) > strtotime($end)) + break; + if (strtotime($date) >= strtotime($start->format('Y-m-d'))) + $occurrences[$date] = $this; + + if (!($current=$this->next())) break; } }