CODE STYLE class.calendar.php

This commit is contained in:
Fernando Ontiveros
2012-10-09 12:33:39 -04:00
parent 29e93052df
commit 280e882df7

View File

@@ -1,6 +1,7 @@
<?php <?php
/** /**
* class.calendar.php * class.calendar.php
*
* @package workflow.engine.classes * @package workflow.engine.classes
* *
* ProcessMaker Open Source Edition * ProcessMaker Open Source Edition
@@ -30,7 +31,7 @@
* @author Hugo Loza <hugo@colosa.com> 2010-03-22 * * @author Hugo Loza <hugo@colosa.com> 2010-03-22 *
*/ */
require_once ( "classes/model/CalendarDefinition.php" ); require_once ("classes/model/CalendarDefinition.php");
/** /**
* A Calendar object where it is defined Working Days, Business Hours and Holidays * A Calendar object where it is defined Working Days, Business Hours and Holidays
@@ -44,18 +45,20 @@
*/ */
class calendar extends CalendarDefinition class calendar extends CalendarDefinition
{ {
/** /**
*
* @param string(32) $userUid * @param string(32) $userUid
* @param string(32) $proUid * @param string(32) $proUid
* @param string(32) $tasUid * @param string(32) $tasUid
*/ */
function calendar($userUid = NULL, $proUid = NULL, $tasUid = NULL) function calendar ($userUid = NULL, $proUid = NULL, $tasUid = NULL)
{ {
$this->userUid = $userUid; $this->userUid = $userUid;
$this->proUid = $proUid; $this->proUid = $proUid;
$this->tasUid = $tasUid; $this->tasUid = $tasUid;
$this->calendarLog = ""; $this->calendarLog = "";
$this->setupCalendar($userUid,$proUid,$tasUid); $this->setupCalendar( $userUid, $proUid, $tasUid );
} }
/** /**
@@ -67,14 +70,15 @@ class calendar extends CalendarDefinition
* @access public * @access public
* *
*/ */
function addCalendarLog($msg) function addCalendarLog ($msg)
{ {
$this->calendarLog .= "\n".date("D M j G:i:s T Y").": " . $msg; $this->calendarLog .= "\n" . date( "D M j G:i:s T Y" ) . ": " . $msg;
} }
/** /**
* setupCalendar is used to generate a valid instance of calendar using $userUid, $proUid and $tasUid * setupCalendar is used to generate a valid instance of calendar using $userUid, $proUid and $tasUid
* to find a valid calendar. If there is no valid calendar then use the Default * to find a valid calendar.
* If there is no valid calendar then use the Default
* *
* @name setupCalendar * @name setupCalendar
* @param string(32) $userUid * @param string(32) $userUid
@@ -82,15 +86,17 @@ class calendar extends CalendarDefinition
* @param string(32) $tasUid * @param string(32) $tasUid
* @return void * @return void
*/ */
function setupCalendar($userUid,$proUid,$tasUid) function setupCalendar ($userUid, $proUid, $tasUid)
{ {
$calendarDefinition = $this->getCalendarFor($userUid,$proUid,$tasUid); $calendarDefinition = $this->getCalendarFor( $userUid, $proUid, $tasUid );
$this->calendarUid = $calendarDefinition['CALENDAR_UID']; $this->calendarUid = $calendarDefinition['CALENDAR_UID'];
$this->calendarDefinition = $calendarDefinition; $this->calendarDefinition = $calendarDefinition;
} }
/** /**
* getnextValidBusinessHoursrange is used recursivily to find a valid BusinessHour * getnextValidBusinessHoursrange is used recursivily to find a valid BusinessHour
* for the given $date and $time. This function use all the exeptions defined for * for the given $date and $time.
* This function use all the exeptions defined for
* Working days, Business Hours and Holidays. * Working days, Business Hours and Holidays.
* *
* @author Hugo Loza <hugo@colosa.com> * @author Hugo Loza <hugo@colosa.com>
@@ -101,127 +107,120 @@ class calendar extends CalendarDefinition
* @var array $businessHoursA Object with all the infromation about the valid Business Hours found * @var array $businessHoursA Object with all the infromation about the valid Business Hours found
* $return array('DATE'=>$date,'TIME'=>$time,'BUSINESS_HOURS'=>$businessHoursA) * $return array('DATE'=>$date,'TIME'=>$time,'BUSINESS_HOURS'=>$businessHoursA)
*/ */
function getNextValidBusinessHoursRange($date, $time) function getNextValidBusinessHoursRange ($date, $time)
{ {
$this->addCalendarLog("================= Start : $date,$time ================"); $this->addCalendarLog( "================= Start : $date,$time ================" );
//First Validate if is a valid date //First Validate if is a valid date
$sw_valid_date = false; $sw_valid_date = false;
$sw_date_changed = false; $sw_date_changed = false;
while (!$sw_valid_date) { while (! $sw_valid_date) {
$dateArray = explode("-",$date); $dateArray = explode( "-", $date );
$hour = 0; $hour = 0;
$minute = 0; $minute = 0;
$second = 0; $second = 0;
$month = $dateArray[1]; $month = $dateArray[1];
$day = $dateArray[2]; $day = $dateArray[2];
$year = $dateArray[0]; $year = $dateArray[0];
$weekDay = date("w",mktime($hour,$minute,$second,$month,$day,$year)); $weekDay = date( "w", mktime( $hour, $minute, $second, $month, $day, $year ) );
$weekDayLabel = date("l",mktime($hour,$minute,$second,$month,$day,$year)); $weekDayLabel = date( "l", mktime( $hour, $minute, $second, $month, $day, $year ) );
$dateInt = mktime($hour,$minute,$second,$month,$day,$year); $dateInt = mktime( $hour, $minute, $second, $month, $day, $year );
$this->addCalendarLog( "**** $weekDayLabel ($weekDay) * $date" );
$this->addCalendarLog("**** $weekDayLabel ($weekDay) * $date");
$sw_week_day = false; $sw_week_day = false;
$sw_not_holiday = true; $sw_not_holiday = true;
if (in_array( $weekDay, $this->calendarDefinition['CALENDAR_WORK_DAYS_A'] )) {
if(in_array($weekDay,$this->calendarDefinition['CALENDAR_WORK_DAYS_A'])){
$sw_week_day = true; $sw_week_day = true;
} }
if(!$sw_week_day){ if (! $sw_week_day) {
$this->addCalendarLog("Valid working Dates: ".$this->calendarDefinition['CALENDAR_WORK_DAYS_A']); $this->addCalendarLog( "Valid working Dates: " . $this->calendarDefinition['CALENDAR_WORK_DAYS_A'] );
$this->addCalendarLog("- Non working Day"); $this->addCalendarLog( "- Non working Day" );
} }
foreach ($this->calendarDefinition['HOLIDAY'] as $key => $holiday) { foreach ($this->calendarDefinition['HOLIDAY'] as $key => $holiday) {
//Normalize Holiday date to SAME year of date //Normalize Holiday date to SAME year of date
$holidayStartA = explode(" ",$holiday['CALENDAR_HOLIDAY_START']);
$holidayStartA = explode("-",$holidayStartA[0]);
$normalizedHolidayStart = date("Y-m-d",mktime($hour,$minute,$second,$holidayStartA[1],$holidayStartA[2],$year)); $holidayStartA = explode( " ", $holiday['CALENDAR_HOLIDAY_START'] );
$normalizedHolidayStartInt = mktime($hour,$minute,$second,$holidayStartA[1],$holidayStartA[2],$year); $holidayStartA = explode( "-", $holidayStartA[0] );
$holidayEndA = explode(" ",$holiday['CALENDAR_HOLIDAY_END']); $normalizedHolidayStart = date( "Y-m-d", mktime( $hour, $minute, $second, $holidayStartA[1], $holidayStartA[2], $year ) );
$holidayEndA = explode("-",$holidayEndA[0]); $normalizedHolidayStartInt = mktime( $hour, $minute, $second, $holidayStartA[1], $holidayStartA[2], $year );
$normalizedHolidayEnd = date("Y-m-d",mktime($hour,$minute,$second,$holidayEndA[1],$holidayEndA[2],$year));
$normalizedHolidayEndInt = mktime($hour,$minute,$second,$holidayEndA[1],$holidayEndA[2],$year); $holidayEndA = explode( " ", $holiday['CALENDAR_HOLIDAY_END'] );
$holidayEndA = explode( "-", $holidayEndA[0] );
$normalizedHolidayEnd = date( "Y-m-d", mktime( $hour, $minute, $second, $holidayEndA[1], $holidayEndA[2], $year ) );
$normalizedHolidayEndInt = mktime( $hour, $minute, $second, $holidayEndA[1], $holidayEndA[2], $year );
$sw_not_holiday_aux = true; $sw_not_holiday_aux = true;
if( $dateInt >= $normalizedHolidayStartInt && $dateInt <= $normalizedHolidayEndInt ){ if ($dateInt >= $normalizedHolidayStartInt && $dateInt <= $normalizedHolidayEndInt) {
$sw_not_holiday = false; $sw_not_holiday = false;
$sw_not_holiday_aux = false; $sw_not_holiday_aux = false;
} }
if(!$sw_not_holiday_aux){ if (! $sw_not_holiday_aux) {
$this->addCalendarLog("It is a holiday -> ".$holiday['CALENDAR_HOLIDAY_NAME']." ($normalizedHolidayStart - $normalizedHolidayEnd)"); $this->addCalendarLog( "It is a holiday -> " . $holiday['CALENDAR_HOLIDAY_NAME'] . " ($normalizedHolidayStart - $normalizedHolidayEnd)" );
} }
} }
$sw_valid_date=$sw_week_day && $sw_not_holiday; $sw_valid_date = $sw_week_day && $sw_not_holiday;
if(!$sw_valid_date){//Go to next day
$date=date("Y-m-d",mktime($hour,$minute+1,$second,$month,$day+1,$year));
$sw_date_changed=true;
}else{
$this->addCalendarLog("FOUND VALID DATE -> $date");
if (! $sw_valid_date) { //Go to next day
$date = date( "Y-m-d", mktime( $hour, $minute + 1, $second, $month, $day + 1, $year ) );
$sw_date_changed = true;
} else {
$this->addCalendarLog( "FOUND VALID DATE -> $date" );
//We got a valid day, now get the valid Business Hours //We got a valid day, now get the valid Business Hours
//Here Need to find a rule to get the most nea //Here Need to find a rule to get the most nea
$businessHoursA=array(); $businessHoursA = array ();
$prevHour="00:00"; $prevHour = "00:00";
if($sw_date_changed){// If date has changed then Use the first available period if ($sw_date_changed) { // If date has changed then Use the first available period
$time="00:01"; $time = "00:01";
} }
foreach($this->calendarDefinition['BUSINESS_DAY'] as $keyBH => $businessHours){ foreach ($this->calendarDefinition['BUSINESS_DAY'] as $keyBH => $businessHours) {
// First the period may correspond to ALL or to the current week day // First the period may correspond to ALL or to the current week day
if(($businessHours['CALENDAR_BUSINESS_DAY']==7)||($businessHours['CALENDAR_BUSINESS_DAY']==$weekDay)){ if (($businessHours['CALENDAR_BUSINESS_DAY'] == 7) || ($businessHours['CALENDAR_BUSINESS_DAY'] == $weekDay)) {
$this->addCalendarLog("Validating ($time/$prevHour) From: ".$businessHours['CALENDAR_BUSINESS_START']." to ".$businessHours['CALENDAR_BUSINESS_END']); $this->addCalendarLog( "Validating ($time/$prevHour) From: " . $businessHours['CALENDAR_BUSINESS_START'] . " to " . $businessHours['CALENDAR_BUSINESS_END'] );
//Prev Hour //Prev Hour
$prevHourA = explode(":",$prevHour); $prevHourA = explode( ":", $prevHour );
$prevHourSeconds = ($prevHourA[0]*60*60)+($prevHour[1]*60); $prevHourSeconds = ($prevHourA[0] * 60 * 60) + ($prevHour[1] * 60);
$calendarBusinessStartA = explode(":",$businessHours['CALENDAR_BUSINESS_START']); $calendarBusinessStartA = explode( ":", $businessHours['CALENDAR_BUSINESS_START'] );
$calendarBusinessStartSeconds = ($calendarBusinessStartA[0]*60*60)+($calendarBusinessStartA[1]*60); $calendarBusinessStartSeconds = ($calendarBusinessStartA[0] * 60 * 60) + ($calendarBusinessStartA[1] * 60);
$calendarBusinessEndA = explode(":",$businessHours['CALENDAR_BUSINESS_END']); $calendarBusinessEndA = explode( ":", $businessHours['CALENDAR_BUSINESS_END'] );
$calendarBusinessEndSeconds = ($calendarBusinessEndA[0]*60*60)+($calendarBusinessEndA[1]*60); $calendarBusinessEndSeconds = ($calendarBusinessEndA[0] * 60 * 60) + ($calendarBusinessEndA[1] * 60);
$timeAuxA = explode(":",$time); $timeAuxA = explode( ":", $time );
$timeAuxSeconds = ($timeAuxA[0]*60*60)+($timeAuxA[1]*60); $timeAuxSeconds = ($timeAuxA[0] * 60 * 60) + ($timeAuxA[1] * 60);
if(($timeAuxSeconds>=$prevHourSeconds)&&($timeAuxSeconds<$calendarBusinessEndSeconds)){ if (($timeAuxSeconds >= $prevHourSeconds) && ($timeAuxSeconds < $calendarBusinessEndSeconds)) {
$this->addCalendarLog("*** FOUND VALID BUSINESS HOUR ".$businessHours['CALENDAR_BUSINESS_START']." - ".$businessHours['CALENDAR_BUSINESS_END']); $this->addCalendarLog( "*** FOUND VALID BUSINESS HOUR " . $businessHours['CALENDAR_BUSINESS_START'] . " - " . $businessHours['CALENDAR_BUSINESS_END'] );
if($timeAuxSeconds<$calendarBusinessStartSeconds){//Out of range then assign first hour if ($timeAuxSeconds < $calendarBusinessStartSeconds) { //Out of range then assign first hour
$this->addCalendarLog("Set to default start hour to: ".$businessHours['CALENDAR_BUSINESS_START']); $this->addCalendarLog( "Set to default start hour to: " . $businessHours['CALENDAR_BUSINESS_START'] );
$time = $businessHours['CALENDAR_BUSINESS_START']; $time = $businessHours['CALENDAR_BUSINESS_START'];
} }
$prevHour = $businessHours['CALENDAR_BUSINESS_END']; $prevHour = $businessHours['CALENDAR_BUSINESS_END'];
$businessHoursA = $businessHours; $businessHoursA = $businessHours;
} }
} }
} }
} }
if(empty($businessHoursA)){ if (empty( $businessHoursA )) {
$this->addCalendarLog("> No Valid Business Hour found for current date, go to next"); $this->addCalendarLog( "> No Valid Business Hour found for current date, go to next" );
$date = date("Y-m-d", mktime($hour,$minute+1,$second,$month,$day+1,$year)); $date = date( "Y-m-d", mktime( $hour, $minute + 1, $second, $month, $day + 1, $year ) );
$sw_date_changed = true; $sw_date_changed = true;
$sw_valid_date = false; $sw_valid_date = false;
} }
} }
$return['DATE'] = $date; $return['DATE'] = $date;
$return['TIME'] = $time; $return['TIME'] = $time;
$return['BUSINESS_HOURS'] = $businessHoursA; $return['BUSINESS_HOURS'] = $businessHoursA;