/var/www/onesupportdemo.onesolution.hk/job/ajax.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
<?php
    
require_once(__DIR__ '/../checkuser.php');
    
session_start();
    global 
$dbh;

    if (
isPost()) {
        if (!
$dbh->beginTransaction()) {
            throw new 
Exception('mysql begin transaction failure.');
        }

        try {
            
$response "";

            if (
$_POST["scope"] == "task_history") {
                
$task_id = (int)$_POST["task_id"];

                
$data_array = array();

                
$sql        "select *, sup_job_detail.createdate as task_createdate, sup_job_detail.lastupdate as task_lastupdate from sup_job 
            INNER JOIN sup_job_detail ON sup_job_detail.job_id = sup_job.id
            where sup_job_detail.id = ?"
;
                
$parameters = array($task_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!');
                }

                
$i 0;
                if (!empty(
$job)) {
                    
$data_array[$i]["job_id"]           = $job["job_id"];
                    
$data_array[$i]["job_date"]         = !empty($job["date"])?$job["date"]:$job["call_date"];
                    
$data_array[$i]["job_type"]         = $job["job_type"];
                    
$data_array[$i]["staff_id"]         = empty($job["other_staff_id"])?$job["staff_id"]:$job["staff_id"].",".$job["other_staff_id"];
                    
$data_array[$i]["task_category_id"] = $job["task_category_id"];
                    
$data_array[$i]["task_title"]       = $job["title"];
                    
$data_array[$i]["task_remarks"]       = $job["remarks"];
                    
$data_array[$i]["task_createdate"]  = $job["task_createdate"];
                    
$data_array[$i]["task_lastupdate"]  = $job["task_lastupdate"];
                    
$i++;
                    
$from_job_pending_id $job["from_job_pending_id"];
                    while (!empty(
$from_job_pending_id)) {
                        
$sql2        "select *, sup_job_detail.createdate as task_createdate, sup_job_detail.lastupdate as task_lastupdate from sup_job_pending 
                                        INNER JOIN sup_job_detail ON sup_job_detail.id = sup_job_pending.job_detail_id
                                        INNER JOIN sup_job ON sup_job.id = sup_job_detail.job_id
                                        where sup_job_pending.id = ?"
;
                        
$parameters2 = array($from_job_pending_id);

                        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");
                        }

                        
$this_job $sth2->fetch(PDO::FETCH_ASSOC);

                        
$data_array[$i]["job_id"]           = $this_job["job_id"];
                        
$data_array[$i]["job_date"]         = !empty($job["date"])?$job["date"]:$job["call_date"];
                        
$data_array[$i]["job_type"]         = $this_job["job_type"];
                        
$data_array[$i]["staff_id"]         = empty($job["other_staff_id"])?$job["staff_id"]:$job["staff_id"].",".$job["other_staff_id"];
                        
$data_array[$i]["task_category_id"] = $this_job["task_category_id"];
                        
$data_array[$i]["task_title"]       = $this_job["title"];
                        
$data_array[$i]["task_remarks"]     = $this_job["remarks"];
                        
$data_array[$i]["task_createdate"]  = $this_job["task_createdate"];
                        
$data_array[$i]["task_lastupdate"]  = $this_job["task_lastupdate"];

                        
$from_job_pending_id $this_job["from_job_pending_id"];
                        
$i++;
                    }
                }

            }


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

            if (
$_POST["scope"] == "task_history") {

                
//task category
                
$sql5 "select task_category_id, category_name 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 * FROM sys_login WHERE deleted = ? ORDER BY username";
                
$parameters = array(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");
                }
                
$staffs $sth->fetchAll();

                foreach (
$staffs as $key => $staff) {
                    
$staffs_list[$staff["id"]] = $staff["username"];
                }

                
//$response = $data_array;
                
$response .= "<table class='table table-striped table-bordered'>
                            <th>Job ID</th>
                            <th>Job Type</th>
                            <th>Job Date</th>
                            <th>Staff</th>
                            <th>Task Category</th>
                            <th>Task Title</th>
                            <th style='width: 300px;'>Task Remarks</th>
                            <th style='width: 75px;;'>Task Date</th>
                            "
;
                foreach (
$data_array as $key => $item){
                    if(
$key == 0){
                        
$style "background: #c3c3c3";
                    }else{
                        
$style "";
                    }

                    
$response .= "<tr style='".$style."'>
                                    <td><a href='modifyform.php?id="
.$item["job_id"]."'>#".$item["job_id"]."</a> </td>
                                    <td>"
.$item["job_type"]."</td>
                                    <td>"
.$item["job_date"]."</td>";

                    
$response .= "<td>";
                    
$staff_array explode(","$item["staff_id"]);

                    foreach (
$staffs as $staff){
                        foreach (
$staff_array as $staff2){
                            if(
$staff['id'] == $staff2){
                                
$response .=  h($staff['username']).", ";
                            }
                        }
                    }

                    
$response substr_replace($response ,"",-2)."</td>";

                    
$task_category_name "";
                    foreach (
$task_categories as $task_category){
                        if(
$task_category["task_category_id"] == $item["task_category_id"]){
                            
$task_category_name $task_category["category_name"];
                            break;
                        }
                    }

                    
$response .= "<td>".$task_category_name."</td>
                                    <td>"
.$item["task_title"]."</td>
                                    <td>"
.$item["task_remarks"]."</td>
                                    <td>"
.$item["task_createdate"]."</td>
                                </tr>"
;
                }

                
$response .= "</table>";

                echo 
$response;
            }



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