/var/www/onesupportdemo.onesolution.hk/job/status.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
<?php
error_reporting
(E_ALL);
ini_set('display_errors'1);
session_start();

require_once(
__DIR__ '/../checkuser.php');
if (
isPost() && isset($_POST['id'], $_POST['status'])) {
extract($_POST);

$sql "SELECT * FROM sup_job WHERE id = ?";
$parameters = array($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");
}
$job $sth->fetch(PDO::FETCH_ASSOC);
if (empty(
$job)) {
    throw new 
Exception('Job not found.');
}

$sql "SELECT * FROM sup_job_detail WHERE job_id = ? ORDER BY id";
$parameters = array($job['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");
}
$job['details'] = $sth->fetchAll();

$sql "SELECT * FROM sys_login WHERE id = ?";
$parameters = array($job['staff_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");
}
$staff $sth->fetch(PDO::FETCH_ASSOC);

//20130815 send email to engine
$sql "SELECT * FROM sys_login WHERE id = ?";
$parameters = array($staff['parentid']);
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");
}
$engine $sth->fetch(PDO::FETCH_ASSOC);

$sql "SELECT * FROM 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_type_options Job::typeOptions();
$job_detail_status_options JobDetail::statusOptions();

if (
$job['status'] != $status) {
// Validation
$errors = array();
if (
$status == 2) {
    if (!
Job::allow_change_status_to_confirm($job['id'])) {
        
$errors['status'] = 'Not allow change status to confirm. Because there are some details status still is OPEN.';
    }
}

$send_job_closed_mail false;
if (!empty(
$errors)) {
    
print_r($errors);
    exit;
} else {
if (!
$dbh->beginTransaction()) {
    throw new 
Exception('mysql begin transaction failure.');
}
try {
    
$sql "UPDATE sup_job SET status = ?, lastupdate = ?, lastupby = ? WHERE id = ?";
    
$parameters = array($statusdate('Y-m-d H:i:s'), $_SESSION['webadmin']['id'], $job['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");
    }

    switch (
$status) {
        case 
1:{

        } 
// Open
            
break;
        case 
3// Void
        
{
            
// Delete pending job detail
            
$sql "UPDATE sup_job_pending SET deleted = ?, lastupdate = ?, lastupby = ? WHERE job_id = ? AND actived = ? AND deleted = ?";
            
$parameters = array(1date('Y-m-d H:i:s'), $_SESSION['webadmin']['id'], $job['id'], 10);
            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");
            }

            
$send_job_closed_mail true;

        }
            break;

        case 
2// Close
        
{
            
$sql "SELECT * FROM sup_job_detail WHERE job_id = ? ORDER BY id";
            
$parameters = array($job['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");
            }
            
$job['details'] = $sth->fetchAll();

            
// Create pending job detail
            
foreach ($job['details'] as $detail) {
                
// Filter status: follow up
                
if ($detail['status'] != 3) {
                    continue;
                }

                
$attributes $detail;

                
// remove genernal fields
                
$genernal_fields = array('id''createby''createdate''lastupby''lastupdate''actived''deleted');
                foreach (
$genernal_fields as $field) {
                    unset(
$attributes[$field]);
                }
                
// remove special fields
                
$special_fields = array('status''from_job_pending_id''task_category_id''follow_up_flag');
                foreach (
$special_fields as $field) {
                    unset(
$attributes[$field]);
                }

                
// Append record time
                
$now date("Y-m-d H:i:s");
                
$attributes array_merge($attributes, array(
                    
'createdate' => $now,
                    
'createby' => $_SESSION['webadmin']['id'],
                    
'lastupdate' => $now,
                    
'lastupby' => $_SESSION['webadmin']['id'],
                ));

                
// Append values
                
$attributes array_merge($attributes, array(
                    
'actived' => 1,
                    
'deleted' => 0,
                    
'job_detail_id' => $detail['id'],
                    
'jpend_customer_id' => $job['customer_id'],

                ));

                
// create pending job detail
                
$columns = array();
                
$values = array();
                
$parameters = array();
                foreach (
$attributes as $column => $value) {
                    
$columns[] = $column;
                    
$parameters[] = !strlen($value) ? null $value;
                    
$values[] = '?';
                }
                
$sql "INSERT sup_job_pending (" 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($parameters)) {
                    throw new 
Exception("sql execute statement failure: $sql");
                }
            }
            
$send_job_closed_mail true;
        }
            break;
    }

    if (!
$dbh->commit()) {
        throw new 
Exception('mysql commit transaction failure.');
    }
} catch (
Exception $exception) {
    if (!
$dbh->rollBack()) {
        throw new 
Exception('mysql roll back transaction failure.');
    }
    throw 
$exception;
}

if (
$send_job_closed_mail) {
// Send job closed mail
ob_start();


?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>One Solution: Support</title>
    <meta name="author" content="One Solution Limited" />

    <style type="text/css">
        th { text-align: left; }
        .green { color: green; }
        .red { color: red; }
        .yellow { color: gold; }
    </style>
</head>
<body>
<div>
    <p>This is a prototype e-mail, it will not send to client currently. If any comments, welcome to contact program team!</p>
    <hr />
    <p>This is a system e-mail, please do not reply.</p>
    <p>The following information is the job,</p>
    <table>
        <tbody>
        <tr>
            <th>Job</th>
            <td>#<?=h($job['id'])?></td>
        </tr>
        <tr>
            <th>Customer</th>
            <td><?=h($customer['company_name'])?></td>
        </tr>
        <tr>
            <th>Staff</th>
            <td><?=h($staff['username'])?></td>
        </tr>
        <tr>
            <th>Date</th>
            <td><?=h($job['date'])?></td>
        </tr>
        <tr>
            <th>Actual Start Time</th>
            <td><?=h(Util::value_to_time_string($job['actual_start_time']))?></td>
        </tr>
        <tr>
            <th>Actual End Time</th>
            <td><?=h(Util::value_to_time_string($job['actual_end_time']))?></td>
        </tr>
        <tr>
            <th>Type</th>
            <td><?=h($job_type_options[$job['type']])?></td>
        </tr>
        <tr>
            <th>Status</th>
            <td>
                <?php
                    
//get new status
                    
$sql2 "SELECT * FROM sup_job WHERE id = ?";
                    
$parameters2 = array($id);
                    if (!(
$sth $dbh->prepare($sql2))) {
                        throw new 
Exception("sql prepare statement failure: $sql2");
                    }
                    
$sth->setFetchMode(PDO::FETCH_ASSOC);
                    if (!
$sth->execute($parameters2)) {
                        throw new 
Exception("sql execute statement failure: $sql2");
                    }
                    
$job2 $sth->fetch(PDO::FETCH_ASSOC);


                    
$statusOptions Job::statusOptions();
                    echo 
$statusOptions[$job2['status']];
                
?>
            </td>
        </tr>
        <tr>
            <th>Remarks</th>
            <td><?=nl2br(h($job['remarks']))?></td>
        </tr>
        </tbody>
    </table>
    <hr />
    <p>The following information are the job's tasks,</p>
    <table>
        <thead>
        <tr>
            <th>Status</th>
            <th>Title</th>
            <th>Remarks</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($job['details'] ?: array() as $job_detail): ?>
            <tr>
                <td>
                    <?php
                        $color 
null;
                        switch (
$job_detail['status']) {
                            case 
1: { $color 'background-color:#F4FA58;'; break; } // Open
                            
case 2: { $color 'background-color:#CEECF5;'; break; } // Completed
                            
case 3: { $color 'background-color:#F4FA58;'; break; } // Follow up
                            
case 4: { $color 'background-color:#CEECF5;'; break; } // Cancel
                        
}
                    
?>
                    <span<?=empty($color) ? '' sprintf(' style="%s"'$color)?>><?=h($job_detail_status_options[$job_detail['status']])?></span>
                </td>
                <td><?=h($job_detail['title'])?></td>
                <td><?=nl2br(h($job_detail['remarks']))?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <p>Thank you for your attention!</p>
    <p>
        Regards,<br />
        One Solution Limited
    </p>
</div>
</body>
</html>
<?php
        $body 
ob_get_contents();
        
ob_end_clean();

        
$mailer createPHPMailer();
        
$mailer->WordWrap 0;
        
$mailer->From 'noreply@onesolution.com.hk';
        
$mailer->FromName 'noreply';
        
$email_addresses = array(
            
'samuel@onesolution.com.hk',
            
'jonathan@onesolution.com.hk',
            
//'kelvinchan@onesolution.com.hk',
            //sprintf('%s@onesolution.com.hk', $staff['loginname'])
            //$engine['email'],
            //$staff['email'],

//                    $job['email1'],
//                    $job['email2'],
        
);
        foreach (
$email_addresses as $email_address) {
            if (!empty(
$email_address)) {
                
$mailer->AddAddress($email_address);
            }
        }

    
//get new status
    
$sql2 "SELECT * FROM sup_job WHERE id = ?";
    
$parameters2 = array($id);
    if (!(
$sth $dbh->prepare($sql2))) {
        throw new 
Exception("sql prepare statement failure: $sql2");
    }
    
$sth->setFetchMode(PDO::FETCH_ASSOC);
    if (!
$sth->execute($parameters2)) {
        throw new 
Exception("sql execute statement failure: $sql2");
    }
    
$job2 $sth->fetch(PDO::FETCH_ASSOC);

    if(
$job2['status'] == 2){
        
$mailer->Subject 'One Support - Job Closed Notification';
    }

    if(
$job2['status'] == 3){
        
$mailer->Subject 'One Support - Job Void Notification';
    }


        
$mailer->Body $body;
        if (!
$mailer->Send()) {
            throw new 
Exception(sprintf('Send mail failure: %s'$mailer->ErrorInfo));
        }


    }

    }

    
$sql "SELECT * FROM sup_job WHERE id = ?";
    
$parameters = array($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");
    }
    
$job $sth->fetch(PDO::FETCH_ASSOC);
    }

    
$statusOptions Job::statusOptions();

    
$_SESSION["remark_job_id"] = $job['id'];

    
$startdate date("Y-m-d");
    
$enddate Date('Y-m-d'strtotime($startdate ."+1 month"));

    
$data = array(
        
'start_date' => $startdate,
        
'end_date' => $enddate,
        
'id' => $job['id'],
        
'message' => 'Changed status to ' $statusOptions[$job['status']] . '.',
    );
    
//redirectAndExit('modifyform.php?'.http_build_query($data));
    
redirectAndExit('index.php?'.http_build_query($data));


    }