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
|
<?php require_once(__DIR__ . '/../checkuser.php'); session_start(); //----------------------------------------------------------------------------- // Check permission //----------------------------------------------------------------------------- //if (!Util::isAdmin()) { // redirectAndExit('index.php?message=No permission!'); //}
//----------------------------------------------------------------------------- // Save if POST method //----------------------------------------------------------------------------- if (isPost()) {
if (!$dbh->beginTransaction()) { throw new Exception('mysql begin transaction failure.'); } try { $all_job_ids = array();
$sql = "SELECT column_name FROM information_schema.columns WHERE table_schema = (SELECT DATABASE()) AND table_name = ?"; $parameters = array('sup_job'); if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } $sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute($parameters)) { throw new Exception("sql execute statement failure: $sql"); } $columns = $sth->fetchAll();
foreach ($columns as $column) {
$column = $column['column_name'];
//if($column != "start_time" || $column != "end_time"){ //do not need to insert time data, it will be update later if (isset($_POST[$column])) { $job[$column] = $_POST[$column]; } //}
}
if (!Util::isAdmin()) { // Copy actual time if (empty($job['actual_start_time'])) { $job['actual_start_time'] = $job['start_time']; } if (empty($job['actual_end_time'])) { $job['actual_end_time'] = $job['end_time']; } }
$sql = "SELECT * FROM v_cm_customer_support V_CM_CUSTOMER_SUPPORT WHERE cust_id = ?"; $parameters = array($job['customer_id']); if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } $sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute($parameters)) { throw new Exception("sql execute statement failure: $sql"); } $customer = $sth->fetch(PDO::FETCH_ASSOC);
$job['email1'] = $customer['email'];
// Append record time $now = date("Y-m-d H:i:s");
$job = array_merge($job, array( 'createdate' => $now, 'createby' => $_SESSION['webadmin']['id'], 'lastupdate' => $now, 'lastupby' => $_SESSION['webadmin']['id'], ));
// Create job $columns = array(); $values = array(); $parameters2 = array();
foreach ($job as $column => $value) { $columns[] = $column; if ($column == "other_staff_id" && count($value) > 0) { $parameters2[] = implode(",", $value); $values[] = '?'; } else { $parameters2[] = !strlen($value) ? null : $value; $values[] = '?'; } }
$remark_job_id = array();
if ($_POST["is_today"] == 1) {//for only today use
$sql = "INSERT sup_job (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $values) . ")"; if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } $sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute($parameters2)) { throw new Exception("sql execute statement failure: $sql"); }
$lastinsertid = $dbh->lastInsertId(); $remark_job_id[] = $lastinsertid;
//need to link the new contract job with the existing monthly task if (isset($_POST["contract_id"]) && $_POST["contract_type"] == 1) {
$_date = $_POST["today_selected_day"]; $_date_year = date("Y", strtotime($_date)); $_date_month = date("n", strtotime($_date));
//check the create job date is within the contract period or not $sql3 = "select * from sup_contract where contract_id = ?"; if (!($sth3 = $dbh->prepare($sql3))) { throw new Exception("sql prepare statement failure: $sql3"); }
if (!$sth3->execute(array((int)$_POST["contract_id"]))) { throw new Exception("sql execute statement failure: $sql3"); }
$row3 = $sth3->fetch(PDO::FETCH_ASSOC); //created new contract job should not later than contract to if ($row3["contract_to"] < $_date) { redirectAndExit('index.php?message=Job Cannot Create. The contract job date should not before the date of contract to.'); }
}
//record all job id $all_job_ids[] = $lastinsertid;
$_staff_id = (int)$_POST["staff_id"];
$_date = $_POST["today_selected_day"]; $_start_time = $_POST["today_start_time"]; $_end_time = $_POST["today_end_time"];
//change the end time from 00 to 59 $time_explode = explode(':', $_end_time); $minutes = $time_explode[0]; $seconds = $time_explode[1];
if ($seconds == "00" && $minutes != "00") { $minutes = $minutes - 1; $seconds = "59";
$_end_time = $minutes . ":" . $seconds; } //end of change time
if (!empty($_date) && !empty($_start_time) && !empty($_end_time)) { //checking whether the staff already has job on that time $sql = "SELECT job.*,customer.* FROM sup_job job,v_cm_customer_support customer WHERE job.customer_id = customer.cust_id and job.date = ? and job.start_time <= ? and ? <= job.end_time and job.staff_id = ? order by job.date ASC";
//$parameters2 = array(0); if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } $sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute(array($_date, $_end_time, $_start_time, $_staff_id))) { throw new Exception("sql execute g failure: $sql"); }
$check_job_time = $sth->fetchAll();
if (!empty($check_job_time)) { //staff time clash foreach ($check_job_time as $check) { $clash_job_array[] = $check["id"] . " (" . $check["company_name"] . ")"; $clash_job_id_array[] = $check["id"]; } }
}
if (empty($_POST["location"])) { if (!empty($customer["billaddress"])) { $location = $customer["billaddress"]; } else { $location = $customer["shipaddress"]; } } else { $location = $_POST["location"]; }
$sql = "UPDATE sup_job SET `date`=?,start_time=?, end_time=?, location=? WHERE id = ?";
if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } $sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute(array($_date, $_start_time, $_end_time, $location, $lastinsertid))) { throw new Exception("sql execute statement failure: $sql"); }
} else { $start_time = $_POST["start_time1"]; $end_time = $_POST["end_time1"]; $selected_day = $_POST["selected_day"]; $arraynum = $_POST["arraynum"];
$_staff_id = (int)$_POST["staff_id"];
$clash_array = array();
foreach ($arraynum as $key => $rownum) { $sql = "INSERT sup_job (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $values) . ")"; if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } //$sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute($parameters2)) { throw new Exception("sql execute statement failure: $sql"); } $lastinsertid = $dbh->lastInsertId(); $remark_job_id[] = $lastinsertid;
//need to link the new contract job with the existing monthly task if (isset($_POST["contract_id"]) && $_POST["contract_type"] == 1) { $_date = $selected_day[$rownum]; $_date_year = date("Y", strtotime($_date)); $_date_month = date("n", strtotime($_date));
//check the create job date is within the contract period or not $sql3 = "select * from sup_contract where contract_id = ?"; if (!($sth3 = $dbh->prepare($sql3))) { throw new Exception("sql prepare statement failure: $sql3"); }
if (!$sth3->execute(array((int)$_POST["contract_id"]))) { throw new Exception("sql execute statement failure: $sql3"); }
$row3 = $sth3->fetch(PDO::FETCH_ASSOC); //created new contract job should not later than contract to if ($row3["contract_to"] < $_date) { redirectAndExit('index.php?message=Job Cannot Create. The contract job date should not before the date of contract to.'); }
}
//record all job id $all_job_ids[] = $lastinsertid;
$_date = $selected_day[$rownum]; $_start_time = $start_time[$rownum]; $_end_time = $end_time[$rownum];
//change the end time from 00 to 59 $time_explode = explode(':', $_end_time); $minutes = $time_explode[0]; $seconds = $time_explode[1];
if ($seconds == "00" && $minutes != "00") { $minutes = $minutes - 1; $seconds = "59";
$_end_time = $minutes . ":" . $seconds; } //end of change time
if (!empty($_date) && !empty($_start_time) && !empty($_end_time)) { //checking whether the staff already has job on that time $sql = "SELECT job.*,customer.* FROM sup_job job,v_cm_customer_support customer WHERE job.customer_id = customer.cust_id and job.date = ? and job.start_time <= ? and ? <= job.end_time and job.staff_id = ? order by job.date ASC";
//$parameters2 = array(0); if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } $sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute(array($_date, $_end_time, $_start_time, $_staff_id))) { throw new Exception("sql execute statement failure: $sql"); }
$check_job_time = $sth->fetchAll();
if (!empty($check_job_time)) { //staff time clash foreach ($check_job_time as $check) { $clash_job_array[] = $check["id"] . " (" . $check["company_name"] . ")"; $clash_job_id_array[] = $check["id"]; } }
}
//update the previous record with correct time if (empty($_POST["location"])) { if (!empty($customer["billaddress"])) { $location = $customer["billaddress"]; } else { $location = $customer["shipaddress"]; } } else { $location = $_POST["location"]; }
$sql = "UPDATE sup_job SET `date`=?,start_time=?, end_time=?, location=? WHERE id = ?";
if (!($sth = $dbh->prepare($sql))) { throw new Exception("sql prepare statement failure: $sql"); } $sth->setFetchMode(PDO::FETCH_ASSOC); if (!$sth->execute(array($_date, $_start_time, $_end_time, $location, $lastinsertid))) { throw new Exception("sql execute statement failure: $sql"); }
}
}
if (!$dbh->commit()) { throw new Exception('mysql commit transaction failure.'); }
$_SESSION["clash_job_array"] = $clash_job_array; $_SESSION["clash_job_id_array"] = $clash_job_id_array; $_SESSION["remark_job_id"] = $remark_job_id;
$job['id'] = $lastinsertid; //$job['id'] = $firstinsertid;
$data = array( //'job_id' => $job['id'], 'id' => $job['id'], );
$_SESSION["all_job_ids"] = $all_job_ids;
$keyValues = $_POST['job_detail']; Job::createDetails($keyValues);
redirectAndExit(Util::link(__DIR__ . '/modifyform.php') . '?' . http_build_query($data));
/*if ($job['project_id'] || $job["contract_id"]) { //no need to add task in the first time
// $startdate = date("Y-m-d"); // $enddate = Date('Y-m-d', strtotime($startdate ."+1 month")); redirectAndExit(Util::link(__DIR__ . '/../task/addform.php') . '?' . http_build_query($data)); } else { redirectAndExit(Util::link(__DIR__ . '/../task/addform.php') . '?' . http_build_query($data)); //redirectAndExit(Util::link(__DIR__ . '/../task/addform.php')); }*/
} catch (Exception $exception) { if (!$dbh->rollBack()) { throw new Exception('mysql roll back transaction failure.'); } throw $exception; }
}
|