This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule_functions.php
468 lines (360 loc) · 14.1 KB
/
schedule_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<?php
/*******************************************************************************
* Copyright 2016 Alan A. Zanotto
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
//Returns 0 or 1. 0 if PP is not available on Dayshift. 1 if it is.
function calculatePP_DS($link, $db, $emp_posted_position, $template_ID, $schedule_ID)
{
$pp_id = $emp_posted_position;//$pp_id is the posted position ID.
//can alter this sql to use * and remove count, this will display the actual row entries. However this is not really needed for this function.
$sql_PP_DS_saved = "
SELECT count(sp.id_posted_position_requirement) total
FROM `".$db."`.`schedule_position` sp,
`".$db."`.`schedule_saved` ss
WHERE ss.id_schedule_position = sp.id
AND ss.id_schedule = ".$schedule_ID ."
AND sp.id_posted_position_requirement = ". $pp_id ."
AND ss.shift = 0";
//echo "HERE IS THE PROBLEM::::::".$sql_PP_DS_saved;
$result_PP_DS_saved = $link->query($sql_PP_DS_saved);
$object_PP_DS_saved = $result_PP_DS_saved->fetch_assoc();
$PP_DS_saved = $object_PP_DS_saved['total'];
$sql_PP_DS_quantity =
"
SELECT sum(quantity) quantity
FROM `".$db."`.`schedule_template_position_list_auto` stpl,
`".$db."`.`schedule_position`sp
WHERE
stpl.id_schedule_position = sp.id
AND stpl.id_template = ".$template_ID ."
AND stpl.shift = 0
AND sp.id_posted_position_requirement = ".$pp_id;
$result_PP_DS_quantity = $link->query($sql_PP_DS_quantity);
$object_PP_DS_quantity = $result_PP_DS_quantity->fetch_assoc();
$PP_DS_quantity = $object_PP_DS_quantity['quantity'];//pp_DS_quantity is the sum of jobs, that require said positions, that are required for night shift.
if ($PP_DS_saved < $PP_DS_quantity)
{
return 1;//There is an available PP spot on Day Shift
}
else
{
return 0;//THere is NOT and available PP spot on Day Shift
}
}//function calculatePP_DS($emp_posted_position)
//Retruns 0 or 1. 0 if PP is not available on Nightshift. 1 if it is.
function calculatePP_NS($link, $db, $emp_posted_position, $template_ID, $schedule_ID)
{
$pp_id = $emp_posted_position;//$pp_id is the posted position ID.
//can alter this sql to use * and remove count, this will display the actual row entries. However this is not really needed for this function.
$sql_PP_NS_saved = "
SELECT count(sp.id_posted_position_requirement) total
FROM `".$db."`.`schedule_position` sp,
`".$db."`.`schedule_saved` ss
WHERE ss.id_schedule_position = sp.id
AND ss.id_schedule = ".$schedule_ID ."
AND sp.id_posted_position_requirement = ". $pp_id ."
AND ss.shift = 1";
$result_PP_NS_saved = $link->query($sql_PP_NS_saved);
$object_PP_NS_saved = $result_PP_NS_saved->fetch_assoc();
$PP_NS_saved = $object_PP_NS_saved['total'];
$sql_PP_NS_quantity =
"
SELECT sum(quantity) quantity
FROM `".$db."`.`schedule_template_position_list_auto` stpl,
`".$db."`.`schedule_position`sp
WHERE
stpl.id_schedule_position = sp.id
AND stpl.id_template = ".$template_ID ."
AND stpl.shift = 1
AND sp.id_posted_position_requirement = ".$pp_id;
$result_PP_NS_quantity = $link->query($sql_PP_NS_quantity);
$object_PP_NS_quantity = $result_PP_NS_quantity->fetch_assoc();
$PP_NS_quantity = $object_PP_NS_quantity['quantity'];//pp_ds_quantity is the sum of jobs, that require said positions, that are required for Night shift.
//echo "ECHO NS_QUANTITY: ".$PP_NS_quantity;
//echo "NS_Saved: ".$PP_NS_saved. " NS_QUANTITY: " .$PP_NS_quantity;
if ($PP_NS_saved < $PP_NS_quantity)
{
return 1;//There is an available PP spot on Night Shift
}
else
{
return 0;//THere is NOT and available PP spot on Night Shift
}
}//function calculatePP_NS($emp_posted_position)
//Retruns 0 or 1. 0 if there is no spot available on SP, 1 if there is a spot available on SP. The Spot looks at sorter positions.
function calculateSP($link, $db, $emp_shift_preference, $template_ID, $schedule_ID)
{
//see how many sorters have been put on the schedule
$sql_SP_saved = "
SELECT count(sp.id_posted_position_requirement) total
FROM `".$db."`.`schedule_position` sp,
`".$db."`.`schedule_saved` ss
WHERE ss.id_schedule_position = sp.id
AND ss.id_schedule = ".$schedule_ID ."
AND sp.id_posted_position_requirement = 18
AND ss.shift = ".$emp_shift_preference;
//echo $sql_SP_saved;
$result_SP_saved = $link->query($sql_SP_saved);
$object_SP_saved = $result_SP_saved->fetch_assoc();
$SP_saved = $object_SP_saved['total'];
//see how many sorters are allowed onto the schedule.
$sql_SP_quantity =
"
SELECT sum(quantity) quantity
FROM `".$db."`.`schedule_template_position_list_auto` stpl,
`".$db."`.`schedule_position`sp
WHERE
stpl.id_schedule_position = sp.id
AND stpl.id_template = ".$template_ID ."
AND stpl.shift = ". $emp_shift_preference ."
AND sp.id_posted_position_requirement = 18";
$result_SP_quantity = $link->query($sql_SP_quantity);
$object_SP_quantity = $result_SP_quantity->fetch_assoc();
$SP_quantity = $object_SP_quantity['quantity'];
//if the amount of sorters currently on the schedule is less than the amount of sorters allowed. Then SP = 1. So there shift preference is then available.
if ($SP_saved < $SP_quantity)
{
return 1;//there is a spot available.
}
else
{
return 0;//there is not a spot available.
}
}//function calculateSP($emp_posted_position)
//Retruns 0 or 1. 0 if there is no spot available on SP, 1 if there is a spot available on SP. The Spot looks at sorter positions.
function isSortersFull($link, $db, $template_ID, $schedule_ID)
{
//Finds and sums up all the remaining available sorter spots.
$sql_sorter_remaining =
"
SELECT sum(quantity) quantity
FROM `".$db."`.`schedule_template_position_list_auto` stpl,
`".$db."`.`schedule_position`sp
WHERE
stpl.id_schedule_position = sp.id
AND stpl.id_template = ".$template_ID ."
AND sp.id_posted_position_requirement = 18
AND stpl.scheduled = 0
";
$result_sorter_remaining = $link->query($sql_sorter_remaining);
$object_sorter_remaining = $result_sorter_remaining->fetch_assoc();
$sorter_remaining = $object_sorter_remaining['quantity'];
//find out how many sorter spots there are on the schedule.
$sql_sorter_total =
"
SELECT sum(quantity) quantity
FROM `".$db."`.`schedule_template_position_list_auto` stpl,
`".$db."`.`schedule_position`sp
WHERE
stpl.id_schedule_position = sp.id
AND stpl.id_template = ".$template_ID ."
AND sp.id_posted_position_requirement = 18
";
$result_sorter_total = $link->query($sql_sorter_total);
$object_sorter_total = $result_sorter_total->fetch_assoc();
$sorter_total = $object_sorter_total['quantity'];
//if the amount of sorters currently on the schedule is equivalent to the amount of sorters allowed, then isSortersFull = 1 (true).
if ( ($sorters_remaining == $sorters_total))
{
return true;//the amount of sorters on the schedule HAS been reached.
}
else
{
return false;//the amount of sorters on the schedule has NOT been reached.
}
}//isSortersFull($link, $template_ID, $schedule_ID)
//finds a job_id in the stpl_auto table and returns that ID. This is basically what a person will be scheduled as.
function findAvailablePP_withShiftPreference($emp_id, $emp_posted_position, $emp_shift_preference, $template_ID, $schedule_ID)
{
//Include database Connection Script
include 'db_connection.php';
$sql = "
SELECT stpla.ID, stpla.ID_template, stpla.ID_schedule_position, stpla.quantity,
stpla.shift, stpla.facility, stpla.station, stpla.scheduled,
sp.ID_posted_position_requirement, sp.name
FROM `".$db."`.`schedule_template_position_list_auto` stpla,
`".$db."`.`schedule_position` sp
WHERE stpla.ID_template = ".$template_ID ."
AND stpla.ID_schedule_position = sp.ID
AND sp.ID_posted_position_requirement = ".$emp_posted_position. "
AND stpla.shift = ". $emp_shift_preference. "
AND stpla.scheduled = 0
";
//echo $sql;
$result = $link->query($sql);
$object = $result->fetch_assoc();
//echo "query rows".mysqli_num_rows($result);
return $object['ID'];
//Include database Termination Script
include 'db_disconnect.php';
}
//takes a job_id and saves it into the schedule and sets that position to be unavailable for the next person.(scheduled column in stpl_auto, 0=unscheduled, 1=scheduled)
function schedule_a_person($schedule_ID, $template_ID, $job_id, $emp_id, $emp_shift, $job_facility, $job_station)
{
//Include database Connection Script
include 'db_connection.php';
//grab the ID_schedule_postion
$ID_schedule_position = findIDSchedulePosition($job_id);
//insert it to schedule saved.
$sql_add_toSchedule = "INSERT INTO `".$db."`.`schedule_saved` (`ID_schedule`, `ID_schedule_position`, `ID_employee`, `shift`, `facility`, `station`, `weight`) VALUES (".$schedule_ID.", ".$ID_schedule_position.", ".$emp_id.", ".$emp_shift.", ".$job_facility. ", ". $job_station.", ".$job_id.")";
$link->query($sql_add_toSchedule);
//UPDATE stpl_auto to have this job_id scheduled column set to 1 so it wont be used anymore for other people.
$sql_hide_from_auto = "
UPDATE `".$db."`.`schedule_template_position_list_auto`
SET scheduled = 1
WHERE ID = ".$job_id;
$link->query($sql_hide_from_auto);
//Include database Termination Script
include 'db_disconnect.php';
}
function findFacility($job_id)
{
//Include database Connection Script
include 'db_connection.php';
$sql = '
SELECT *
FROM `'.$db.'`.`schedule_template_position_list_auto`
WHERE ID = '. $job_id;
$result = $link->query($sql);
$object = $result->fetch_assoc();
$facility_id = $object['facility'];
//Include database Termination Script
include 'db_disconnect.php';
return $facility_id;
}
function findFacilityName($job_id)
{
//Include database Connection Script
include 'db_connection.php';
$sql = '
SELECT *
FROM `'.$db.'`.`schedule_facility`
WHERE ID = '. $job_id;
$result = $link->query($sql);
$object = $result->fetch_assoc();
$facility_name = $object['name'];
//Include database Termination Script
include 'db_disconnect.php';
return $facility_name;
}
function findStation($job_id)
{
//Include database Connection Script
include 'db_connection.php';
$sql = '
SELECT *
FROM `'.$db.'`.`schedule_template_position_list_auto`
WHERE ID = '. $job_id;
$result = $link->query($sql);
$object = $result->fetch_assoc();
$station_id = $object['station'];
//Include database Termination Script
include 'db_disconnect.php';
return $station_id;
}
function findStationName($job_id)
{
//Include database Connection Script
include 'db_connection.php';
$sql = '
SELECT *
FROM `'.$db.'`.`schedule_station`
WHERE ID = '. $job_id;
$result = $link->query($sql);
$object = $result->fetch_assoc();
$station_name = $object['name'];
//Include database Termination Script
include 'db_disconnect.php';
return $station_name;
}
function findIDSchedulePosition($job_id)
{
//Include database Connection Script
include 'db_connection.php';
$sql = '
SELECT *
FROM `'.$db.'`.`schedule_template_position_list_auto`
WHERE ID = '. $job_id;
$result = $link->query($sql);
$object = $result->fetch_assoc();
$ID_schedule_position = $object['ID_schedule_position'];
//Include database Termination Script
include 'db_disconnect.php';
return $ID_schedule_position;
}
function isScheduleFull($link, $db, $schedule_ID, $template_ID)
{
//CHECK TO SEE IF SCHEUDLE IS FULL. IF FULL THEN SCHEDULE IS DONE.
//By looking at the how many positions are needed for the schedule and comparing that with how many have been scheduled, we can figure out whether or not the schedule is full.
//$num_positions_needed SQL QUERY
$sql_num_positions_needed =
"
SELECT SUM(quantity) num_positions_needed FROM `".$db."`.`schedule_template_position_list_auto` WHERE id_template = ".$template_ID;
$result_num_positions_needed = $link->query($sql_num_positions_needed);
//echo $sql_num_positions_needed;
$object_num_positions_needed = $result_num_positions_needed->fetch_assoc();
$num_positions_needed = $object_num_positions_needed['num_positions_needed'];
//echo " number of positoins needed: " .$num_positions_needed;
//num of positions in the schedule
$sql_num_saved_positions_scheduled =
"
SELECT COUNT(ID_schedule) num_saved_positions_scheduled
FROM `".$db."`.`schedule_saved`
WHERE `schedule_saved`.`ID_schedule` = ". $schedule_ID;
$result_num_saved_positions_scheduled = $link->query($sql_num_saved_positions_scheduled);
$object_num_saved_positions_scheduled = $result_num_saved_positions_scheduled->fetch_assoc();
$num_saved_positions_scheduled = $object_num_saved_positions_scheduled['num_saved_positions_scheduled'];
//echo " num_saved_positions_scheduled: ".$num_saved_positions_scheduled;
if ($num_saved_positions_scheduled < $num_positions_needed)
{
return 0;//Scheudle is not full.
}
else
{
return 1;//Scheudle is full.
}
}//function isScheduleFull($schedule_ID, $template_ID)
function addUnfilledPositionsToSchedule($link, $db, $schedule_ID, $template_ID)
{
//Identify All the Positions that are not filled.
$sql_unfilled_positions =
"
SELECT *
FROM `".$db."`.`schedule_template_position_list_auto`
WHERE
id_template = ".$template_ID ."
AND scheduled = 0
";
$result_unfilled_positions = $link->query($sql_unfilled_positions);
//If there is any positions that are unfilled. Add them to the scheudle with an employee id as 0.
//echo "results from addingpositios....".mysqli_num_rows($result_unfilled_positions);
if (mysqli_num_rows($result_unfilled_positions) > 0)
{
//Loop through the list of unfilled positions and then add them to the schedule with an employee id = 0.
while($row = $result_unfilled_positions->fetch_assoc())
{
$job_id = $row['ID'];
$ID_schedule_position = $row['ID_schedule_position'];
//echo "Unfilled positions job id=".$job_id;
//echo "Unfilled positions ID_schedule_position=".$ID_schedule_position;
$emp_id = 0;
$emp_shift = $row['shift'];
$job_facility = $row['facility'];
$job_station = $row['station'];
schedule_a_person($schedule_ID, $template_ID, $job_id, $emp_id, $emp_shift, $job_facility, $job_station);
}
}//end if
}
?>