/var/www/hkosl.com/citysuper/webadmin/frontend_function.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
<?php
    
include_once("../webadmin/configure.php");

    if (isset(
$_SESSION["vip_code"])) {
        
$vip_code $_SESSION["vip_code"];
    } else {
        
$vip_code 0;
    }


    
/***************************************************
     * _member
     */

    
if (!function_exists('all_member_info')) {
        function 
all_member_info()
        {
            global 
$dbh;
            
$sql        "select * from member where deleted = ? ";
            
$parameters = array("0");
            if (!(
$sth $dbh->prepare($sql))) {
                throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
            }

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

            return 
$sth->fetchAll();
        }

    }


    
/***************************************************
     * _category
     */
    
if (!function_exists('get_category_info')) {
        function 
get_category_info($categoryid)
        {
            global 
$dbh;
            
$sql        "select * from pro_category where categoryid = ? and status = ? and deleted = ?";
            
$parameters = array($categoryid"1""0");
            if (!(
$sth $dbh->prepare($sql))) {
                throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
            }

            if (!
$sth->execute($parameters)) {
                throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
            }
            return 
$sth->fetch(PDO::FETCH_ASSOC);
        }
    }

    if (!
function_exists('get_default_cid_pcid')) {
        function 
get_default_cid_pcid($productid)
        {
            global 
$dbh;
            
$sql        "select * from pro_product_category ppcate inner join pro_category pcate on ppcate.categoryid = pcate.categoryid where ppcate.productid = ? and pcate.status = ? and deleted = ? order by ppcate.procatsort ASC";
            
$parameters = array($productid"1""0");

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

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

            
$result         $sth->fetch(PDO::FETCH_ASSOC);
            
$cid_pcid_array = array("cid" => $result{"categoryid"}, "pcid" => $result{"parentid"});

            return 
$cid_pcid_array;

        }

    }

    
/***************************************************
     * _product
     */
    
if (!function_exists('get_product_info')) {
        function 
get_product_info($productid)
        {
            global 
$dbh;
            
$sql        "select * from pro_product where productid = ? and status = ? and deleted = ? and old_record = ? order by lastupdate DESC limit 1";
            
$parameters = array($productid"1""0""0");

            return 
bind_pdo($sql$parameters"selectone");
        }
    }

    if (!
function_exists('get_product_image')) {
        function 
get_product_image($productid)
        {
            global 
$dbh;
            
$sql        "select * from pro_image where productid = ? and status = ? and deleted = ? order by sort ASC";
            
$parameters = array($productid"1""0");

            return 
bind_pdo($sql$parameters"selectall");
        }
    }


    if (!
function_exists('get_category_product')) {
        function 
get_category_product($categoryid)
        {

            global 
$dbh$langcode;

            
$sql        "SELECT *, pd.createdate as pd_createdate FROM pro_product pd INNER JOIN pro_product_category pdc ON pd.productid = pdc.productid INNER JOIN pro_category pc ON pc.categoryid = pdc.categoryid WHERE pd.deleted = ? and pd.old_record = ? AND pdc.categoryid = ? and pc.status = ? and pc.deleted = ? ORDER BY pd.sort ASC ";
            
$parameters = array("0""0", (int)$categoryid"1""0");

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

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

            
$all_product_info $sth->fetchAll();

            
$real_all_product_info = array(array());
            
$count                 0;
            foreach (
$all_product_info as $key => $product_info) {
                
$sku_info get_default_sku($product_info{"productid"});

                if (
$sku_info) {
                    
$sku_sale_price_info                           get_product_discount($sku_info{"proskuid"});
                    
$real_all_product_info[$count]["categoryid"]   = $product_info{"categoryid"};
                    
$real_all_product_info[$count]["categoryname"] = $product_info{"categoryname_" $langcode};
                    
$real_all_product_info[$count]["pcategoryid"]  = $product_info{"parentid"};

                    
$product_img_info get_product_image($product_info{"productid"});
                    if (
$product_img_info) {
                        
$real_all_product_info[$count]["image"] = $product_img_info[0]{"photo"};
                    } else {
                        
$real_all_product_info[$count]["image"] = "";
                    }

                    
$real_all_product_info[$count]["productname"] = $product_info{"productname_" $langcode};
                    
$real_all_product_info[$count]["price"]       = $product_info{"categoryid"};
                    
$real_all_product_info[$count]["productid"]   = $product_info{"productid"};
                    
$real_all_product_info[$count]["createdate"]  = $product_info{"pd_createdate"};

                    if (
$sku_sale_price_info) {
                        
$real_all_product_info[$count]["price"] = $sku_sale_price_info{"sale_price"};
                    } else {
                        
$real_all_product_info[$count]["price"] = $sku_info{"standardprice"};
                    }

                    
$count++;
                }

            }

            return 
$real_all_product_info;
        }
    }


    
/*if (!function_exists('product_share')) {
        function product_share($from_vip_code, $to_vip_code, $productid, $message_content)
        {
            global $dbh;
            $sql = "insert into (message_type, from_vip_code, to_vip_code,productid, message_title_en, message_title_tc, message_title_sc, message_content, createby, createdate, lastupby, lastupdate) values (?,?,?,?,?,?,?,?,?,?,?,?)";

            $product_info = get_product_info($productid);

            $message_title_en = "SHARE: I want to share product (" . $product_info{"productname_en"} . "-" . $product_info{"productitemcode"} . ") to you.";
            $message_title_tc = "分享: 我想分享有關 (" . $product_info{"productname_tc"} . "-" . $product_info{"productitemcode"} . ") 的資訊給你";
            $message_title_sc = "分享: 我想分享有? (" . $product_info{"productname_sc"} . "-" . $product_info{"productitemcode"} . ") 的???你";

            $parameters = array("2", $from_vip_code, $to_vip_code, $productid, $message_title_en, $message_title_tc, $message_title_sc, $message_content, "0", date("Y-m-d H:i:s"), "0", date("Y-m-d H:i:s"),);
            if (!($sth = $dbh->prepare($sql))) {
                throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
            }

            if (!$sth->execute($parameters)) {
                throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
            }
        }
    }*/


    /***************************************************
     * product sku
     */
    
if (!function_exists('get_all_sku')) {
        function 
get_all_sku($productid)
        {
            global 
$dbh;
            
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and old_record = ? order by sort";
            
$parameters = array($productid"1""0""0");

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

            return 
$sth->fetchAll();
        }
    }


    if (!
function_exists('get_default_sku')) {
        function 
get_default_sku($productid)
        {
            global 
$dbh;
            
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and old_record = ? order by sort ASC, lastupdate DESC limit 1";
            
$parameters = array($productid"1""0""0");

            return 
bind_pdo($sql$parameters"selectone");
        }
    }

    if (!
function_exists('get_product_size')) {
        function 
get_product_size($productid)
        {
            global 
$dbh;

            
$sql        "SELECT * FROM ( select * from `pro_product_sku` order by sort ASC) as prosku_temp where productid = ? and status = ? and deleted = ? and old_record = ? group by size_code order by sort ASC";
            
$parameters = array($productid"1""0""0");

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

            return 
$sth->fetchAll();
        }
    }


    if (!
function_exists('get_product_color')) {
        function 
get_product_color($productid$size_code)
        {
            global 
$dbh;

            
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and old_record = ? and size_code = ? order by sort ASC";
            
$parameters = array($productid"1""0""0"$size_code);

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

            return 
$sth->fetchAll();

        }
    }

    if (!
function_exists('get_sku_info')) {
        function 
get_sku_info($proskuid)
        {
            global 
$dbh;

            
$sql        "select * from pro_product_sku where proskuid = ? and status = ? and deleted = ? and old_record = ? order by lastupdate DESC limit 1";
            
$parameters = array($proskuid"1""0""0");

            
$sku_info bind_pdo($sql$parameters"selectone");

            if(!empty(
$sku_info)){
                
$sku_discount_info get_product_discount($proskuid);

                if (!
$sku_discount_info) { //no discount, show standard price
                    
$price $sku_info{"standardprice"};
                } else {
                    
$price $sku_discount_info{"sale_price"};
                }

                
$sku_info["saleprice"] = $price;
            }


            return 
$sku_info;
        }
    }

    if (!
function_exists('get_sku_info2')) {
        function 
get_sku_info2($productid$size_code$color_code)
        {
            global 
$dbh;

            
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and size_code = ? and color_code = ? and old_record = ? order by lastupdate DESC";
            
$parameters = array($productid"1""0"$size_code$color_code"0");

            return 
bind_pdo($sql$parameters"selectone");
        }
    }


    
/*if (!function_exists('matched_sku_info')) {
        function matched_sku_info($product_group)
        {

            if (!empty($product_group)) {
                global $dbh;
                $matched_sku = array(array());

                $sql2        = "select * from product_group where progroup_id = ? and old_record = ? and deleted = ?";
                $parameters2 = array($product_group, "0", "0");
                if (!($sth2 = $dbh->prepare($sql2))) {
                    throw new Exception('[' . $sth2->errorCode() . ']: ' . print_r($sth2->errorInfo()));
                    exit;
                }

                if (!$sth2->execute($parameters2)) {
                    throw new Exception('[' . $sth2->errorCode() . ']: ' . print_r($sth2->errorInfo()));
                    exit;
                }
                $product_group_info = $sth2->fetch(PDO::FETCH_ASSOC);
                $proskuids          = $product_group_info{"proskuids"};
                $proskuids          = explode($proskuids, ",");

                //search this proskuid in ord_list
                foreach ($proskuids as $proskuid) {
                    $sql3        = "select * from ord_list_temp temp, pro_product_sku sku where temp.vip_code = ? and temp.proskuid = sku.proskuid and temp.proskuid = ?";
                    $parameters3 = array($_SESSION["vip_code"], $proskuid);
                    if (!($sth3 = $dbh->prepare($sql3))) {
                        throw new Exception('[' . $sth3->errorCode() . ']: ' . print_r($sth3->errorInfo()));
                    }

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

                    $matched_sku[] = $sth3->fetch(PDO::FETCH_ASSOC);
                }

                return $matched_sku;
            }

        }
    }*/


    /***************************************************
     * member
     */

    
if (!function_exists('get_member_info')) {
        function 
get_member_info($vip_code)
        {
            global 
$dbh;
            
$sql        "select * from VIP where VIP_CODE = ? ";
            
$parameters = array($vip_code);
            
$member_info bind_pdo($sql$parameters"selectone");

            if(!empty(
$member_info)){
                
$sql2 "select * from VIPVerifingTel where verifyingTel = ?";
                
$parameters2 = array("852".$member_info{"VIP_TEL"});
                
$active_member_info bind_pdo($sql2$parameters2"selectone");
            }

            if(!empty(
$active_member_info)){
                
$sql "select * from jPOS_VIPGrade where VIP_GRADE = ?";
                
$parameters = array($member_info{"VIP_GRADE"});
                
$vip_grade_info bind_pdo($sql$parameters"selectone");

                
$member_info["VIP_LEVEL_DISCOUNT_RATE"] = (float)$vip_grade_info{"VIP_LEVEL_DISCOUNT_RATE"};

                return 
$member_info;
            }else{
                return 
NULL;
            }
        }
    }


    if (!
function_exists('get_member_name')) {
        function 
get_member_name($member_info)
        {
            global 
$dbh;

            
//if (preg_match("/[a-zA-Z]/", $member_info{"VIP_LAST_NAME"}) && preg_match("/[a-zA-Z]/", $member_info{"VIP_FIRST_NAME"})) {
            
if (preg_match('/^([a-zA-Z\s]+)$/'$member_info{"VIP_LAST_NAME"}) && preg_match('/^([a-zA-Z\s]+)$/'$member_info{"VIP_FIRST_NAME"})) {

                
$member_name $member_info{"VIP_FIRST_NAME"} . " " $member_info{"VIP_LAST_NAME"};
            } else {

                
$member_name $member_info{"VIP_LAST_NAME"} . $member_info{"VIP_FIRST_NAME"};
            }

            return 
$member_name;
        }
    }

    if (!
function_exists('check_member_login')) {
        function 
check_member_login()
        {
            if (empty(
$_SESSION["vip_code"]) || !isset($_SESSION["vip_code"])) {
                return 
"<script>alert('Please login first.');top.location.href='member_login.php'; </script>";
            }else{
                return 
NULL;
            }

        }
    }

    
/***************************************************
     * discount
     */

    
if (!function_exists('get_product_discount')) {
        function 
get_product_discount($proskuid)
        {
            global 
$dbh;

            
$sql        "select * from product_discount where proskuid = ? and status = ? and deleted = ? and ? between start_date and end_date";
            
$parameters = array($proskuid"1""0"date('Y-m-d'));

            return 
bind_pdo($sql$parameters"selectone");
        }
    }


    if (!
function_exists('get_discount_info')) {
        function 
get_discount_info($discount_id NULL)
        {
            global 
$dbh;

            if(empty(
$discount_id)){
                
$sql                     "select * from discount where status = ? and deleted = ? and old_record = ? and ? between datefrom and dateto order by priority ASC";
                
$parameters              = array(100date("Y-m-d"));
                
$discount_info bind_pdo($sql$parameters"selectall");
            }else{
                
$sql                     "select * from discount where discount_id = ?";
                
$parameters              = array($discount_id);
                
$discount_info bind_pdo($sql$parameters"selectone");
            }

            return 
$discount_info;
        }
    }

    
/***************************************************
     * _checkout _order
     */
    
if (!function_exists('get_order_info')) {
        function 
get_order_info($vip_code$order_id NULL){
            global 
$dbh;

            
//display temp order
            
if(empty($order_id)){
                
$sql        "select * from `order` where vip_code = ? and temp = ? order by lastupdate DESC limit 1 ";
                
$parameters = array($vip_code1);
                
$order_info bind_pdo($sql$parameters"selectone");
            }else{  
//display real order
                //$sql        = "select order_id,vip_code,INV_NO,ordernumber,subtotal,totalamount,mvip_discount,member_deduct,coupon_deduct,point_deduct,discount_deduct,specialgift_deduct,order_status,lastname,firstname,telephone,shipping_method,address,district_id,store_id,createdate from `order` where vip_code = ? and temp = ? and order_id = ? order by lastupdate DESC limit 1 ";
                
$sql        "select * from `order` where vip_code = ? and temp = ? and order_id = ? order by lastupdate DESC limit 1 ";
                
$parameters = array($vip_code0$order_id);
                
$order_info bind_pdo($sql$parameters"selectone");

            }

            return 
$order_info;
        }
    }

    if (!
function_exists('order_temp_total')) {
        function 
order_temp_total($vip_code)
        {
            global 
$dbh;

            
$order_temp_total 0;

            
$result get_order_temp($vip_code);

            
//add more info in temp order list pool
            
foreach ($result as $key => $temp_order) {
                
$sku_info     get_sku_info($temp_order["proskuid"]);
                
$order_temp_total += ($sku_info["standardprice"]*$temp_order["quantity"]);

            }

            return 
$order_temp_total;
        }
    }


    if (!
function_exists('onchange_size_color')) {
        function 
onchange_size($productid$size_code$color_code)
        {
            global 
$dbh;
            
$result = array(
                
"chanage_price"            => "",
                
"change color"             => "",
                
"change_promotion_package" => "",
                
"change_others_promotion"  => "",
            );

            
//both change

            //find the proskuid
            
if (empty($color_code)) {
                
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and old_record = ? and size_code = ? order by sort limit 1";
                
$parameters = array($productid"1""0""0"$size_code);

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

                
$product_color get_product_color($productid$size_code);

                
$product_color_html "";
                foreach (
$product_color as $this_color) {
                    
//sample
                    
$product_color_html .= "<div class='' id='" $this_color{"color_code"} . "' style=\"border: 1px solid #b3b3b3; background: url('../images/color_images/" $this_color{"color_img"} . "') no-repeat; width: 50px; height:50px;float:left;margin-right:10px;\" >&nbsp;</div>";
                }

                
//html with change color
                
$result["change color"] = $product_color_html;

            } else {
                
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and old_record = ? and size_code = ? and color_code = ? limit 1";
                
$parameters = array($productid"1""0""0"$size_code$color_code);

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

            }

            
//change price
            
$prosku_discount get_product_discount($proskuid);
            if (!empty(
$prosku_discount)) {
                
//html with sale price and ori price
                
$result["chanage_price"] = "";
            } else {
                
//html with ori price
                
$result["chanage_price"] = "";
            }

            
//change promotion package
            //array 0 = promotion package, array 1 = others promotion

            
$discount_info     get_valid_discount($proskuid);
            
$promotion_package $discount_info[0];
            
$others_promotion  $discount_info[1];

            if (!empty(
$promotion_package)) {
                
//html with promotion package, should include pop up css
                
$result["promotion_package"] = "";
            } else {
                
//no promotion package
            
}

            if (!empty(
$others_promotion)) {
                
//html with others promotion
                
$result["others_promotion"] = "";
            } else {

            }

            return 
json_encode($result);

        }
    }

    
//similar to onchange_size_color
    //ajax function, direct display html as result
    
if (!function_exists('pop_up_onchange_size_color')) {
        function 
pop_up_onchange_size_color($productid$size_code$color_code)
        {
            global 
$dbh;
            
$result2 = array(
                
"'pop_up_chanage_price" => "",
                
"'pop_up_hange color"   => "",

            );

            
//both change

            //find the proskuid
            
if (empty($color_code)) {
                
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and old_record = ? and size_code = ? order by sort limit 1";
                
$parameters = array($productid"1""0""0"$size_code);

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

                
$product_color get_product_color($productid$size_code);

                
$product_color_html "";
                foreach (
$product_color as $this_color) {
                    
//sample
                    
$product_color_html .= "<div class='' id='" $this_color{"color_code"} . "' style=\"border: 1px solid #b3b3b3; background: url('../images/color_images/" $this_color{"color_img"} . "') no-repeat; width: 50px; height:50px;float:left;margin-right:10px;\" >&nbsp;</div>";
                }

                
//html with change color
                
$result2["change color"] = $product_color_html;

            } else {
                
$sql        "select * from pro_product_sku where productid = ? and status = ? and deleted = ? and old_record = ? and size_code = ? and color_code = ? limit 1";
                
$parameters = array($productid"1""0""0"$size_code$color_code);

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

            }

            
//change price
            
$prosku_discount get_product_discount($proskuid);
            if (!empty(
$prosku_discount)) {
                
//html with sale price and ori price
                
$result2["chanage_price"] = "";
            } else {
                
//html with ori price
                
$result2["chanage_price"] = "";
            }

        }
    }

    if (!
function_exists('get_shipping_info')) {
        function 
get_shipping_info($location_id)
        {
            global 
$dbh;

            
$sql        "select * from shipping_fee where location_id = ? ";
            
$parameters = array($location_id);
            return 
bind_pdo($sql$parameters"selectone");
        }
    }

    
/***************************************************
     * _others function
     */

    
if (!function_exists('get_user_visit')) {
        function 
get_user_visit($current_productid)
        {
            global 
$dbh$vip_code$langcode;

            
$real_product_info = array(array());
            if (!empty(
$vip_code)) { //get user visit by cookies
                
$sql        "select * from user_visit where vip_code = ? and productid != ? group by productid";
                
$parameters = array($vip_code$current_productid);
            } else {  
//get user visit by member
                
if (isset($_COOKIE['sinomax_user_visit'])) {
                    
$sql        "select * from user_visit where user_cookies = ? and productid != ? group by productid";
                    
$parameters = array($_COOKIE['sinomax_user_visit'], $current_productid);
                } else {
                    return 
NULL;
                }

            }

            
$user_visit_info bind_pdo($sql$parameters"selectall");

            
$count 0;
            foreach (
$user_visit_info as $user_visit) {
                
$productid $user_visit{"productid"};

                
$sql        "SELECT *, pd.createdate as pd_createdate FROM pro_product pd INNER JOIN pro_product_category pdc ON pd.productid = pdc.productid INNER JOIN pro_category pc ON pc.categoryid = pdc.categoryid WHERE pd.deleted = ? and pd.old_record = ? AND pd.productid = ? and pc.status = ? and pc.deleted = ? ORDER BY pd.sort ASC limit 1";
                
$parameters = array("0""0"$productid"1""0");

                
$product_info bind_pdo($sql$parameters"selectone");

                
$sku_info get_default_sku($product_info{"productid"});

                if (
$sku_info) {
                    
$sku_sale_price_info                       get_product_discount($sku_info{"proskuid"});
                    
$real_product_info[$count]["categoryid"]   = $product_info{"categoryid"};
                    
$real_product_info[$count]["categoryname"] = $product_info{"categoryname_" $langcode};
                    
$real_product_info[$count]["pcategoryid"]  = $product_info{"parentid"};
                    
$product_img_info                          get_product_image($product_info{"productid"});
                    
$real_product_info[$count]["image"]        = $product_img_info[0]{"photo"};
                    
$real_product_info[$count]["productname"]  = $product_info{"productname_" $langcode};
                    
$real_product_info[$count]["productid"]    = $product_info{"productid"};

                    if (
$sku_sale_price_info) {
                        
$real_product_info[$count]["price"] = $sku_sale_price_info{"sale_price"};
                    } else {
                        
$real_product_info[$count]["price"] = $sku_info{"standardprice"};
                    }

                    
$count++;
                }

            }


            return 
$real_product_info;
        }
    }


    if (!
function_exists('large_to_small')) {
        function 
large_to_small($a$b)
        {
            if (
$a["product_count"] == $b["product_count"]) {
                return 
0;
            }
            return (
$a["product_count"] > $b["product_count"]) ? -1;
        }
    }

    if (!
function_exists('numberformat')) {
        function 
numberformat($data)
        {
            return 
number_format($data"2""."",");
        }
    }


    include_once(
'device_detect.php');
    
$device_detect = new Mobile_Detect();

    
$deviceType    = ($device_detect->isMobile() ? ($device_detect->isTablet() ? 'tablet' 'phone') : 'computer');
    
$scriptVersion $device_detect->getScriptVersion();


    
/***************************************************
     * _VIP function
     */


    /*if (!function_exists('vip_upgrade_checking')) {
        function vip_upgrade_checking($member_info)
        {
            $message = "1";
            global $dbh;

            //this vip type can upgrade to next level
            if ($member_info{"mvip_upgrade"} == 1 && !empty($member_info{"next_vip_level"})) {
                $message            = "2";
                $pass_one_bill_over = false;    //condition 1
                $pass_accumulate    = false;    //condition 2
                $vip_code           = $_SESSION["vip_code"];

                $sql        = "select * from member_vip where mvip_type = ? and old_record = ? and deleted = ?";
                $parameters = array($member_info{"next_vip_level"}, "0", "0");
                if (!($sth = $dbh->prepare($sql))) {
                    throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
                }

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

                $next_vip_level_info = $sth->fetch(PDO::FETCH_ASSOC);

                if ($next_vip_level_info) {
                    $message = "3";
                    //check if pass one bill over
                    $sql        = "select * from `order` where vip_code = ? and temp = ? and deleted = ? and order_status = ?";
                    $parameters = array($vip_code, "0", "0", "4");
                    if (!($sth = $dbh->prepare($sql))) {
                        throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
                    }

                    if (!$sth->execute($parameters)) {
                        throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
                    }
                    //looping each `order` and check if any bill over one bill amount
                    while ($order_info = $sth->fetch(PDO::FETCH_ASSOC)) {

                        if ($order_info{"totalamount"} >= $next_vip_level_info{"one_bill_over"}) {
                            $pass_one_bill_over = true;
                            break;
                        }
                    }


                    //check if user total amount accumulate over defined year and defined points
                    $accumulate_total_amount = 0;
                    $sql                     = "select * from `order` where vip_code = ? and temp = ? and deleted = ? and order_status = ? and createdate between ? and ?";
                    $parameters              = array($vip_code, "0", "0", "5", date("Y-m-d H:i:s"),
                        date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . "-" . $next_vip_level_info{"year_required"} . " year")));
                    if (!($sth = $dbh->prepare($sql))) {
                        throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
                    }

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

                    while ($order_info = $sth->fetch(PDO::FETCH_ASSOC)) {
                        $accumulate_total_amount += $order_info{"totalamount"};
                    }

                    if ($accumulate_total_amount >= $next_vip_level_info{"year_required_points"}) {
                        $pass_accumulate = true;
                    }

                    //user allow to update
                    if ($pass_one_bill_over || $pass_accumulate) {

                        $message = "4";
                        //check if this message already post
                        $sql        = "select * from message where to_vip_code = ? and message_type = ? and from_admin = ? and createdate > ? and deleted = ?";
                        $parameters = array($member_info{"vip_code"}, "3", "1", date("Y-m-d H:i:s", strtotime(date("Y-m-d H:i:s") . "-1 month")), "0");

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

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

                        if ($sth->rowCount() <= 0) { //allow to post a new message
                            $message = "5";
                            //system message: user can upgrade
                            $sql              = "insert into (message_type, from_admin, to_vip_code, message_title_en, message_title_tc, message_title_sc, message_content_en, message_content_tc, message_content_sc,system_message_flag, createby, createdate, lastupby, lastupdate) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
                            $message_title_en = "Congratulation! You can upgrade to next vip level. You can click the 'Upgrade' button and we will approve it as soon as possible.";
                            $message_title_tc = "恭喜您,你的會員級別已經可以晉升,請按'晉升'按鈕,我們將盡快升級您的會員級別。";
                            $message_title_sc = "恭喜您,你的会员级别已经可以晋升,请按'晋升'按钮,我们将尽快升级您的会员级别。";

                            $message_content_en = "Congratulation! You can upgrade to next vip level. You can click the 'upgrade button' and we will approve it as soon as possible.";
                            $message_content_tc = "恭喜您,你的會員級別已經可以晉升,請按'晉升'按鈕,我們將盡快升級您的會員級別。";
                            $message_content_sc = "恭喜您,你的会员级别已经可以晋升,请按'晋升'按钮,我们将尽快升级您的会员级别。";

                            $parameters = array("3", "1", $member_info{"vip_code"}, $message_title_en, $message_title_tc, $message_title_sc, $message_content_en, $message_content_tc, $message_content_sc, "1", "0", date("Y-m-d H:i:s"), "0", date("Y-m-d H:i:s"),);

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

                            if (!$sth->execute($parameters)) {
                                throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
                            }
                        }
                    }
                }
            }
            return $message;
        }
    }

    if (!function_exists('vip_downgrade_checking')) {
        function vip_downgrade_checking($member_info)
        {
            global $dbh;

            if ($member_info{"mvip_downgrade"} == 1 && !empty($member_info{"down_vip_level"}) && $member_info{"permanent"} == 0 && !empty($member_info{"mvip_year"})) {
                //membership expired
                if (date("Y-m-d H:i:s") > strtotime("+" . $member_info{"mvip_year"} . " year", $member_info{"member_createdate"})) {

                    $can_downgrade = true;

                    //insert record to membership for remark and update member record
                    $sql        = "insert into membership set(vip_code, current_mvip_id, down_mvip_id, createby, createdate, lastupby, lastupdaet) values (?,?,?,?,?,?,?)";
                    $parameters = array($member_info{"vip_code"}, $member_info{"mvip_id"}, $member_info{"down_vip_level"}, "0", date("Y-m-d H:i:s"), "0", date("Y-m-d H:i:s"));
                    if (!($sth = $dbh->prepare($sql))) {
                        throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
                    }

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

                    //system message: user can downgrade
                    $sql              = "insert into (message_type, from_admin, to_vip_code, message_title_en, message_title_tc, message_title_sc, message_content_en, message_content_tc, message_content_sc,system_message_flag, createby, createdate, lastupby, lastupdate) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
                    $message_title_en = "Your VIP level has been downgraded. Please check current VIP level in your profile.";
                    $message_title_tc = "你的會員級別已被降級,請到個人資料中查看你現在的會員等級。";
                    $message_title_sc = "你的会员级别已被降级,请到个人资料中查看你现在的会员等级。";

                    $message_content_en = "Your VIP level has been downgraded. Please check current VIP level in your profile.";
                    $message_content_tc = "你的會員級別已被降級,請到個人資料中查看你現在的會員等級。";
                    $message_content_sc = "你的会员级别已被降级,请到个人资料中查看你现在的会员等级。";

                    $parameters = array("3", "1", $member_info{"vip_code"}, $message_title_en, $message_title_tc, $message_title_sc, $message_content_en, $message_content_tc, $message_content_sc, "2", "0", date("Y-m-d H:i:s"), "0", date("Y-m-d H:i:s"));
                    if (!($sth = $dbh->prepare($sql))) {
                        throw new Exception('[' . $sth->errorCode() . ']: ' . print_r($sth->errorInfo()));
                    }

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

        }
    }*/

    
if (!function_exists('get_member_cart_num')) {
        function 
get_member_cart_num()
        {
            global 
$dbh;

            if (!empty(
$_SESSION["vip_code"])) {
                
$sql        "select * from ord_list_temp where vip_code = ? and special_gift = ?";
                
$parameters = array($_SESSION["vip_code"], "0");
            } else {
                
$sql        "select * from ord_list_temp where session_id = ? and special_gift = ?";
                
$parameters = array(session_id(), "0");
            }

            
$all_order_temp_info bind_pdo($sql$parameters"selectall");
            
$ori_cart_num count($all_order_temp_info);

            foreach (
$all_order_temp_info as $order_temp_info) {
                
$sku_info           get_sku_info($order_temp_info{"proskuid"});

                if (empty(
$sku_info)) {
                    
//delete this old sku
                    
$sql2        "delete from ord_list_temp where ordtemp_id = ?";
                    
$parameters2 = array($order_temp_info{"ordtemp_id"});
                    
bind_pdo($sql2$parameters2);

                    
$ori_cart_num--;
                }
            }
            return 
$ori_cart_num;
        }
    }

    if (!
function_exists('get_order_temp')) {
        function 
get_order_temp($vip_code )
        {
            global 
$dbh;

            if (!empty(
$vip_code)) {
                
$sql        "select * from ord_list_temp where vip_code = ? and special_gift = ?";
                
$parameters = array($vip_code0);
            } else {
                
$sql        "select * from ord_list_temp where session_id = ? and special_gift = ?";
                
$parameters = array(session_id(), 0);
            }

            
$result bind_pdo($sql$parameters"selectall");

            return 
$result;
        }
    }

    if (!
function_exists('get_member_order')) {
        function 
get_member_order($order_id$vip_code)
        {
            global 
$dbh;

            
$sql        "select * from ord_list ord_list inner join `order` `order` on ord_list.order_id = `order`.order_id where `order`.order_id = ? and `order`.vip_code = ? and ord_list.special_gift = ? ";
            
$parameters = array($order_id$vip_code"0");

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

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

            return 
$sth->fetchAll();
        }
    }

    if (!
function_exists('get_order_points')) {
        function 
get_order_points($order_id$vip_code)
        {
            global 
$dbh;

            
$sql        "select * from point_record where order_id = ? and vip_code = ? and deleted = ? and add_deduct = ? ";
            
$parameters = array($order_id$vip_code"0""+");

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

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

            
$point_record_info $sth->fetch(PDO::FETCH_ASSOC);
            return 
$point_record_info{"point_change"};
        }
    }

    if (!
function_exists('langcode_translate')) {
        function 
langcode_translate($langcode)
        {
            if(
$langcode == "en")
                return 
"en";
            else if(
$langcode == "tc")
                return 
"zh";
            else if(
$langcode == "sc")
                return 
"cn";
            else
                return 
NULL;

        }
    }

    if (!
function_exists('product_detail_img_style')) {
        function 
product_detail_img_style($img_src)
        {

            list(
$width$height$type$attr) = getimagesize($img_src);

            if(
$width >= $height){
                
$style "width:100%; height:auto";
            }else{
                
$style "width: auto; height:100%";
            }

            return 
$style;
        }
    }


    if (!
function_exists('get_redeem_order')) {
        function 
get_redeem_order()
        {
            global 
$dbh;

            
$sql        "select * from `order` where vip_code = ? and temp = ? and deleted = ? and redeem = ?";
            
$parameters = array($_SESSION["vip_code"], "1""0""1");
            if (!(
$sth $dbh->prepare($sql))) {
                throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
            }

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


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

        }
    }


    
//call functions
    
if (isset($_SESSION["vip_code"])) {

        
$member_info get_member_info($vip_code);

        
$order_info get_order_info($vip_code);

    }


    
//check if cookies exist

    
if (isset($_COOKIE['sinomax_user_visit']) || isset($_SESSION['sinomax_user_visit'])) {
        
$_SESSION['sinomax_user_visit'] = $_COOKIE['sinomax_user_visit'];
    } else {
        
$cookie_code random_string(20);
        unset(
$_COOKIE['sinomax_user_visit']);
        
setcookie('sinomax_user_visit'$cookie_codetime() + 3600 24 365 10'/');
        
//setcookie("sinomax_user_visit", $cookie_code, time() + 3600 * 24 * 365 * 10);
        
$_SESSION['sinomax_user_visit'] = $cookie_code;
    }


        
//auto login - get the cookies and checking
    
if (isset($_COOKIE["sinomax_cookies1"]) && isset($_COOKIE["sinomax_cookies2"]) && empty($_SESSION["vip_code"])) { //user has cookies and not login yet
        
$temp_var rsa_crypt($_COOKIE["sinomax_cookies1"], 2);    //decrypt cookies

        
$sql        "select * from auto_login where expired_date >= ? and http_user_agent = ? and temp_var = ?";
        
$parameters = array($nowdate$_SERVER["HTTP_USER_AGENT"], $_COOKIE["sinomax_cookies1"]);
        if (!(
$sth $dbh->prepare($sql))) {
            throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
        }

        if (!
$sth->execute($parameters)) {
            throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
        }
        
$valid_cookies_info $sth->fetch(PDO::FETCH_ASSOC);

        
$_SESSION["vip_code"] = $valid_cookies_info{"vip_code"};

        
$_SESSION["autologin_lasturl"] = $_SERVER['REQUEST_URI'];

        
//make sure all login should visit index page
        
header("Location: ../" $_COOKIE["sinomax_cookies2"] . "/index.php");;
    }