This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
/
create_schedule.php
323 lines (262 loc) · 12 KB
/
create_schedule.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
<?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.
*******************************************************************************/
//Retrieve POST Values
//RETRIEVE SCHEDULE SETUP INFORMATION
//SCHEDULE TEMPLATE
//SCHEDULE DATE
$schedule_date = $_POST["schedule_date"];
$schedule_templates_day = $_POST['templates_day'];
$schedule_templates_night = $_POST['templates_night'];
//pass a string of template ids "_3_5_34_56"
$templates_day = explode("_", $schedule_templates_day);
$templates_night = explode("_", $schedule_templates_night);
//Put together a temporary template.
$temp_template_id = rand(1, 100);
//Include database Connection Script
include 'db_connection.php';
include 'schedule_functions.php';
//loop through day, then night, add positions to stpl_auto
//DAY LOOP.
for ($x = 1; $x < count($templates_day); $x++){
$temp_template = $templates_day[$x];
$sql_stpl =
'
SELECT * FROM `'.$db.'`.`schedule_template_position_list`
WHERE ID_template = '.$temp_template;
$result_stpl = $link->query($sql_stpl);
while ($row = $result_stpl->fetch_assoc()){
for ($y = 0; $y < $row['quantity']; $y++)
{
$sql_insert_auto =
'
INSERT INTO `'.$db.'`.`schedule_template_position_list_auto`
(`ID`, `ID_template`, `ID_schedule_position`, `quantity`, `shift`, `facility`, `station`)
VALUES (NULL, '.$temp_template_id.', '.$row['ID_schedule_position'].', 1, 0, '.$row['facility'].', '.$row['station'].')';
$link->query($sql_insert_auto);
}
}
}
//loop through day, then night, add positions to stpl_auto
//NIGHT LOOP.
for ($x = 1; $x < count($templates_night); $x++){
$temp_template = $templates_night[$x];
$sql_stpl =
'
SELECT * FROM `'.$db.'`.`schedule_template_position_list`
WHERE ID_template = '.$temp_template;
$result_stpl = $link->query($sql_stpl);
while ($row = $result_stpl->fetch_assoc()){
for ($y = 0; $y < $row['quantity']; $y++)
{
$sql_insert_auto =
'
INSERT INTO `'.$db.'`.`schedule_template_position_list_auto`
(`ID`, `ID_template`, `ID_schedule_position`, `quantity`, `shift`, `facility`, `station`)
VALUES (NULL, '.$temp_template_id.', '.$row['ID_schedule_position'].', 1, 1, '.$row['facility'].', '.$row['station'].')';
$link->query($sql_insert_auto);
}
}
}
//$template_ID = ....
//CREATE A SCHEDULE...
//INSERT NEW SCHEDULE ENTRY newSchedule($date, $id_template)
//SELECT NEW SCHEDULE_ID...
$sql_newSchedule =
"
INSERT INTO `".$db."`.`schedule` (`ID`, `ID_template`, `date`) VALUES (NULL, '". $temp_template_id ."', '". $schedule_date ."');
";
$link->query($sql_newSchedule);
$sql_newSchedule_ID = "SELECT LAST_INSERT_ID()";
$result_newSchedule_ID = $link->query($sql_newSchedule_ID);
$row_schedule_ID = $result_newSchedule_ID->fetch_assoc();
$schedule_ID = $row_schedule_ID['LAST_INSERT_ID()'];
//echo $schedule_ID;
//START A LOOP AND INSERT PEOPLE INTO THE SCHEDULE....IN ORDER OF SENORITY.
$sql_employee =
'SELECT *
FROM `'.$db.'`.`employee` e, `'.$db.'`.`employee_shift_preference` esp
WHERE e.`ID` = esp.`ID_employee` AND esp.`available` = 1 ORDER BY e.senority';
//echo $sql_employee;//debugging
$result_employee = $link->query($sql_employee);
while ($employee = $result_employee->fetch_assoc())
{
//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
//By looking at the how many positions are needed for the schedule($num_positions_needed) and comparing that with how many have been scheduled($num_saved_positions_scheduled),
//we can figure out whether or not the schedule is full. In the event that it is, we break out of the loop.
//echo isScheduleFull($link, $db, $schedule_ID, $schedule_ID);
if (isScheduleFull($link, $db, $schedule_ID, $temp_template_id))
{//
break;//Terminate the while loop. Schedule composition is completed.
}
else//schedule said employee
{
//ELSE...put the person onto the schedule.
//RETRIEVE A PERSONS INFORMATION
$emp_id = $employee['ID'];
$emp_senority = $employee['senority'];
$emp_first_name = $employee['first_name'];
$emp_last_name = $employee['last_name'];
$emp_shift = $employee['shift'];
$emp_posted_position = $employee['posted_position'];
$emp_shift_num;
$emp_shift_num_opposite;
if ($emp_shift == 0)
{
$emp_shift_num = 0;
$emp_shift_num_opposite = 1;
}
else
{
$emp_shift_num = 1;
$emp_shift_num_opposite = 0;
}
//debugging
//echo "</br> ".$emp_id.", ". $emp_senority . ", ". $emp_first_name .", ". $emp_last_name .", ". $emp_shift .", ". $emp_posted_position . "->";
//RETRIEVE AVAILABLE SPOT INFORMATION
//RETRIEVE PP_DS Is PP available on DS.
//RETRIEVE PP_NS is PP available on NS.
//PP = Posted Position
//DS = Day Shift
//NS = Night Shift
//This is a preference variable. It will use this to try and give them their preferred shift.
$PP_DS = calculatePP_DS($link, $db, $emp_posted_position, $temp_template_id, $schedule_ID);
//echo "</br>";
//echo "PP_DS: ".$PP_DS ."</br>";
$PP_NS = calculatePP_NS($link, $db, $emp_posted_position, $temp_template_id, $schedule_ID);
//echo "PP_NS: ".$PP_NS ."</br>";
//RETRIEVE SP IS AVAILABLE OR NOT.
$SP = calculateSP($link, $db, $emp_shift, $temp_template_id, $schedule_ID);
//echo "SP: ".$SP ."</br></br>";
//IF CHECK THROUGH 5 IF SCENARIOS IF (PP_DS && PP_NS & SP) THEN....
if ( (($PP_DS == 1) && ($PP_NS == 1) && ($SP == 1)) ||
(($PP_DS == 1) && ($PP_NS == 1) && ($SP == 0)) ||
(($PP_DS == 1) && (isset($PP_NS) == false) && ($SP == 1)) )
{
//echo "FIRST CASE: ".$emp_first_name . " ".$emp_last_name;
//In this case, Give the emp their posted position on their shift preference. Best case for employee.
//echo $emp_first_name ." got to this point";
//echo "<br>PP_DS: ". $PP_DS;
//echo "<br>PP_NS: ". $PP_NS;
//echo "<br>___SP: ". $SP;
$job_id = findAvailablePP_withShiftPreference($emp_id, $emp_posted_position, $emp_shift, $temp_template_id, $schedule_ID);
$job_facility = findFacility($job_id);
$job_station = findStation($job_id);
//echo "jobid = ".$job_id;
//if ($job_id == "")
//echo "HERE";
schedule_a_person($schedule_ID, $temp_template_id, $job_id, $emp_id, $emp_shift, $job_facility, $job_station);
}
elseif ( (($PP_DS == 1) && ($PP_NS == 0) && ($SP == 1)) ||
(($PP_DS == 1) && ($PP_NS == 0) && ($SP == 0)) ||
(($PP_DS == 1) && ($PP_NS == NULL) && ($SP == 0)) ||
(($PP_DS == 1) && ($PP_NS == NULL) && ($SP == 1)) )
{
//echo "SECOND CASE: ".$emp_first_name . " ".$emp_last_name;;
//The employee will get their PP job but on the opposite shift that they prefer. They wanted Nights But get bumped to Days due to their PP.
//Give emp $PP_DS
$emp_shift_num = 0;//only day shift PP available. 0 = Day shift
$job_id = findAvailablePP_withShiftPreference($emp_id, $emp_posted_position, $emp_shift_num, $temp_template_id, $schedule_ID);
$job_facility = findFacility($job_id);
$job_station = findStation($job_id);
//echo "jobid = ".$job_id;
schedule_a_person($schedule_ID, $temp_template_id, $job_id, $emp_id, $emp_shift_num, $job_facility, $job_station);
}
elseif (( ($PP_DS == 0) && ($PP_NS == 1) && ($SP == 1)) ||
( ($PP_DS == 0) && ($PP_NS == 1) && ($SP ==0)) ||
( ($PP_DS == NULL) && ($PP_NS == 1) && ($SP == 0) ) ||
( ($PP_DS == NULL) && ($PP_NS == 1) && ($SP == 1) ) )
{
echo "THIRD CASE: ".$emp_first_name . " ".$emp_last_name;;
//The employee will get their PP but on the opposite shift they prefered. They wanted Day, But get bumped to Nights due to their PP.
//Give PP_NS
$emp_shift_num = 1;//only night shift PP available. 1 = Night shift
$job_id = findAvailablePP_withShiftPreference($emp_id, $emp_posted_position, $emp_shift_num, $temp_template_id, $schedule_ID);
$job_facility = findFacility($job_id);
$job_station = findStation($job_id);
//echo "jobid = ".$job_id;
schedule_a_person($schedule_ID, $temp_template_id, $job_id, $emp_id, $emp_shift_num, $job_facility, $job_station);
}
elseif ( ($PP_DS == 0) && ($PP_NS == 0) && ($SP ==1))
{
//echo "FORTH CASE: ".$emp_first_name . " ".$emp_last_name;;
//This case happens when their Posted Position are all taken, but there is still room on the schedule as a sorter.
//So they will get scheduled onto their shift prefence(day or night) as a sorter
//Give SP(Shift Preference) as a sorter
$emp_posted_position = "18";//18 is sorter
//echo "emp_id=".$emp_id;
//echo "jobid=".$job_id;
//echo "findAvailablePP_withShiftPreference...";
$job_id = findAvailablePP_withShiftPreference($emp_id, $emp_posted_position, $emp_shift, $temp_template_id, $schedule_ID);
//echo "findAvailablePP_withShiftPreference...done...returned job_id: ".$job_id;
//echo "jobid=".$job_id;
//echo "</br>";
$job_facility = findFacility($job_id);
$job_station = findStation($job_id);
//echo "jobid = ".$job_id;
schedule_a_person($schedule_ID, $temp_template_id, $job_id, $emp_id, $emp_shift, $job_facility, $job_station);
}
//Maybe add another elseif....$PP_DS == 0) && ($PP_NS == 0) && ($SP ==0) && PP="Sorter"..then they don't make it onto the schedule or it would go to trained & capable positions.
elseif ( ($PP_DS == 0) && ($PP_NS == 0) && ($SP ==0))
{
//echo "SIXTH CASE: ".$emp_first_name . " ".$emp_last_name;;
//This case happens when their Posted Position are all taken, and their shift prefence is all taken as well.
//So they will get scheduled onto (day or night) as a sorter. ****IF there is a spot available, else the schedule is full at this point****
//Give SP as a sorter if there is a spot on the schedule
//check if sorters PP is full.
if ( !isSortersFull($link, $db, $temp_template_id, $schedule_ID) )
{
$emp_posted_position = "18";//18 is sorter
$job_id = findAvailablePP_withShiftPreference($emp_id, $emp_posted_position, $emp_shift_num_opposite, $temp_template_id, $schedule_ID);
$job_facility = findFacility($job_id);
$job_station = findStation($job_id);
//echo "jobid = ".$job_id;
schedule_a_person($schedule_ID, $temp_template_id, $job_id, $emp_id, $emp_shift, $job_facility, $job_station);
}
else
{
//echo "The schedule is now typically FULL at this point, or the schedule is looking for the non rotational and or a specific persons posted position to fill the remaining jobs.";
//Ex: No sorters spots left, but schedule needs to find a PP that hasn't been filled yet. Can mean a lower senority person with a PP.
//echo "Sorry! No more sorters allowed on the schedule";
}
}//end elseif SIXTH CASE
else
{
//fail..what just happened?. The code should never reach this else, but if it does, it shouldn't do anything to how the schedule is created. I have yet to see this echo appear.
//I can't think of a scenario where code will get to this spot.
echo "SEVENTH CASE: OMG!!! ITS HAPPENING!!!! CALL ALAN!!!!!";
//go to the next person in the list.
//The schedule probably had very little amount of positions and specific
//posted position requirement.
}
//echo "JobID=".$job_id;
//echo "<br>";
//echo "<br>";
//echo "<br>";
}//end ELSE(scheduling of said employee).
}//END LOOP
//Add Positions That are not filled to the schedule. Use and employee ID as 0.
addUnfilledPositionsToSchedule($link, $db, $schedule_ID, $temp_template_id);
//echo "delte auto table....";
$sql_truncate_stpl_auto = "TRUNCATE `".$db."`.`schedule_template_position_list_auto`";
//echo $sql_truncate_stpl_auto;
$link->query($sql_truncate_stpl_auto);
echo "<p><b>The Schedule has been created</b>...Click on Schedule Page to view it.</p>";
//Include database Termination Script
include 'db_disconnect.php';
?>