CODE STYLE class.dates.php
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
/**
|
||||
* class.dates.php
|
||||
*
|
||||
* @package workflow.engine.classes
|
||||
*
|
||||
* ProcessMaker Open Source Edition
|
||||
@@ -22,8 +23,8 @@
|
||||
* For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
|
||||
* Coral Gables, FL, 33134, USA, or email info@colosa.com.
|
||||
* /
|
||||
|
||||
/*
|
||||
*
|
||||
* /*
|
||||
* Created on 21/01/2008
|
||||
* This class is used for handling dates
|
||||
*
|
||||
@@ -34,9 +35,11 @@
|
||||
require_once ("classes/model/HolidayPeer.php");
|
||||
|
||||
/**
|
||||
*
|
||||
* @package workflow.engine.classes
|
||||
*/
|
||||
class dates {
|
||||
class dates
|
||||
{
|
||||
private $holidays = array ();
|
||||
private $weekends = array ();
|
||||
private $range = array ();
|
||||
@@ -48,7 +51,8 @@ class dates {
|
||||
/**
|
||||
* Function that calculate a final date based on $sInitDate and $iDuration
|
||||
* This function also uses a Calendar component (class.calendar.php) where all the definition of
|
||||
* a User, task, Process or default calendar is defined. base on that information is possible to setup different calendars
|
||||
* a User, task, Process or default calendar is defined.
|
||||
* base on that information is possible to setup different calendars
|
||||
* and apply them to a task, process or user. Each calendar have Working Days, Business Hours and Holidays
|
||||
*
|
||||
* @name calculateDate
|
||||
@@ -74,8 +78,12 @@ class dates {
|
||||
$calendarObj = new calendar( $UsrUid, $ProUid, $TasUid );
|
||||
//Get next Business Hours/Range based on :
|
||||
switch (strtoupper( $sTimeUnit )) {
|
||||
case 'DAYS': $hoursToProcess=$iDuration*8; break;//In Hours
|
||||
default: $hoursToProcess=$iDuration; break;//In Hours
|
||||
case 'DAYS':
|
||||
$hoursToProcess = $iDuration * 8;
|
||||
break; //In Hours
|
||||
default:
|
||||
$hoursToProcess = $iDuration;
|
||||
break; //In Hours
|
||||
}
|
||||
$dateArray = explode( " ", $sInitDate );
|
||||
$currentDate = $dateArray[0];
|
||||
@@ -83,7 +91,6 @@ class dates {
|
||||
|
||||
$startTime = (float) array_sum( explode( ' ', microtime() ) );
|
||||
|
||||
|
||||
$calendarObj->addCalendarLog( "* Starting at: $startTime" );
|
||||
$calendarObj->addCalendarLog( ">>>>> Hours to Process: $hoursToProcess" );
|
||||
$calendarObj->addCalendarLog( ">>>>> Current Date: $currentDate" );
|
||||
@@ -128,8 +135,7 @@ class dates {
|
||||
$diff = abs( $normalizedDateSeconds - $calendarBusinessEndSeconds );
|
||||
$diffHours = $diff / 3600;
|
||||
$hoursToProcess = $hoursToProcess - $diffHours;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
$currentDateTimeA = explode( " ", $possibleTime );
|
||||
$currentDate = $currentDateTimeA[0];
|
||||
$currentTime = $currentDateTimeA[1];
|
||||
@@ -143,6 +149,7 @@ class dates {
|
||||
//$result['OLD_DUE_DATE'] = date("Y-m-d H:i:s",$oldDate);
|
||||
//$result['OLD_DUE_DATE_SECONDS']= $oldDate;
|
||||
|
||||
|
||||
$endTime = (float) array_sum( explode( ' ', microtime() ) );
|
||||
$calendarObj->addCalendarLog( "* Ending at: $endTime" );
|
||||
$calcTime = round( $endTime - $startTime, 3 );
|
||||
@@ -157,6 +164,7 @@ class dates {
|
||||
* Returns: Integer timestamp of the result.
|
||||
* Warning: It will hangs if there is no possible days to count as
|
||||
* "laborable".
|
||||
*
|
||||
* @param date $sInitDate
|
||||
* @param double $iDuration
|
||||
* @param string $sTimeUnit
|
||||
@@ -168,10 +176,12 @@ class dates {
|
||||
* @deprecated renamed by Hugo Loza (see calculateDate new function)
|
||||
*/
|
||||
|
||||
function calculateDate_noCalendar( $sInitDate, $iDuration, $sTimeUnit, $iTypeDay, $UsrUid = NULL, $ProUid = NULL, $TasUid =NULL ) {
|
||||
function calculateDate_noCalendar ($sInitDate, $iDuration, $sTimeUnit, $iTypeDay, $UsrUid = NULL, $ProUid = NULL, $TasUid = NULL)
|
||||
{
|
||||
//load in class variables the config of working days, holidays etc..
|
||||
$this->prepareInformation( $UsrUid, $ProUid, $TasUid );
|
||||
$iHours = 0; $iDays = 0;
|
||||
$iHours = 0;
|
||||
$iDays = 0;
|
||||
//convert the $iDuration and $sTimeUnit in hours and days, take in mind 8 hours = 1 day. and then we will have similar for 5 days = 1 weekends
|
||||
if (strtolower( $sTimeUnit ) == 'hours') {
|
||||
$iAux = intval( abs( $iDuration ) );
|
||||
@@ -190,8 +200,7 @@ class dates {
|
||||
$iEndDate = $this->addDays( $iInitDate, $iDays, $addSign );
|
||||
// if there are hours calculate the hours, and probably add a day if the quantity of hours for last day > 8 hours
|
||||
$iEndDate = $this->addHours( $iEndDate, $iHours, $addSign );
|
||||
}
|
||||
else { // $task->getTasTypeDay() == 2 // calendar days
|
||||
} else { // $task->getTasTypeDay() == 2 // calendar days
|
||||
$iEndDate = strtotime( $addSign . $iDays . ' days ', $iInitDate );
|
||||
$iEndDate = strtotime( $addSign . $iHours . ' hours ', $iEndDate );
|
||||
}
|
||||
@@ -200,6 +209,7 @@ class dates {
|
||||
|
||||
/**
|
||||
* Calculate duration of the $sInitDate - $sEndDate.
|
||||
*
|
||||
* @param date $sInitDate
|
||||
* @param date $sEndDate
|
||||
* @param string $UsrUid
|
||||
@@ -208,7 +218,8 @@ class dates {
|
||||
* @return int
|
||||
*
|
||||
*/
|
||||
function calculateDuration( $sInitDate, $sEndDate = '', $UsrUid = NULL, $ProUid = NULL, $TasUid = NULL) {
|
||||
function calculateDuration ($sInitDate, $sEndDate = '', $UsrUid = NULL, $ProUid = NULL, $TasUid = NULL)
|
||||
{
|
||||
$this->prepareInformation( $UsrUid, $ProUid, $TasUid );
|
||||
if ((string) $sEndDate == '') {
|
||||
$sEndDate = date( 'Y-m-d H:i:s' );
|
||||
@@ -228,10 +239,12 @@ class dates {
|
||||
$fHours1 = 0.0;
|
||||
$fHours2 = 0.0;
|
||||
if (count( $aInitDate ) != 3) {
|
||||
$aInitDate = array(0, 0, 0);
|
||||
$aInitDate = array (0,0,0
|
||||
);
|
||||
}
|
||||
if (count( $aEndDate ) != 3) {
|
||||
$aEndDate = array(0, 0, 0);
|
||||
$aEndDate = array (0,0,0
|
||||
);
|
||||
}
|
||||
if ($aInitDate !== $aEndDate) {
|
||||
while (! $bFinished && ($i < 10000)) {
|
||||
@@ -243,8 +256,7 @@ class dates {
|
||||
}
|
||||
}
|
||||
$i ++;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$bFinished = true;
|
||||
}
|
||||
}
|
||||
@@ -257,8 +269,7 @@ class dates {
|
||||
$fHours2 = $aAux2[1][0] + ($aAux2[1][1] / 60) + ($aAux2[1][2] / 3600);
|
||||
}
|
||||
$fDuration = ($iWorkedDays * 24) + $fHours1 + $fHours2;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$fDuration = (strtotime( $sEndDate ) - strtotime( $sInitDate )) / 3600;
|
||||
}
|
||||
return $fDuration;
|
||||
@@ -266,6 +277,7 @@ class dates {
|
||||
|
||||
/**
|
||||
* Configuration functions
|
||||
*
|
||||
* @param string $UsrUid
|
||||
* @param string $ProUid
|
||||
* @param string $TasUid
|
||||
@@ -274,8 +286,7 @@ class dates {
|
||||
function prepareInformation ($UsrUid = NULL, $ProUid = NULL, $TasUid = NULL)
|
||||
{
|
||||
// setup calendarDays according the task
|
||||
if (isset($TasUid))
|
||||
{
|
||||
if (isset( $TasUid )) {
|
||||
$task = TaskPeer::retrieveByPK( $TasUid );
|
||||
if (! is_null( $task )) {
|
||||
$this->calendarDays = ($task->getTasTypeDay() == 2);
|
||||
@@ -289,13 +300,15 @@ class dates {
|
||||
$holidays[] = strtotime( $holiday->getHldDate() );
|
||||
|
||||
// by default the weekdays are from monday to friday
|
||||
$this->weekends = array(0,6);
|
||||
$this->weekends = array (0,6
|
||||
);
|
||||
$this->holidays = $holidays;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set to repeat for every year all dates defined in $this->holiday
|
||||
*
|
||||
* @param $bSkipEveryYear
|
||||
* @return void
|
||||
*/
|
||||
@@ -306,27 +319,33 @@ class dates {
|
||||
|
||||
/**
|
||||
* Add a single date to holidays
|
||||
*
|
||||
* @param data $sDate
|
||||
* @return void
|
||||
*/
|
||||
function addHoliday ($sDate)
|
||||
{
|
||||
if ($date=strtotime( $sDate )) $this->holidays[]=self::truncateTime($date);
|
||||
else throw new Exception("Invalid date: $sDate.");
|
||||
if ($date = strtotime( $sDate ))
|
||||
$this->holidays[] = self::truncateTime( $date );
|
||||
else
|
||||
throw new Exception( "Invalid date: $sDate." );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all the holidays
|
||||
*
|
||||
* @param date/array $aDate must be an array of (strtotime type) dates
|
||||
* @return void
|
||||
*/
|
||||
function setHolidays ($aDates)
|
||||
{
|
||||
foreach($aDates as $sDate) $this->holidays = $aDates;
|
||||
foreach ($aDates as $sDate)
|
||||
$this->holidays = $aDates;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all the weekends
|
||||
*
|
||||
* @param array/integers $aWeekends must be an array of integers [1,7]
|
||||
* 1=Sunday
|
||||
* 7=Saturday
|
||||
@@ -339,6 +358,7 @@ class dates {
|
||||
|
||||
/**
|
||||
* Add one day of week to the weekends list
|
||||
*
|
||||
* @param $iDayNumber must be an array of integers [1,7]
|
||||
* 1=Sunday
|
||||
* 7=Saturday
|
||||
@@ -346,29 +366,42 @@ class dates {
|
||||
*/
|
||||
function skipDayOfWeek ($iDayNumber)
|
||||
{
|
||||
if ($iDayNumber<1 || $iDayNumber>7) throw new Exception("The day of week must be a number from 1 to 7.");
|
||||
if ($iDayNumber < 1 || $iDayNumber > 7)
|
||||
throw new Exception( "The day of week must be a number from 1 to 7." );
|
||||
$this->weekends[] = $iDayNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a range of non working dates
|
||||
*
|
||||
* @param date $sDateA must be a (strtotime type) dates
|
||||
* @param date $sDateB must be a (strtotime type) dates
|
||||
* @return void
|
||||
*/
|
||||
function addNonWorkingRange ($sDateA, $sDateB)
|
||||
{
|
||||
if ($date=strtotime( $sDateA )) $iDateA=self::truncateTime($date);
|
||||
else throw new Exception("Invalid date: $sDateA.");
|
||||
if ($date=strtotime( $sDateB )) $iDateB=self::truncateTime($date);
|
||||
else throw new Exception("Invalid date: $sDateB.");
|
||||
if ($iDateA>$iDateB) { $s=$iDateA;$iDateA=$iDateB;$iDateB=$s; };
|
||||
$this->range[]=array( $iDateA , $iDateB );
|
||||
if ($date = strtotime( $sDateA ))
|
||||
$iDateA = self::truncateTime( $date );
|
||||
else
|
||||
throw new Exception( "Invalid date: $sDateA." );
|
||||
if ($date = strtotime( $sDateB ))
|
||||
$iDateB = self::truncateTime( $date );
|
||||
else
|
||||
throw new Exception( "Invalid date: $sDateB." );
|
||||
if ($iDateA > $iDateB) {
|
||||
$s = $iDateA;
|
||||
$iDateA = $iDateB;
|
||||
$iDateB = $s;
|
||||
}
|
||||
;
|
||||
$this->range[] = array ($iDateA,$iDateB
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* PRIVATE UTILITARY FUNCTIONS
|
||||
* Add days to the date
|
||||
*
|
||||
* @param date $iInitDate
|
||||
* @param int $iDaysCount
|
||||
* @param string $addSign
|
||||
@@ -381,13 +414,15 @@ class dates {
|
||||
for ($r = 1; $r <= $iDaysCount; $r ++) {
|
||||
$iEndDate = strtotime( $addSign . "1 day", $iEndDate );
|
||||
$dayOfWeek = idate( 'w', $iEndDate ); //now sunday=0
|
||||
if ( array_search( $dayOfWeek, $this->weekends )!== false ) $r--; //continue loop, but we are adding one more day.
|
||||
if (array_search( $dayOfWeek, $this->weekends ) !== false)
|
||||
$r --; //continue loop, but we are adding one more day.
|
||||
}
|
||||
return $iEndDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hours to the date
|
||||
*
|
||||
* @param date $iInitDate
|
||||
* @param int $iHoursCount
|
||||
* @param string $addSign
|
||||
@@ -402,6 +437,7 @@ class dates {
|
||||
|
||||
/**
|
||||
* Compare if the date is in range
|
||||
*
|
||||
* @param $iDate = valid timestamp
|
||||
* @return true if it is within any of the ranges defined.
|
||||
*/
|
||||
@@ -409,21 +445,21 @@ class dates {
|
||||
{
|
||||
$aRange = $this->range;
|
||||
$iYear = idate( 'Y', $iDate );
|
||||
foreach($aRange as $key => $rang)
|
||||
{
|
||||
if ($this->skipEveryYear)
|
||||
{
|
||||
foreach ($aRange as $key => $rang) {
|
||||
if ($this->skipEveryYear) {
|
||||
$deltaYears = idate( 'Y', $rang[1] ) - idate( 'Y', $rang[0] );
|
||||
$rang[0] = self::changeYear( $rang[0], $iYear );
|
||||
$rang[1] = self::changeYear( $rang[1], $iYear + $deltaYears );
|
||||
}
|
||||
if (($iDate>=$rang[0]) && ($iDate<=$rang[1])) return true;
|
||||
if (($iDate >= $rang[0]) && ($iDate <= $rang[1]))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate a date
|
||||
*
|
||||
* @param $iDate = valid timestamp
|
||||
* @return date
|
||||
*/
|
||||
@@ -434,16 +470,19 @@ class dates {
|
||||
|
||||
/**
|
||||
* Get time
|
||||
*
|
||||
* @param timestamp $iDate
|
||||
* @return date
|
||||
*/
|
||||
private function getTime ($iDate)
|
||||
{
|
||||
return array(idate('H',$iDate),idate('m',$iDate),idate('s',$iDate));
|
||||
return array (idate( 'H', $iDate ),idate( 'm', $iDate ),idate( 's', $iDate )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set time
|
||||
*
|
||||
* @param timestamp $iDate
|
||||
* @param timestamp $aTime
|
||||
* @return date
|
||||
@@ -458,14 +497,14 @@ class dates {
|
||||
* year changed to $iYear.
|
||||
* Warning: Don't know what to do if change a 29-02-2004 to 29-02-2005
|
||||
* the last one doesn't exist.
|
||||
*
|
||||
* @param List $iYear
|
||||
* @return array
|
||||
*/
|
||||
private function listForYear ($iYear)
|
||||
{
|
||||
$aList = $this->holidays;
|
||||
foreach($aList as $k => $v)
|
||||
{
|
||||
foreach ($aList as $k => $v) {
|
||||
$aList[$k] = self::changeYear( $v, $iYear );
|
||||
}
|
||||
return $aList;
|
||||
@@ -476,14 +515,14 @@ class dates {
|
||||
* year changed to $iYear.
|
||||
* Warning: Don't know what to do if change a 29-02-2004 to 29-02-2005
|
||||
* the last one doesn't exist.
|
||||
*
|
||||
* @param array $iYear
|
||||
* @param date $iDate
|
||||
* @return array
|
||||
*/
|
||||
private function changeYear ($iDate, $iYear)
|
||||
{
|
||||
if ($delta = ( $iYear - idate('Y',$iDate) ) )
|
||||
{
|
||||
if ($delta = ($iYear - idate( 'Y', $iDate ))) {
|
||||
$iDate = strtotime( "$delta year", $iDate );
|
||||
}
|
||||
return $iDate;
|
||||
|
||||
Reference in New Issue
Block a user