PM-2917 "Case Scheduler issues" SOLVED

Issue:
    Case Scheduler issues
Cause:
    No se considero ciertas combinaciones de configuracion para: Daily, Monthly
Solution:
    - Se mejoro los metodos/algoritmos involucrados para la "generacion de la siguiente fecha de ejecucion"
    - Se reviso todas las opciones de configuracion: Daily, Weekly, Monthly, One time only, Every
This commit is contained in:
Victor Saisa Lopez
2015-06-10 12:12:22 -04:00
parent 5a893d0ca8
commit d8d3b32d42
4 changed files with 118 additions and 273 deletions

View File

@@ -600,164 +600,111 @@ class CaseScheduler extends BaseCaseScheduler
$this->Update( $Fields );
}
public function updateNextRun($sOption, $sValue = "", $sActualTime = "", $sDaysPerformTask = "", $sWeeks = "", $sStartDay = "", $sMonths = "", $currentDate = "", $flagOptionWeeklyNextRun = true)
public function updateNextRun($option, $optionMonth = "", $date = "", $daysPerformTask = "", $weeks = "", $startDay = "", $months = "", $currentDate = "", $flagNoTodayForNextRun = true)
{
$nActualDate = $currentDate . " " . $sActualTime;
$dEstimatedDate = '';
$sWeeks = trim($sWeeks, " |");
try {
$dateNextRun = "";
switch ($sOption) {
case '1':
switch ($sValue) {
case '1':
$dEstimatedDate = date( 'Y-m-d H:i:s', strtotime( "$nActualDate +1 day" ) );
$currentDate = trim($currentDate . " " . $date); //$date and $currentDate are the same
$weeks = trim($weeks, " |");
$months = trim($months, " |");
$arrayMonthsShort = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
$arrayWeekdays = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
switch ((int)($option)) {
case 1:
//Daily
$dateNextRun = date("Y-m-d H:i:s", strtotime(($flagNoTodayForNextRun)? "$currentDate +1 day" : $currentDate));
break;
case '2':
$nDayOfTheWeek = date( 'w', strtotime( $sActualTime ) );
$nDayOfTheWeek = ($nDayOfTheWeek == 0) ? 7 : $nDayOfTheWeek;
case 2:
//Weekly
if ($weeks != "") {
$weekday = (int)(date("w", strtotime($date)));
$weekday = ($weekday == 0)? 7 : $weekday;
if ($nDayOfTheWeek >= 5) {
$dEstimatedDate = date( 'Y-m-d H:i:s', strtotime( "$nActualDate +3 day" ) );
} else {
$dEstimatedDate = date( 'Y-m-d H:i:s', strtotime( "$nActualDate +1 day" ) );
}
break;
case '3':
$dEstimatedDate = date( 'Y-m-d H:i:s', strtotime( "$nActualDate + " . $sDaysPerformTask . " day" ) );
break;
}
break;
case '2':
if ($sWeeks != "") {
$aDaysWeek = array ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
$arrayWeekdaysData = explode("|", $weeks);
$nDayOfTheWeek = (int)(date("w", strtotime($sActualTime)));
$nDayOfTheWeek = ($nDayOfTheWeek == 0)? 7 : $nDayOfTheWeek;
$firstWeekday = (int)($arrayWeekdaysData[0]);
$arrayWeekdays = explode("|", $sWeeks);
$firstDay = (int)($arrayWeekdays[0]);
$lastDay = (int)($arrayWeekdays[count($arrayWeekdays) - 1]);
$flagFound1 = $nDayOfTheWeek < $firstDay || in_array($nDayOfTheWeek, $arrayWeekdays);
$flagFound2 = ($flagFound1)? false : ($firstDay <= $nDayOfTheWeek && $nDayOfTheWeek <= $lastDay);
if ($flagFound1 || $flagFound2) {
$nextWeekday = $firstWeekday;
$typeStatement = "this";
$flag = false;
if ($flagFound1) {
$indexDay = (in_array($nDayOfTheWeek, $arrayWeekdays))? $nDayOfTheWeek : $firstDay;
foreach ($arrayWeekdaysData as $value) {
$d = (int)($value);
if ($flagOptionWeeklyNextRun) {
$index = array_search($nDayOfTheWeek, $arrayWeekdays);
if ((!$flagNoTodayForNextRun && $d >= $weekday) || ($flagNoTodayForNextRun && $d > $weekday)) {
$nextWeekday = $d;
$flag = true;
break;
}
}
if ($index !== false && isset($arrayWeekdays[$index + 1])) {
$indexDay = $arrayWeekdays[$index + 1];
} else {
if (!$flag) {
$typeStatement = "next";
$indexDay = $firstDay;
}
}
}
if ($flagFound2) {
$indexDay = $firstDay;
foreach ($arrayWeekdays as $value) {
$day = (int)($value);
if ($day > $nDayOfTheWeek) {
$indexDay = $day;
break;
}
}
}
$indexDay--;
$dEstimatedDate = date("Y-m-d", strtotime($nActualDate . " " . $typeStatement . " " . $aDaysWeek[$indexDay])) . " " . date("H:i:s", strtotime($sActualTime));
} else {
$nEveryDays = $sDaysPerformTask;
$typeStatement = ($firstDay >= $nDayOfTheWeek || $nEveryDays == 1)? "next" : "last";
$indexDay = $firstDay - 1;
if ($nEveryDays == 1) {
$dEstimatedDate = date("Y-m-d", strtotime($nActualDate . " " . $typeStatement . " " . $aDaysWeek[$indexDay])) . " " . date("H:i:s", strtotime($sActualTime));
} else {
$nEveryDays = 1;
$nDataTmp = date( 'Y-m-d', strtotime( "$nActualDate + " . $nEveryDays . " Week" ) );
$dEstimatedDate = date("Y-m-d", strtotime($nDataTmp . " " . $typeStatement . " " . $aDaysWeek[$indexDay])) . " " . date("H:i:s", strtotime($sActualTime));
}
}
$dateNextRun = date("Y-m-d", strtotime($currentDate . " " . $typeStatement . " " . $arrayWeekdays[$nextWeekday - 1])) . " " . date("H:i:s", strtotime($date));
}
break;
case '3':
if (strlen( $sMonths ) > 0) {
// Must have at least one selected month
// Calculamos para la siguiente ejecucion, acorde a lo seleccionado
$aStartDay = explode( '|', $sStartDay );
$nYear = date( "Y", strtotime( $sActualTime ) );
$nCurrentMonth = date( "m", strtotime( $sActualTime ) );
$nCurrentDay = date( "d", strtotime( $sActualTime ) );
$aMonths = explode( '|', $sMonths );
case 3:
//Monthly
if ($months != "") {
$year = (int)(date("Y", strtotime($date)));
$month = (int)(date("m", strtotime($date)));
$nSW = 0;
$nNextMonth = 0;
foreach ($aMonths as $value) {
if ($value > $nCurrentMonth) {
$nNextMonth = $value - 1;
$nSW = 1;
$arrayStartDay = explode("|", $startDay);
$arrayMonthsData = explode("|", $months);
$firstMonth = (int)($arrayMonthsData[0]);
$nextMonth = $firstMonth;
$flag = false;
foreach ($arrayMonthsData as $value) {
$m = (int)($value);
if ((!$flagNoTodayForNextRun && $m >= $month) || ($flagNoTodayForNextRun && $m > $month)) {
$nextMonth = $m;
$flag = true;
break;
}
}
if ($nSW == 1) {
$nExecNextMonth = $nNextMonth;
} else {
$nExecNextMonth = $aMonths[0] - 1;
$nYear ++;
if (!$flag) {
$year++;
}
switch ($sValue) {
case '1':
$nExecNextMonth ++;
$nCurrentDay = $aStartDay[1];
$dEstimatedDate = date( 'Y-m-d', strtotime( "$nYear-$nExecNextMonth-$nCurrentDay" ) ) . ' ' . date( 'H:i:s', strtotime( $sActualTime ) );
switch ((int)($optionMonth)) {
case 1:
$day = (int)($arrayStartDay[1]);
$dateNextRun = date("Y-m-d", strtotime("$year-$nextMonth-$day")) . " " . date("H:i:s", strtotime($date));
break;
case '2':
$aMontsShort = array ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$aWeeksShort = array ('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
$sNumDayWeek = $aStartDay[1];
$sDayWeek = ($aStartDay[2] == 7 ? 0 : $aStartDay[2]);
switch ($sNumDayWeek) {
case '1':
$sDaysWeekOpt = "+0";
break;
case '2':
$sDaysWeekOpt = "+1";
break;
case '3':
$sDaysWeekOpt = "+2";
break;
case '4':
$sDaysWeekOpt = "+3";
break;
case '5':
$sDaysWeekOpt = "-1";
$nExecNextMonth ++;
if ($nExecNextMonth >= 12) {
$nExecNextMonth = 0;
$nYear ++;
}
break;
}
$dEstimatedDate = date( 'Y-m-d', strtotime( $sDaysWeekOpt . ' week ' . $aWeeksShort[$sDayWeek - 1] . ' ' . $aMontsShort[$nExecNextMonth] . ' ' . $nYear ) ) . ' ' . date( 'H:i:s', strtotime( $sActualTime ) );
case 2:
$arrayFormat = array(
1 => "+0 week %s %s %d", //First
2 => "+1 week %s %s %d", //Second
3 => "+2 week %s %s %d", //Third
4 => "+3 week %s %s %d", //Fourth
5 => "last %s of %s %d" //Last
);
$day = (int)($arrayStartDay[2]);
$dateNextRun = date("Y-m-d", strtotime(sprintf($arrayFormat[(int)($arrayStartDay[1])], $arrayWeekdays[$day - 1], $arrayMonthsShort[$nextMonth - 1], $year))) . " " . date("H:i:s", strtotime($date));
break;
}
}
break;
}
return $dEstimatedDate;
//Return
return $dateNextRun;
} catch (Exception $e) {
throw $e;
}
}
public function Exists ($sUid)
@@ -836,45 +783,35 @@ class CaseScheduler extends BaseCaseScheduler
public function getTimeNextRunByDate(array $arrayCaseSchedulerData, $date, $flagUpdateTimeNextRun = true)
{
try {
$arrayNextDate = array();
//Get date
$caseSchedulerOption = (int)($arrayCaseSchedulerData["SCH_OPTION"]);
$caseSchedulerTimeNextRun = $arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"];
$caseSchedulerStartTime = date("H:i:s", strtotime($arrayCaseSchedulerData["SCH_START_TIME"]));
list($value, $daysPerformTask, $weeks, $startDay, $months) = $this->getVariablesFromRecord($arrayCaseSchedulerData);
$timeDate = strtotime($date); //Current time
$timeCaseSchedulerTimeNextRun = strtotime($caseSchedulerTimeNextRun);
$flagTimeNextRun = false;
$flagTimeNextRun = true;
$flagUpdate = false;
if ($caseSchedulerOption != 1) {
//Others
$flagTimeNextRun = true;
} else {
switch ($caseSchedulerOption) {
case 1:
case 2:
case 3:
//Daily
$arrayDate = array(
date("Y-m-d", strtotime($arrayCaseSchedulerData["SCH_START_DATE"])) . " " . date("H:i:s", $timeCaseSchedulerTimeNextRun),
date("Y-m-d", $timeDate) . " " . date("H:i:s", $timeCaseSchedulerTimeNextRun)
);
//Weekly
//Monthly
$caseSchedulerTimeNextRun = date("Y-m-d", strtotime($arrayCaseSchedulerData["SCH_START_DATE"])) . " " . $caseSchedulerStartTime;
$caseSchedulerTimeNextRun = $this->updateNextRun($caseSchedulerOption, $value, $caseSchedulerTimeNextRun, $daysPerformTask, $weeks, $startDay, $months, "", false);
$flagTimeNextRun = true;
foreach ($arrayDate as $d) {
$caseSchedulerTimeNextRun2 = $d;
$timeCaseSchedulerTimeNextRun2 = strtotime($caseSchedulerTimeNextRun2);
if ($timeDate < $timeCaseSchedulerTimeNextRun2) {
$caseSchedulerTimeNextRun = $caseSchedulerTimeNextRun2;
$timeCaseSchedulerTimeNextRun = strtotime($caseSchedulerTimeNextRun);
if ($timeCaseSchedulerTimeNextRun > $timeDate) {
$flagTimeNextRun = false;
$flagUpdate = true;
}
break;
}
}
}
if ($flagTimeNextRun) {
switch ($caseSchedulerOption) {
@@ -884,7 +821,7 @@ class CaseScheduler extends BaseCaseScheduler
//Daily
//Weekly
//Monthly
$caseSchedulerTimeNextRun = date("Y-m-d", $timeDate) . " " . date("H:i:s", $timeCaseSchedulerTimeNextRun);
$caseSchedulerTimeNextRun = date("Y-m-d", $timeDate) . " " . $caseSchedulerStartTime;
$caseSchedulerTimeNextRun = $this->updateNextRun($caseSchedulerOption, $value, $caseSchedulerTimeNextRun, $daysPerformTask, $weeks, $startDay, $months, "", false);
$timeCaseSchedulerTimeNextRun = strtotime($caseSchedulerTimeNextRun);
@@ -895,7 +832,7 @@ class CaseScheduler extends BaseCaseScheduler
break;
case 4:
//One time only
$caseSchedulerTimeNextRun = date("Y-m-d", $timeDate) . " " . date("H:i:s", $timeCaseSchedulerTimeNextRun);
$caseSchedulerTimeNextRun = date("Y-m-d", $timeDate) . " " . $caseSchedulerStartTime;
$timeCaseSchedulerTimeNextRun = strtotime($caseSchedulerTimeNextRun);

View File

@@ -897,7 +897,6 @@ class CaseScheduler
$sDaysPerformTask = "";
$sWeeks = "";
$sMonths = "";
$sStartDay = "";
switch ($option) {
case "INS":
@@ -992,7 +991,6 @@ class CaseScheduler
}
$arrayCaseSchedulerData["SCH_MONTHS"] = $sMonths;
$sStartDay = $arrayCaseSchedulerData["SCH_START_DAY"];
$sValue = $nStartDay;
break;
}
@@ -1013,123 +1011,33 @@ class CaseScheduler
$arrayCaseSchedulerData["SCH_END_DATE"] = ($arrayData["SCH_END_DATE"] != "")? $arrayData["SCH_END_DATE"] : null;
//If the start date has changed then recalculate the next run time
$recalculateDate = ($arrayData["SCH_START_DATE"] == $arrayData["PREV_SCH_START_DATE"])? false : true;
$recalculateTime = (date("H:i:s", strtotime($arrayData["SCH_START_TIME"])) == date("H:i:s", strtotime($arrayData["PREV_SCH_START_TIME"])))? false : true;
$recalculateDate = ($arrayData["SCH_START_DATE"] != $arrayData["PREV_SCH_START_DATE"])? true : false;
$recalculateTime = (date("H:i:s", strtotime($arrayData["SCH_START_TIME"])) != date("H:i:s", strtotime($arrayData["PREV_SCH_START_TIME"])))? true : false;
break;
}
$nActualTime = $arrayData["SCH_START_TIME"];
if ($caseSchedulerOption != 1 && $caseSchedulerOption != 4 && $caseSchedulerOption != 5) {
if ($sStartDay == "") {
$sStartDay = date("Y-m-d");
}
$dCurrentDay = (int)(date("d"));
$dCurrentMonth = (int)(date("m"));
$aStartDay = ($caseSchedulerOption == 3)? explode("|", $arrayCaseSchedulerData["SCH_START_DAY"]) : array();
if ($caseSchedulerOption == 3 && $aStartDay[0] == "1") {
$monthsArray = explode("|", $sMonths);
foreach ($monthsArray as $row) {
switch ($option) {
case "INS":
if ((int)($row) == $dCurrentMonth && $dCurrentDay <= (int)($aStartDay[1])) {
$startTime = $arrayData["SCH_START_TIME"] . ":00";
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = date("Y") . "-" . $row . "-" . $aStartDay[1] . " " . $startTime;
break;
} else {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->updateNextRun($caseSchedulerOption, $sValue, $nActualTime, $sDaysPerformTask, $sWeeks, $sStartDay, $sMonths, $sDateTmp, false);
}
break;
case "UPD":
if ($dCurrentMonth == $row && $dCurrentDay < $aStartDay[1]) {
$startTime = $arrayData["SCH_START_TIME"] . ":00";
if ($recalculateDate) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = date("Y") . "-" . $row . "-" . $aStartDay[1] . " " . $startTime;
} else {
if ($recalculateTime) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->getSchTimeNextRun("Y-m-d") . " " . $arrayData["SCH_START_TIME"] . ":00";
}
}
break;
} else {
if ($recalculateDate) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->updateNextRun($caseSchedulerOption, $sValue, $nActualTime, $sDaysPerformTask, $sWeeks, $sStartDay, $sMonths, $sDateTmp, false);
} else {
if ($recalculateTime) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->getSchTimeNextRun("Y-m-d") . " " . $arrayData["SCH_START_TIME"] . ":00";
}
}
}
break;
}
}
} else {
switch ($option) {
case "INS":
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->updateNextRun($caseSchedulerOption, $sValue, $nActualTime, $sDaysPerformTask, $sWeeks, $sStartDay, $sMonths, $sDateTmp, false);
break;
case "UPD":
if ($recalculateDate) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->updateNextRun($caseSchedulerOption, $sValue, $nActualTime, $sDaysPerformTask, $sWeeks, $sStartDay, $sMonths, $sDateTmp, false);
} else {
if ($recalculateTime) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->getSchTimeNextRun("Y-m-d") . " " . $arrayData["SCH_START_TIME"] . ":00";
}
}
break;
}
}
} else {
if ($caseSchedulerOption == 4) {
switch ($caseSchedulerOption) {
case 4:
$arrayCaseSchedulerData["SCH_END_DATE"] = $arrayCaseSchedulerData["SCH_START_TIME"];
}
switch ($option) {
case "INS":
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $arrayCaseSchedulerData["SCH_START_TIME"];
break;
case "UPD":
if ($recalculateDate) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $arrayCaseSchedulerData["SCH_START_TIME"];
} else {
if ($recalculateTime) {
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = $caseScheduler->getSchTimeNextRun("Y-m-d") . " " . $arrayData["SCH_START_TIME"] . ":00";
}
}
break;
}
if ($caseSchedulerOption == 5) {
case 5:
switch ($option) {
case "INS":
$arrayCaseSchedulerData["SCH_START_TIME"] = time();
$arrayCaseSchedulerData["SCH_START_DATE"] = $arrayCaseSchedulerData["SCH_START_TIME"];
$date = $arrayCaseSchedulerData["SCH_START_TIME"];
break;
case "UPD":
$date = $caseScheduler->getSchLastRunTime();
if (is_null($date)) {
$date = $caseScheduler->getSchStartTime();
}
$date = strtotime($date);
break;
}
$arrayCaseSchedulerData["SCH_REPEAT_EVERY"] = $arrayData["SCH_REPEAT_EVERY"];
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = date("Y-m-d H:i:s", $date + (((int)($arrayData["SCH_REPEAT_EVERY"])) * 60 * 60));
}
break;
}
switch ($option) {
case "INS":
$arrayCaseSchedulerData["SCH_TIME_NEXT_RUN"] = date("Y-m-d") . " " . (($caseSchedulerOption != 5)? $arrayData["SCH_START_TIME"] . ":00" : date("H:i:s"));
if ($arrayData["SCH_END_DATE"] != "") {
$arrayCaseSchedulerData["SCH_END_DATE"] = $arrayData["SCH_END_DATE"];
}
@@ -1138,13 +1046,13 @@ class CaseScheduler
break;
}
if (!empty($arrayData["SCH_REPEAT_TASK_CHK"])) {
if ($arrayData["SCH_REPEAT_EVERY_OPT"] . "" == "2") {
$arrayCaseSchedulerData["SCH_REPEAT_EVERY"] = ((int)($arrayData["SCH_REPEAT_EVERY"])) * 60;
} else {
$arrayCaseSchedulerData["SCH_REPEAT_EVERY"] = (int)($arrayData["SCH_REPEAT_EVERY"]);
}
}
//if (!empty($arrayData["SCH_REPEAT_TASK_CHK"])) {
// if ($arrayData["SCH_REPEAT_EVERY_OPT"] . "" == "2") {
// $arrayCaseSchedulerData["SCH_REPEAT_EVERY"] = ((int)($arrayData["SCH_REPEAT_EVERY"])) * 60;
// } else {
// $arrayCaseSchedulerData["SCH_REPEAT_EVERY"] = (int)($arrayData["SCH_REPEAT_EVERY"]);
// }
//}
//Create/Update
$caseSchedulerAux = new \CaseScheduler();
@@ -1193,11 +1101,11 @@ class CaseScheduler
break;
case 4:
//One time only
$flagUpdateTimeNextRun = $arrayCaseSchedulerData["SCH_START_TIME"] != $arrayCaseSchedulerDataOld["SCH_START_TIME"];
$flagUpdateTimeNextRun = $recalculateTime || $arrayCaseSchedulerData["SCH_START_TIME"] != $arrayCaseSchedulerDataOld["SCH_START_TIME"];
break;
case 5:
//Every
$flagUpdateTimeNextRun = $arrayCaseSchedulerData["SCH_REPEAT_EVERY"] != $arrayCaseSchedulerDataOld["SCH_REPEAT_EVERY"];
$flagUpdateTimeNextRun = $option == "INS" || $arrayCaseSchedulerData["SCH_REPEAT_EVERY"] != $arrayCaseSchedulerDataOld["SCH_REPEAT_EVERY"];
break;
}

View File

@@ -180,7 +180,7 @@
<td class="FormFieldContent">
<table style="width: 100%">
<tr>
<td style="width: 20%">{$form.SCH_START_DAY}</td>
<td style="width: 35%">{$form.SCH_START_DAY}</td>
<td> {$SCH_START_DAY_OPT_1} {$form.SCH_START_DAY_OPT_1} <br/>
{$SCH_START_DAY_OPT_2_WEEKS} {$SCH_START_DAY_OPT_2_DAYS_WEEK}
{$form.SCH_START_DAY_OPT_2_WEEKS} {$form.SCH_START_DAY_OPT_2_DAYS_WEEK} <br/>

View File

@@ -109,7 +109,7 @@ WHERE A.PRO_UID='@#PRO_UID' AND A.TAS_START = 'TRUE' ]]><en><![CDATA[Task]]></en
<en><![CDATA[Monthly]]></en>
</SELECT_3>
<SCH_START_DAY type="radiogroup" required="0" mode="edit" options="Array">
<en><![CDATA[]]><option name="1"><![CDATA[Day]]></option><option name="2"><![CDATA[The]]></option></en>
<en><![CDATA[]]><option name="1"><![CDATA[Day of month]]></option><option name="2"><![CDATA[The day]]></option></en>
</SCH_START_DAY>
<SCH_START_DAY_OPT_1 type="text" maxlength="2" validate="Int" mask="##" required="0" readonly="0" defaultvalue="1" size="2" mode="edit">
<en><![CDATA[]]></en>