/var/www/onesupportsys.onesolution.hk/report/print_JobListByStaff.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
<?php
    
include '../inc/configure.php';
    
date_default_timezone_set('Asia/Hong_Kong');

    
error_reporting(E_ALL E_NOTICE E_WARNING);
    
ini_set('display_errors'1);

// Check if the user is logged in

    // Check permission
    
if (!Util::isAdmin()) {
        
redirectAndExit(Util::link(__DIR__ '/../main.php'));
    }

    require_once 
"phpexcel/PHPExcel.php";
    require_once 
'func_tcpdf.php';

    
/*--------------------------------END OF FUNCTIONS--------------------------------------*/


    
$staff_id    = isset($_POST["_staff_id"]) ? $_POST["_staff_id"] : null;
    
$start_date  = isset($_POST["_start_date"]) ? $_POST["_start_date"] : null;
    
$end_date    = isset($_POST["_end_date"]) ? $_POST["_end_date"] : null;


    
$sql '
select j.id, c.company_name, j.call_date, j.call_num,  
        CASE
        WHEN j.status = 1 THEN concat("Schedule Visit"      , " ( ", j.call_num, " )")
        WHEN j.status = 2 THEN concat("Service Call"        , " ( ", j.call_num, " )")
        WHEN j.status = 3 THEN concat("Urgent Call"          , " ( ", j.call_num, " )")
        WHEN j.status = 4 THEN concat("Remote Service Call" , " ( ", j.call_num, " )")
        ELSE ""
        END as serviceType,   
        j.date, j.actual_start_time, j.actual_end_time,d.remarks, 
        CASE
        WHEN j.type = 1 THEN "Open"
        WHEN j.type = 2 THEN "Completed"
        WHEN j.type = 3 THEN "Follow up"
        WHEN j.type = 4 THEN "Cancel"
        ELSE ""
        END as taskStatus, j.status, j.type, u.userName as user_name, 
        SUBTIME(actual_end_time, actual_start_time) as manTime
from sup_job j,  v_cm_customer_support c, sup_job_detail d, sys_login u
 where j.customer_id = c.cust_id
  and j.id = d.job_id
  and j.deleted = 0 and d.deleted = 0 
  and j.staff_id = u.id
  and (COALESCE(LENGTH(?), 0) = 0 OR j.staff_id = ?)
    AND (COALESCE(LENGTH(?), 0) = 0 OR `date` >= ?)
    AND (COALESCE(LENGTH(?), 0) = 0 OR `date` <= ?)
    order by userName, j.id, date
'
;
    
$parameters = array(
        
$staff_id$staff_id,
        
$start_date$start_date,
        
$end_date$end_date,
    );

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

    
$result $sth->fetchAll();



$sql "select sum(call_num) as callnum FROM sup_job
  where type = ?
    and (COALESCE(LENGTH(?), 0) = 0 OR staff_id = ?)
    AND (COALESCE(LENGTH(?), 0) = 0 OR `date` >= ?)
    AND (COALESCE(LENGTH(?), 0) = 0 OR `date` <= ?)
"
;
    
$parameters = array(
        
1,
        
$staff_id$staff_id,
        
$start_date$start_date,
        
$end_date$end_date,
    );

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

    
$type1 $sth->fetchAll();

    
$parameters[0] = 2;

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

    
$type2 $sth->fetchAll();


$sql "select SEC_TO_TIME(sum(d)) as manHour from (
    SELECT     TIME_TO_SEC(actual_end_time) - TIME_TO_SEC(actual_start_time) as d
    FROM sup_job 
  where type = ?
    and (COALESCE(LENGTH(?), 0) = 0 OR staff_id = ?)
    AND (COALESCE(LENGTH(?), 0) = 0 OR `date` >= ?)
    AND (COALESCE(LENGTH(?), 0) = 0 OR `date` <= ?)
    ) q      
"
;
$parameters[0] = 3;

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

    
$type3 $sth->fetchAll();
    
    
$parameters[0] = 4;

    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");
    }
    
    
$type4 $sth->fetchAll();

    
//var_dump($abc[0]["manhour"]);
    //exit;



    
$staffName "All";
    
    if ( !empty(
$staff_id) ) {
        
$staffName $result[0]["user_name"];
    } 


    
$defaultFontface 'Times New Roman';

    @
set_time_limit(0);

    
ini_set('post_max_size''10M');
    
ini_set('memory_limit''1024M');
    
$showimg true;

    
$objPHPexcel PHPExcel_IOFactory::load('phpexcel/JobListByStaff.xls');
    
$objWorksheet $objPHPexcel->getSheetByName('JobListByStaff');

    
$lastColumn "";
    if ( empty(
$staff_id) ) {
        
$objWorksheet->removeRow(81);
        
$lastColumn "I";
    } else {
        
$objWorksheet->removeRow(91);
        
$lastColumn "H";
    }
    

    
$rownum 3;

    
$objWorksheet->getCell('B' $rownum)->setValue$start_date  " To " $end_date);
    
    
$rownum += 2;
    
$objWorksheet->getCell('B' $rownum)->setValue($staffName);
    
    
$rownum += 4;
    
$objWorksheet->getCell('A' $rownum)->setValue("Start");


    
$count_all_job 0;
    
$selected_job_ids = array();
    
    
//$scheduleCall = 0;
    //$serviceCall = 0;
    //$urgentCall = 0;
    //$remoteCall = 0;    
    //$projectCall = 0;    
    
    
    
$serviceType "";    
    
$taskStatus "";    
    
    
$count 0;    
    
$saveId 0;

//$objWorksheet->getCell("E3")->setValue("manhour");
//$objWorksheet->getCell("F3")->setValue($abc[0]["manhour"]);
    
    
$startRow $firstRow $rownum;
    foreach (
$result as $row) {
        
$count = (int)$row["call_num"];
        
$curId = (int)$row["id"];
        switch (
$row["type"]) {
          case 
1:
            
$serviceType "Schedule Call" " ( " $row["call_num"] . " )";
            
//$scheduleCall += $id <> $curId ? $count : 0;
            
break;
          case 
2:
            
$serviceType "Service Call" " ( " $row["call_num"] . " )";
            
//$serviceCall += $id <> $curId ? $count : 0;
            
break;
          case 
3:
            
$serviceType "Urgent Call" " ( " $row["call_num"] . " )";
            
//$urgentCall += $id <> $curId ? $count : 0;
            
break;
          case 
4:
            
$serviceType "Remote Call" " ( " $row["call_num"] . " )";
            
//$remoteCall += $id <> $curId ? $count : 0;
            
break;
          default:
            
//nothing in this case
        
};        
        switch (
$row["type"]) {
          case 
1:
            
$taskStatus "Open";
            break;
          case 
2:
            
$taskStatus "Completed";
            break;
          case 
3:
            
$taskStatus "Follow up";
            break;
          case 
4:
            
$taskStatus "Cancel";
            break;
          default:
            
//nothing in this case
        
};                
        
        if ( !empty(
$staff_id) ) { 
            
$objWorksheet->getCell("A" $rownum)->setValue($row["company_name"]);
            
$objWorksheet->getCell("B" $rownum)->setValue($row["call_date"]);
            
$objWorksheet->getCell("C" $rownum)->setValue($serviceType);
            
$objWorksheet->getCell("D" $rownum)->setValue($row["date"]);
            
$objWorksheet->getCell("E" $rownum)->setValue($row["actual_start_time"]);
            
$objWorksheet->getCell("F" $rownum)->setValue($row["actual_end_time"]);
            
$objWorksheet->getCell("G" $rownum)->setValue($row["remarks"]);
            
$objWorksheet->getCell("H" $rownum)->setValue($taskStatus);
        } else {
            
$objWorksheet->getCell("A" $rownum)->setValue($row["user_name"]);
            
$objWorksheet->getCell("B" $rownum)->setValue($row["company_name"]);
            
$objWorksheet->getCell("C" $rownum)->setValue($row["call_date"]);
            
$objWorksheet->getCell("D" $rownum)->setValue($serviceType);
            
$objWorksheet->getCell("E" $rownum)->setValue($row["date"]);
            
$objWorksheet->getCell("F" $rownum)->setValue($row["actual_start_time"]);
            
$objWorksheet->getCell("G" $rownum)->setValue($row["actual_end_time"]);
            
$objWorksheet->getCell("H" $rownum)->setValue($row["remarks"]);
            
$objWorksheet->getCell("I" $rownum)->setValue($taskStatus);
        }
        
        
$rownum++;
        
$id $curId;
    }
    
$endRow $rownum 1;
    
    
$objWorksheet->getStyle('A' $startRow ':'$lastColumn $endRow)->applyFromArray(
        array(
            
'borders' => array(
                
'allborders' => array(
                    
'style' => PHPExcel_Style_Border::BORDER_THIN,
                    
'color' => array('argb' => '766f6e')
                    )
                )
            )
        );    
    
// $objWorksheet->getCell("j" . 1)->setValue('A' . $startRow . ':'. $lastColumn . $endRow);        
    // $font = new PHPExcel_Style_Font();        
    // $font->setSize(12);    
    // $font->setName('Times New Roman');
    // $objWorksheet->getStyle('A' . $startRow . ':'. $lastColumn . $endRow)->getFont()->applyFromArray(
    //     array(
    //         'font' => $font
    //         )
    //     );        
    
//$objWorksheet->getStyle('A' . $startRow . ':'. $lastColumn . $endRow)->getFont()->setName('Times New Roman');    
    
    
$rownum += 2;
    
$startRow $rownum;
    
$objWorksheet->mergeCells('A' $rownum ':B' $rownum);
    
$objWorksheet->getStyle('A' $rownum ':B' $rownum)->getFont()->setBoldtrue );
    
    
$objWorksheet->getCell('A' $rownum)->setValue("Service Call Count :");
    
$rownum++;
    
$objWorksheet->getCell('A' $rownum)->setValue("Schedule Call :");
    
// $objWorksheet->getCell('B' . $rownum)->setValue($scheduleCall);
    
$objWorksheet->getCell('B' $rownum)->setValue($type1[0]["callnum"]);
    
$rownum++;
    
$objWorksheet->getCell('A' $rownum)->setValue("Service Call :");
    
//$objWorksheet->getCell('B' . $rownum)->setValue($serviceCall);
    
$objWorksheet->getCell('B' $rownum)->setValue($type2[0]["callnum"]);
    
$rownum++;
    
$objWorksheet->mergeCells('A' $rownum ':B' $rownum);
    
    
$objWorksheet->getCell('A' $rownum)->setValue("Man Hours :");
    
$rownum++;
    
$objWorksheet->getCell('A' $rownum)->setValue("Remote Call :");
    
$objWorksheet->getCell('B' $rownum)->setValue($type3[0]["manhour"]);
    
$rownum++;
    
$objWorksheet->getCell('A' $rownum)->setValue("Urgent Call :");
    
$objWorksheet->getCell('B' $rownum)->setValue($type4[0]["manhour"]);
    
$rownum++;
    
$objWorksheet->getCell('A' $rownum)->setValue("Project Call :");
    
$objWorksheet->getCell('B' $rownum)->setValue($projectCall);
    
$endRow $rownum;

    
$startRow ++;
    
    
    
$objWorksheet->getStyle('A' $startRow ':B' $endRow)->applyFromArray(
        array(
            
'borders' => array(
                
'allborders' => array(
                    
'style' => PHPExcel_Style_Border::BORDER_THIN,
                    
'color' => array('argb' => '766f6e')
                    )
                )
            )
        );
        
            
        
    foreach (
range('A'$lastColumn ) as $letra) {            
        
$objWorksheet->getColumnDimension($letra)->setAutoSize(true);
    }

    foreach (
range(1$endRow ) as $letra) {            
        
$objWorksheet->getRowDimension($letra)->setRowHeight(22);
    }
    
$objWorksheet->getRowDimension(1)->setRowHeight(35);
    
$objWorksheet->getRowDimension(8)->setRowHeight(32);
    
    
header('Content-Type: application/vnd.ms-excel');

    if (
date("m"strtotime($start_date)) == date("m"strtotime($end_date)))
        
header('Content-Disposition: attachment;filename="JobListByStaff_' date("M"strtotime($start_date)) . '.xls"');
    else
        
header('Content-Disposition: attachment;filename="JobListByStaff_' date("M"strtotime($start_date)) . '_to_' date("M"strtotime($end_date)) . '.xls"');

    
header('Cache-Control: max-age=0');

    
$objPHPexcel->setActiveSheetIndex(0);
    
$objWriter PHPExcel_IOFactory::createWriter($objPHPexcel'Excel5');
    
$objWriter->save('php://output');

    
flush();

?>