Commit d99b0120 authored by Cédric Anne's avatar Cédric Anne Committed by Johan Cwiklinski

Fix creation of reccurent ticket using a calendar; fixes #6205 #6263 #6264 #6257

parent 0f908362
......@@ -304,8 +304,7 @@ class Calendar extends CommonDropdown {
$timeend = strtotime($end);
$datestart = date('Y-m-d', $timestart);
$dateend = date('Y-m-d', $timeend);
// Need to finish at the closing day : set hour to midnight :
/// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
// Need to finish at the closing day : set hour to midnight (23:59:59 for PHP)
$timerealend = strtotime($dateend.' 23:59:59');
$activetime = 0;
......@@ -321,8 +320,8 @@ class Calendar extends CommonDropdown {
if (!$this->isHoliday($actualdate)) {
$beginhour = '00:00:00';
/// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
$endhour = '23:59:59';
// Calendar segment work with '24:00:00' format for midnight
$endhour = '24:00:00';
$dayofweek = self::getDayNumberInWeek($actualtime);
$timeoftheday = 0;
......@@ -494,6 +493,8 @@ class Calendar extends CommonDropdown {
// If day is an holiday must start on the begin of next working day
$actualdate = date('Y-m-d', $actualtime);
$dayofweek = self::getDayNumberInWeek($actualtime);
$firstworkhour = CalendarSegment::getFirstWorkingHour($this->fields['id'],
$dayofweek);
if ($this->isHoliday($actualdate)
|| ($cache_duration[$dayofweek] == 0)) {
......@@ -506,6 +507,9 @@ class Calendar extends CommonDropdown {
$firstworkhour = CalendarSegment::getFirstWorkingHour($this->fields['id'],
$dayofweek);
$actualtime = strtotime($actualdate.' '.$firstworkhour);
} else if (date('H:i:s', $actualtime) < $firstworkhour) {
// Go to first working hour of current day if actual time is before first working hour
$actualtime = strtotime($actualdate.' '.$firstworkhour);
}
while ($delay > 0) {
......@@ -544,36 +548,24 @@ class Calendar extends CommonDropdown {
if (!$this->isHoliday($actualdate)) {
$dayofweek = self::getDayNumberInWeek($actualtime);
$beginhour = '00:00:00';
/// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
$endhour = '23:59:59';
if ($actualdate == $datestart) { // First day cannot use cache
$beginhour = date('H:i:s', $timestart);
$timeoftheday = CalendarSegment::getActiveTimeBetween($this->fields['id'],
$dayofweek, $beginhour,
$endhour);
'24:00:00');
} else {
$timeoftheday = $cache_duration[$dayofweek];
}
// Day do not complete the delay : pass to next day
if ($timeoftheday < $delay && !$negative_delay) {
$actualtime = self::getActualTime($actualtime, DAY_TIMESTAMP);
$delay -= $timeoftheday;
} else if ($timeoftheday > $delay && $negative_delay) {
$actualtime = self::getActualTime($actualtime, DAY_TIMESTAMP, true);
if ($timeoftheday <= $delay && !$negative_delay
|| $timeoftheday >= $delay && $negative_delay) {
// Delay is greater or equal than remaining time in day
// -> pass to next day
$actualtime = self::getActualTime($actualtime, DAY_TIMESTAMP, $negative_delay);
$delay -= $timeoftheday;
} else { // End of the delay in the day : get hours with this delay
$beginhour = '00:00:00';
/// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
$endhour = '23:59:59';
if ($actualdate == $datestart) {
$beginhour = date('H:i:s', $timestart);
}
} else {
// End of the delay in the day : get hours with this delay
$endhour = CalendarSegment::addDelayInDay($this->fields['id'], $dayofweek,
$beginhour, $delay);
return $actualdate.' '.$endhour;
......@@ -631,9 +623,8 @@ class Calendar extends CommonDropdown {
$results = [];
for ($i=0; $i<7; $i++) {
/// Before PHP 5.3 need to be 23:59:59 and not 24:00:00
$results[$i] = CalendarSegment::getActiveTimeBetween($this->fields['id'], $i, '00:00:00',
'23:59:59');
'24:00:00');
}
return $results;
}
......
......@@ -170,8 +170,12 @@ class CalendarSegment extends CommonDBChild {
// Do not check hour if day before the end day of after the begin day
$iterator = $DB->request([
'SELECT' => [
new \QueryExpression("TIMEDIFF(LEAST(" . $DB->quoteValue($end_time) .", " . $DB->quoteName('end') . ")"),
new \QueryExpression("GREATEST(" . $DB->quoteName('begin') . ", " . $DB->quoteValue($begin_time) . ")) AS " . $DB->quoteName('TDIFF'))
new \QueryExpression("
TIMEDIFF(
LEAST(" . $DB->quoteValue($end_time) .", " . $DB->quoteName('end') . "),
GREATEST(" . $DB->quoteName('begin') . ", " . $DB->quoteValue($begin_time) . ")
) AS " . $DB->quoteName('TDIFF')
)
],
'FROM' => 'glpi_calendarsegments',
'WHERE' => [
......
......@@ -372,25 +372,40 @@ class TicketRecurrent extends CommonDropdown {
}
// First occurence of creation
$occurence_time = strtotime($begin_date);
$creation_time = $occurence_time - $create_before;
// Add steps while creation time is in past
while ($creation_time < $now) {
$creation_time = strtotime("+ $periodicity_as_interval", $creation_time);
$occurence_time = $creation_time + $create_before;
// Stop if end date reached
if ($has_end_date && $occurence_time > strtotime($end_date)) {
return 'NULL';
}
}
// Add steps while start time is not in working hours
$calendar = new Calendar();
if ($calendars_id && $calendar->getFromDB($calendars_id) && $calendar->hasAWorkingDay()) {
while (!$calendar->isAWorkingHour($occurence_time)) {
$creation_time = strtotime("+ $periodicity_as_interval", $creation_time);
// Base computation on calendar if calendar is defined
$occurence_date = $calendar->computeEndDate(
$begin_date,
1, // 1 second delay for first occurence as calendar method does not handle 0 delay
0,
$periodicity_in_seconds >= DAY_TIMESTAMP
);
$occurence_time = strtotime($occurence_date) - 1;
$creation_time = $occurence_time - $create_before;
while ($creation_time < $now) {
$occurence_date = $calendar->computeEndDate(
date('Y-m-d H:i:s', $occurence_time),
$periodicity_in_seconds,
0,
$periodicity_in_seconds >= DAY_TIMESTAMP
);
$occurence_time = strtotime($occurence_date);
$creation_time = $occurence_time - $create_before;
// Stop if end date reached
if ($has_end_date && $occurence_time > strtotime($end_date)) {
return 'NULL';
}
};
} else {
$occurence_time = strtotime($begin_date);
$creation_time = $occurence_time - $create_before;
// Add steps while creation time is in past
while ($creation_time < $now) {
$creation_time = strtotime("+ $periodicity_as_interval", $creation_time);
$occurence_time = $creation_time + $create_before;
// Stop if end date reached
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment