/var/www/(Del)eizo.hkosl.com/global/webadmin/import_customer_warranty_post.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
<?php
    
include 'config.php';
    
// Check if the user is logged in
    
if ((!isSet($_SESSION['loginname'])) || ($loggin <> '1')) {
        
header("Location: login.php");
        exit;
    }

    
$nowdate date("Y-m-d H:i:s");
    if (!empty(
$_POST["excelfile_name"])) {

        function 
validateDate($date$format 'Y-m-d H:i:s')
        {
            
$d DateTime::createFromFormat($format$date);
            return 
$d && $d->format($format) == $date;
        }


        
//data decryption
        //include_once('../phpseclib0.3.6/Crypt/RSA.php');
        
set_include_path(get_include_path() . PATH_SEPARATOR 'phpseclib0.3.6');

        include(
'Crypt/RSA.php');

        
$rsa_decrypt = new Crypt_RSA();
        
$rsa_encrypt = new Crypt_RSA();

        
$private_key "MIICXAIBAAKBgQDH0i//hwmPSCUNAS9UiD3wSH43NxwRKwGtHQKGarANuYNacSXb +rS2N0WKihz+aSkevePj5GZPtu21Fpx0Tt541DBlUrYL0vPK4S6QUX8I5vsOQ/Xu Iyh9NKcD2fbmf/1QrC+q7pv6xa6ctaV8u1RPGI7CAIPtMCrh9Pbg+EFo0wIDAQAB AoGATSi2HRMtV1UR7YggGSG03SR5DGQs49o5/vldQL9QtTQF6zYW2u9fuZ71LO5x DLJZzQOhTJ4k/bIS7yLIjkboM4x75DlRpG14Umi+d2GePB+r9UiqeZ67Zh7OvKFU 1CIkNC2J9G0U2G4RSG2UAZKkimIofmptq7dXa5oYYNL74t0CQQDi4RHoxIhqh9mw rLxvawgcq6gseKuY5zEgJMHGtNQTIGqn1LgxCCwNx5473xc7jW6GQbgCtDVwGqeb VkejSSsFAkEA4XgGfrAFHSQdZ9+GkNsT+eZioX5wyxP78b3csTRPPii4NudJmyeu yHmmhhJV6fknQ9ZsYqLwHH3gJpzXeH/79wJAEL3S+H4oItRLeAIHFaGsQ5FXI5Dz +KB2u4x60BLf41ltBV1pChU0SwAD6R8ZM8fH9Bg9X9W0a8VLwOao+p+XkQJBANG3 MGNaGgeW9fsOAD5wZ85Dqm4IC6X4upmKPeJdg2+PtHjs/Jvid0JlVKzUFTGB3DHP GhVM9QWrV3jS3eBpOtUCQB6Fyf7bkMHxRwa2QLoB3bM/gvk0mJPCBFpILfI5RZZG XezjDt3Xo/MqppBr8nybC42OxjbID9ALPPe/VX9AVmA=";

        
$rsa_decrypt->loadKey($private_key); // private key

        
$public_key "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH0i//hwmPSCUNAS9UiD3wSH43 NxwRKwGtHQKGarANuYNacSXb+rS2N0WKihz+aSkevePj5GZPtu21Fpx0Tt541DBl UrYL0vPK4S6QUX8I5vsOQ/XuIyh9NKcD2fbmf/1QrC+q7pv6xa6ctaV8u1RPGI7C AIPtMCrh9Pbg+EFo0wIDAQAB";

        
$rsa_encrypt->loadKey($public_key); // public key

        
include 'phpexcel/PHPExcel.php';
        include 
'phpexcel/PHPExcel/Writer/Excel2007.php';
        require_once 
'phpexcel/PHPExcel/IOFactory.php';

        
$file_name $_POST["excelfile_name"];
        
$reader    PHPExcel_IOFactory::createReader('Excel2007'); // 讀取2007 excel 檔案
        
$PHPExcel  $reader->load("excel_file/$file_name"); // 檔案名稱
        
$sheet     $PHPExcel->getSheet(0); // 讀取第一個工作表(編號從 0 開始)
        //$highestRow = $sheet->getHighestRow(); // 取得總列數
        
$highestRow $sheet->getHighestDataRow(); // 取得總列數

        
$highestColumn 14// set column
        //echo "==".$highestRow."<br />";
        // 一次讀取一列
        
for ($excel_row 2$excel_row <= $highestRow$excel_row++) {
            
//echo "<tr>";
            
$valid_row            1;
            
$error_msg            "";
            
$first_main_error_msg "";
            for (
$excel_column 0$excel_column <= $highestColumn$excel_column++) {
                
$val $sheet->getCellByColumnAndRow($excel_column$excel_row)->getValue();
                
// $val . ',';

                
if ($excel_column == 0) {
                    
$file_series_code $val;
                    
//echo "<td class='content'>".$val."</td>";
                
}

                if (
$excel_column == 1) {
                    
$file_model_code $val;
                    
//echo "<td class='content'>".$val."</td>";
                
}

                if (
$excel_column == 2) {
                    
$file_serial_number $val;
                    
//echo "<td class='content'>".$val."</td>";
                
}

                if (
$excel_column == 3) {
                    
$file_region $val;

                    if (
$file_region == "CN" || $file_region == "HK" || $file_region == "MO") {
                        
//echo "<td class='content'>".$val."</td>";
                    
} else {
                        
//echo "<td class='content import_product_data_error'>".$val."</td>";
                        
$error_msg .= "Region: Only CN, HK and MO allowed, ";
                    }

                }


                if (
$excel_column 3) {
                    
//check if this serial number exist
                    
$sql "select * from serial_number sn, model model, series series where sn.series_code = series.series_code and sn.model_code = model.model_code and sn.series_code = ? and sn.model_code = ? and sn.serial_number = ? and sn.deleted = ? and sn.region = ? and model.region = ? and series.region = ? group by sn.serial_number_id ";

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

                    if (
$file_region == "HK" || $file_region == "MO") {
                        
$this_region "HK";
                    } else if (
$file_region == "CN") {
                        
$this_region "CN";
                    } else {
                        
$this_region "CN";
                        
$valid_row 0;
                    }


                    if (!
$sth->execute(array($file_series_code$file_model_code$file_serial_number"0"$this_region$this_region$this_region))) {
                        throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                    }

                    
$row2 $sth->fetch(PDO::FETCH_ASSOC);

                    if (
$sth->rowCount() > 0) { //make sure the series and model exist


                        
$sql "select * from customer_warranty where series_code=? and model_code=? and serial_number=? and region=? and deleted=? ";
                        if (!(
$sth $dbh->prepare($sql))) {
                            throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                        }

                        
$parameter = array($file_series_code$file_model_code$file_serial_number$this_region"0");
                        if (!
$sth->execute($parameter)) {
                            throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                        }

                        
$row $sth->fetch(PDO::FETCH_ASSOC);

                        
$customer_id $row{"customer_id"};

                        if (
$sth->rowCount() > 0) { //update old data and check the changed part

                            
if ($excel_column == 4) {
                                
//$file_date_of_purchase = $val;
                                
$file_date_of_purchase PHPExcel_Style_NumberFormat::toFormattedString($val"YYYY-MM-DD");

                                if (
$row{"date_of_purchase"} == $file_date_of_purchase) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    if (!
validateDate($file_date_of_purchase'Y-m-d')) {
                                        
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                        
$valid_row 0;
                                        
$error_msg .= "Date of Purchase: Wrong date format, ";
                                    } else {
                                        
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                    
}
                                }
                            }

                            if (
$excel_column == 5) {
                                
$file_merchant_name $val;
                                if (
$row{"merchant_name"} == $file_merchant_name) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}
                            }

                            if (
$excel_column == 6) {
                                
$file_customer_name $val;

                                echo 
$file_customer_name;
                                if (
$rsa_decrypt->decrypt(base64_decode($row{"customer_name"})) == $file_customer_name) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}
                            }

                            if (
$excel_column == 7) {
                                
$file_gender $val;
                                if (
$row{"gender"} == $file_gender) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    if (
$file_gender == "男" || $file_gender == "女" || $file_gender == "Male" || $file_gender == "Female") {
                                        
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                    
} else {

                                        
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                        
$valid_row 0;
                                        
$error_msg .= "Gender: 男, 女, Male or Female allowed, ";
                                    }

                                }
                            }

                            if (
$excel_column == 8) {
                                
$file_age_group $val;
                                if (
$row{"age_group"} == $file_age_group) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}
                            }

                            if (
$excel_column == 9) {
                                
$file_email $val;
                                if (
$rsa_decrypt->decrypt(base64_decode($row{"email"})) == $file_email) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    if (!
filter_var($file_emailFILTER_VALIDATE_EMAIL)) {
                                        
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                        
$valid_row 0;
                                        
$error_msg .= "Email: Wrong email format, ";
                                    } else {
                                        
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                    
}
                                }
                            }

                            if (
$excel_column == 10) {
                                
$file_contact_number $val;
                                if (
$rsa_decrypt->decrypt(base64_decode($row{"contact_number"})) == $file_contact_number) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    if (!
is_numeric($file_contact_number)) {
                                        
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                        
$valid_row 0;
                                        
$error_msg .= "Contact Number: Only number allowed, ";
                                    } else {
                                        
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                    
}
                                }
                            }

                            if (
$excel_column == 11) {
                                
$file_address $val;
                                if (
$rsa_decrypt->decrypt(base64_decode($row{"address"})) == $file_address) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}
                            }

                            if (
$excel_column == 12) {
                                
$file_education_level $val;
                                if (
$row{"education_level"} == $file_education_level) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}
                            }

                            if (
$excel_column == 13) {
                                
$file_occupation $val;
                                if (
$row{"occupation"} == $file_occupation) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}
                            }

                            if (
$excel_column == 14) {
                                
$file_monthly_income $val;
                                if (
$row{"monthly_income"} == $file_monthly_income) {
                                    
//echo "<td class='content'>".$val."</td>";
                                
} else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}
                            }

                            
$action "Update";
                            echo 
$excel_row $action;


                        } else { 
//insert new data and check the series, model, serial number, region

                            
if ($excel_column == 4) {
                                
$file_date_of_purchase $val;

                                if (!
validateDate($file_date_of_purchase'Y-m-d')) {
                                    
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                    
$valid_row 0;
                                    
$error_msg .= "Date of Purchase: Wrong date format, ";
                                }

                            }

                            if (
$excel_column == 5) {
                                
$file_merchant_name $val;

                            }

                            if (
$excel_column == 6) {
                                
$file_customer_name $val;

                            }

                            if (
$excel_column == 7) {
                                
$file_gender $val;

                                if (
$file_gender == "男" || $file_gender == "女" || $file_gender == "Male" || $file_gender == "Female") {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
} else {

                                    
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                    
$valid_row 0;
                                    
$error_msg .= "Gender: 男, 女, Male or Female allowed, ";
                                }


                            }

                            if (
$excel_column == 8) {
                                
$file_age_group $val;

                            }

                            if (
$excel_column == 9) {
                                
$file_email $val;

                                if (!
filter_var($file_emailFILTER_VALIDATE_EMAIL)) {
                                    
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                    
$valid_row 0;
                                    
$error_msg .= "Email: Wrong email format, ";
                                } else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}

                            }

                            if (
$excel_column == 10) {
                                
$file_contact_number $val;

                                if (!
is_numeric($file_contact_number)) {
                                    
//echo "<td class='content import_product_data_error'>".$val."</td>";
                                    
$valid_row 0;
                                    
$error_msg .= "Contact Number: Only number allowed, ";
                                } else {
                                    
//echo "<td class='content import_product_data_changed'>".$val."</td>";
                                
}

                            }

                            if (
$excel_column == 11) {
                                
$file_address $val;

                            }

                            if (
$excel_column == 12) {
                                
$file_education_level $val;

                            }

                            if (
$excel_column == 13) {
                                
$file_occupation $val;

                            }

                            if (
$excel_column == 14) {
                                
$file_monthly_income $val;

                            }

                            
$action "Insert";
                            echo 
$excel_row $action;

                        }
                    } else { 
//series, model, serial number, region not match
                        //echo "<td class='content'>".$val."</td>";
                        
$first_main_error_msg "Series, Model, Serial Number and Purchase Region do not match, ";
                        
$valid_row            0;
                    }
                }
            }

            if(
$action == "Update"){
                if (
$valid_row == 1) {

                    
$file_customer_name  base64_encode($rsa_encrypt->encrypt($file_customer_name));
                    
$file_email          base64_encode($rsa_encrypt->encrypt($file_email));
                    
$file_contact_number base64_encode($rsa_encrypt->encrypt($file_contact_number));
                    
$file_address        base64_encode($rsa_encrypt->encrypt($file_address));

                    
$datetime1 = new DateTime($date_of_purchase);
                    
$datetime2 = new DateTime($nowdate);

                    
$difference $datetime1->diff($datetime2);

                    
/*echo 'Difference: '.$difference->y.' years, '
                        .$difference->m.' months, '
                        .$difference->d.' days';

                    var_dump($row);*/
                    
if ($row2{"free_mix_color"} == && ($difference->>= 30 || $difference->>= 1)) {
                        
$free_mix_color 1;

                    } else {
                        
$free_mix_color 0;
                    }

                    if (
$difference->>= 30 || $difference->>= 1) {
                        
$extend_warranty_year $row2{"extend_warranty_year"};

                    } else {
                        
$extend_warranty_year 0;

                    }

                    if (
$extend_warranty_year 0) {
                        
$warranty_detail_sc $row2{"warranty_detail_extend_sc"};
                        
$warranty_detail_tc $row2{"warranty_detail_extend_tc"};
                        
$warranty_detail_en $row2{"warranty_detail_extend_en"};
                    } else {
                        
$warranty_detail_sc $row2{"warranty_detail_sc"};
                        
$warranty_detail_tc $row2{"warranty_detail_tc"};
                        
$warranty_detail_en $row2{"warranty_detail_en"};
                    }

                    
$sql2 "update customer_warranty set customer_name=?, gender=?, email=?, contact_number=?, address=?, age_group=?, education_level=?, occupation=?, monthly_income=?, merchant_name=?, date_of_purchase=?, free_mix_color=?,extend_warranty_year=?, lastupby=?, lastupdate=? where customer_id = ?";


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

                    
$parameters2 = array($file_customer_name$file_gender$file_email$file_contact_number$file_address$file_age_group$file_education_level$file_occupation$file_monthly_income$file_merchant_name$file_date_of_purchase$file_free_mix_color$file_extend_warranty_year$_SESSION['cmsloginid'], $nowdate$customer_id);


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

                }
            }else if(
$action == "Insert"){
                if (
$valid_row == 1) {

                    
$file_customer_name  base64_encode($rsa_encrypt->encrypt($file_customer_name));
                    
$file_email          base64_encode($rsa_encrypt->encrypt($file_email));
                    
$file_contact_number base64_encode($rsa_encrypt->encrypt($file_contact_number));
                    
$file_address        base64_encode($rsa_encrypt->encrypt($file_address));

                    
$datetime1 = new DateTime($date_of_purchase);
                    
$datetime2 = new DateTime($nowdate);

                    
$difference $datetime1->diff($datetime2);

                    
/*echo 'Difference: '.$difference->y.' years, '
                        .$difference->m.' months, '
                        .$difference->d.' days';

                    var_dump($row);*/
                    
if ($row2{"free_mix_color"} == && ($difference->>= 30 || $difference->>= 1)) {
                        
$free_mix_color 1;

                    } else {
                        
$free_mix_color 0;
                    }

                    if (
$difference->>= 30 || $difference->>= 1) {
                        
$extend_warranty_year $row2{"extend_warranty_year"};

                    } else {
                        
$extend_warranty_year 0;

                    }

                    if (
$extend_warranty_year 0) {
                        
$warranty_detail_sc $row2{"warranty_detail_extend_sc"};
                        
$warranty_detail_tc $row2{"warranty_detail_extend_tc"};
                        
$warranty_detail_en $row2{"warranty_detail_extend_en"};
                    } else {
                        
$warranty_detail_sc $row2{"warranty_detail_sc"};
                        
$warranty_detail_tc $row2{"warranty_detail_tc"};
                        
$warranty_detail_en $row2{"warranty_detail_en"};
                    }

                    
//record data in database
                    
$sql2 "insert into customer_warranty (customer_name, gender, email, contact_number, address, region, age_group, education_level, occupation, monthly_income, merchant_name, date_of_purchase, series_code, model_code, serial_number, free_mix_color,fixed_warranty_year, extend_warranty_year, warranty_detail_sc, warranty_detail_tc, warranty_detail_en, createby, createdate, lastupby, lastupdate ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

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


                    
$parameters2 = array($file_customer_name$file_gender$file_email$file_contact_number$file_address$file_region$file_age_group$file_education_level$file_occupation$file_monthly_income$file_merchant_name$file_date_of_purchase$file_series_code$file_model_code$file_serial_number$free_mix_color$row2{"fixed_warranty_year"}, $extend_warranty_year$warranty_detail_sc$warranty_detail_tc$warranty_detail_en$_SESSION['cmsloginid'], $nowdate$_SESSION['cmsloginid'], $nowdate);


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


        }

        
unlink("excel_file/$file_name");
        
header("Location: import_customer_warranty_index.php?msg=Update Customer Warranty Successfully");
    }

?>