/** * Compute next creation date of a ticket * * New parameter in version 0.84 : $calendars_id * * @param $begin_date datetime Begin date of the recurrent ticket * @param $end_date datetime End date of the recurrent ticket * @param $periodicity timestamp Periodicity of creation * @param $create_before timestamp Create before specific timestamp * @param $calendars_id integer ID of the calendar to used * * @return datetime next creation date **/ function computeNextCreationDate($begin_date, $end_date, $periodicity, $create_before, $calendars_id) { if (empty($begin_date) || $begin_date == 'NULL') { return 'NULL'; } if (!empty($end_date) && $end_date != 'NULL') { if (strtotime($end_date) < time()) { return 'NULL'; } } $check = true; if (preg_match('/([0-9]+)MONTH/', $periodicity) || preg_match('/([0-9]+)YEAR/', $periodicity)) { $check = false; } if ($check && $create_before > $periodicity) { Session::addMessageAfterRedirect(__('Invalid frequency. It must be greater than the preliminary creation.'), false, ERROR); return 'NULL'; } if ($periodicity != 0) { // Standard time computation $timestart = strtotime($begin_date) - $create_before; $now = time(); if ($now > $timestart) { $value = $periodicity; $step = "second"; if (preg_match('/([0-9]+)MONTH/', $periodicity, $matches)) { $value = $matches[1]; $step = 'MONTH'; } else { if (preg_match('/([0-9]+)YEAR/', $periodicity, $matches)) { $value = $matches[1]; $step = 'YEAR'; } else { if ($value % DAY_TIMESTAMP == 0) { $value = $value / DAY_TIMESTAMP; $step = "DAY"; } else { $value = $value / HOUR_TIMESTAMP; $step = "HOUR"; } } } while ($timestart < $now) { $timestart = strtotime("+ {$value} {$step}", $timestart); } } // Time start over end date if (!empty($end_date) && $end_date != 'NULL') { if ($timestart > strtotime($end_date)) { return 'NULL'; } } $calendar = new Calendar(); if ($calendars_id && $calendar->getFromDB($calendars_id)) { $durations = $calendar->getDurationsCache(); if (array_sum($durations) > 0) { // working days exists while (!$calendar->isAWorkingDay($timestart)) { $timestart = strtotime("+ 1 day", $timestart); } } } return date("Y-m-d H:i:s", $timestart); } return 'NULL'; }