PluginProbe
ShiftController Employee Shift Scheduling / 2.1.0
ShiftController Employee Shift Scheduling v2.1.0
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / application / controllers / admin / schedules.php

schedules.php in ShiftController Employee Shift Scheduling 2.1.0, at application/controllers/admin/schedules.php

607 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Schedules_controller extends Backend_controller
4 {
5 function __construct()
6 {
7 $this->conf = array(
8 'path' => 'admin/schedules',
9 );
10 parent::__construct( User_model::LEVEL_MANAGER );
11
12 /* check how many locations do we have */
13 $lm = new Location_Model;
14 $location_count = $lm->count();
15 $this->data['location_count'] = $location_count;
16 }
17
18 function publish()
19 {
20 $args = hc_parse_args( func_get_args() );
21
22 $sm = new Schedule_Model;
23 $sm->start = $args['start'];
24 $sm->end = $args['end'];
25 $sm->staff_id = isset($args['staff']) ? $args['staff'] : 0;
26 $sm->location_id = isset($args['location']) ? $args['location'] : 0;
27
28 $count = $sm->publish();
29
30 if( $count > 0 )
31 {
32 $msg = array();
33 $label = $count . ' ';
34 $label .= ($count > 1) ? lang('shifts') : lang('shift');
35
36 $msg[] = $label;
37 $msg[] = lang('shift_publish');
38 $msg[] = lang('common_ok');
39 $msg = join( ': ', $msg );
40 $this->session->set_flashdata( 'message', $msg );
41 }
42
43 $this->load->library('user_agent');
44 if ($this->agent->is_referral())
45 {
46 $redirect_to = $this->agent->referrer();
47 }
48 else
49 {
50 $redirect_to = array('admin/schedules/index/all', $start_date);
51 }
52 $this->redirect( $redirect_to );
53 return;
54 }
55
56 function unpublish( $start_date, $end_date )
57 {
58 $args = hc_parse_args( func_get_args() );
59
60 $sm = new Schedule_Model;
61 $sm->start = $args['start'];
62 $sm->end = $args['end'];
63 $sm->staff_id = isset($args['staff']) ? $args['staff'] : 0;
64 $sm->location_id = isset($args['location']) ? $args['location'] : 0;
65
66 $count = $sm->unpublish();
67
68 if( $count > 0 )
69 {
70 $msg = array();
71 $label = $count . ' ';
72 $label .= ($count > 1) ? lang('shifts') : lang('shift');
73
74 $msg[] = $label;
75 $msg[] = lang('shift_unpublish');
76 $msg[] = lang('common_ok');
77 $msg = join( ': ', $msg );
78 $this->session->set_flashdata( 'message', $msg );
79 }
80
81 $this->load->library('user_agent');
82 if ($this->agent->is_referral())
83 {
84 $redirect_to = $this->agent->referrer();
85 }
86 else
87 {
88 $redirect_to = array('admin/schedules/index/all', $start_date);
89 }
90 $this->redirect( $redirect_to );
91 return;
92 }
93
94 function delete( $start_date, $end_date )
95 {
96 $sm = new Shift_Model;
97 $sm
98 ->where( 'date >=', $start_date )
99 ->where( 'date <=', $end_date )
100 ->get()
101 ->delete_all();
102
103 $msg = array();
104 $msg[] = lang('common_delete');
105 $msg[] = lang('common_ok');
106 $msg = join( ': ', $msg );
107 $this->session->set_flashdata( 'message', $msg );
108
109 $this->load->library('user_agent');
110 // if ($this->agent->is_referral())
111 // {
112 // $redirect_to = $this->agent->referrer();
113 // }
114 // else
115 // {
116 $redirect_to = array('admin/schedules/index/browse', $start_date, $end_date);
117 // }
118 $this->redirect( $redirect_to );
119 return;
120 }
121
122 private function _load_shifts( $date = 0, $staff_id = 0 )
123 {
124 if( $staff_id )
125 {
126 $um = new User_Model;
127 $um->get_by_id( $staff_id );
128 $shift_model = $um->shift;
129 $timeoff_model = $um->timeoff;
130 }
131 else
132 {
133 $shift_model = new Shift_Model;
134 $timeoff_model = new Timeoff_Model;
135 }
136
137 if( $date )
138 {
139 if( is_array($date) ) // to-from
140 {
141 $this->hc_time->setDateDb( $date[1] );
142 $this->hc_time->modify( '+1 day' );
143 $tomorrow = $this->hc_time->formatDate_Db();
144 $this->hc_time->setDateDb( $date[0] );
145 $this->hc_time->modify( '-1 day' );
146 $yesterday = $this->hc_time->formatDate_Db();
147 }
148 else
149 {
150 $this->hc_time->setDateDb( $date );
151 $this->hc_time->modify( '+1 day' );
152 $tomorrow = $this->hc_time->formatDate_Db();
153 $this->hc_time->setDateDb( $date );
154 $this->hc_time->modify( '-1 day' );
155 $yesterday = $this->hc_time->formatDate_Db();
156 }
157
158 $shift_model->where('date <=', $tomorrow);
159 $shift_model->where('date >=', $yesterday);
160
161 $timeoff_model->where('date <', $tomorrow);
162 $timeoff_model->where('date_end >', $yesterday);
163 }
164
165 $shift_model
166 ->include_related( 'location', 'show_order' )
167 ->include_related( 'location', 'id' )
168 ->include_related( 'location', 'name' )
169 ->include_related( 'user', 'id' )
170 ->order_by( 'date', 'ASC' )
171 ->order_by( 'start', 'ASC' )
172 ->order_by( 'location_show_order', 'ASC' );
173
174 if( $this->hc_modules->exists('shift_trades') )
175 {
176 $shift_model->include_related( 'trade', 'id' );
177 $shift_model->include_related( 'trade', 'status' );
178 }
179 $this->data['shifts'] = $shift_model
180 ->get()->all;
181
182 $this->data['timeoffs'] = $timeoff_model
183 ->where_in('status', array(TIMEOFF_MODEL::STATUS_ACTIVE))
184 ->include_related( 'user', 'id' )
185 ->order_by( 'date', 'ASC' )
186 ->order_by( 'start', 'ASC' )
187 ->get()->all;
188
189 /* load the shifts group ids count and dates*/
190 $groups = $shift_model
191 ->select( 'group_id, COUNT(id) AS count, MIN(date) AS min_date, MAX(date) AS max_date' )
192 ->group_by( 'group_id' )
193 ->get();
194
195 $this->data['shift_groups'] = array();
196 foreach( $groups as $g )
197 {
198 if( ! $g->group_id )
199 continue;
200 $this->data['shift_groups'][$g->group_id] = array(
201 'count' => $g->count,
202 'min_date' => $g->min_date,
203 'max_date' => $g->max_date,
204 );
205 }
206 }
207
208 function day_staff( $date, $staff_id )
209 {
210 $this->data['display'] = 'staff';
211 $this->data['staff_id'] = $staff_id;
212
213 /* load shifts if needed */
214 if( ! isset($this->data['shifts']) )
215 {
216 $this->_load_shifts( $date );
217 }
218
219 /* my shifts */
220 $this->data['my_shifts'] = array();
221 /* filter all shifts */
222 foreach( $this->data['shifts'] as $sh )
223 {
224 if( $sh->date > $date )
225 break;
226 if( $sh->date < $date )
227 continue;
228 if( $sh->user_id != $staff_id )
229 continue;
230 $this->data['my_shifts'][] = $sh;
231 }
232
233 /* my timeoffs */
234 $this->data['my_timeoffs'] = array();
235 /* filter all timeoffs */
236 foreach( $this->data['timeoffs'] as $to )
237 {
238 if( $to->date > $date )
239 break;
240 if( $to->user_id != $staff_id )
241 continue;
242 if( $to->date_end < $date )
243 continue;
244 $this->data['my_timeoffs'][] = $to;
245 }
246
247 $this->data['date'] = $date;
248
249 $this->set_include( 'day_staff' );
250 $this->load->view( $this->template, $this->data);
251 }
252
253 function day_location( $date, $location_id )
254 {
255 $this->data['display'] = 'location';
256 $this->data['location_id'] = $location_id;
257
258 /* load shifts if needed */
259 if( ! isset($this->data['shifts']) )
260 {
261 $this->_load_shifts( $date );
262 }
263
264 /* my shifts */
265 $this->data['my_shifts'] = array();
266 /* filter all shifts */
267 foreach( $this->data['shifts'] as $sh )
268 {
269 if( $sh->date > $date )
270 break;
271 if( $sh->date < $date )
272 continue;
273 if( $sh->location_id != $location_id )
274 continue;
275 $this->data['my_shifts'][] = $sh;
276 }
277
278 /* my timeoffs */
279 $this->data['my_timeoffs'] = array();
280
281 $this->data['date'] = $date;
282
283 $this->set_include( 'day_location' );
284 $this->load->view( $this->template, $this->data);
285 }
286
287 function day( $date )
288 {
289 $this->data['display'] = 'all';
290 $sm = new Shift_Model;
291
292 /* load shifts if needed */
293 if( ! isset($this->data['shifts']) )
294 {
295 $this->_load_shifts( $date );
296 }
297
298 /* filter all shifts */
299 $this->data['my_shifts'] = array();
300 foreach( $this->data['shifts'] as $sh )
301 {
302 if( $sh->date > $date )
303 break;
304 if( $sh->date < $date )
305 continue;
306 $this->data['my_shifts'][] = $sh;
307 }
308
309 $um = new User_Model;
310 $staffs = $um->get()->all;
311 $this->data['staffs'] = array();
312 foreach( $staffs as $sta )
313 {
314 $this->data['staffs'][ $sta->id ] = $sta;
315 }
316
317 $this->data['date'] = $date;
318
319 $this->set_include( 'day' );
320 $this->load->view( $this->template, $this->data);
321 }
322
323 function browse()
324 {
325 $start = $this->input->post( 'start' );
326 $end = $this->input->post( 'end' );
327 $redirect_to = array( $this->conf['path'], 'index/browse', $start, $end );
328 $this->redirect( $redirect_to );
329 return;
330 }
331
332 function status()
333 {
334 $args = hc_parse_args( func_get_args() );
335
336 $count = array();
337 $this->data['location_id'] = 0;
338 $this->data['staff_id'] = 0;
339 if( isset($args['staff']) && $args['staff'] )
340 {
341 $um = new User_Model;
342 $um->get_by_id( $args['staff'] );
343 $sm = $um->shift;
344 $this->data['staff_id'] = $args['staff'];
345 }
346 elseif( isset($args['location']) && $args['location'] )
347 {
348 $lm = new Location_Model;
349 $lm->get_by_id( $args['location'] );
350 $sm = $lm->shift;
351 $this->data['location_id'] = $args['location'];
352 }
353 else
354 {
355 $sm = new Shift_Model;
356 }
357
358 /* duration */
359 $sm->clear();
360 // $sm->include_related( 'user', 'id' );
361 $sm
362 ->where( 'date >=', $args['start'] )
363 ->where( 'date <=', $args['end'] )
364 ->select_sum( 'end' )
365 ->select_sum( 'start' )
366 ->get();
367 $count['duration'] = ($sm->end - $sm->start);
368
369 /* count unpublished shifts */
370 $sm->clear();
371 $sm->include_related( 'user', 'id' );
372 $count['active'] = $sm
373 ->where( 'date >=', $args['start'] )
374 ->where( 'date <=', $args['end'] )
375 ->where( 'status', SHIFT_MODEL::STATUS_ACTIVE )
376 ->where( 'user_id IS NOT ', 'NULL', FALSE )
377 ->count();
378
379 /* published */
380 $sm->clear();
381 $sm->include_related( 'user', 'id' );
382 $count['draft'] = $sm
383 ->where( 'date >=', $args['start'] )
384 ->where( 'date <=', $args['end'] )
385 ->where( 'status <>', SHIFT_MODEL::STATUS_ACTIVE )
386 ->where( 'user_id IS NOT ', 'NULL', FALSE )
387 ->count();
388
389 /* total */
390 $sm->clear();
391 $count['total'] = $sm
392 ->where( 'date >=', $args['start'] )
393 ->where( 'date <=', $args['end'] )
394 ->count();
395
396 /* not assigned */
397 $sm->clear();
398 $sm->include_related( 'user', 'id' );
399 $count['not_assigned'] = $sm
400 ->where( 'date >=', $args['start'] )
401 ->where( 'date <=', $args['end'] )
402 ->where( 'user_id IS ', 'NULL', FALSE )
403 ->count();
404
405 $this->data['count'] = $count;
406 $this->data['start_date'] = $args['start'];
407 $this->data['end_date'] = $args['end'];
408
409 $this->set_include( 'status' );
410 $this->load->view( $this->template, $this->data);
411 }
412
413 function index( $display = 'all', $date = '', $end_date = '' )
414 {
415 $range = 'month'; // may also be 'week'
416
417 /* check if schedule for this date exists */
418 if( $end_date )
419 {
420 $start_date = $date;
421 if( $end_date < $start_date )
422 $end_date = $start_date;
423 }
424 else
425 {
426 if( $date )
427 {
428 $this->hc_time->setDateDb( $date );
429 }
430 else
431 {
432 $this->hc_time->setNow();
433 }
434
435 switch( $range )
436 {
437 case 'week':
438 $this->hc_time->setStartWeek();
439 $start_date = $this->hc_time->formatDate_Db();
440 $this->hc_time->setEndWeek();
441 $end_date = $this->hc_time->formatDate_Db();
442 break;
443
444 case 'month':
445 $this->hc_time->setStartMonth();
446 $start_date = $this->hc_time->formatDate_Db();
447 $this->hc_time->setEndMonth();
448 $end_date = $this->hc_time->formatDate_Db();
449 break;
450 }
451 }
452
453 $this->data['start_date'] = $start_date;
454 $this->data['end_date'] = $end_date;
455 $this->data['range'] = $range;
456
457 $um = new User_Model;
458 if( $display == 'staff' )
459 {
460 $um->where('active', USER_MODEL::STATUS_ACTIVE);
461 }
462 $staffs = $um->get()->all;
463
464 $this->data['staffs'] = array();
465 foreach( $staffs as $sta )
466 {
467 $this->data['staffs'][ $sta->id ] = $sta;
468 }
469
470 $lm = new Location_Model;
471 $locations = $lm
472 ->get()->all;
473 $this->data['locations'] = array();
474 foreach( $locations as $loc )
475 {
476 $this->data['locations'][ $loc->id ] = $loc;
477 }
478
479 $this->data['display'] = $display;
480
481 /* load shifts so that they can be reused in module displays to save queries */
482 $this->_load_shifts( array($start_date, $end_date) );
483
484 /* decide which view */
485 switch( $display )
486 {
487 case 'all':
488 $view = 'index';
489 break;
490
491 case 'location':
492 $view = 'index_location';
493 break;
494
495 case 'staff':
496 $view = 'index_staff';
497 break;
498
499 case 'browse':
500 $view = 'index_browse';
501 break;
502
503 case 'export':
504 return $this->export();
505 break;
506
507 default:
508 $view = 'index';
509 break;
510 }
511
512 $this->set_include( $view );
513 $this->load->view( $this->template, $this->data);
514 return;
515 }
516
517 function export()
518 {
519 $separator = $this->app_conf->get( 'csv_separator' );
520
521 // header
522 $headers = array(
523 lang('time_date'),
524 lang('time'),
525 lang('time_duration'),
526 lang('user_level_staff'),
527 lang('location'),
528 lang('shift_status')
529 );
530
531 $data = array();
532 $data[] = join( $separator, $headers );
533
534 // shifts
535 reset( $this->data['shifts'] );
536 foreach( $this->data['shifts'] as $sh )
537 {
538 if( $sh->date < $this->data['start_date'] )
539 continue;
540 if( $sh->date > $this->data['end_date'] )
541 continue;
542
543 $values = array();
544
545 // date
546 $this->hc_time->setDateDb( $sh->date );
547 $date_view = '';
548 $date_view .= $this->hc_time->formatWeekdayShort();
549 $date_view .= ', ';
550 $date_view .= $this->hc_time->formatDate();
551 $values[] = $date_view;
552
553 // time
554 $time_view = hc_format_time_of_day($sh->start) . ' - ' . hc_format_time_of_day($sh->end);
555 $values[] = $time_view;
556
557 // duration
558 $values[] = $this->hc_time->formatPeriodShort($sh->get_duration(), 'hour');
559
560 // staff
561 if( $sh->user_id && isset($this->data['staffs'][$sh->user_id]) )
562 {
563 $staff_title = $this->data['staffs'][$sh->user_id]->full_name();
564 }
565 else
566 {
567 $staff_title = '';
568 }
569 $values[] = $staff_title;
570
571 // location
572 $values[] = $sh->location_name;
573
574 // status
575 $conflicts = $sh->conflicts( $this->data['shifts'], $this->data['timeoffs'] );
576 if( $sh->user_id )
577 {
578 $status = ( $sh->status == SHIFT_MODEL::STATUS_ACTIVE ) ? lang('shift_status_active') : lang('shift_status_draft');
579 if( count($conflicts) )
580 {
581 $status = lang('shift_conflict');
582 }
583 }
584 else
585 {
586 $status = lang('shift_not_assigned');
587 }
588 $values[] = $status;
589
590 /* add csv line */
591 $data[] = hc_build_csv( array_values($values), $separator );
592 }
593
594 // output
595 $out = join( "\n", $data );
596
597 $file_name = isset( $this->conf['export'] ) ? $this->conf['export'] : 'export';
598 $file_name .= '-' . date('Y-m-d_H-i') . '.csv';
599
600 $this->load->helper('download');
601 force_download($file_name, $out);
602 return;
603 }
604 }
605
606 /* End of file customers.php */
607 /* Location: ./application/controllers/admin/categories.php */