/
pre_sale.php
executable file
·1232 lines (1068 loc) · 45.2 KB
/
pre_sale.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
<?php
/**
* ECSHOP 预售活动前台文件
* ============================================================================
* * 版权所有 2008-2015 秦皇岛商之翼网络科技有限公司,并保留所有权利。
* 网站地址: http://www.68ecshop.com;
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
* 使用;不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* $Author: 倪庆洋 $
* $Id: pre_sale.php 17217 2015-06-24 10:57:00Z 倪庆洋 $
*/
define('IN_ECS', true);
require (dirname(__FILE__) . '/includes/init.php');
// require(dirname(__FILE__) . '/includes/lib_goods.php');
if((DEBUG_MODE & 2) != 2)
{
$smarty->caching = true;
}
/* ------------------------------------------------------ */
// -- act 操作项的初始化
/* ------------------------------------------------------ */
if(empty($_REQUEST['act']))
{
// 没有ID则跳转到预售商品详情页
if(empty($_REQUEST['id']))
{
$_REQUEST['act'] = 'list';
}
// 有ID则跳转到预售商品详情页
else
{
$_REQUEST['act'] = 'view';
}
}
$function_name = 'action_' . $_REQUEST['act'];
if(! function_exists($function_name))
{
$function_name = 'action_list';
}
call_user_func($function_name);
return;
/* ------------------------------------------------------ */
// -- 预售商品 --> 预售活动商品列表
/* ------------------------------------------------------ */
function action_list ()
{
$smarty = $GLOBALS['smarty'];
/* 取得预售活动总数 */
$count = pre_sale_count();
if($count > 0)
{
/* 取得每页记录数 */
$size = isset($_CFG['page_size']) && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 12;
/* 计算总页数 */
$page_count = ceil($count / $size);
/* 取得当前页 */
$page = isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;
$page = $page > $page_count ? $page_count : $page;
/* 缓存id:语言 - 每页记录数 - 当前页 */
$cache_id = $_CFG['lang'] . '-' . $size . '-' . $page;
$cache_id = sprintf('%X', crc32($cache_id));
}
else
{
/* 缓存id:语言 */
$cache_id = $_CFG['lang'];
$cache_id = sprintf('%X', crc32($cache_id));
}
assign_template();
/* 如果没有缓存,生成缓存 */
if(! $smarty->is_cached('pre_sale_list.dwt', $cache_id) || true)
{
if($count > 0)
{
/* 取得当前页的预售活动 */
$ps_list = pre_sale_list($size, $page);
$smarty->assign('ps_list', $ps_list);
/* 设置分页链接 */
$pager = get_pager('pre_sale.php', array(
'act' => 'list'
), $count, $page, $size);
$smarty->assign('pager', $pager);
}
/* 模板赋值 */
$smarty->assign('cfg', $_CFG);
assign_template();
$position = assign_ur_here();
$smarty->assign('page_title', $position['title']); // 页面标题
$smarty->assign('ur_here', $position['ur_here']); // 当前位置
$smarty->assign('categories', get_categories_tree()); // 分类树
$smarty->assign('helps', get_shop_help()); // 网店帮助
$smarty->assign('top_goods', get_top10()); // 销售排行
$smarty->assign('promotion_info', get_promotion_info());
$smarty->assign('feed_url', ($_CFG['rewrite'] == 1) ? "feed-typepre_sale.xml" : 'feed.php?type=pre_sale'); // RSS
// URL
assign_dynamic('pre_sale_list');
}
/* 显示模板 */
$smarty->display('pre_sale_list.dwt', $cache_id);
}
/* ------------------------------------------------------ */
// -- 预售商品 --> 商品详情
/* ------------------------------------------------------ */
function action_view ()
{
$smarty = $GLOBALS['smarty'];
$db = $GLOBALS['db'];
$ecs = $GLOBALS['ecs'];
/* 取得参数:预售活动id */
$pre_sale_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
if($pre_sale_id <= 0)
{
ecs_header("Location: pre_sale.php\n");
exit();
}
/* 取得预售活动信息 */
$pre_sale = pre_sale_info($pre_sale_id);
if(empty($pre_sale))
{
ecs_header("Location: pre_sale.php\n");
exit();
}
// elseif ($pre_sale['is_on_sale'] == 0 || $pre_sale['is_alone_sale'] == 0)
// {
// header("Location: ./\n");
// exit;
// }
/* 评价数量 */
$pre_sale['comment_count'] = goods_comment_count($pre_sale['goods_id']);
/* 累计销量 */
$pre_sale['sale_count'] = goods_sale_count($pre_sale['goods_id']);
/* 赠送积分 */
$pre_sale['give_integral'] = $pre_sale['gift_integral'];
/* 缓存id:语言,预售活动id,状态,(如果是进行中)当前数量和是否登录 */
$cache_id = $_CFG['lang'] . '-' . $pre_sale_id . '-' . $pre_sale['status'];
// 活动进行中
if($pre_sale['status'] == PSS_UNDER_WAY)
{
$cache_id = $cache_id . '-' . $pre_sale['valid_goods'] . '-' . intval($_SESSION['user_id'] > 0);
}
$cache_id = sprintf('%X', crc32($cache_id));
/* 如果没有缓存,生成缓存 */
if(! $smarty->is_cached('pre_sale_goods.dwt', $cache_id) || true)
{
$pre_sale['gmt_end_date'] = $pre_sale['end_date'];
$smarty->assign('pre_sale', $pre_sale);
/* 取得预售商品信息 */
$goods_id = $pre_sale['goods_id'];
$goods = get_goods_info($goods_id);
if(empty($goods))
{
ecs_header("Location: pre_sale.php\n");
exit();
}
$goods['url'] = build_uri('goods', array(
'gid' => $goods_id
), $goods['goods_name']);
$goods = array_merge($goods, $pre_sale);
$gift_integral = $pre_sale['gift_integral'];
$goods['give_integral'] = $pre_sale['gift_integral'];
// $parent_cat_id = get_parent_cat_id($goods['cat_id']);
// $goods['child_cat'] = get_child_cat($parent_cat_id); // 相关分类
// $goods['get_cat_brands'] = get_cat_brands($parent_cat_id);; // 同类品牌
$smarty->assign('url', $_SERVER["REQUEST_URI"]);
$smarty->assign('volume_price', $goods_volume_price);
$smarty->assign('goods_id', $goods['goods_id']);
$smarty->assign('promote_end_time', $goods['gmt_end_time']);
$smarty->assign('helps', get_shop_help()); // 网店帮助
$smarty->assign('top_goods', get_top10()); // 销售排行
$smarty->assign('promotion_info', get_promotion_info());
// yyy添加start
$count1 = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('comment') . " where comment_type=0 and id_value ='$goods_id' and status=1");
$smarty->assign('review_count', $count1); // 评论数
// 评价晒单 增加 by www.68ecshop.com
$rank_num['rank_a'] = $GLOBALS['db']->getOne("SELECT COUNT(*) AS num FROM " . $GLOBALS['ecs']->table('comment') . " WHERE id_value = '$goods_id' AND status = 1 AND comment_rank in (5,4)");
$rank_num['rank_b'] = $GLOBALS['db']->getOne("SELECT COUNT(*) AS num FROM " . $GLOBALS['ecs']->table('comment') . " WHERE id_value = '$goods_id' AND status = 1 AND comment_rank in (3,2)");
$rank_num['rank_c'] = $GLOBALS['db']->getOne("SELECT COUNT(*) AS num FROM " . $GLOBALS['ecs']->table('comment') . " WHERE id_value = '$goods_id' AND status = 1 AND comment_rank = 1");
$rank_num['rank_total'] = $rank_num['rank_a'] + $rank_num['rank_b'] + $rank_num['rank_c'];
$rank_num['rank_pa'] = ($rank_num['rank_a'] > 0) ? round(($rank_num['rank_a'] / $rank_num['rank_total']) * 100, 1) : 0;
$rank_num['rank_pb'] = ($rank_num['rank_b'] > 0) ? round(($rank_num['rank_b'] / $rank_num['rank_total']) * 100, 1) : 0;
$rank_num['rank_pc'] = ($rank_num['rank_c'] > 0) ? round(($rank_num['rank_c'] / $rank_num['rank_total']) * 100, 1) : 0;
$rank_num['shaidan_num'] = $GLOBALS['db']->getOne("SELECT COUNT(*) AS num FROM " . $GLOBALS['ecs']->table('shaidan') . " WHERE goods_id = '$goods_id' AND status = 1");
$smarty->assign('rank_num', $rank_num);
$res = $GLOBALS['db']->getAll("SELECT * FROM " . $GLOBALS['ecs']->table('goods_tag') . " WHERE goods_id = '$goods_id' AND state = 1");
foreach($res as $v)
{
$v['tag_num'] = $GLOBALS['db']->getOne("SELECT COUNT(*) AS num FROM " . $GLOBALS['ecs']->table('comment') . " WHERE id_value = '$goods_id' AND status = 1 AND FIND_IN_SET($v[tag_id],comment_tag)");
$tag_arr[] = $v;
}
require_once 'includes/lib_comment.php';
$tag_arr = array_sort($tag_arr, 'tag_num', 'desc');
if($tag_arr)
{
foreach($tag_arr as $key => $val)
{
if($_CFG['tag_show_num'] > 0)
{
if(($key + 1) <= $_CFG['tag_show_num'])
{
$comment_tags[] = $val;
}
}
else
{
$comment_tags[] = $val;
}
}
}
$smarty->assign('comment_tags', $comment_tags);
/* meta */
$smarty->assign('keywords', htmlspecialchars($goods['keywords']));
$smarty->assign('description', htmlspecialchars($goods['goods_brief']));
$goods['goods_style_name'] = add_style($goods['goods_name'], $goods['goods_name_style']);
$smarty->assign('goods', $goods);
$smarty->assign('goods_id', $goods['goods_id']);
/* 取得商品的规格 */
$properties = get_goods_properties($goods_id);
$smarty->assign('specification', $properties['spe']); // 商品规格
$smarty->assign('pictures', get_goods_gallery_attr_2($goods_id, $goods_attr_id)); // 商品相册
$smarty->assign('new_goods', get_recommend_goods('new')); // 最新商品
$smarty->assign('shop_country', $_CFG['shop_country']);
/* 代码增加_start By www.ecshop68.com */
$sql_attr = "SELECT a.attr_id, ga.goods_attr_id FROM " . $GLOBALS['ecs']->table('attribute') . " AS a left join " . $GLOBALS['ecs']->table('goods_attr') . " AS ga on a.attr_id=ga.attr_id WHERE a.is_attr_gallery=1 and ga.goods_id='" . $goods_id . "' order by ga.goods_attr_id ";
$goods_attr = $GLOBALS['db']->getRow($sql_attr);
if($goods_attr)
{
$goods_attr_id = $goods_attr['goods_attr_id'];
$smarty->assign('attr_id', $goods_attr['attr_id']);
}
else
{
$smarty->assign('attr_id', 0);
}
$prod_exist_arr = array();
$sql_prod = "select goods_attr from " . $GLOBALS['ecs']->table('products') . " where product_number>0 and goods_id='$goods_id' order by goods_attr";
$res_prod = $GLOBALS['db']->query($sql_prod);
while($row_prod = $GLOBALS['db']->fetchRow($res_prod))
{
$prod_exist_arr[] = "|" . $row_prod['goods_attr'] . "|";
}
$smarty->assign('prod_exist_arr', $prod_exist_arr);
// 模板赋值
$smarty->assign('cfg', $_CFG);
assign_template();
$position = assign_ur_here(0, $goods['goods_name']);
$smarty->assign('page_title', $position['title']); // 页面标题
$smarty->assign('ur_here', $position['ur_here']); // 当前位置
/* 代码增加_start By www.68ecshop.com */
$goods['supplier_name'] = "网站自营";
if($goods['supplier_id'] > 0)
{
$sql_supplier = "SELECT s.supplier_id,s.supplier_name,s.add_time,sr.rank_name FROM " . $ecs->table("supplier") . " as s left join " . $ecs->table("supplier_rank") . " as sr ON s.rank_id=sr.rank_id WHERE s.supplier_id=" . $goods[supplier_id] . " AND s.status=1";
$shopuserinfo = $db->getRow($sql_supplier);
$goods['supplier_name'] = $shopuserinfo['supplier_name'];
get_dianpu_baseinfo($goods['supplier_id'], $shopuserinfo);
}
assign_dynamic('pre_sale_goods');
}
// 更新商品点击次数
$sql = 'UPDATE ' . $GLOBALS['ecs']->table('goods') . ' SET click_count = click_count + 1 ' . "WHERE goods_id = '" . $pre_sale['goods_id'] . "'";
$GLOBALS['db']->query($sql);
$smarty->assign('now_time', gmtime()); // 当前系统时间
$smarty->display('pre_sale_goods.dwt', $cache_id);
}
/* ------------------------------------------------------ */
// -- 预售商品 --> 购买
/* ------------------------------------------------------ */
function action_buy ()
{
$smarty = $GLOBALS['smarty'];
$db = $GLOBALS['db'];
$ecs = $GLOBALS['ecs'];
/* 查询:判断是否登录 */
if($_SESSION['user_id'] <= 0)
{
show_message($_LANG['ps_error_login'], '', '', 'error');
}
/* 查询:取得参数:预售活动id */
$pre_sale_id = isset($_POST['pre_sale_id']) ? intval($_POST['pre_sale_id']) : 0;
if($pre_sale_id <= 0)
{
ecs_header("Location: pre_sale.php\n");
exit();
}
/* 查询:取得数量 */
$number = isset($_POST['number']) ? intval($_POST['number']) : 1;
$number = $number < 1 ? 1 : $number;
/* 查询:取得预售活动信息 */
$pre_sale = pre_sale_info($pre_sale_id, $number);
if(empty($pre_sale))
{
ecs_header("Location: pre_sale.php\n");
exit();
}
/* 查询:检查预售活动是否是进行中 */
if($pre_sale['status'] != PSS_UNDER_WAY)
{
show_message($_LANG['ps_error_status'], '', '', 'error');
}
/* 查询:取得预售商品信息 */
$goods = goods_info($pre_sale['goods_id']);
if(empty($goods))
{
ecs_header("Location: pre_sale.php\n");
exit();
}
/* 查询:判断数量是否足够 */
if(($pre_sale['restrict_amount'] > 0 && $number > ($pre_sale['restrict_amount'] - $pre_sale['valid_goods'])) || $number > $goods['goods_number'])
{
show_message($_LANG['ps_error_goods_lacking'], '', '', 'error');
}
/* 查询:取得规格 */
$specs = '';
foreach($_POST as $key => $value)
{
if(strpos($key, 'spec_') !== false)
{
$specs .= ',' . intval($value);
}
}
$specs = trim($specs, ',');
/* 查询:如果商品有规格则取规格商品信息 配件除外 */
if($specs)
{
$_specs = explode(',', $specs);
$product_info = get_products_info($goods['goods_id'], $_specs);
}
empty($product_info) ? $product_info = array(
'product_number' => 0, 'product_id' => 0
) : '';
/* 查询:判断指定规格的货品数量是否足够 */
if($specs && $number > $product_info['product_number'] && false) // 测试
{
show_message($_LANG['ps_error_goods_lacking'], '', '', 'error');
}
/* 查询:查询规格名称和值,不考虑价格 */
$attr_list = array();
$sql = "SELECT a.attr_name, g.attr_value " . "FROM " . $ecs->table('goods_attr') . " AS g, " . $ecs->table('attribute') . " AS a " . "WHERE g.attr_id = a.attr_id " . "AND g.goods_attr_id " . db_create_in($specs);
$res = $db->query($sql);
while($row = $db->fetchRow($res))
{
$attr_list[] = $row['attr_name'] . ': ' . $row['attr_value'];
}
$goods_attr = join(chr(13) . chr(10), $attr_list);
/* 更新:清空购物车中所有预售商品 */
include_once (ROOT_PATH . 'includes/lib_order.php');
clear_cart(CART_pre_sale_GOODS);
/* 更新:加入购物车 */
$goods_price = $pre_sale['deposit'] > 0 ? $pre_sale['deposit'] : $pre_sale['cur_price'];
$cart = array(
'user_id' => $_SESSION['user_id'], 'session_id' => SESS_ID, 'goods_id' => $pre_sale['goods_id'], 'product_id' => $product_info['product_id'], 'goods_sn' => addslashes($goods['goods_sn']), 'goods_name' => addslashes($goods['goods_name']), 'market_price' => $goods['market_price'], 'goods_price' => $goods_price, 'goods_number' => $number, 'goods_attr' => addslashes($goods_attr), 'goods_attr_id' => $specs, 'is_real' => $goods['is_real'], 'extension_code' => addslashes($goods['extension_code']), 'parent_id' => 0, 'rec_type' => CART_PRE_SALE_GOODS, 'is_gift' => 0
);
$db->autoExecute($ecs->table('cart'), $cart, 'INSERT');
$_SESSION['sel_cartgoods'] = $db->insert_id();
$_SESSION['pre_sale_cart'] = $cart;
/* 更新:记录购物流程类型:预售 */
$_SESSION['flow_type'] = CART_PRE_SALE_GOODS;
$_SESSION['extension_code'] = PRE_SALE_CODE;
$_SESSION['extension_id'] = $pre_sale_id;
/* 进入收货人页面 */
ecs_header("Location: ./flow.php?step=checkout\n");
exit();
}
/* ------------------------------------------------------ */
// -- 预售商品 --> 检查预售订单,取消在尾款支付时间范围内尚未支付尾款的订单
/* ------------------------------------------------------ */
function action_check_order ()
{
$smarty = $GLOBALS['smarty'];
$db = $GLOBALS['db'];
$ecs = $GLOBALS['ecs'];
/* 获取订单状态为 已确认、未付款、未发货 的 预售订单列表 */
$sql = "select a.*, (a.money_paid + a.surplus + a.integral_money + a.bonus) AS total_money, b.ext_info from " . $GLOBALS['ecs']->table('order_info') . " as a LEFT JOIN " . $GLOBALS['ecs']->table('goods_activity') . " as b on (a.extension_id = b.act_id) where (a.money_paid + a.surplus + a.integral_money + a.bonus) > 0 and a.extension_code = '" . PRE_SALE_CODE . "' and b.is_finished = " . PSS_SUCCEED . " and a.shipping_status = " . SS_UNSHIPPED . " and order_status = " . OS_CONFIRMED . " and pay_status = " . PS_UNPAYED;
$order_list = $GLOBALS['db']->getAll($sql);
foreach($order_list as $order)
{
$ext_info = unserialize($order['ext_info']);
// $order = array_merge($order, $ext_info);
// 总金额:goods_amount + shipping_fee + insure_fee + pay_fee + pack_fee +
// card_fee + tax - discount
// $total_fee = $order['goods_amount'] + $order['shipping_fee'] +
// $order['insure_fee'] + $order['pay_fee'] + $order['pack_fee'] +
// $order['card_fee'] + $order['tax'] - $order['discount'];
// 已支付:money_paid + surplus + integral_money + bonus
// $total_money = $order['money_paid'] + $order['surplus'] +
// $order['integral_money'] + $order['bonus'];
// 已付金额
$total_money = $order['total_money'];
// 定金
$deposit = $ext_info['deposit'];
// 定金为0跳过
if($deposit == 0)
{
continue;
}
// 已支付金额等于定金
if($total_money == $deposit)
{
$retainage_start = $ext_info['retainage_start'];
$retainage_end = $ext_info['retainage_end'];
$cur_time = gmtime();
// 当前时间大于尾款支付结束时间则支付超时,取消订单
if($cur_time > $retainage_end)
{
cancel_ps_order($order['order_id'], $order['user_id']);
}
}
}
}
/* 取得预售活动总数 */
function pre_sale_count ()
{
$now = gmtime();
// $sql = "SELECT COUNT(*) " . "FROM " .
// $GLOBALS['ecs']->table('goods_activity') . "WHERE act_type = '" .
// GAT_PRE_SALE . "' " . "AND start_time <= '$now' AND is_finished < 3";
$sql = "SELECT COUNT(*) " . "FROM " . $GLOBALS['ecs']->table('goods_activity') . "WHERE act_type = '" . GAT_PRE_SALE . "' " . " AND is_finished < 3";
return $GLOBALS['db']->getOne($sql);
}
/**
* 取得某页的所有预售活动
*
* @param int $size
* 每页记录数
* @param int $page
* 当前页
* @return array
*/
function pre_sale_list ($size, $page)
{
/* 取得预售活动 */
$ps_list = array();
$now = gmtime();
// $sql = "SELECT b.*, IFNULL(g.goods_thumb, '') AS goods_thumb, b.act_id AS
// pre_sale_id, " . "b.start_time AS start_date, b.end_time AS end_date,
// g.shop_price " . "FROM " . $GLOBALS['ecs']->table('goods_activity') . "
// AS b " . "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON
// b.goods_id = g.goods_id " . "WHERE b.act_type = '" . GAT_PRE_SALE . "' "
// . "AND b.start_time <= '$now' AND b.is_finished < '" . PSS_SUCCEED . "'
// ORDER BY b.act_id DESC";
$sql = "SELECT b.*, IFNULL(g.goods_thumb, '') AS goods_thumb, b.act_id AS pre_sale_id, " . "b.start_time AS start_date, b.end_time AS end_date, g.shop_price " . "FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS b " . "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON b.goods_id = g.goods_id " . "WHERE b.act_type = '" . GAT_PRE_SALE . "' " . " AND b.is_finished < '" . PSS_SUCCEED . "' ORDER BY b.act_id DESC";
$res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
while($pre_sale = $GLOBALS['db']->fetchRow($res))
{
$ext_info = unserialize($pre_sale['ext_info']);
$pre_sale = array_merge($pre_sale, $ext_info);
$stat = pre_sale_stat($row['act_id'], $ext_info['deposit']);
$pre_sale = array_merge($pre_sale, $stat);
/* 格式化时间 */
$pre_sale['formated_start_date'] = local_date($GLOBALS['_CFG']['time_format'], $pre_sale['start_time']);
$pre_sale['formated_end_date'] = local_date($GLOBALS['_CFG']['time_format'], $pre_sale['end_time']);
// 本地时间,用于倒计时显示,符合JS格式
$pre_sale['local_end_date'] = local_date('Y, m-1, d, H, i, s', $pre_sale['end_time']);
$pre_sale['local_start_date'] = local_date('Y, m-1, d, H, i, s', $pre_sale['start_time']);
/* 格式化保证金 */
$pre_sale['formated_deposit'] = price_format($pre_sale['deposit'], false);
/* 处理价格阶梯 */
$price_ladder = $pre_sale['price_ladder'];
if(! is_array($price_ladder) || empty($price_ladder))
{
$price_ladder = array(
array(
'amount' => 0, 'price' => 0
)
);
}
else
{
foreach($price_ladder as $key => $amount_price)
{
$price_ladder[$key]['formated_price'] = price_format($amount_price['price']);
}
}
$pre_sale['price_ladder'] = $price_ladder;
/* 计算当前价 */
$cur_price = $price_ladder[0]['price']; // 初始化
$cur_amount = $stat['valid_goods']; // 当前数量
foreach($price_ladder as $amount_price)
{
if($cur_amount >= $amount_price['amount'])
{
$cur_price = $amount_price['price'];
}
else
{
break;
}
}
$pre_sale['cur_price'] = $cur_price;
$pre_sale['formated_cur_price'] = price_format($cur_price, false);
;
$pre_sale['formated_shope_price'] = price_format($pre_sale['shope_price'], false);
;
$status = pre_sale_status($pre_sale);
$pre_sale['start_time'] = local_date($GLOBALS['_CFG']['date_format'], $pre_sale['start_time']);
$pre_sale['end_time'] = local_date($GLOBALS['_CFG']['date_format'], $pre_sale['end_time']);
$pre_sale['cur_status'] = $GLOBALS['_LANG']['pss'][$status];
$pre_sale['status'] = $status;
/* 处理图片 */
if(empty($pre_sale['goods_thumb']))
{
$pre_sale['goods_thumb'] = get_image_path($pre_sale['goods_id'], $pre_sale['goods_thumb'], true);
}
/* 处理链接 */
$pre_sale['url'] = build_uri('pre_sale', array(
'pre_sale_id' => $pre_sale['pre_sale_id']
));
/* 加入数组 */
$ps_list[] = $pre_sale;
}
return $ps_list;
}
/**
* 取消一个用户的预售订单
*
* @access public
* @param int $order_id
* 订单ID
* @param int $user_id
* 用户ID
*
* @return void
*/
function cancel_ps_order ($order_id, $user_id = 0)
{
/* 查询订单信息,检查状态 */
$sql = "SELECT user_id, order_id, order_sn , surplus , integral , bonus_id, order_status, shipping_status, pay_status FROM " . $GLOBALS['ecs']->table('order_info') . " WHERE order_id = '$order_id' and extension_code = '" . PRE_SALE_CODE . "'";
$order = $GLOBALS['db']->GetRow($sql);
if(empty($order))
{
$GLOBALS['err']->add($GLOBALS['_LANG']['order_exist']);
return false;
}
// 如果用户ID大于0,检查订单是否属于该用户
if($user_id > 0 && $order['user_id'] != $user_id)
{
$GLOBALS['err']->add($GLOBALS['_LANG']['no_priv']);
return false;
}
// 订单状态只能是“未确认”或“已确认”
if($order['order_status'] != OS_UNCONFIRMED && $order['order_status'] != OS_CONFIRMED)
{
$GLOBALS['err']->add($GLOBALS['_LANG']['current_os_not_unconfirmed']);
return false;
}
// 发货状态只能是“未发货”
if($order['shipping_status'] != SS_UNSHIPPED)
{
$GLOBALS['err']->add($GLOBALS['_LANG']['current_ss_not_cancel']);
return false;
}
// 如果付款状态是“已付款”、“付款中”,不允许取消,要取消和商家联系
if($order['pay_status'] != PS_UNPAYED)
{
$GLOBALS['err']->add($GLOBALS['_LANG']['current_ps_not_cancel']);
return false;
}
// 将用户订单设置为取消
$sql = "UPDATE " . $GLOBALS['ecs']->table('order_info') . " SET order_status = '" . OS_CANCELED . "' WHERE order_id = '$order_id'";
if($GLOBALS['db']->query($sql))
{
/* 载入语言文件 */
require (ROOT_PATH . 'languages/' . $_CFG['lang'] . '/user.php');
// 记录log
$note = $GLOBALS['_LANG']['ps_timeout_system_cancel'];
order_action($order['order_sn'], OS_CANCELED, $order['shipping_status'], PS_UNPAYED, $note, 'system');
/**
* // 退货用户余额、积分、红包
* if ($order['user_id'] > 0 && $order['surplus'] > 0)
* {
* $change_desc = sprintf($GLOBALS['_LANG']['return_surplus_on_cancel'],
* $order['order_sn']);
* log_account_change($order['user_id'], $order['surplus'], 0, 0, 0,
* $change_desc);
* }
* if ($order['user_id'] > 0 && $order['integral'] > 0)
* {
* $change_desc =
* sprintf($GLOBALS['_LANG']['return_integral_on_cancel'],
* $order['order_sn']);
* log_account_change($order['user_id'], 0, 0, 0, $order['integral'],
* $change_desc);
* }
* if ($order['user_id'] > 0 && $order['bonus_id'] > 0)
* {
* change_user_bonus($order['bonus_id'], $order['order_id'], false);
* }
*/
// 如果使用库存,且下订单时减库存,则增加库存
if($GLOBALS['_CFG']['use_storage'] == '1' && $GLOBALS['_CFG']['stock_dec_time'] == SDT_PLACE)
{
change_order_goods_storage($order['order_id'], false, 1);
}
/**
* // 修改订单
* $arr = array(
* 'bonus_id' => 0,
* 'bonus' => 0,
* 'integral' => 0,
* 'integral_money' => 0,
* 'surplus' => 0
* );
* update_order($order['order_id'], $arr);
*/
return true;
}
else
{
die($GLOBALS['db']->errorMsg());
}
}
/* ------------------------------------------------------ */
// -- PRIVATE FUNCTION
/* ------------------------------------------------------ */
/**
* 获得指定商品的关联商品
*
* @access public
* @param integer $goods_id
* @return array
*/
function get_linked_goods ($goods_id)
{
$sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price AS org_price, ' . "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, " . 'g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date ' . 'FROM ' . $GLOBALS['ecs']->table('link_goods') . ' lg ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = lg.link_goods_id ' . "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " . "WHERE lg.goods_id = '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 " . "LIMIT " . $GLOBALS['_CFG']['related_goods_number'];
$res = $GLOBALS['db']->query($sql);
$arr = array();
while($row = $GLOBALS['db']->fetchRow($res))
{
$arr[$row['goods_id']]['goods_id'] = $row['goods_id'];
$arr[$row['goods_id']]['goods_name'] = $row['goods_name'];
$arr[$row['goods_id']]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$arr[$row['goods_id']]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$arr[$row['goods_id']]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$arr[$row['goods_id']]['market_price'] = price_format($row['market_price']);
$arr[$row['goods_id']]['shop_price'] = price_format($row['shop_price']);
$arr[$row['goods_id']]['url'] = build_uri('goods', array(
'gid' => $row['goods_id']
), $row['goods_name']);
if($row['promote_price'] > 0)
{
$arr[$row['goods_id']]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$arr[$row['goods_id']]['formated_promote_price'] = price_format($arr[$row['goods_id']]['promote_price']);
}
else
{
$arr[$row['goods_id']]['promote_price'] = 0;
}
}
return $arr;
}
/**
* 获得指定商品的关联文章
*
* @access public
* @param integer $goods_id
* @return void
*/
function get_linked_articles ($goods_id)
{
$sql = 'SELECT a.article_id, a.title, a.file_url, a.open_type, a.add_time ' . 'FROM ' . $GLOBALS['ecs']->table('goods_article') . ' AS g, ' . $GLOBALS['ecs']->table('article') . ' AS a ' . "WHERE g.article_id = a.article_id AND g.goods_id = '$goods_id' AND a.is_open = 1 " . 'ORDER BY a.add_time DESC';
$res = $GLOBALS['db']->query($sql);
$arr = array();
while($row = $GLOBALS['db']->fetchRow($res))
{
$row['url'] = $row['open_type'] != 1 ? build_uri('article', array(
'aid' => $row['article_id']
), $row['title']) : trim($row['file_url']);
$row['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
$row['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
$arr[] = $row;
}
return $arr;
}
/**
* 获得指定商品的各会员等级对应的价格
*
* @access public
* @param integer $goods_id
* @return array
*/
function get_user_rank_prices ($goods_id, $shop_price)
{
$sql = "SELECT rank_id, IFNULL(mp.user_price, r.discount * $shop_price / 100) AS price, r.rank_name, r.discount " . 'FROM ' . $GLOBALS['ecs']->table('user_rank') . ' AS r ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . " AS mp " . "ON mp.goods_id = '$goods_id' AND mp.user_rank = r.rank_id " . "WHERE r.show_price = 1 OR r.rank_id = '$_SESSION[user_rank]'";
$res = $GLOBALS['db']->query($sql);
$arr = array();
while($row = $GLOBALS['db']->fetchRow($res))
{
$arr[$row['rank_id']] = array(
'rank_name' => htmlspecialchars($row['rank_name']), 'price' => price_format($row['price'])
);
}
return $arr;
}
/**
* 获得购买过该商品的人还买过的商品
*
* @access public
* @param integer $goods_id
* @return array
*/
function get_also_bought ($goods_id)
{
$sql = 'SELECT COUNT(b.goods_id ) AS num, g.goods_id, g.goods_name, g.goods_thumb, g.goods_img, g.shop_price, g.promote_price, g.promote_start_date, g.promote_end_date ' . 'FROM ' . $GLOBALS['ecs']->table('order_goods') . ' AS a ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('order_goods') . ' AS b ON b.order_id = a.order_id ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = b.goods_id ' . "WHERE a.goods_id = '$goods_id' AND b.goods_id <> '$goods_id' AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 " . 'GROUP BY b.goods_id ' . 'ORDER BY num DESC ' . 'LIMIT ' . $GLOBALS['_CFG']['bought_goods'];
$res = $GLOBALS['db']->query($sql);
$key = 0;
$arr = array();
while($row = $GLOBALS['db']->fetchRow($res))
{
$arr[$key]['goods_id'] = $row['goods_id'];
$arr[$key]['goods_name'] = $row['goods_name'];
$arr[$key]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
$arr[$key]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$arr[$key]['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);
$arr[$key]['shop_price'] = price_format($row['shop_price']);
$arr[$key]['url'] = build_uri('goods', array(
'gid' => $row['goods_id']
), $row['goods_name']);
if($row['promote_price'] > 0)
{
$arr[$key]['promote_price'] = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
$arr[$key]['formated_promote_price'] = price_format($arr[$key]['promote_price']);
}
else
{
$arr[$key]['promote_price'] = 0;
}
$key ++;
}
return $arr;
}
/**
* 获得指定商品的销售排名
*
* @access public
* @param integer $goods_id
* @return integer
*/
function get_goods_rank ($goods_id)
{
/* 统计时间段 */
$period = intval($GLOBALS['_CFG']['top10_time']);
if($period == 1) // 一年
{
$ext = " AND o.add_time > '" . local_strtotime('-1 years') . "'";
}
elseif($period == 2) // 半年
{
$ext = " AND o.add_time > '" . local_strtotime('-6 months') . "'";
}
elseif($period == 3) // 三个月
{
$ext = " AND o.add_time > '" . local_strtotime('-3 months') . "'";
}
elseif($period == 4) // 一个月
{
$ext = " AND o.add_time > '" . local_strtotime('-1 months') . "'";
}
else
{
$ext = '';
}
/* 查询该商品销量 */
$sql = 'SELECT IFNULL(SUM(g.goods_number), 0) ' . 'FROM ' . $GLOBALS['ecs']->table('order_info') . ' AS o, ' . $GLOBALS['ecs']->table('order_goods') . ' AS g ' . "WHERE o.order_id = g.order_id " . "AND o.order_status = '" . OS_CONFIRMED . "' " . "AND o.shipping_status " . db_create_in(array(
SS_SHIPPED, SS_RECEIVED
)) . " AND o.pay_status " . db_create_in(array(
PS_PAYED, PS_PAYING
)) . " AND g.goods_id = '$goods_id'" . $ext;
$sales_count = $GLOBALS['db']->getOne($sql);
if($sales_count > 0)
{
/* 只有在商品销售量大于0时才去计算该商品的排行 */
$sql = 'SELECT DISTINCT SUM(goods_number) AS num ' . 'FROM ' . $GLOBALS['ecs']->table('order_info') . ' AS o, ' . $GLOBALS['ecs']->table('order_goods') . ' AS g ' . "WHERE o.order_id = g.order_id " . "AND o.order_status = '" . OS_CONFIRMED . "' " . "AND o.shipping_status " . db_create_in(array(
SS_SHIPPED, SS_RECEIVED
)) . " AND o.pay_status " . db_create_in(array(
PS_PAYED, PS_PAYING
)) . $ext . " GROUP BY g.goods_id HAVING num > $sales_count";
$res = $GLOBALS['db']->query($sql);
$rank = $GLOBALS['db']->num_rows($res) + 1;
if($rank > 10)
{
$rank = 0;
}
}
else
{
$rank = 0;
}
return $rank;
}
/**
* 获得商品选定的属性的附加总价格
*
* @param integer $goods_id
* @param array $attr
*
* @return void
*/
function get_attr_amount ($goods_id, $attr)
{
$sql = "SELECT SUM(attr_price) FROM " . $GLOBALS['ecs']->table('goods_attr') . " WHERE goods_id='$goods_id' AND " . db_create_in($attr, 'goods_attr_id');
return $GLOBALS['db']->getOne($sql);
}
/**
* 取得跟商品关联的礼包列表
*
* @param string $goods_id
* 商品编号
*
* @return 礼包列表
*/
function get_package_goods_list ($goods_id)
{
$now = gmtime();
$sql = "SELECT pg.goods_id, ga.act_id, ga.act_name, ga.act_desc, ga.goods_name, ga.start_time,
ga.end_time, ga.is_finished, ga.ext_info
FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS ga, " . $GLOBALS['ecs']->table('package_goods') . " AS pg
WHERE pg.package_id = ga.act_id
AND ga.start_time <= '" . $now . "'
AND ga.end_time >= '" . $now . "'
AND pg.goods_id = " . $goods_id . "
GROUP BY ga.act_id
ORDER BY ga.act_id ";
$res = $GLOBALS['db']->getAll($sql);
foreach($res as $tempkey => $value)
{
$subtotal = 0;
$row = unserialize($value['ext_info']);
unset($value['ext_info']);
if($row)
{
foreach($row as $key => $val)
{
$res[$tempkey][$key] = $val;
}
}
$sql = "SELECT pg.package_id, pg.goods_id, pg.goods_number, pg.admin_id, p.goods_attr, g.goods_sn, g.goods_name, g.market_price, g.goods_thumb, IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS rank_price
FROM " . $GLOBALS['ecs']->table('package_goods') . " AS pg
LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g
ON g.goods_id = pg.goods_id
LEFT JOIN " . $GLOBALS['ecs']->table('products') . " AS p
ON p.product_id = pg.product_id
LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp
ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]'
WHERE pg.package_id = " . $value['act_id'] . "
ORDER BY pg.package_id, pg.goods_id";
$goods_res = $GLOBALS['db']->getAll($sql);
foreach($goods_res as $key => $val)
{
$goods_id_array[] = $val['goods_id'];
$goods_res[$key]['goods_thumb'] = get_image_path($val['goods_id'], $val['goods_thumb'], true);
$goods_res[$key]['market_price'] = price_format($val['market_price']);
$goods_res[$key]['rank_price'] = price_format($val['rank_price']);
$subtotal += $val['rank_price'] * $val['goods_number'];
}
/* 取商品属性 */
$sql = "SELECT ga.goods_attr_id, ga.attr_value
FROM " . $GLOBALS['ecs']->table('goods_attr') . " AS ga, " . $GLOBALS['ecs']->table('attribute') . " AS a
WHERE a.attr_id = ga.attr_id
AND a.attr_type = 1
AND " . db_create_in($goods_id_array, 'goods_id');
$result_goods_attr = $GLOBALS['db']->getAll($sql);