bhq@iemsoft.cn
2018-11-27 e2b48dac099e43f4b3243cdf19a7522e4b5eccbe
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
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
<?php
 
/**
 *  虚拟卡商品管理程序
 */
 
define('IN_ECS', true);
 
/* 包含文件 */
require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/lib_code.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
 
/*------------------------------------------------------ */
//-- 补货处理
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'replenish')
{
    assign_query_info();
 
    /* 检查权限 */
    admin_priv('virualcard');
    /* 验证goods_id是否合法 */
    if (empty($_REQUEST['goods_id']))
    {
        $link[] = array('text'=>$_LANG['go_back'],'href'=>'virtual_goods_card.php?act=list');
        sys_msg($_LANG['replenish_no_goods_id'], 1, $link);
    }
    else
    {
        $goods_name = $db->GetOne("SELECT goods_name From ".$ecs->table('goods')." WHERE goods_id='".$_REQUEST['goods_id']."' AND is_real = 0 AND extension_code='virtual_card' ");
        if (empty($goods_name))
        {
            $link[] = array('text'=>$_LANG['go_back'],'href'=>'virtual_goods_card.php?act=list');
            sys_msg($_LANG['replenish_no_get_goods_name'],1, $link);
        }
    }
    
    $card = array('goods_id'=>$_REQUEST['goods_id'],'goods_name'=>$goods_name, 'end_date'=>date('Y-m-d', strtotime('+1 year')));
    $smarty->assign('card', $card);
 
    $smarty->assign('ur_here', $_LANG['replenish']);
    $smarty->assign('action_link', array('text'=>$_LANG['go_list'], 'href'=>'virtual_goods_card.php?act=card&goods_id='.$card['goods_id']));
    $smarty->display('virtual_goods_card_info.htm');
}
 
/*------------------------------------------------------ */
//-- 编辑虚拟卡信息
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_replenish')
{
    /* 检查权限 */
    admin_priv('virualcard');
    /* 获取卡片信息 */
    $sql = "SELECT T1.card_id, T1.goods_id, T2.goods_name,T1.card_sn,  T1.end_date  FROM ".
            $ecs->table('virtual_goods_card')." AS T1, ".$ecs->table('goods')." AS T2 ".
            "WHERE T1.goods_id = T2.goods_id AND T1.card_id = '$_REQUEST[card_id]'";
    $card = $db->GetRow($sql);
    $card['card_sn']=str_mid_replace($card['card_sn']);  
    $smarty->assign('ur_here',     $_LANG['replenish']);
    $smarty->assign('action_link', array('text'=>'返回虚拟券订单列表', 'href'=>'virtual_goods_card.php?act=all_card'));
    $smarty->assign('card',        $card);
    $smarty->display('virtual_goods_card_info.htm');
}
 
elseif ($_REQUEST['act'] == 'action')
{
    /* 检查权限 */
    admin_priv('virualcard');
        /* 更新数据 */
        $end_date = strtotime($_POST['end_dateYear'] . "-" . $_POST['end_dateMonth'] . "-" . $_POST['end_dateDay']);
        $sql = "UPDATE ".$ecs->table('virtual_goods_card')." SET end_date='$end_date' ".
               "WHERE card_id='$_POST[card_id]'";
        $db->query($sql);
        $link[] = array('text'=>$_LANG['go_list'], 'href'=>'virtual_goods_card.php?act=all_card');
        $link[] = array('text'=>$_LANG['continue_add'], 'href'=>'virtual_goods_card.php?act=edit_replenish&card_id='.$_POST['card_id']);
        sys_msg($_LANG['action_success'], 0, $link);
}
 
//获得虚拟卡列表
elseif ($_REQUEST['act'] == 'all_card')
{
    /* 检查权限 */
    admin_priv('virualcard');
 
    if (empty($_REQUEST['order_sn']))
    {
        $_REQUEST['order_sn'] = '';
    }
 
    $smarty->assign('full_page',    1);
    $smarty->assign('lang',         $_LANG);
 
    $list = get_virtual_order_list();
    $smarty->assign('ur_here', $_LANG['virtual_order_list']);
    $smarty->assign('order_list',    $list['item']);
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);
 
    $sort_flag = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);
 
    assign_query_info();
    $smarty->display('virtual_goods_card_list.htm');
}
 
/*
 * 虚拟商品验证显示验证信息列表
 */
if($_REQUEST['act']=='verification_info'){
    $supplier_id = empty($_SESSION['supplier_id'])?0:intval($_SESSION['supplier_id']);
    $card_sn = trim($_REQUEST['verification']);
    if(!empty($card_sn)){
        $sql = "select v.*,g.goods_name,g.goods_thumb,shop_price from ".$ecs->table('virtual_goods_card')."as v left join ".$ecs->table('goods')." as g on v.goods_id= g.goods_id where v.card_sn = '$card_sn' and v.is_saled = 1 and v.supplier_id = $supplier_id";
        $res = $db -> getRow($sql);
        if($res){
        $res['end_date'] = local_date('Y-m-d',$res['end_date']);
        $res['buy_date'] = empty($res['buy_date'])?'未使用':local_date('Y-m-d',$res['buy_date']);
        $res['is_verification'] = $res['is_verification']==1?'已验证':'未验证';
        }else{
             $res['msg'] = '0';
        }
        $smarty -> assign('result',$res);
    }
    $smarty->assign('ur_here', $_LANG['virtual_verification']);
    $smarty -> display("virtual_validate.htm");
}
/*
 * 虚拟商品验证
 */
if($_REQUEST['act']=='verification'){
    $result = array();
    $card_sn = trim($_REQUEST['verification']);
    $now = gmtime();
    if(!empty($card_sn)){
        $sql = "select * from ".$ecs->table('virtual_goods_card')." where card_sn = '$card_sn' and is_saled = 1 and supplier_id = $_SESSION[supplier_id]";
        $card = $db -> getRow($sql);
        if($card){
            if($card['is_verification'] == '0'){   
                if($card['end_date'] >= $now){           
                    $result['msg'] = '1';
                    $result['is_verification'] = $_LANG['message']['verification_success']; 
                }else{
                    $result['msg'] = '3';
                    $result['is_verification'] = $_LANG['message']['verification_overdue'];
                }
            }else{
                $result['msg'] = '2';
                $result['is_verification'] = $_LANG['message']['verification_used'];
            }
        }else{
            $result['msg'] = '0';
            $result['is_verification'] = $_LANG['message']['not_found_verification'];
        }
    }
   if($result['msg'] == '1'){
        $sql1 = "update ".$ecs->table('virtual_goods_card')." set is_verification = 1 , buy_date = ".gmtime()." where card_sn = '$card_sn'";
        $db -> query($sql1);
 
         /* 改变订单状态 */
        if($card){
             $sql = "select count(*) from ".$ecs->table('virtual_goods_card')." where card_sn = '$card_sn' and  supplier_id = $_SESSION[supplier_id] and is_verification=1";
            if($db->getOne($sql)){
                $sql = "update ".$ecs->table('order_info')." set order_status = '5', shipping_status = '2' , shipping_time_end = ".gmtime()." where order_sn = '$card[order_sn]'";
            }else{
                $sql = "update ".$ecs->table('order_info')." set shipping_status = '2' , shipping_time_end = ".gmtime()." where order_sn = '$card[order_sn]'";
            }
            $db -> query($sql);
                        
       }
   }
    
    $goods = $db -> getRow("select goods_name,goods_thumb,shop_price from ".$ecs->table('goods')." where goods_id = $card[goods_id]");
    $result['goods_name'] = $goods['goods_name'];
    $result['goods_thumb'] = $goods['goods_thumb'];
    $result['order_sn'] = $card['order_sn'];
    $result['end_date'] = local_date('Y-m-d',$card['end_date']);
    $result['card_id'] = $card['card_id'];
    $result['card_sn'] = $card['card_sn'];
    $result['buy_date'] = empty($res['buy_date'])?'未使用':local_date('Y-m-d',$res['buy_date']);
    $result['shop_price'] = $goods['shop_price'];
    $smarty->assign('ur_here', $_LANG['virtual_verification']);
    $smarty -> assign('result',$result);
    $smarty -> display("virtual_validate.htm");
}
 
/*------------------------------------------------------ */
//-- 虚拟卡列表,用于排序、翻页
/*------------------------------------------------------ */
 
elseif ($_REQUEST['act'] == 'query_card')
{
    $list = get_virtual_order_list();
 
    $smarty->assign('order_list',    $list['item']);
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);
 
    $sort_flag = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);
 
    make_json_result($smarty->fetch('virtual_goods_card_list.htm'), '',
        array('filter'=>$list['filter'], 'page_count'=>$list['page_count']));
}
 
/* 批量删除card */
elseif ($_REQUEST['act'] == 'batch_drop_card')
{
    /* 检查权限 */
    admin_priv('virualcard');
 
    $num = count($_POST['checkboxes']);
    $sql = "DELETE FROM ".$ecs->table('virtual_goods_card')." WHERE card_id ".db_create_in(implode(',',$_POST['checkboxes']));
    if ($db->query($sql))
    {
        /* 商品数量减$num */
        update_goods_number(intval($_REQUEST['goods_id']));
        $link[] = array('text'=>$_LANG['go_list'], 'href'=>'virtual_goods_card.php?act=card&goods_id='.$_REQUEST['goods_id']);
        sys_msg($_LANG['action_success'], 0, $link);
    }
}
 
elseif ($_REQUEST['act'] == 'del_virtual_order')
{
    /* 检查权限 */
    admin_priv('virualcard');
 
    $order_ids = $_POST['checkboxes'];
    foreach($order_ids as $order_id){
         $sql = " update ".$ecs->table('order_info')." set order_status = ".OS_INVALID." WHERE order_id = $order_id";
         $db->query($sql); 
         $order_sn = $db->getOne("select order_sn from ".$ecs->table('order_info')." where order_id = $order_id");
         $db->query("update ".$ecs->table('virtual_goods_card')." set is_saled = 0 where order_sn = '$order_sn'"); 
    }
        //设置验证码为未出售
    
        $link[] = array('text'=>'返回订单列表', 'href'=>'virtual_goods_card.php?act=all_card');
        sys_msg($_LANG['action_success'], 0, $link);   
}
/* 批量上传页面 */
 
elseif ($_REQUEST['act'] == 'batch_card_add')
{
    /* 检查权限 */
    admin_priv('virualcard');
 
    $smarty->assign('ur_here',          $_LANG['batch_card_add']);
    $smarty->assign('action_link',      array('text'=>$_LANG['virtual_card_list'], 'href'=>'virtual_goods.php?act=list&extension_code=virtual_card'));
    $smarty->assign('goods_id',           $_REQUEST['goods_id']);
    $smarty->display('batch_card_info.htm');
}
 
elseif ($_REQUEST['act'] == 'batch_confirm')
{
    /* 检查上传是否成功 */
    if ($_FILES['uploadfile']['tmp_name'] == '' || $_FILES['uploadfile']['tmp_name'] == 'none')
    {
        sys_msg($_LANG['uploadfile_fail'], 1);
    }
 
    $data = file($_FILES['uploadfile']['tmp_name']);
    $rec = array(); //数据数组
    $i = 0;
    $separator = trim($_POST['separator']);
    foreach ($data as $line)
    {
        $row = explode($separator, $line);
        switch(count($row))
        {
            case '3':
                $rec[$i]['end_date'] = $row[2];
            case '2':
                $rec[$i]['card_password'] = $row[1];
            case '1':
                $rec[$i]['card_sn']  = $row[0];
                break;
            default:
                $rec[$i]['card_sn']  = $row[0];
                $rec[$i]['card_password'] = $row[1];
                $rec[$i]['end_date'] = $row[2];
                break;
        }
        $i++;
    }
 
    $smarty->assign('ur_here',          $_LANG['batch_card_add']);
    $smarty->assign('action_link',      array('text'=>$_LANG['batch_card_add'], 'href'=>'virtual_goods_card.php?act=batch_card_add&goods_id='.$_REQUEST['goods_id']));
    $smarty->assign('list',               $rec);
    $smarty->display('batch_card_confirm.htm');
 
}
/* 批量上传处理 */
//elseif ($_REQUEST['act'] == 'batch_insert')
//{
//    /* 检查权限 */
//    admin_priv('virualcard');
//
//    $add_time = gmtime();
//    $i = 0;
//    foreach ($_POST['checked'] as $key)
//    {
//        $rec['card_sn']  = encrypt($_POST['card_sn'][$key]);
//        $rec['card_password'] = encrypt($_POST['card_password'][$key]);
//        $rec['crc32']    = crc32(AUTH_KEY);
//        $rec['end_date'] = empty($_POST['end_date'][$key]) ? 0 : strtotime($_POST['end_date'][$key]);
//        $rec['goods_id'] = $_POST['goods_id'];
//        $rec['add_date'] = $add_time;
//        $db->AutoExecute($ecs->table('virtual_goods_card'), $rec, 'INSERT');
//        $i++;
//    }
//
//    /* 更新商品库存 */
//    update_goods_number(intval($_REQUEST['goods_id']));
//    $link[] = array('text' => $_LANG['card'] , 'href' => 'virtual_goods_card.php?act=card&goods_id='.$_POST['goods_id']);
//    sys_msg(sprintf($_LANG['batch_card_add_ok'], $i) , 0, $link);
//}
 
/*------------------------------------------------------ */
//-- 更改加密串
/*------------------------------------------------------ */
 
//elseif ($_REQUEST['act'] == 'change')
//{
//    /* 检查权限 */
//    admin_priv('virualcard');
//
//    $smarty->assign('ur_here', $_LANG['virtual_card_change']);
//
//    assign_query_info();
//    $smarty->display('virtual_card_change.htm');
//}
 
/*------------------------------------------------------ */
//-- 提交更改
/*------------------------------------------------------ */
 
//elseif ($_REQUEST['act'] == 'submit_change')
//{
//    /* 检查权限 */
//    admin_priv('virualcard');
//
//    if (isset($_POST['old_string']) && isset($_POST['new_string']))
//    {
//        // 检查原加密串是否正确
//        if ($_POST['old_string'] != OLD_AUTH_KEY)
//        {
//            sys_msg($_LANG['invalid_old_string'], 1);
//        }
//
//        // 检查新加密串是否正确
//        if ($_POST['new_string'] != AUTH_KEY)
//        {
//            sys_msg($_LANG['invalid_new_string'], 1);
//        }
//
//        // 检查原加密串和新加密串是否相同
//        if ($_POST['old_string'] == $_POST['new_string'] || crc32($_POST['old_string']) == crc32($_POST['new_string']))
//        {
//            sys_msg($_LANG['same_string'], 1);
//        }
//
//
//
//        // 重新加密卡号和密码
//        $old_crc32 = crc32($_POST['old_string']);
//        $new_crc32 = crc32($_POST['new_string']);
//        $sql = "SELECT card_id, card_sn, card_password FROM " . $ecs->table('virtual_goods_card') . " WHERE crc32 = '$old_crc32'";
//        $res = $db->query($sql);
//        while ($row = $db->fetchRow($res))
//        {
//            $row['card_sn'] = encrypt(decrypt($row['card_sn'], $_POST['old_string']), $_POST['new_string']);
//            $row['card_password'] = encrypt(decrypt($row['card_password'], $_POST['old_string']), $_POST['new_string']);
//            $row['crc32'] = $new_crc32;
//            $db->autoExecute($ecs->table('virtual_goods_card'), $row, 'UPDATE', 'card_id = ' . $row['card_id']);
//        }
//
//        // 记录日志
//        //admin_log();
//
//        // 返回
//        sys_msg($_LANG['change_key_ok'], 0, array(array('href' => 'virtual_goods_card.php?act=list', 'text' => $_LANG['virtual_card_list'])));
//    }
//}
 
/*------------------------------------------------------ */
//-- 切换是否已出售状态
/*------------------------------------------------------ */
 
elseif ($_REQUEST['act'] == 'toggle_sold')
{
    check_authz_json('virualcard');
 
    $id = intval($_POST['id']);
    $val = intval($_POST['val']);
 
    $sql = "UPDATE ".$ecs->table('virtual_goods_card')." SET is_saled= '$val' WHERE card_id='$id'";
 
    if ($db->query($sql, 'SILENT'))
    {
        /* 修改商品库存 */
        $sql = "SELECT goods_id FROM " . $ecs->table('virtual_goods_card') . " WHERE card_id = '$id' LIMIT 1";
        $goods_id = $db->getOne($sql);
 
        update_goods_number($goods_id);
        make_json_result($val);
    }
    else
    {
        make_json_error($_LANG['action_fail'] . "\n" .$db->error());
    }
}
 
/*------------------------------------------------------ */
//-- 删除卡片
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'remove_card')
{
    check_authz_json('virualcard');
 
    $id = intval($_GET['id']);
 
    $row = $db->GetRow('SELECT card_sn, goods_id FROM ' . $ecs->table('virtual_goods_card') . " WHERE card_id = '$id'");
 
    $sql = 'DELETE FROM ' . $ecs->table('virtual_goods_card') . " WHERE card_id = '$id'";
    if ($db->query($sql, 'SILENT'))
    {
        /* 修改商品数量 */
        update_goods_number($row['goods_id']);
 
        $url = 'virtual_goods_card.php?act=query_card&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
 
        ecs_header("Location: $url\n");
        exit;
    }
    else
    {
        make_json_error($db->error());
    }
}
 
 
elseif ($_REQUEST['act'] == 'on_change')
{
   // 重新加密卡号和密码
   $each_num    = 1;
   $old_crc32   = crc32(OLD_AUTH_KEY);
   $new_crc32   = crc32(AUTH_KEY);
   $updated     = intval($_GET['updated']);
 
   $sql = "SELECT card_id, card_sn, card_password ".
            " FROM " . $ecs->table('virtual_goods_card') .
            " WHERE crc32 = '$old_crc32' LIMIT $each_num";
   $res = $db->query($sql);
 
   while ($row = $db->fetchRow($res))
   {
       $row['card_sn']       = encrypt(decrypt($row['card_sn'], OLD_AUTH_KEY));
       $row['card_password'] = encrypt(decrypt($row['card_password'], OLD_AUTH_KEY));
       $row['crc32']         = $new_crc32;
 
       if (!$db->autoExecute($ecs->table('virtual_goods_card'), $row, 'UPDATE', 'card_id = ' . $row['card_id']))
       {
           make_json_error($updated, 0, $_LANG['update_error'] ."\n". $db->error());
       }
 
       $updated++;
    }
 
    // 查询是否还有未更新的
    $sql      = "SELECT COUNT(*) FROM " . $ecs->table('virtual_goods_card') . " WHERE crc32 = '$old_crc32' ";
    $left_num = $db->getOne($sql);
 
    if ($left_num > 0)
    {
        make_json_result($updated);
    }
    else
    {
        // 查询统计信息
        $stat = array('new' => 0, 'unknown' => 0);
        $sql = "SELECT crc32, count(*) AS cnt FROM " . $GLOBALS['ecs']->table('virtual_goods_card') . " GROUP BY crc32";
        $res = $GLOBALS['db']->query($sql);
        while ($row = $db->fetchRow($res))
        {
            if ($new_crc32 == $row['crc32'])
            {
                $stat['new'] += $row['cnt'];
            }
            else
            {
                $stat['unknown'] += $row['cnt'];
            }
        }
 
        make_json_result($updated, sprintf($_LANG['new_stat'], $stat['new'], $stat['unknown']));
    }
}
 
 
elseif ($_REQUEST['act'] == 'info')
{
    /* 根据订单id或订单号查询订单信息 */
    if (isset($_REQUEST['order_id']))
    {
        $order_id = intval($_REQUEST['order_id']);
        $order = order_info($order_id);
    }
    elseif (isset($_REQUEST['order_sn']))
    {
        $order_sn = trim($_REQUEST['order_sn']);
        $order = order_info(0, $order_sn);
    }
    else
    {
        /* 如果参数不存在,退出 */
        die('invalid parameter');
    }
 
    /* 如果订单不存在,退出 */
    if (empty($order))
    {
        die('order does not exist');
    }
 
   
 
    /* 如果管理员属于某个办事处,检查该订单是否也属于这个办事处 */
    $sql = "SELECT agency_id FROM " . $ecs->table('admin_user') . " WHERE user_id = '$_SESSION[admin_id]'";
    $agency_id = $db->getOne($sql);
    if ($agency_id > 0)
    {
        if ($order['agency_id'] != $agency_id)
        {
            sys_msg($_LANG['priv_error']);
        }
    }
    
    //如果为预售活动则需要判断预售活动是否已经成功结束了,如果未成功结束则不允许发货
    if($order['extension_code'] == PRE_SALE_CODE)
    {
        $pre_sale_id = $order['extension_id'];
        $sql = "select is_finished from " . $ecs->table('goods_activity') . " where act_id = '" . $pre_sale_id . "'";
        $is_finished = $db->getOne($sql);
        if($is_finished == PSS_SUCCEED)
        {
            $smarty->assign('pre_sale_success', '1');
        }
        else
        {
            $smarty->assign('pre_sale_success', '0');
        }
        $smarty->assign('is_pre_sale', '1');
    }
    else
    {
        $smarty->assign('is_pre_sale', '0');
    }
 
    /* 取得上一个、下一个订单号 */
    if (!empty($_COOKIE['ECSCP']['lastfilter']))
    {
        $filter = unserialize(urldecode($_COOKIE['ECSCP']['lastfilter']));
        if (!empty($filter['composite_status']))
        {
            $where = '';
            //综合状态
            switch($filter['composite_status'])
            {
                case CS_AWAIT_PAY :
                    $where .= order_query_sql('await_pay');
                    break;
 
                case CS_AWAIT_SHIP :
                    $where .= order_query_sql('await_ship');
                    break;
 
                case CS_FINISHED :
                    $where .= order_query_sql('finished');
                    break;
 
                default:
                    if ($filter['composite_status'] != -1)
                    {
                        $where .= " AND o.order_status = '$filter[composite_status]' ";
                    }
            }
        }
    }
    $sql = "SELECT MAX(order_id) FROM " . $ecs->table('order_info') . " as o WHERE supplier_id = '$order[supplier_id]' and order_id < '$order[order_id]' and extension_code = 'virtual_good'";
    if ($agency_id > 0)
    {
        $sql .= " AND agency_id = '$agency_id'";
    }
    if (!empty($where))
    {
        $sql .= $where;
    }
    $smarty->assign('prev_id', $db->getOne($sql));
    $sql = "SELECT MIN(order_id) FROM " . $ecs->table('order_info') . " as o WHERE supplier_id = '$order[supplier_id]' and order_id > '$order[order_id]' and extension_code = 'virtual_good'";
 
    if ($agency_id > 0)
    {
        $sql .= " AND agency_id = '$agency_id'";
    }
    if (!empty($where))
    {
        $sql .= $where;
    }
    $smarty->assign('next_id', $db->getOne($sql));
 
    /* 取得用户名 */
    if ($order['user_id'] > 0)
    {
        $user = user_info($order['user_id']);
        if (!empty($user))
        {
            $order['user_name'] = $user['user_name'];
        }
    }
 
    /* 取得所有办事处 */
    $sql = "SELECT agency_id, agency_name FROM " . $ecs->table('agency');
    $smarty->assign('agency_list', $db->getAll($sql));
 
    /* 取得区域名 */
    $sql = "SELECT concat(IFNULL(c.region_name, ''), '  ', IFNULL(p.region_name, ''), " .
                "'  ', IFNULL(t.region_name, ''), '  ', IFNULL(d.region_name, '')) AS region " .
            "FROM " . $ecs->table('order_info') . " AS o " .
                "LEFT JOIN " . $ecs->table('region') . " AS c ON o.country = c.region_id " .
                "LEFT JOIN " . $ecs->table('region') . " AS p ON o.province = p.region_id " .
                "LEFT JOIN " . $ecs->table('region') . " AS t ON o.city = t.region_id " .
                "LEFT JOIN " . $ecs->table('region') . " AS d ON o.district = d.region_id " .
            "WHERE o.order_id = '$order[order_id]'";
    $order['region'] = $db->getOne($sql);
 
    /* 格式化金额 */
    if ($order['order_amount'] < 0)
    {
        $order['money_refund']          = abs($order['order_amount']);
        $order['formated_money_refund'] = price_format(abs($order['order_amount']));
    }
 
    /* 其他处理 */
    $order['order_time']    = local_date($_CFG['time_format'], $order['add_time']);
    $order['pay_time']      = $order['pay_time'] > 0 ?
        local_date($_CFG['time_format'], $order['pay_time']) : $_LANG['ps'][PS_UNPAYED];
    $order['shipping_time'] = $order['shipping_time'] > 0 ?
        local_date($_CFG['time_format'], $order['shipping_time']) : $_LANG['ss'][SS_UNSHIPPED];
    $order['status']        = $_LANG['os'][$order['order_status']] . ',' . $_LANG['ps'][$order['pay_status']] . ',' . $_LANG['ss'][$order['shipping_status']];
    $order['invoice_no']    = $order['shipping_status'] == SS_UNSHIPPED || $order['shipping_status'] == SS_PREPARING ? $_LANG['ss'][SS_UNSHIPPED] : $order['invoice_no'];
 
    /* 取得订单的来源 */
    if ($order['from_ad'] == 0)
    {
        $order['referer'] = empty($order['referer']) ? $_LANG['from_self_site'] : $order['referer'];
    }
    elseif ($order['from_ad'] == -1)
    {
        $order['referer'] = $_LANG['from_goods_js'] . ' ('.$_LANG['from'] . $order['referer'].')';
    }
    else
    {
        /* 查询广告的名称 */
         $ad_name = $db->getOne("SELECT ad_name FROM " .$ecs->table('ad'). " WHERE ad_id='$order[from_ad]'");
         $order['referer'] = $_LANG['from_ad_js'] . $ad_name . ' ('.$_LANG['from'] . $order['referer'].')';
    }
 
    /* 此订单的发货备注(此订单的最后一条操作记录) */
    $sql = "SELECT action_note FROM " . $ecs->table('order_action').
           " WHERE order_id = '$order[order_id]' AND shipping_status = 1 ORDER BY log_time DESC";
    $order['invoice_note'] = $db->getOne($sql);
 
    /* 取得订单商品总重量 */
 
 
 
    /* 参数赋值:订单 */
    $smarty->assign('order', $order);
 
    /* 取得用户信息 */
    if ($order['user_id'] > 0)
    {
        /* 用户等级 */
        if ($user['user_rank'] > 0)
        {
            $where = " WHERE rank_id = '$user[user_rank]' ";
        }
        else
        {
            $where = " WHERE min_points <= " . intval($user['rank_points']) . " ORDER BY min_points DESC ";
        }
        $sql = "SELECT rank_name FROM " . $ecs->table('user_rank') . $where;
        $user['rank_name'] = $db->getOne($sql);
 
        // 用户红包数量
        $day    = getdate();
        $today  = local_mktime(23, 59, 59, $day['mon'], $day['mday'], $day['year']);
        $sql = "SELECT COUNT(*) " .
                "FROM " . $ecs->table('bonus_type') . " AS bt, " . $ecs->table('user_bonus') . " AS ub " .
                "WHERE bt.type_id = ub.bonus_type_id " .
                "AND ub.user_id = '$order[user_id]' " .
                "AND ub.order_id = 0 " .
                "AND bt.use_start_date <= '$today' " .
                "AND bt.use_end_date >= '$today'";
        $user['bonus_count'] = $db->getOne($sql);
        $smarty->assign('user', $user);
 
        // 地址信息
        $sql = "SELECT * FROM " . $ecs->table('user_address') . " WHERE user_id = '$order[user_id]'";
        $smarty->assign('address_list', $db->getAll($sql));
    }
 
    /* 取得订单商品及货品 */
    $goods_list = array();
    $goods_attr = array();
    $sql = "SELECT o.*, IF(o.product_id > 0, p.product_number, g.goods_number) AS storage, o.goods_attr, g.suppliers_id, IFNULL(b.brand_name, '') AS brand_name, p.product_sn
            FROM " . $ecs->table('order_goods') . " AS o
                LEFT JOIN " . $ecs->table('products') . " AS p
                    ON p.product_id = o.product_id
                LEFT JOIN " . $ecs->table('goods') . " AS g
                    ON o.goods_id = g.goods_id
                LEFT JOIN " . $ecs->table('brand') . " AS b
                    ON g.brand_id = b.brand_id
            WHERE o.order_id = '$order[order_id]'";
    $res = $db->query($sql);
    while ($row = $db->fetchRow($res))
    {
        /* 虚拟商品支持 */
        if ($row['is_real'] == 0)
        {
            /* 取得语言项 */
            $filename = ROOT_PATH . 'plugins/' . $row['extension_code'] . '/languages/common_' . $_CFG['lang'] . '.php';
            if (file_exists($filename))
            {
                include_once($filename);
                if (!empty($_LANG[$row['extension_code'].'_link']))
                {
                    $row['goods_name'] = $row['goods_name'] . sprintf($_LANG[$row['extension_code'].'_link'], $row['goods_id'], $order['order_sn']);
                }
            }
        }
 
        $row['formated_subtotal']       = price_format($row['goods_price'] * $row['goods_number']);
        $row['formated_goods_price']    = price_format($row['goods_price']);
 
        $goods_attr[] = explode(' ', trim($row['goods_attr'])); //将商品属性拆分为一个数组
 
        if ($row['extension_code'] == 'package_buy')
        {
            $row['storage'] = '';
            $row['brand_name'] = '';
            $row['package_goods_list'] = get_package_goods($row['goods_id']);
        }
        $sql_back = "SELECT bg.*, bo.back_type FROM " . $ecs->table('back_goods') . " AS bg " .
                    " LEFT JOIN " . $ecs->table('back_order') . " AS bo " .
                    " ON bg.back_id = bo.back_id " .
                    " WHERE bo.order_id = " . $order['order_id'] .
                    " AND bg.goods_id = " . $row['goods_id'] .
                    " AND bg.product_id = " . $row['product_id'] .
                    " AND bg.status_back < 6";
        $back_info = $db->getRow($sql_back);
        
        if (count($back_info['back_id']) > 0)
        {
            switch ($back_info['status_back'])
            {
                case '3' : $sb = "已完成"; break;
                case '5' : $sb = "已申请"; break;
                //case '6' : $sb = ""; break;
                //case '7' : $sb = ""; break;
                default : $sb = "正在"; break;
            }
            
            switch ($back_info['back_type'])
            {
                case '1' : $bt = "退货"; break;
                case '3' : $bt = "申请维修"; break;
                case '4' : $bt = "退款"; break;
                default : break;
            }
            
            $shouhou = $sb." ".$bt;
        }
        else
        {
            $shouhou = "正常";
        }
        
        $row['shouhou'] = $shouhou;
 
        $goods_list[] = $row;
    }
 
    $attr = array();
    $arr  = array();
    foreach ($goods_attr AS $index => $array_val)
    {
        foreach ($array_val AS $value)
        {
            $arr = explode(':', $value);//以 : 号将属性拆开
            $attr[$index][] =  @array('name' => $arr[0], 'value' => $arr[1]);
        }
    }
 
    $smarty->assign('goods_attr', $attr);
    $smarty->assign('goods_list', $goods_list);
 
    /* 取得订单操作记录 */
    $act_list = array();
    $sql = "SELECT * FROM " . $ecs->table('order_action') . " WHERE order_id = '$order[order_id]' ORDER BY log_time DESC,action_id DESC";
    $res = $db->query($sql);
    while ($row = $db->fetchRow($res))
    {
        $row['order_status']    = $_LANG['os'][$row['order_status']];
        $row['pay_status']      = $_LANG['ps'][$row['pay_status']];
        $row['shipping_status'] = $_LANG['ss'][$row['shipping_status']];
        $row['action_time']     = local_date($_CFG['time_format'], $row['log_time']);
        $act_list[] = $row;
    }
    $smarty->assign('action_list', $act_list);
 
    /* 取得是否存在实体商品 */
    $smarty->assign('exist_real_goods', exist_real_goods($order['order_id']));
 
    /* 是否打印订单,分别赋值 */
    if (isset($_GET['print']))
    {
        $smarty->assign('shop_name',    $_CFG['shop_name']);
        $smarty->assign('shop_url',     $ecs->url());
        $smarty->assign('shop_address', $_CFG['shop_address']);
        $smarty->assign('service_phone',$_CFG['service_phone']);
        $smarty->assign('print_time',   local_date($_CFG['time_format']));
        $smarty->assign('action_user',  $_SESSION['admin_name']);
 
        $smarty->template_dir = '../' . DATA_DIR;
        $smarty->display('order_print.html');
    }
    /* 打印快递单 */
    elseif (isset($_GET['shipping_print']))
    {
        //$smarty->assign('print_time',   local_date($_CFG['time_format']));
        //发货地址所在地
        $region_array = array();
        $region_id = !empty($_CFG['shop_country']) ? $_CFG['shop_country'] . ',' : '';
        $region_id .= !empty($_CFG['shop_province']) ? $_CFG['shop_province'] . ',' : '';
        $region_id .= !empty($_CFG['shop_city']) ? $_CFG['shop_city'] . ',' : '';
        $region_id = substr($region_id, 0, -1);
        $region = $db->getAll("SELECT region_id, region_name FROM " . $ecs->table("region") . " WHERE region_id IN ($region_id)");
        if (!empty($region))
        {
            foreach($region as $region_data)
            {
                $region_array[$region_data['region_id']] = $region_data['region_name'];
            }
        }
        $smarty->assign('shop_name',    $_CFG['shop_name']);
        $smarty->assign('order_id',    $order_id);
        $smarty->assign('province', $region_array[$_CFG['shop_province']]);
        $smarty->assign('city', $region_array[$_CFG['shop_city']]);
        $smarty->assign('shop_address', $_CFG['shop_address']);
        $smarty->assign('service_phone',$_CFG['service_phone']);
        $shipping = $db->getRow("SELECT * FROM " . $ecs->table("shipping") . " WHERE shipping_id = " . $order['shipping_id']);
 
        //打印单模式
        if ($shipping['print_model'] == 2)
        {
            /* 可视化 */
            /* 快递单 */
            $shipping['print_bg'] = empty($shipping['print_bg']) ? '' : get_site_root_url() . $shipping['print_bg'];
 
            /* 取快递单背景宽高 */
            if (!empty($shipping['print_bg']))
            {
                $_size = @getimagesize($shipping['print_bg']);
 
                if ($_size != false)
                {
                    $shipping['print_bg_size'] = array('width' => $_size[0], 'height' => $_size[1]);
                }
            }
 
            if (empty($shipping['print_bg_size']))
            {
                $shipping['print_bg_size'] = array('width' => '1024', 'height' => '600');
            }
 
            /* 标签信息 */
            $lable_box = array();
            $lable_box['t_shop_country'] = $region_array[$_CFG['shop_country']]; //网店-国家
            $lable_box['t_shop_city'] = $region_array[$_CFG['shop_city']]; //网店-城市
            $lable_box['t_shop_province'] = $region_array[$_CFG['shop_province']]; //网店-省份
            $lable_box['t_shop_name'] = $_CFG['shop_name']; //网店-名称
            $lable_box['t_shop_district'] = ''; //网店-区/县
            $lable_box['t_shop_tel'] = $_CFG['service_phone']; //网店-联系电话
            $lable_box['t_shop_address'] = $_CFG['shop_address']; //网店-地址
            $lable_box['t_customer_country'] = $region_array[$order['country']]; //收件人-国家
            $lable_box['t_customer_province'] = $region_array[$order['province']]; //收件人-省份
            $lable_box['t_customer_city'] = $region_array[$order['city']]; //收件人-城市
            $lable_box['t_customer_district'] = $region_array[$order['district']]; //收件人-区/县
            $lable_box['t_customer_tel'] = $order['tel']; //收件人-电话
            $lable_box['t_customer_mobel'] = $order['mobile']; //收件人-手机
            $lable_box['t_customer_post'] = $order['zipcode']; //收件人-邮编
            $lable_box['t_customer_address'] = $order['address']; //收件人-详细地址
            $lable_box['t_customer_name'] = $order['consignee']; //收件人-姓名
 
            $gmtime_utc_temp = gmtime(); //获取 UTC 时间戳
            $lable_box['t_year'] = date('Y', $gmtime_utc_temp); //年-当日日期
            $lable_box['t_months'] = date('m', $gmtime_utc_temp); //月-当日日期
            $lable_box['t_day'] = date('d', $gmtime_utc_temp); //日-当日日期
 
            $lable_box['t_order_no'] = $order['order_sn']; //订单号-订单
            $lable_box['t_order_postscript'] = $order['postscript']; //备注-订单
            $lable_box['t_order_best_time'] = $order['best_time']; //送货时间-订单
            $lable_box['t_pigeon'] = '√'; //√-对号
            $lable_box['t_custom_content'] = ''; //自定义内容
 
            //标签替换
            $temp_config_lable = explode('||,||', $shipping['config_lable']);
            if (!is_array($temp_config_lable))
            {
                $temp_config_lable[] = $shipping['config_lable'];
            }
            foreach ($temp_config_lable as $temp_key => $temp_lable)
            {
                $temp_info = explode(',', $temp_lable);
                if (is_array($temp_info))
                {
                    $temp_info[1] = $lable_box[$temp_info[0]];
                }
                $temp_config_lable[$temp_key] = implode(',', $temp_info);
            }
            $shipping['config_lable'] = implode('||,||',  $temp_config_lable);
 
            $smarty->assign('shipping', $shipping);
 
            $smarty->display('print.htm');
        }
        elseif (!empty($shipping['shipping_print']))
        {
            /* 代码 */
            echo $smarty->fetch("str:" . $shipping['shipping_print']);
        }
        else
        {
            $shipping_code = $db->getOne("SELECT shipping_code FROM " . $ecs->table('shipping') . " WHERE shipping_id=" . $order['shipping_id']);
            if ($shipping_code)
            {
                include_once(ROOT_PATH . 'includes/modules/shipping/' . $shipping_code . '.php');
            }
 
            if (!empty($_LANG['shipping_print']))
            {
                echo $smarty->fetch("str:$_LANG[shipping_print]");
            }
            else
            {
                echo $_LANG['no_print_shipping'];
            }
        }
    }
    else
    {
        /* 模板赋值 */
        $smarty->assign('ur_here', $_LANG['order_info']);
        $smarty->assign('action_link', array('href' => 'virtual_goods_card.php?act=all_card', 'text' => $_LANG['01_order_list']));
 
        /* 显示模板 */
        assign_query_info();
        $smarty->display('virtual_order_info.htm');
    }
}
 
 
/**
 * 获取虚拟订单列表
 * @return type
 */
function get_virtual_order_list(){    
    /* 查询条件 */
    $filter['goods_id']    = empty($_REQUEST['goods_id'])    ? 0 : intval($_REQUEST['goods_id']);
    $filter['search_type'] = empty($_REQUEST['search_type']) ? 0 : trim($_REQUEST['search_type']);
    $filter['order_sn']    = empty($_REQUEST['order_sn'])    ? 0 : trim($_REQUEST['order_sn']);
    $filter['keyword']     = empty($_REQUEST['keyword'])     ? 0 : trim($_REQUEST['keyword']);
   // $filter['is_verification'] = empty($_REQUEST['is_verification'])? 0 :trim($_REQUEST['is_verification']);
 
    $filter['supplier_id'] = (empty($_REQUEST['supplier_id']) && $_REQUEST['supplier_id'] != '0')? '' : intval($_REQUEST['supplier_id']);
    
 
    if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
    {
        $filter['keyword'] = json_str_iconv($filter['keyword']);
        
    }
    $filter['sort_by']     = empty($_REQUEST['sort_by'])     ? 'card_id' : trim($_REQUEST['sort_by']);
    $filter['sort_order']  = empty($_REQUEST['sort_order'])  ? 'DESC' : trim($_REQUEST['sort_order']);
 
    $where  = (!empty($filter['goods_id'])) ? " AND goods_id = '" . $filter['goods_id'] . "' " : '';
    $where .= (!empty($filter['order_sn'])) ? " AND order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%' " : '';
  //  $where .= ($filter['is_verification'] !='') ? " AND is_verification = '" . $filter['is_verification'] . "' " : '';
    $where .= (empty($filter['supplier_id']) && $filter['supplier_id'] != '0') ? '' : " AND a.supplier_id = '" . $filter['supplier_id'] . "' ";
    //$where .= " and supplier_id = $_SESSION[supplier_id]";
    if (!empty($filter['keyword']))
    {
        if ($filter['search_type'] == 'card_sn')
        {
            $where .= " AND card_sn = '" .$filter['keyword']. "'";
        }
        else
        {
            $where .= " AND order_sn LIKE '%" . mysql_like_quote($filter['keyword']). "%' ";
        }
    }
 
    $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('order_info') . "  WHERE 1 and  supplier_id = ".$_SESSION['supplier_id']." AND extension_code = 'virtual_good'  and order_status <> '".OS_INVALID."' " .$where;
 
    $filter['record_count'] = $GLOBALS['db']->getOne($sql);
    
    /* 分页大小 */
    $filter = page_and_size($filter);
    $start  = ($filter['page'] - 1) * $filter['page_size'];
 
    /* 查询 */
       $sql = "select a.order_id, a.order_sn, a.order_status, a.pay_status,(". order_amount_field('a.') . ") AS total_fee, b.goods_price, d.supplier_id, c.supplier_name, f.user_name  from ". $GLOBALS['ecs']->table('order_info') .
            "as a left join ". $GLOBALS['ecs']->table('order_goods') ." as b on a.order_id = b.order_id".
            " left join ". $GLOBALS['ecs']->table('goods')." as d on d.goods_id = b.goods_id".
            " left join ".$GLOBALS['ecs']->table('supplier')." as c on c.supplier_id = d.supplier_id".
            " left join ".$GLOBALS['ecs']->table('users')." as f on f.user_id = a.user_id".
            " where a.supplier_id= ".$_SESSION['supplier_id']." and a.extension_code = 'virtual_good' and a.order_status <> '".OS_INVALID."' ".$where.
            " ORDER BY a.order_id DESC ".
            " LIMIT $start, ".$filter['page_size'];
    $all = $GLOBALS['db']->getAll($sql);
 
    $arr = array();
    foreach ($all AS $key => $row)
    {
        $card = get_order_card($row['order_sn']);
        $row['card'] = $card;
        $arr[] = $row;
    }
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
 
/**
 * 返回补货列表
 *
 * @return array
 */
function get_replenish_list()
{
    /* 查询条件 */
    $filter['goods_id']    = empty($_REQUEST['goods_id'])    ? 0 : intval($_REQUEST['goods_id']);
    $filter['search_type'] = empty($_REQUEST['search_type']) ? 0 : trim($_REQUEST['search_type']);
    $filter['order_sn']    = empty($_REQUEST['order_sn'])    ? 0 : trim($_REQUEST['order_sn']);
    $filter['keyword']     = empty($_REQUEST['keyword'])     ? 0 : trim($_REQUEST['keyword']);
    if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
    {
        $filter['keyword'] = json_str_iconv($filter['keyword']);
    }
    $filter['sort_by']     = empty($_REQUEST['sort_by'])     ? 'card_id' : trim($_REQUEST['sort_by']);
    $filter['sort_order']  = empty($_REQUEST['sort_order'])  ? 'DESC' : trim($_REQUEST['sort_order']);
 
    $where  = (!empty($filter['goods_id'])) ? " AND goods_id = '" . $filter['goods_id'] . "' " : '';
    $where .= (!empty($filter['order_sn'])) ? " AND order_sn LIKE '%" . mysql_like_quote($filter['order_sn']) . "%' " : '';
    $where .= " and supplier_id = $_SESSION[supplier_id]";
    if (!empty($filter['keyword']))
    {
        if ($filter['search_type'] == 'card_sn')
        {
            $where .= " AND card_sn = '" .$filter['keyword']. "'";
        }
        else
        {
            $where .= " AND order_sn LIKE '%" . mysql_like_quote($filter['keyword']). "%' ";
        }
    }
 
    $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('virtual_goods_card') . " WHERE 1 $where";
    $filter['record_count'] = $GLOBALS['db']->getOne($sql);
 
    /* 分页大小 */
    $filter = page_and_size($filter);
    $start  = ($filter['page'] - 1) * $filter['page_size'];
    
    /* 查询 */
    $sql = "SELECT card_id, goods_id, card_sn, add_date, end_date, buy_date, is_saled, order_sn, is_verification".
            " FROM ".$GLOBALS['ecs']->table('virtual_goods_card').
            " WHERE 1 ".$where.
            " ORDER BY $filter[sort_by] $filter[sort_order] ".
            " LIMIT $start, ".$filter['page_size'];
    $all = $GLOBALS['db']->getAll($sql);
 
    $arr = array();
    foreach ($all AS $key => $row)
    {
 
        $row['end_date'] = $row['end_date'] == 0 ? '' : date($GLOBALS['_CFG']['date_format'], $row['end_date']);
        $row['add_date'] = $row['add_date'] == 0 ? '' : date($GLOBALS['_CFG']['date_format'], $row['add_date']);
        $row['buy_date'] = $row['buy_date'] == 0 ? '' : date($GLOBALS['_CFG']['date_format'], $row['buy_date']);
        $order_goods = get_orderid_by_ordersn($row['order_sn']);
        $row['order_id'] = $order_goods['order_id'];
        $row['goods_price'] = $order_goods['goods_price']; 
        $row['card_sn'] = str_mid_replace($row['card_sn']); 
        $arr[] = $row;
    }
   
 
    return array('item' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}
 
/**
 * 验证码加密
 * @param type $string
 * @return type
 */
function str_mid_replace($string) {
    if (! $string || !isset($string[1])) return $string;
 
    $len = strlen($string);
    $starNum = floor($len / 2); 
    $noStarNum = $len - $starNum;
    $leftNum = ceil($noStarNum / 2); 
    $rightNum = $noStarNum - $leftNum;
 
    $result = substr($string, 0, $leftNum);
    $result .= str_repeat('*', $starNum);
    $result .= substr($string, $len-$rightNum);
 
    return $result; 
}
/**
 * 更新虚拟商品的商品数量
 *
 * @access  public
 * @param   int     $goods_id
 *
 * @return bool
 */
function update_goods_number($goods_id)
{
    $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('virtual_goods_card') . " WHERE goods_id = '$goods_id' AND is_saled = 0";
    $goods_number = $GLOBALS['db']->getOne($sql);
 
    $sql = "UPDATE " . $GLOBALS['ecs']->table('goods') . " SET goods_number = '$goods_number' WHERE goods_id = '$goods_id' AND extension_code='virtual_card'";
 
    return $GLOBALS['db']->query($sql);
}
 
 
function get_orderid_by_ordersn($order_sn){
    $sql = "select a.order_id, b.goods_price from ". $GLOBALS['ecs']->table('order_info') .
            "as a left join ". $GLOBALS['ecs']->table('order_goods') ." as b on a.order_id = b.order_id".
            " where a.order_sn = '$order_sn'";
    $res = $GLOBALS['db'] -> getRow($sql);
    return $res;
}
 
function get_order_card($order_sn){
    $sql = "SELECT card_id, goods_id, card_sn, add_date, end_date, buy_date, is_saled, is_verification".
          " FROM ".$GLOBALS['ecs']->table('virtual_goods_card')." where order_sn = '".$order_sn."'";
    $card_list = $GLOBALS['db']->getAll($sql);
    foreach($card_list as $k=>$v){
        $card_list[$k]['end_date'] = $v['end_date'] == 0 ? '' : date($GLOBALS['_CFG']['date_format'], $v['end_date']);
        $card_list[$k]['add_date'] = $v['add_date'] == 0 ? '' : date($GLOBALS['_CFG']['date_format'], $v['add_date']);
        $card_list[$k]['buy_date'] = $v['buy_date'] == 0 ? '' : date($GLOBALS['_CFG']['date_format'], $v['buy_date']);
        $card_list[$k]['card_sn'] = str_mid_replace($v['card_sn']);
    }
    return $card_list;
}
?>