/var/www/onesupportdemo.onesolution.hk/job/modify.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
<?php
require_once(__DIR__ '/../checkuser.php');
session_start();
//-----------------------------------------------------------------------------
// Save if POST method
//-----------------------------------------------------------------------------
if (isPost()) {
    if (!
$dbh->beginTransaction()) {
        throw new 
Exception('mysql begin transaction failure.');
    }
    try {
        unset(
$_SESSION["all_job_ids"]);

        
$post $_POST;
        unset(
$post['new_job_detail']);
        unset(
$post['job_detail']);
        
        
$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 (isset(
$post[$column])) {
                
$job[$column] = $post[$column];
            }
            
            if(
$column == "date"){ 
                
$newCol "today_selected_day";
                
var_dump($newCol$post[$newCol]);
                if (isset(
$post[$newCol])) {
                    
$job[$column] = $post[$newCol];
                }                    
            }            
            if(
$column == "start_time"){ 
                
$newCol "today_$column";
                
var_dump($newCol$post[$newCol]);
                if (isset(
$post[$newCol])) {
                    
$job[$column] = $post[$newCol];
                }                    
            }            
            if(
$column == "end_time"){ 
                
$newCol "today_$column";
                
var_dump($newCol$post[$newCol]);
                if (isset(
$post[$newCol])) {
                    
$job[$column] = $post[$newCol];
                }                    
            }            
//var_dump($job);            
            
        
}
            
        
// Append record time
        
$now date("Y-m-d H:i:s");
        
$job array_merge($job, array(
            
'lastupdate' => $now,
            
'lastupby' => $_SESSION['webadmin']['id'],
        ));
        
        
// Update job
        
$remark_job_id = array();
        
$values = array();
        
$parameters = array();
        foreach (
$job as $column => $value) {
            if (
$column != 'id') {

                if(
$column == "end_time"){
                    
//change the end time from 00 to 59
                    
$time_explode explode(':'$value);
                    
$minutes      $time_explode[0];
                    
$seconds      $time_explode[1];

                    if (
$seconds == "00" && $minutes != "00") {
                        
$minutes $minutes 1;
                        
$seconds "59";

                        
$value $minutes ":" $seconds;
                    }
                    
//end of change time
                
}

                if(
$column == "date" || $column == "start_time" || $column == "end_time"){ 
                }


                
$parameters[] = !strlen($value) ? null $value;
                
$values[] = "`$column` = ?";
            }


            if(
$column == "other_staff_id"){
                if(
count($value) > 0){
                    
$parameters[] = implode(","$value);
                    
$values[] = "`$column` = ?";
                }else{
                    
$parameters[] = "";
                    
$values[] = "`$column` = ?";
                }
            }
        }
        
$sql "UPDATE sup_job SET " implode(', '$values) . " WHERE id = ?";
        
        
        
$parameters[] = $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");
        }


//var_dump($sql, $parameters);        
var_dump("====================================================================="$_POST);
//throw new Exception("sql prepare statement failure: $sql");

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


        
//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 = '".$job['date']."' and job.start_time <= '".$job['end_time']."' and '".$job['start_time']."' <= job.end_time and job.staff_id = '".$job['staff_id']."' and job.id != '".$job['id']."' order by `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($parameters2)) {
            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"];
            }
        }




        
// Create details
        
Job::createDetails($_POST['new_job_detail']);

        
//exit;

        // Update details
        
$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();
        
Job::updateDetails($_POST['job_detail'], $job['details']);


        
//update period task
        //$period_task_ids = $_POST["period_task_id"];
        
$period_task_status $_POST["period_task_status"];
        
$period_task_remarks $_POST["period_task_remarks"];
        
$completed_date date("Y-m-d");

        foreach(
$period_task_remarks as $ptask_id => $remarks){ //looping all task id of that customer and update the task status

            
if($period_task_status[$ptask_id] == 2){    //period task change status to completed and record completed date

                
$sql "update sup_period_task set status=?,task_remarks=?,completed_date=?, lastupdate=?, lastupby=? where ptask_id=?";
                
$parameters2 = array($period_task_status[$ptask_id], $period_task_remarks[$ptask_id],$completed_date$now,$_SESSION['webadmin']['id'], $ptask_id);
            }else{
                
$sql "update sup_period_task set status=?,task_remarks=?, lastupdate=?, lastupby=? where ptask_id=?";
                
$parameters2 = array($period_task_status[$ptask_id], $period_task_remarks[$ptask_id], $now,$_SESSION['webadmin']['id'], $ptask_id);
            }

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

        }


        
//update follow up task
        
$pending_task_ids $_POST["pending_task_id"];
        
$pending_task_status $_POST["pending_task_status"];
        
$pending_task_title $_POST["pending_task_title"];
        
$pending_task_remarks $_POST["pending_task_remarks"];
        
$pending_task_category_id $_POST["pending_task_category_id"];

        foreach(
$pending_task_ids as $pending_task_id){

            
//update job detail status
            
$sql "update sup_job_detail set job_id=?, status=?, title=?, remarks=?, task_category_id=?, lastupdate=?, lastupby=? where id=?";

            
$parameters2 = array($job['id'], $pending_task_status[$pending_task_id], $pending_task_title[$pending_task_id], $pending_task_remarks[$pending_task_id], $pending_task_category_id[$pending_task_id], $now,$_SESSION['webadmin']['id'], $pending_task_id);

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

            
//update follow up task table
            
$sql "update sup_job_pending set actived=?, lastupdate=?, lastupby=? where job_detail_id=?";

            
$parameters3 = array("0"$now,$_SESSION['webadmin']['id'], $pending_task_id);

            if (!(
$sth $dbh->prepare($sql))) {
                throw new 
Exception("sql prepare statement failure: $sql");
            }
            
$sth->setFetchMode(PDO::FETCH_ASSOC);
            if (!
$sth->execute($parameters3)) {
                throw new 
Exception("sql execute statement failure: $sql");
            }

        }





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

    
$_SESSION["clash_job_array"] = $clash_job_array;
    
$_SESSION["clash_job_id_array"] = $clash_job_id_array;
    
$_SESSION["remark_job_id"] = $remark_job_id;

    
//redirectAndExit('index.php?message=Saved.');
    
$job_id = (int)$_POST["id"];
    
//redirectAndExit('modifyform.php?id='.$job_id.'&message=Saved.');

    
$startdate date("Y-m-d");
    
$enddate Date('Y-m-d'strtotime($startdate ."+1 month"));
    
redirectAndExit('index.php?start_date='.$startdate.'&end_date='.$enddate.'&message=Job Saved.');
}