| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Reminders_Query |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Settings $settings, |
| 7 |
SH4_Reminders_QueryLog $remindersQueryLog, |
| 8 |
|
| 9 |
SH4_Employees_Query $employeesQuery, |
| 10 |
SH4_App_Query $appQuery, |
| 11 |
SH4_Shifts_Query $shiftsQuery, |
| 12 |
|
| 13 |
HC3_Time $t, |
| 14 |
HC3_Hooks $hooks |
| 15 |
) |
| 16 |
{ |
| 17 |
$this->t = $t; |
| 18 |
$this->settings = $hooks->wrap( $settings ); |
| 19 |
|
| 20 |
$this->remindersQueryLog = $hooks->wrap( $remindersQueryLog ); |
| 21 |
$this->employeesQuery = $hooks->wrap( $employeesQuery ); |
| 22 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 23 |
$this->shiftsQuery = $hooks->wrap( $shiftsQuery ); |
| 24 |
} |
| 25 |
|
| 26 |
public function find( $range, $date ) |
| 27 |
{ |
| 28 |
$reminderInclude = $this->settings->get( 'reminders_include' ); |
| 29 |
|
| 30 |
// echo "RANGE = '$range'<br>"; |
| 31 |
switch( $range ){ |
| 32 |
case SH4_Reminders_Model::RANGE_DAILY: |
| 33 |
case SH4_Reminders_Model::RANGE_DAILY_SMS: |
| 34 |
$start = $this->t->setDateDb( $date )->formatDateTimeDb(); |
| 35 |
$end = $this->t->setDateDb( $date )->modify('+1 day')->formatDateTimeDb(); |
| 36 |
break; |
| 37 |
|
| 38 |
case SH4_Reminders_Model::RANGE_WEEKLY: |
| 39 |
$start = $this->t->setDateDb( $date )->setStartWeek()->formatDateTimeDb(); |
| 40 |
$end = $this->t->setDateDb( $date )->setStartWeek()->modify('+1 week')->formatDateTimeDb(); |
| 41 |
break; |
| 42 |
|
| 43 |
case SH4_Reminders_Model::RANGE_MONTHLY: |
| 44 |
$start = $this->t->setDateDb( $date )->setStartMonth()->formatDateTimeDb(); |
| 45 |
$end = $this->t->setDateDb( $date )->setStartMonth()->modify('+1 month')->formatDateTimeDb(); |
| 46 |
break; |
| 47 |
} |
| 48 |
|
| 49 |
// move the date to the start of the range |
| 50 |
$date = $this->t->setDateTimeDb( $start )->formatDateDb(); |
| 51 |
|
| 52 |
$this->shiftsQuery |
| 53 |
->setStart( $start ) |
| 54 |
->setEnd( $end ) |
| 55 |
; |
| 56 |
$allShifts = $this->shiftsQuery->find(); |
| 57 |
|
| 58 |
$shifts = array(); |
| 59 |
|
| 60 |
// filter |
| 61 |
foreach( $allShifts as $shift ){ |
| 62 |
if( ! $shift->isPublished() ) continue; |
| 63 |
if( $shift->getStart() >= $end ) continue; |
| 64 |
if( $shift->getStart() < $start ) continue; |
| 65 |
|
| 66 |
$shiftCalendar = $shift->getCalendar(); |
| 67 |
|
| 68 |
if( 'all' == $reminderInclude ){ |
| 69 |
} |
| 70 |
if( 'shift' == $reminderInclude ){ |
| 71 |
if( $shiftCalendar->isTimeoff() ){ |
| 72 |
continue; |
| 73 |
} |
| 74 |
} |
| 75 |
if( 'timeoff' == $reminderInclude ){ |
| 76 |
if( $shiftCalendar->isShift() ){ |
| 77 |
continue; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
$shifts[] = $shift; |
| 82 |
} |
| 83 |
|
| 84 |
// check logs if already sent |
| 85 |
$args = array(); |
| 86 |
$args[] = array( 'range', '=', $range ); |
| 87 |
$args[] = array( 'date', '=', $date ); |
| 88 |
$reminderLogs = $this->remindersQueryLog->read( $args ); |
| 89 |
|
| 90 |
$sentForEmployees = array(); |
| 91 |
foreach( $reminderLogs as $log ){ |
| 92 |
$sentForEmployees[ $log->employee_id ] = $log; |
| 93 |
} |
| 94 |
|
| 95 |
// find employees |
| 96 |
$employees = array(); |
| 97 |
$shiftsByEmployee = array(); |
| 98 |
|
| 99 |
foreach( $shifts as $shift ){ |
| 100 |
$employee = $shift->getEmployee(); |
| 101 |
if( (! $employee) OR (! $employee->getId() ) ) continue; |
| 102 |
|
| 103 |
$employeeId = $employee->getId(); |
| 104 |
// if( isset($sentForEmployees[$employeeId]) ) continue; |
| 105 |
|
| 106 |
$employees[ $employeeId ] = $employee; |
| 107 |
|
| 108 |
if( ! isset($shiftsByEmployee[$employeeId]) ) $shiftsByEmployee[$employeeId] = array(); |
| 109 |
$shiftsByEmployee[$employeeId][] = $shift; |
| 110 |
} |
| 111 |
|
| 112 |
$employeeIds = array_keys( $employees ); |
| 113 |
$employees = $this->employeesQuery->findManyById( $employeeIds ); |
| 114 |
|
| 115 |
$ret = array(); |
| 116 |
foreach( $employees as $employee ){ |
| 117 |
$user = $this->appQuery->findUserByEmployee( $employee ); |
| 118 |
|
| 119 |
// sms can be sent without user |
| 120 |
if( in_array($range, array(SH4_Reminders_Model::RANGE_DAILY_SMS) ) ){ |
| 121 |
|
| 122 |
} |
| 123 |
else { |
| 124 |
if( ! $user ) continue; |
| 125 |
} |
| 126 |
|
| 127 |
$employeeId = $employee->getId(); |
| 128 |
|
| 129 |
$model = new SH4_Reminders_Model; |
| 130 |
|
| 131 |
$model->date = $date; |
| 132 |
$model->range = $range; |
| 133 |
$model->user = $user; |
| 134 |
$model->employee = $employee; |
| 135 |
$model->shifts = $shiftsByEmployee[ $employeeId ]; |
| 136 |
$model->log = isset( $sentForEmployees[$employeeId] ) ? $sentForEmployees[$employeeId] : NULL; |
| 137 |
|
| 138 |
$ret[] = $model; |
| 139 |
} |
| 140 |
|
| 141 |
return $ret; |
| 142 |
} |
| 143 |
} |