/var/www/onesolution.com.hk/onesupport/cron_job/auto_email.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
    
require_once('../inc/configure.php');
    
//require_once(__DIR__ . '/../checkuser.php');

// Send job closed mail

$nowdate date("Y-m-d");
    
//$nowdate = date("2015-01-01");
    
$email_num 0;
    
$email_to_list = array();
    
$job_info_admin = array(array());

    
$sql5 "select * from sup_task_category where deleted = 0 order by sort ASC";
    if (!(
$sth5 $dbh->prepare($sql5))) {
        throw new 
Exception("sql prepare statement failure: $sql5");
    }

    if (!
$sth5->execute()) {
        throw new 
Exception("sql execute statement failure: $sql5");
    }
    
$task_categories $sth5->fetchAll();

    
$sql "SELECT job.*, staff.* FROM sup_job job, sys_login staff WHERE job.staff_id = staff.id and job.`date` = ? and job.actived = ? and job.deleted = ? group by job.staff_id order by staff.username ASC";

    
$parameters = array($nowdate"1""0");
    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_job_list $sth->fetchAll();

//generate the email content and send email to each staff who has jobs today
    
foreach($staff_job_list as $key => $staff_job){

        
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>

        <?php //foreach($staff_job_list as $key => $staff_job){ ?>

        <div>
        <p>This is a auto system e-mail for Job Date: <span style="font-size: 18px;font-weight: bold;"><?=date("Y-m-d H:i:s")?></span></p>
        <hr />
        <p>The following information is the job:</p>
        <table>
        <tbody>
        <?php
            $sql2 
"select job.*,customer.*  from sup_job job, v_cm_customer_support customer where job.customer_id = customer.cust_id and job.staff_id = ? and job.`date` = ? and job.actived = ? and job.deleted = ? order by job.start_time ASC";

            
$parameters2 = array($staff_job["staff_id"],$nowdate"1""0");
            if (!(
$sth2 $dbh->prepare($sql2))) {
                throw new 
Exception("sql prepare statement failure: $sql2");
            }
            
$sth2->setFetchMode(PDO::FETCH_ASSOC);
            if (!
$sth2->execute($parameters2)) {
                throw new 
Exception("sql execute statement failure: $sql2");
            }
            
$job_list $sth2->fetchAll();

            foreach(
$job_list as $key2 => $this_job){

                
$job_info_admin[$this_job["id"]]["username"] = $staff_job['username'];
                
$job_info_admin[$this_job["id"]]["company_name"] = $this_job["company_name"];
                
$job_info_admin[$this_job["id"]]["start_time"] = $this_job["start_time"];
                
$job_info_admin[$this_job["id"]]["end_time"] = $this_job["end_time"];

                
//looping the job of this staff in today
                /*if(($key2+1)%2 == 0)
                    $row_color = "style='background-color: #E0E0E0;'";
                else{*/
                    
$row_color "style='background-color: white;'";
                
//}
                
?>

                <tr valign="top" <?=$row_color?>>
                <td style="padding-right: 20px;border: 1px solid #000000;min-width: 400px;max-width: 600px;">
                    <table>
                        <tr>
                            <th>Jod ID</th>
                            <td>:</td>
                            <td><a href="http://www.onesolution.com.hk/onesupport/job/modifyform.php?id=<?=$this_job["id"]?>" target="_blank">#<?=$this_job["id"]?></a></td>
                        </tr>

                        <tr>
                            <th>Staff</th>
                            <td>:</td>
                            <td><?=h($staff_job['username'])?></td>
                        </tr>

                        <tr>
                            <th>Job Date</th>
                            <td>:</td>
                            <td><?php echo h($staff_job['date']);?></td>
                        </tr>

                        <?php if(empty($this_job["contract_id"]) && empty($this_job["project_id"])){ ?>
                            <tr>
                                <th>Job Type</th>
                                <td>:</td>
                                <td>Normal Job</td>
                            </tr>
                        <?php ?>

                        <?php if(!empty($this_job["contract_id"])){ ?>
                            <tr>
                                <th>Job Type</th>
                                <td>:</td>
                                <td>Contract Job (Monthly Job)</td>
                            </tr>
                        <?php ?>

                        <?php if(!empty($this_job["project_id"])){ ?>
                            <tr>
                                <th>Job Type</th>
                                <td>:</td>
                                <td>Project Job</td>
                            </tr>
                        <?php ?>

                        <tr>
                            <th valign="top" style="width: 110px;">Customer Name</th>
                            <td valign="top">:</td>
                            <td><?php echo $this_job["company_name"];?></td>
                        </tr>

                        <tr>
                            <th valign="top">Location</th>
                            <td valign="top">:</td>
                            <td style="word-wrap: break-word;width: 330px;">
                                <?php
                                    
if(empty($this_job["location"]))
                                        echo 
$this_job["billaddress"];
                                    else
                                        echo 
$this_job["location"];
                                
?>
                            </td>
                        </tr>

                        <?php
                            
if(!empty($this_job["project_id"])){
                                
$sql3 "select * from sup_project where id = ?";

                                if (!(
$sth3 $dbh->prepare($sql3))) {
                                    throw new 
Exception("sql prepare statement failure: $sql3");
                                }

                                if (!
$sth3->execute(array($this_job["project_id"]))) {
                                    throw new 
Exception("sql execute statement failure: $sql3");
                                }
                                
$project $sth3->fetch(PDO::FETCH_ASSOC);

                                
?>

                                <tr>
                                    <th>Project Name</th>
                                    <td>:</td>
                                    <td><?=$project["project_name"]?></td>
                                </tr>

                                <tr>
                                    <th>Project On-Site</th>
                                    <td>:</td>
                                    <td>
                                        <?php
                                            
if($this_job["on_site"] == 1)
                                                echo 
"Yes";
                                            else
                                                echo 
"No";
                                        
?>
                                    </td>
                                </tr>

                            <?php ?>

                        <tr>
                            <th>Start Time</th>
                            <td>:</td>
                            <td><?=$this_job["start_time"]?></td>
                        </tr>

                        <tr>
                            <th>End Time</th>
                            <td>:</td>
                            <td><?=$this_job["end_time"]?></td>
                        </tr>

                        <?php if(empty($this_job["project_id"])){ ?>
                            <tr>
                                <th>Call Type</th>
                                <td>:</td>
                                <td><?=Job::typeOptions2($this_job["type"]);?></td>
                            </tr>
                        <?php ?>

                        <tr>
                            <th>Remarks</th>
                            <td>:</td>
                            <td><?=$this_job["remarks"];?></td>
                        </tr>



                    </table>
                </td>

                <?php

                    
//select the tasks which are normal job or project job
                    
                        //1 to 1 connect
                        
$sql4 "select *, task.remarks as task_remarks, task.status as task_status from sup_job_detail task, sup_job job, sup_task_category task_category where task.task_category_id = task_category.task_category_id and task.job_id = job.id and job.id=?";

                        if (!(
$sth4 $dbh->prepare($sql4))) {
                            throw new 
Exception("sql prepare statement failure: $sql4");
                        }
                        if (!
$sth4->execute(array($this_job["id"]))) {
                            throw new 
Exception("sql execute statement failure: $sql4");
                        }
                        
$tasks $sth4->fetchAll();

                        foreach(
$tasks as $key3 => $task){
                            
$key3++;
                            
//looping each task
                            
?>
                            <td style="border: 1px solid #000000;min-width: 300px;max-width: 400px;">
                                <table>
                                    <tr>
                                        <th>#<?=$key3?></th>
                                        <th style="width: 105px;">Task Category</th>
                                        <td>:</td>
                                        <td><?=$task["category_name"]?></td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <th valign="top">Task Title</th>
                                        <td valign="top">:</td>
                                        <td style="width: 170px;"><?=$task["title"]?></td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <th>Task Remarks</th>
                                        <td colspan="2">:</td>
                                        <td></td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <td colspan="3" style="width:270px;"><?=$task["task_remarks"]?></td>
                                    </tr>

                                </table>
                            </td>

                        <?php }


                        
//for contract job monthly task


                        
$sql4 "select * from sup_period_task where customer_id = ? and deleted =? and status != ? and ? >= start_date and ? <= end_date order by start_date";
                        
$parameters = array($this_job["customer_id"], "0""3"date("Y-m-d"), date("Y-m-d"));

                        if (!(
$sth4 $dbh->prepare($sql4))) {
                            throw new 
Exception("sql prepare statement failure: $sql4");
                        }

                        if (!
$sth4->execute($parameters)) {
                            throw new 
Exception("sql execute statement failure: $sql4");
                        }
                        
$period_tasks $sth4->fetchAll();

                        foreach(
$period_tasks as $key3 => $ptask){
                            
$key3++;
                            
?>
                            <td style="border: 1px solid #000000;min-width: 300px;">
                                <table>
                                    <tr>
                                        <th>#<?=$key3?></th>
                                        <th style="width: 105px;">Task Category</th>
                                        <td>:</td>
                                        <td>
                                            <?php
                                                
foreach ($task_categories as $key4 => $task_category) {
                                                    if(
$ptask["task_category_id"] == $task_category["task_category_id"])
                                                        echo 
$task_category["category_name"];
                                                }
                                            
?>
                                            </td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <th valign="top">Task Title</th>
                                        <td valign="top">:</td>
                                        <td style="width: 170px;"><?=$ptask["task_title"]?></td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <th>Task Remarks</th>
                                        <td colspan="2">:</td>
                                        <td></td>
                                    </tr>

                                    <tr>
                                        <td></td>
                                        <td colspan="3" style="width:270px;"><?=$ptask["task_remarks"]?></td>
                                    </tr>

                                </table>
                            </td>
                        <?php
                        
}

                
?>

                </tr>
            <?php ?>

        </table>

        </div>

        <?php //} ?>

        </body>
        </html>
        <?php //}

        
$body ob_get_contents();
        
ob_end_clean();

        
$mailer createPHPMailer();
        
$mailer->WordWrap 50;
        
$mailer->From 'noreply@onesolution.com.hk';
        
$mailer->FromName 'One Solution Limited';
        
$email_addresses = array(

            
//'kelvinchan@onesolution.com.hk',

            //send email to staff
            
$staff_job["email"],

        );
        foreach (
$email_addresses as $email_address) {
            if (!empty(
$email_address)) {
                
$mailer->AddAddress($email_address);
            }
        }

        
$mailer->Subject 'One Support - '.$nowdate." - Job Announcement (".$staff_job["username"].")";
        
$mailer->Body $body;
        if (!
$mailer->Send()) {
            throw new 
Exception(sprintf('Send mail failure: %s'$mailer->ErrorInfo));

            
$sql5 "insert into email_log (email_type, email_to, email_sent, sent_time) values (?, ?, ?, ?)";

            if (!(
$sth5 $dbh->prepare($sql5))) {
                throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
            }

            
$parameters5 = array("Daily Job Email"$staff_job["username"], "0"date("Y-m-d H:i:s"));

            if (!
$sth5->execute($parameters5)) {
                throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
            }

        }else{
            
//send email successfully
            
$email_num++;
            
$email_to_list[] = $staff_job["username"];

            
$sql5 "insert into email_log (email_type, email_to, email_sent, sent_time) values (?, ?, ?, ?)";

            if (!(
$sth5 $dbh->prepare($sql5))) {
                throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
            }

            
$parameters5 = array("Daily Job Email"$staff_job["username"], "1"date("Y-m-d H:i:s"));

            if (!
$sth5->execute($parameters5)) {
                throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
            }


        }

    }

    
//email simple summary for admin
    
$mailer2 createPHPMailer();
    
$mailer2->WordWrap 50;
    
$mailer2->From 'noreply@onesolution.com.hk';
    
$mailer2->FromName 'One Solution Limited';
    
$email_addresses2 = array(
        
'samuel@onesolution.com.hk',
        
'jonathan@onesolution.com.hk',
        
'skycheng@onesolution.com.hk',
        
//'kelvinchan@onesolution.com.hk',


    
);
    foreach (
$email_addresses2 as $email_address) {
        if (!empty(
$email_address)) {
            
$mailer2->AddAddress($email_address);
        }
    }

    
$mailer2->AddBCC('kelvinchan@onesolution.com.hk');

    
$mailer2->Subject 'One Support - '.$nowdate." - Send Job Email";

    
/*$body2 = "<html><body>
    This is a auto system e-mail for Job Date: <b>".date("Y-m-d H:i:s")."</b><br><br>
    The system has been sent ".$email_num." email to staffs.<br><br>
    They are ".implode(", ",$email_to_list)."
    </body>
    </html>
    ";*/

    
$body2 "<html><body>
    This is a auto system e-mail for Job Date: <b>"
.date("Y-m-d H:i:s")."</b><br><br>
    The system has been sent "
.$email_num." email to staffs.<br><br>
    They are "
.implode(", ",$email_to_list).".<br><br>";

    
$body2 .= "<table border='1' cellpadding='8' style='border-collapse: collapse;'><tr>
                <th style='border: 1px solid black;'>Job ID</th>
                <th style='border: 1px solid black;'>Staff Name</th>
                <th style='border: 1px solid black;'>Start Time</th>
                <th style='border: 1px solid black;'>End Time</th>
                <th style='border: 1px solid black;'>Company Name</th>
                </tr>"
;

    foreach(
$job_info_admin as $key => $job_info){
        if(!empty(
$job_info)){
            
$body2 .= "<tr>
                        <td style='border: 1px solid black;'><a href='http://www.onesolution.com.hk/onesupport/job/modifyform.php?id="
.$key."' target='_blank'>#".$key."</a></td>
                        <td style='border: 1px solid black;'>"
.$job_info["username"]."</td>
                        <td style='border: 1px solid black;'>"
.$job_info["start_time"]."</td>
                        <td style='border: 1px solid black;'>"
.$job_info["end_time"]."</td>
                        <td style='border: 1px solid black;' >"
.$job_info["company_name"]."</td>
                    </tr>"
;
        }

    }

    
$body2 .= "</table></body></html>";
    
$mailer2->Body $body2;
    if (!
$mailer2->Send()) {
        throw new 
Exception(sprintf('Send mail failure: %s'$mailer2->ErrorInfo));

        
$sql5 "insert into email_log (email_type, email_to, email_sent, sent_time) values (?, ?, ?, ?)";

        if (!(
$sth5 $dbh->prepare($sql5))) {
            throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
        }

        
$parameters5 = array("Daily Job Email""Samuel, Jonathan, Sky""0"date("Y-m-d H:i:s"));

        if (!
$sth5->execute($parameters5)) {
            throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
        }

    }else{
        echo 
"Job Date: ".date("Y-m-d H:i:s")." cron job email sent successfully!";

        
$sql5 "insert into email_log (email_type, email_to, email_sent, sent_time) values (?, ?, ?, ?)";

        if (!(
$sth5 $dbh->prepare($sql5))) {
            throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
        }

        
$parameters5 = array("Daily Job Email""Samuel, Jonathan, Sky""1"date("Y-m-d H:i:s"));

        if (!
$sth5->execute($parameters5)) {
            throw new 
Exception('[' $sth5->errorCode() . ']: ' print_r($sth5->errorInfo()));
        }

    }


?>