| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Schedule_Html_View_Report |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Ui $ui, |
| 8 |
HC3_Request $request, |
| 9 |
HC3_Time $t, |
| 10 |
|
| 11 |
SH4_Shifts_Duration $shiftsDuration, |
| 12 |
SH4_Shifts_DurationService $shiftsDurationService, |
| 13 |
SH4_Calendars_Presenter $calendarsPresenter, |
| 14 |
SH4_Employees_Presenter $employeesPresenter, |
| 15 |
|
| 16 |
SH4_Schedule_Html_View_Common $common |
| 17 |
) |
| 18 |
{ |
| 19 |
$this->self = $hooks->wrap($this); |
| 20 |
|
| 21 |
$this->ui = $ui; |
| 22 |
$this->t = $t; |
| 23 |
$this->request = $request; |
| 24 |
|
| 25 |
$this->calendarsPresenter = $hooks->wrap( $calendarsPresenter ); |
| 26 |
$this->employeesPresenter = $hooks->wrap( $employeesPresenter ); |
| 27 |
|
| 28 |
$this->common = $hooks->wrap( $common ); |
| 29 |
$this->shiftsDuration = $hooks->wrap( $shiftsDuration ); |
| 30 |
$this->shiftsDurationService = $hooks->wrap( $shiftsDurationService ); |
| 31 |
} |
| 32 |
|
| 33 |
public function render() |
| 34 |
{ |
| 35 |
$params = $this->request->getParams(); |
| 36 |
$shifts = $this->common->getShifts(); |
| 37 |
|
| 38 |
$header = array( |
| 39 |
'qty' => '__Number Of Shifts__', |
| 40 |
'duration' => '__Hours__', |
| 41 |
); |
| 42 |
|
| 43 |
$hideui = isset( $params['hideui'] ) ? $params['hideui'] : array(); |
| 44 |
if( array_intersect(array('report-qty'), $hideui) ){ |
| 45 |
unset( $header['qty'] ); |
| 46 |
} |
| 47 |
if( array_intersect(array('report-duration'), $hideui) ){ |
| 48 |
unset( $header['duration'] ); |
| 49 |
} |
| 50 |
|
| 51 |
$rows = array(); |
| 52 |
|
| 53 |
$this->shiftsDuration->reset(); |
| 54 |
foreach( $shifts as $shift ){ |
| 55 |
$this->shiftsDuration->add( $shift ); |
| 56 |
} |
| 57 |
|
| 58 |
$row = array(); |
| 59 |
$row['qty'] = $this->shiftsDuration->getQty(); |
| 60 |
$row['duration'] = $this->shiftsDuration->formatDuration(); |
| 61 |
|
| 62 |
$rows[] = $row; |
| 63 |
|
| 64 |
$out = $this->ui->makeTable( $header, $rows ); |
| 65 |
|
| 66 |
return $out; |
| 67 |
} |
| 68 |
|
| 69 |
public function renderByCalendar() |
| 70 |
{ |
| 71 |
$params = $this->request->getParams(); |
| 72 |
$calendars = $this->common->getCalendars(); |
| 73 |
$employees = $this->common->getEmployees(); |
| 74 |
$shifts = $this->common->getShifts(); |
| 75 |
|
| 76 |
// finalize calendars to include those that employee was removed from but still have shifts in |
| 77 |
$listCalendarId = array_keys( $calendars ); |
| 78 |
$dictCalendarId = array_combine( $listCalendarId, $listCalendarId ); |
| 79 |
foreach( $shifts as $shift ){ |
| 80 |
$calendar = $shift->getCalendar(); |
| 81 |
if( ! $calendar ) continue; |
| 82 |
$calendarId = $calendar->getId(); |
| 83 |
$dictCalendarId[ $calendarId ] = $calendarId; |
| 84 |
} |
| 85 |
if( count($dictCalendarId) > count($calendars) ){ |
| 86 |
$allCalendars = $this->common->findAllCalendars(); |
| 87 |
$calendars = array_intersect_key( $allCalendars, $dictCalendarId ); |
| 88 |
} |
| 89 |
|
| 90 |
// we can probably have archived calendars so if such calendars have no shifts then skip them |
| 91 |
$removeArchivedCalendars = array(); |
| 92 |
foreach( $calendars as $calendar ){ |
| 93 |
if( $calendar->isArchived() ){ |
| 94 |
$removeArchivedCalendars[ $calendar->getId() ] = $calendar->getId(); |
| 95 |
} |
| 96 |
} |
| 97 |
if( $removeArchivedCalendars ){ |
| 98 |
foreach( $shifts as $shift ){ |
| 99 |
$calendar = $shift->getCalendar(); |
| 100 |
$id = $calendar ? $calendar->getId() : 0; |
| 101 |
if( isset($removeArchivedCalendars[$id]) ){ |
| 102 |
unset( $removeArchivedCalendars[$id] ); |
| 103 |
} |
| 104 |
if( ! $removeArchivedCalendars ){ |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
foreach( $removeArchivedCalendars as $id ){ |
| 109 |
unset( $calendars[$id] ); |
| 110 |
} |
| 111 |
reset( $shifts ); |
| 112 |
} |
| 113 |
|
| 114 |
$header = array( |
| 115 |
'label' => NULL, |
| 116 |
'qty' => '__Number Of Shifts__', |
| 117 |
'duration' => '__Hours__', |
| 118 |
); |
| 119 |
|
| 120 |
$hideui = isset( $params['hideui'] ) ? $params['hideui'] : array(); |
| 121 |
if( array_intersect(array('report-qty'), $hideui) ){ |
| 122 |
unset( $header['qty'] ); |
| 123 |
} |
| 124 |
if( array_intersect(array('report-duration'), $hideui) ){ |
| 125 |
unset( $header['duration'] ); |
| 126 |
} |
| 127 |
|
| 128 |
$viewBy = array(); |
| 129 |
$rows = array(); |
| 130 |
|
| 131 |
foreach( $calendars as $calendar ){ |
| 132 |
$calendarId = $calendar->getId(); |
| 133 |
$rows[ $calendarId ] = array('label' => '', 'qty' => 0, 'duration' => 0); |
| 134 |
|
| 135 |
$label = $this->calendarsPresenter->presentTitle( $calendar ); |
| 136 |
// $label = $calendar->getTitle(); |
| 137 |
$rows[ $calendarId ]['label'] = $label; |
| 138 |
|
| 139 |
$viewBy[ $calendarId ] = array(); |
| 140 |
} |
| 141 |
|
| 142 |
foreach( $shifts as $shift ){ |
| 143 |
$calendar = $shift->getCalendar(); |
| 144 |
$calendarId = $calendar->getId(); |
| 145 |
|
| 146 |
if( ! array_key_exists($calendarId, $viewBy) ){ |
| 147 |
continue; |
| 148 |
} |
| 149 |
$viewBy[ $calendarId ][] = $shift; |
| 150 |
} |
| 151 |
|
| 152 |
reset( $viewBy ); |
| 153 |
foreach( $viewBy as $calendarId => $thisShifts ){ |
| 154 |
$this->shiftsDuration->reset(); |
| 155 |
foreach( $thisShifts as $shift ){ |
| 156 |
$this->shiftsDuration->add( $shift ); |
| 157 |
} |
| 158 |
|
| 159 |
$rows[ $calendarId ]['qty'] = $this->shiftsDuration->getQty(); |
| 160 |
$rows[ $calendarId ]['duration'] = $this->shiftsDuration->formatDuration(); |
| 161 |
} |
| 162 |
|
| 163 |
$out = $this->ui->makeTable( $header, $rows ); |
| 164 |
return $out; |
| 165 |
} |
| 166 |
|
| 167 |
public function renderByEmployee() |
| 168 |
{ |
| 169 |
$params = $this->request->getParams(); |
| 170 |
$calendars = $this->common->getCalendars(); |
| 171 |
$employees = $this->common->getEmployees(); |
| 172 |
$shifts = $this->common->getShifts(); |
| 173 |
|
| 174 |
// finalize employees to include those that employee was removed from but still have shifts in |
| 175 |
$listEmployeeId = array_keys( $employees ); |
| 176 |
$dictEmployeeId = array_combine( $listEmployeeId, $listEmployeeId ); |
| 177 |
foreach( $shifts as $shift ){ |
| 178 |
$employee = $shift->getEmployee(); |
| 179 |
if( ! $employee ) continue; |
| 180 |
$employeeId = $employee->getId(); |
| 181 |
$dictEmployeeId[ $employeeId ] = $employeeId; |
| 182 |
} |
| 183 |
if( count($dictEmployeeId) > count($employees) ){ |
| 184 |
$allEmployees = $this->common->findAllEmployees(); |
| 185 |
$employees = array_intersect_key( $allEmployees, $dictEmployeeId ); |
| 186 |
} |
| 187 |
|
| 188 |
// we can probably have archived employees so if such employees have no shifts then skip them |
| 189 |
$removeArchivedEmployees = array(); |
| 190 |
foreach( $employees as $employee ){ |
| 191 |
if( $employee->isArchived() ){ |
| 192 |
$removeArchivedEmployees[ $employee->getId() ] = $employee->getId(); |
| 193 |
} |
| 194 |
} |
| 195 |
if( $removeArchivedEmployees ){ |
| 196 |
foreach( $shifts as $shift ){ |
| 197 |
$employee = $shift->getEmployee(); |
| 198 |
$id = $employee ? $employee->getId() : 0; |
| 199 |
if( isset($removeArchivedEmployees[$id]) ){ |
| 200 |
unset( $removeArchivedEmployees[$id] ); |
| 201 |
} |
| 202 |
if( ! $removeArchivedEmployees ){ |
| 203 |
break; |
| 204 |
} |
| 205 |
} |
| 206 |
foreach( $removeArchivedEmployees as $id ){ |
| 207 |
unset( $employees[$id] ); |
| 208 |
} |
| 209 |
reset( $shifts ); |
| 210 |
} |
| 211 |
|
| 212 |
// we can probably have archived calendars so if such calendars have no shifts then skip them |
| 213 |
$removeArchivedCalendars = array(); |
| 214 |
foreach( $calendars as $calendar ){ |
| 215 |
if( $calendar->isArchived() ){ |
| 216 |
$removeArchivedCalendars[ $calendar->getId() ] = $calendar->getId(); |
| 217 |
} |
| 218 |
} |
| 219 |
if( $removeArchivedCalendars ){ |
| 220 |
foreach( $shifts as $shift ){ |
| 221 |
$calendar = $shift->getCalendar(); |
| 222 |
$id = $calendar ? $calendar->getId() : 0; |
| 223 |
if( isset($removeArchivedCalendars[$id]) ){ |
| 224 |
unset( $removeArchivedCalendars[$id] ); |
| 225 |
} |
| 226 |
if( ! $removeArchivedCalendars ){ |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
foreach( $removeArchivedCalendars as $id ){ |
| 231 |
unset( $calendars[$id] ); |
| 232 |
} |
| 233 |
reset( $shifts ); |
| 234 |
} |
| 235 |
|
| 236 |
$header = array( |
| 237 |
'label' => NULL, |
| 238 |
); |
| 239 |
|
| 240 |
foreach( $calendars as $calendar ){ |
| 241 |
$calendarId = $calendar->getId(); |
| 242 |
$calendarView = $this->calendarsPresenter->presentTitle( $calendar ); |
| 243 |
$header['calendar_' . $calendarId] = $calendarView; |
| 244 |
} |
| 245 |
$header['total'] = '__Total__'; |
| 246 |
|
| 247 |
$viewBy = array(); |
| 248 |
$rows = array(); |
| 249 |
|
| 250 |
$hideui = isset( $params['hideui'] ) ? $params['hideui'] : array(); |
| 251 |
if( array_intersect(array('report-qty'), $hideui) ){ |
| 252 |
unset( $header['qty'] ); |
| 253 |
} |
| 254 |
if( array_intersect(array('report-duration'), $hideui) ){ |
| 255 |
unset( $header['duration'] ); |
| 256 |
} |
| 257 |
|
| 258 |
foreach( $employees as $employee ){ |
| 259 |
$employeeId = $employee ? $employee->getId() : 0; |
| 260 |
$rows[ $employeeId ] = array('label' => '', 'qty' => 0, 'duration' => 0); |
| 261 |
|
| 262 |
// $label = $employee->getTitle(); |
| 263 |
$label = $this->employeesPresenter->presentTitle( $employee ); |
| 264 |
$label = $this->ui->makeSpan( $label ) |
| 265 |
->tag('font-size', 4) |
| 266 |
; |
| 267 |
$rows[ $employeeId ]['label'] = $label; |
| 268 |
|
| 269 |
$viewBy[ $employeeId ] = array(); |
| 270 |
} |
| 271 |
|
| 272 |
foreach( $shifts as $shift ){ |
| 273 |
$employee = $shift->getEmployee(); |
| 274 |
$employeeId = $employee ? $employee->getId() : 0; |
| 275 |
|
| 276 |
if( ! array_key_exists($employeeId, $viewBy) ){ |
| 277 |
continue; |
| 278 |
} |
| 279 |
$viewBy[ $employeeId ][] = $shift; |
| 280 |
} |
| 281 |
|
| 282 |
reset( $viewBy ); |
| 283 |
foreach( $viewBy as $employeeId => $thisShifts ){ |
| 284 |
$durations = array(); |
| 285 |
|
| 286 |
$durations[0] = $this->shiftsDurationService->newCounter(); |
| 287 |
reset( $calendars ); |
| 288 |
foreach( $calendars as $calendar ){ |
| 289 |
$calendarId = $calendar->getId(); |
| 290 |
$durations[ $calendarId ] = $this->shiftsDurationService->newCounter(); |
| 291 |
} |
| 292 |
|
| 293 |
reset( $thisShifts ); |
| 294 |
foreach( $thisShifts as $shift ){ |
| 295 |
$calendar = $shift->getCalendar(); |
| 296 |
$calendarId = $calendar->getId(); |
| 297 |
if( ! array_key_exists($calendarId, $durations) ){ |
| 298 |
continue; |
| 299 |
} |
| 300 |
$durations[$calendarId]->add( $shift ); |
| 301 |
$durations[0]->add( $shift ); |
| 302 |
} |
| 303 |
|
| 304 |
reset( $calendars ); |
| 305 |
foreach( $calendars as $calendar ){ |
| 306 |
$calendarId = $calendar->getId(); |
| 307 |
if( array_intersect(array('report-duration'), $hideui) ){ |
| 308 |
$rows[ $employeeId ]['calendar_' . $calendarId] = $durations[$calendarId]->getQty(); |
| 309 |
} |
| 310 |
else { |
| 311 |
$rows[ $employeeId ]['calendar_' . $calendarId] = $durations[$calendarId]->formatDuration(); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if( array_intersect(array('report-duration'), $hideui) ){ |
| 316 |
$rows[ $employeeId ]['total'] = $durations[0]->getQty(); |
| 317 |
} |
| 318 |
else { |
| 319 |
$rows[ $employeeId ]['total'] = $durations[0]->formatDuration(); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
$out = $this->ui->makeTable( $header, $rows ); |
| 324 |
|
| 325 |
return $out; |
| 326 |
} |
| 327 |
} |