| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Shifts_IDuration |
| 3 |
{ |
| 4 |
public function reset(); |
| 5 |
public function add( SH4_Shifts_Model $shift ); |
| 6 |
public function getQty(); |
| 7 |
public function getDuration(); |
| 8 |
public function getDurationHours(); |
| 9 |
public function formatDuration(); |
| 10 |
} |
| 11 |
|
| 12 |
class SH4_Shifts_Duration implements SH4_Shifts_IDuration |
| 13 |
{ |
| 14 |
protected $qty = 0; |
| 15 |
protected $duration = 0; |
| 16 |
protected $shifts = array(); |
| 17 |
protected $qtyTimeoff = 0; |
| 18 |
protected $durationTimeoff = 0; |
| 19 |
protected $settings; |
| 20 |
protected $t; |
| 21 |
|
| 22 |
public function __construct( |
| 23 |
HC3_Settings $settings, |
| 24 |
HC3_Time $t |
| 25 |
) |
| 26 |
{ |
| 27 |
$this->settings = $settings; |
| 28 |
$this->t = $t; |
| 29 |
} |
| 30 |
|
| 31 |
public function getShifts() |
| 32 |
{ |
| 33 |
return $this->shifts; |
| 34 |
} |
| 35 |
|
| 36 |
public function reset() |
| 37 |
{ |
| 38 |
$this->qty = 0; |
| 39 |
$this->duration = 0; |
| 40 |
$this->qtyTimeoff = 0; |
| 41 |
$this->durationTimeoff = 0; |
| 42 |
$this->shifts = array(); |
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
public function add( SH4_Shifts_Model $model ) |
| 47 |
{ |
| 48 |
$id = $model->getId(); |
| 49 |
if( isset($this->shifts[$id]) ){ |
| 50 |
return $this; |
| 51 |
} |
| 52 |
$this->shifts[$id] = $model; |
| 53 |
|
| 54 |
$start = $model->getStart(); |
| 55 |
$end = $model->getEnd(); |
| 56 |
|
| 57 |
$this->t->setDateTimeDb( $start ); |
| 58 |
$startTs = $this->t->getTimestamp(); |
| 59 |
$startDayStart = $this->t->setStartDay()->getTimestamp(); |
| 60 |
$startDate = $this->t->formatDateDb(); |
| 61 |
$startDayTs = $startTs - $startDayStart; |
| 62 |
|
| 63 |
$this->t->setDateTimeDb( $end ); |
| 64 |
$endTs = $this->t->getTimestamp(); |
| 65 |
$endDayStart = $this->t->setStartDay()->getTimestamp(); |
| 66 |
$endDate = $this->t->formatDateDb(); |
| 67 |
$endDayTs = $endTs - $endDayStart; |
| 68 |
|
| 69 |
$duration = abs( $endTs - $startTs ); |
| 70 |
if( ($startDayTs == 0) && ($endDayTs == 0) ){ |
| 71 |
$fullDayCountAs = $this->settings->get('full_day_count_as'); |
| 72 |
|
| 73 |
// $durationDays = $duration / (24 * 60 * 60); |
| 74 |
|
| 75 |
$durationDays = 0; |
| 76 |
$rexDate = $startDate; |
| 77 |
$this->t->setDateDb( $rexDate ); |
| 78 |
while( $rexDate < $endDate ){ |
| 79 |
$durationDays++; |
| 80 |
$rexDate = $this->t |
| 81 |
->modify( '+1 day' ) |
| 82 |
->formatDateDb() |
| 83 |
; |
| 84 |
} |
| 85 |
|
| 86 |
$duration = $durationDays * $fullDayCountAs; |
| 87 |
// echo "DD = '$durationDays'<br>"; |
| 88 |
} |
| 89 |
|
| 90 |
$noBreak = $this->settings->get( 'shifttypes_nobreak' ); |
| 91 |
if( ! $noBreak ){ |
| 92 |
$breakStart = (string) $model->getBreakStart(); |
| 93 |
$breakEnd = (string) $model->getBreakEnd(); |
| 94 |
|
| 95 |
if( $breakStart OR $breakEnd ){ |
| 96 |
// if( $breakStart < $start ) $breakStart = $start; |
| 97 |
// if( $breakEnd > $end ) $breakEnd = $end; |
| 98 |
|
| 99 |
if( ($breakStart >= $start) && ($breakEnd <= $end) ){ |
| 100 |
$this->t->setDateTimeDb( $breakStart ); |
| 101 |
$breakStartTs = $this->t->getTimestamp(); |
| 102 |
$this->t->setDateTimeDb( $breakEnd ); |
| 103 |
$breakEndTs = $this->t->getTimestamp(); |
| 104 |
|
| 105 |
$breakDuration = abs( $breakEndTs - $breakStartTs ); |
| 106 |
$duration = $duration - $breakDuration; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$this->qty++; |
| 112 |
$this->duration += $duration; |
| 113 |
|
| 114 |
// is timeoff |
| 115 |
$calendar = $model->getCalendar(); |
| 116 |
if( $calendar->isTimeoff() ){ |
| 117 |
$this->qtyTimeoff++; |
| 118 |
$this->durationTimeoff += $duration; |
| 119 |
} |
| 120 |
|
| 121 |
return $this; |
| 122 |
} |
| 123 |
|
| 124 |
public function getDuration() |
| 125 |
{ |
| 126 |
return $this->duration; |
| 127 |
} |
| 128 |
|
| 129 |
public function getDurationTimeoff() |
| 130 |
{ |
| 131 |
return $this->durationTimeoff; |
| 132 |
} |
| 133 |
|
| 134 |
public function getDurationHours() |
| 135 |
{ |
| 136 |
$return = $this->getDuration(); |
| 137 |
$return = $return / (60 * 60); |
| 138 |
$return = sprintf( '%0.2f', $return ); |
| 139 |
return $return; |
| 140 |
} |
| 141 |
|
| 142 |
public function formatDuration( $duration = NULL ) |
| 143 |
{ |
| 144 |
if( NULL === $duration ){ |
| 145 |
$duration = $this->getDuration(); |
| 146 |
} |
| 147 |
|
| 148 |
$hours = floor( $duration / (60 * 60) ); |
| 149 |
$remain = $duration - $hours * (60 * 60); |
| 150 |
$minutes = floor( $remain / 60 ); |
| 151 |
|
| 152 |
$hoursView = $hours; |
| 153 |
$minutesView = sprintf( '%02d', $minutes ); |
| 154 |
|
| 155 |
$return = $hoursView . ':' . $minutesView; |
| 156 |
// $return = gmdate( "H:i", $this->getDuration() ); |
| 157 |
return $return; |
| 158 |
} |
| 159 |
|
| 160 |
public function getQty() |
| 161 |
{ |
| 162 |
return $this->qty; |
| 163 |
} |
| 164 |
|
| 165 |
public function getQtyTimeoff() |
| 166 |
{ |
| 167 |
return $this->qtyTimeoff; |
| 168 |
} |
| 169 |
} |