wangtengyu
2018-12-07 f459412e0dac4ed94106da043b4c6f8576bfe496
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
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
<?php
 
define('IN_ECS', true);
 
require('../includes/init.php');
require('./includes/lib_order.php');
include('../includes/cls_json.php');
$json   = new JSON;
/* 载入语言文件 */
require_once('../languages/zh_cn/shopping_flow.php');
require_once('../languages/zh_cn/user.php');
 
$_LANG['your_discount'] = '根据优惠活动<a href="javascript:void(0)" onclick="activity()"><font style="color:#F93">%s</font></a>,您可以享受折扣 %s';
 
$userid = intval($_REQUEST['userid']);
if($userid > 0){
    //需要获取用户登陆的相关信息
    $_SESSION['user_id'] = $userid;
}
$smarty->assign('lang',             $_LANG);
 
$smarty->template_dir = ROOT_PATH . 'json/tpl';//app部分模板所在位置
 
 
/*
 * 获取购物车中的商品
 */
if (!empty($_REQUEST['act']) && $_REQUEST['act'] == 'selcart')
{
    
    $res    = array('error' => 0, 'result' => '', 'message' => '');
    
 
  /* 取得商品列表,计算合计 */
    $cart_goods = get_cart_goods();
    if(count($cart_goods['goods_list'])<=0){
        $res['error'] = 1;
        $res['message'] = '请先添加商品!';
        die($json->encode($res));
    }
 
 
    /* 取得优惠活动 */
    $favourable_list = favourable_list($_SESSION['user_rank']);
 
    if($favourable_list){
        $new_fav = array();
        foreach($favourable_list as $key => $val){
            if(isset($cart_goods['goods_list'][$val['supplier_id']])){
                $cart_goods['goods_list'][$val['supplier_id']]['favourable'][] = $val;
            }
        }
    }
    //echo "<pre>";
    //print_r($cart_goods['goods_list']);
    
    foreach($cart_goods['goods_list'] as $k=>$v){
        $discount = compute_discount($k);
        if(is_array($discount)){
            $cart_goods['goods_list'][$k]['discount']['discount'] = $discount['discount'];
            $favour_name = empty($discount['name']) ? '' : join(',', $discount['name']);
            $cart_goods['goods_list'][$k]['discount']['your_discount'] = sprintf($_LANG['your_discount'], $favour_name, price_format($discount['discount']));
        }
    }
    
    //选择优惠活动中的赠品时所要执行的部分_start
    if(isset($_REQUEST['is_ajax']) && intval($_REQUEST['is_ajax']) > 0){
        $res    = array('error' => 0, 'result' => '');
        if (isset($_REQUEST['suppid']))
        {
            $smarty->assign('favourable_list', $cart_goods['goods_list'][intval($_REQUEST['suppid'])]['favourable']);
            $res['result'] =  $smarty->fetch("favourable_app.lbi");
        }
        else
        {
            $res['result'] = '您一个商品都没选,这怎么行捏!!真的不行哦!';
        }    
        die($json->encode($res));
    }
    //选择优惠活动中的赠品时所要执行的部分_end
    
    $res['result'] = $cart_goods;
    die($json->encode($res));
}
 
/*
 * 修改购物车中商品的数量
 */
if($_REQUEST['step']=='update_group_cart')
{
    $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
    $rec_id = intval($_REQUEST['rec_id']);
    $number = intval($_REQUEST['number']);
    $goods_id = intval($_REQUEST['goods_id']);
    $result['suppid'] = intval($_REQUEST['suppid']);
    $result['rec_id'] = $rec_id;
    $result['number']=$number;
 
 
    
    $goods_number = $GLOBALS['db']->getOne("select goods_number from ".$GLOBALS['ecs']->table('goods')." where goods_id='$goods_id'");
    if($number>$goods_number)
    {
        $result['error'] = 1;
        $result['content'] ='对不起,您选择的数量超出库存您最多可购买'.$goods_number."件";
        $result['number']=$GLOBALS['db']->getOne("select goods_number from ".$GLOBALS['ecs']->table('cart')." where rec_id = '$rec_id'");
        die($json->encode($result));
    }
    
    $min_price = get_min_price($goods_id,$number,$rec_id);//取得当前用户该商品的最小单价
    //file_put_contents('./567.txt',$min_price);
    $sql = "UPDATE " . $GLOBALS['ecs']->table('cart') . " SET goods_number = '$number',goods_price = '$min_price' WHERE rec_id = $rec_id";
    $GLOBALS['db']->query($sql);
 
    /* 取得商品列表,计算合计 */
    $cart_goods = get_cart_goods();
    $result['min_price'] = $min_price;
    //折扣活动
    $result['your_discount'] = '';
    $discount = compute_discount($result['suppid']);
    if(is_array($discount)){
        $favour_name = empty($discount['name']) ? '' : join(',', $discount['name']);
        $result['your_discount'] = sprintf($_LANG['your_discount'], $favour_name, price_format($discount['discount']));
    }
 
    $subtotal = $GLOBALS['db']->getONE("select goods_price * goods_number AS subtotal from ".$GLOBALS['ecs']->table('cart')." where rec_id = $rec_id");
    $result['subtotal'] = price_format($subtotal, false);
    //$result['cart_amount_desc'] = sprintf($_LANG['shopping_money'], $cart_goods['total']['goods_price']);
    $result['cart_amount_desc'] = $cart_goods['total']['goods_price'];
    $shopping_money = sprintf($_LANG['shopping_money'], $cart_goods['total']['goods_price']);
    $result['market_amount_desc'] = $shopping_money;
    if ($_CFG['show_marketprice'])
    {
        $market_price_desc= sprintf($_LANG['than_market_price'],$cart_goods['total']['market_price'], $cart_goods['total']['saving'], $cart_goods['total']['save_rate']);
        $result['market_amount_desc'].= ",".$market_price_desc ;
    }
 
    die($json->encode($result));
}
 
/*------------------------------------------------------ */
//-- 添加商品到购物车
/*------------------------------------------------------ */
elseif ($_REQUEST['step'] == 'add_to_cart')
{
 
    $_REQUEST['goods']=strip_tags(urldecode($_REQUEST['goods']));
    $_REQUEST['goods'] = json_str_iconv($_REQUEST['goods']);
    
    $result = array('error' => 0, 'message' => '', 'content' => '', 'goods_id' => '');
    if (!empty($_REQUEST['goods_id']) && empty($_REQUEST['goods']))
    {
        if (!is_numeric($_REQUEST['goods_id']) || intval($_REQUEST['goods_id']) <= 0)
        {
            $result['error'] = ERR_NOT_EXISTS;
            $result['message'] = '非法操作!';
            die($json->encode($result));
        }
        $goods_id = intval($_REQUEST['goods_id']);
        exit;
    }
 
    if (empty($_REQUEST['goods']))
    {
        $result['error'] = 1;
        die($json->encode($result));
    }
 
    $goods = $json->decode($_REQUEST['goods']);
 
    /* 检查:如果商品有规格,而post的数据没有规格,把商品的规格属性通过JSON传到前台 */
    if (empty($goods->spec) AND empty($goods->quick))
    {
        $sql = "SELECT a.attr_id, a.attr_name, a.attr_type, ".
            "g.goods_attr_id, g.attr_value, g.attr_price " .
        'FROM ' . $GLOBALS['ecs']->table('goods_attr') . ' AS g ' .
        'LEFT JOIN ' . $GLOBALS['ecs']->table('attribute') . ' AS a ON a.attr_id = g.attr_id ' .
        "WHERE a.attr_type != 0 AND g.goods_id = '" . $goods->goods_id . "' " .
        'ORDER BY a.sort_order, g.attr_price, g.goods_attr_id';
 
        $res = $GLOBALS['db']->getAll($sql);
 
        if (!empty($res))
        {
            $spe_arr = array();
            foreach ($res AS $row)
            {
                $spe_arr[$row['attr_id']]['attr_type'] = $row['attr_type'];
                $spe_arr[$row['attr_id']]['name']     = $row['attr_name'];
                $spe_arr[$row['attr_id']]['attr_id']     = $row['attr_id'];
                $spe_arr[$row['attr_id']]['values'][] = array(
                                                            'label'        => $row['attr_value'],
                                                            'price'        => $row['attr_price'],
                                                            'format_price' => price_format($row['attr_price'], false),
                                                            'id'           => $row['goods_attr_id']);
            }
            $i = 0;
            $spe_array = array();
            foreach ($spe_arr AS $row)
            {
                $spe_array[]=$row;
            }
            $result['error']   = ERR_NEED_SELECT_ATTR;
            $result['goods_id'] = $goods->goods_id;
            $result['parent'] = $goods->parent;
            $result['message'] = $spe_array;
 
            die($json->encode($result));
        }
    }
 
    /* 更新:如果是一步购物,先清空购物车 */
    //if ($_CFG['one_step_buy'] == '1')
    if(isset($_REQUEST['shop_cat']) && $_REQUEST['shop_cat'] == 'now')
    {
        if($userid <= 0){
            $result['error']   = 0;
            $result['message'] = '请选登陆!';
            die($json->encode($result));
        }else{
            clear_cart();
        }
    }
 
    /* 检查:商品数量是否合法 */
    if (!is_numeric($goods->number) || intval($goods->number) <= 0)
    {
        $result['error']   = 1;
        $result['message'] = $_LANG['invalid_number'];
    }
    /* 更新:购物车 */
    else
    {
        if(!empty($goods->spec))
        {
            foreach ($goods->spec as  $key=>$val )
            {
                $goods->spec[$key]=intval($val);
            }
        }
        // 更新:添加到购物车
        if (addto_cart($goods->goods_id, $goods->number, $goods->spec, $goods->parent))
        {
            if ($_CFG['cart_confirm'] > 2)
            {
                $result['message'] = '';
            }
            else
            {
                $result['message'] = $_CFG['cart_confirm'] == 1 ? $_LANG['addto_cart_success_1'] : $_LANG['addto_cart_success_2'];
            }
 
            $result['content'] = insert_cart_info();
            $result['one_step_buy'] = $_CFG['one_step_buy'];
        }
        else
        {
            $result['message']  = $err->last_message();
            $result['error']    = $err->error_no;
            $result['goods_id'] = stripslashes($goods->goods_id);
            if (is_array($goods->spec))
            {
                $result['product_spec'] = implode(',', $goods->spec);
            }
            else
            {
                $result['product_spec'] = $goods->spec;
            }
        }
    }
    $rows = $GLOBALS['db']->getRow("select goods_brief,shop_price,goods_name,promote_end_date,promote_start_date,promote_price,goods_thumb from ".$GLOBALS['ecs']->table('goods')." where goods_id=".$goods->goods_id);
    $time1 = gmtime();
    if ($time1 >= $rows['promote_start_date'] && $time1 <= $rows['promote_end_date'] && $rows['promote_price'] > 0)
    {
        $result['shop_price'] = price_format($rows['promote_price']);
    }else{
        $result['shop_price'] = price_format($rows['shop_price']);
    }
    $result['goods_name'] = $rows['goods_name'];
    $result['goods_thumb'] = $rows['goods_thumb'];
    $result['goods_brief'] = $rows['goods_brief'];
    $result['goods_id'] = $goods->goods_id;
    $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";//添加 
    $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount' .
    ' FROM ' . $GLOBALS['ecs']->table('cart') .
    " WHERE ".$sql_where." AND rec_type = '" . CART_GENERAL_GOODS . "'";
    $rowss = $GLOBALS['db']->GetRow($sql);
    $result['goods_price'] = price_format($rowss['amount']);
    $result['goods_number'] = $rowss['number'];
    $result['confirm_type'] = !empty($_CFG['cart_confirm']) ? $_CFG['cart_confirm'] : 2;
    die($json->encode($result));
}
elseif ($_REQUEST['step'] == 'add_favourable')
{
    $result = array('error' => 0, 'message' => '', 'content' => '');
    /* 取得优惠活动信息 */
    $act_id = intval($_POST['act_id']);
    $favourable = favourable_info($act_id);
    if (empty($favourable))
    {
        //show_message($_LANG['favourable_not_exist']);
        $result['error'] = 1;
        $result['message'] = $_LANG['favourable_not_exist'];
        die($json->encode($result));
    }
 
    /* 判断用户能否享受该优惠 */
    if (!favourable_available($favourable))
    {
        //show_message($_LANG['favourable_not_available']);
        $result['error'] = 1;
        $result['message'] = $_LANG['favourable_not_available'];
        die($json->encode($result));
    }
 
    /* 检查购物车中是否已有该优惠 */
    $cart_favourable = cart_favourable();
    if (favourable_used($favourable, $cart_favourable))
    {
        //show_message($_LANG['favourable_used']);
        $result['error'] = 1;
        $result['message'] = $_LANG['favourable_used'];
        die($json->encode($result));
    }
    
    $_POST['gift'] = array_filter(explode(',',$_POST['gift']));
 
    /* 赠品(特惠品)优惠 */
    if ($favourable['act_type'] == FAT_GOODS)
    {
        /* 检查是否选择了赠品 */
        if (empty($_POST['gift']))
        {
            //show_message($_LANG['pls_select_gift']);
            $result['error'] = 1;
            $result['message'] = $_LANG['pls_select_gift'];
            die($json->encode($result));
        }
 
        $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";//添加 
        /* 检查是否已在购物车 */
        $sql = "SELECT goods_name" .
                " FROM " . $ecs->table('cart') .
                " WHERE $sql_where " .
                " AND rec_type = '" . CART_GENERAL_GOODS . "'" .
                " AND is_gift = '$act_id'" .
                " AND goods_id " . db_create_in($_POST['gift']);
        $gift_name = $db->getCol($sql);
        if (!empty($gift_name))
        {
            //show_message(sprintf($_LANG['gift_in_cart'], join(',', $gift_name)));
            $result['error'] = 1;
            $result['message'] = sprintf($_LANG['gift_in_cart'], join(',', $gift_name));
            die($json->encode($result));
        }
 
        /* 检查数量是否超过上限 */
        $count = isset($cart_favourable[$act_id]) ? $cart_favourable[$act_id] : 0;
        if ($favourable['act_type_ext'] > 0 && $count + count($_POST['gift']) > $favourable['act_type_ext'])
        {
            //show_message($_LANG['gift_count_exceed']);
            $result['error'] = 1;
            $result['message'] = $_LANG['gift_count_exceed'];
            die($json->encode($result));
        }
 
        /* 添加赠品到购物车 */
        foreach ($favourable['gift'] as $gift)
        {
            if (in_array($gift['id'], $_POST['gift']))
            {
                add_gift_to_cart($act_id, $gift['id'], $gift['price']);
            }
        }
    }
    elseif ($favourable['act_type'] == FAT_DISCOUNT)
    {
        add_favourable_to_cart($act_id, $favourable['act_name'], cart_favourable_amount($favourable) * (100 - $favourable['act_type_ext']) / 100);
    }
    elseif ($favourable['act_type'] == FAT_PRICE)
    {
        add_favourable_to_cart($act_id, $favourable['act_name'], $favourable['act_type_ext']);
    }
    
    die($json->encode($result));
    /* 刷新购物车 */
    ecs_header("Location: flow.php\n");
    exit;
}
 
/*------------------------------------------------------ */
//-- 删除购物车中的商品
/*------------------------------------------------------ */
 
elseif ($_REQUEST['step'] == 'drop_goods')
{
    $result = array('error' => 0, 'message' => '', 'content' => '');
    $rec_id = intval($_GET['id']);
    flow_drop_cart_goods($rec_id);
    
    die($json->encode($result));
    ecs_header("Location: flow.php\n");
    exit;
}
 
/*
 * 结算页面
 */
elseif ($_REQUEST['step'] == 'checkout')
{
    $result = array('error' => 0, 'result' => '');
    
/*------------------------------------------------------ */
    //-- 订单确认
    /*------------------------------------------------------ */
    
    /*
     * 检查用户是否已经登录
     * 如果没有登录则跳转到登录和注册页面
     */
    if ($_SESSION['user_id'] == 0)
    {
        $result['error'] = 2;
        $result['result'] = $_LANG['no_goods_in_cart'];
        die($json->encode($result));
        exit;
    }
    
 /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
    /* 团购标志 */
    if ($flow_type == CART_GROUP_BUY_GOODS)
    {
        $smarty->assign('is_group_buy', 1);
    }
    /* 积分兑换商品 */
    elseif ($flow_type == CART_EXCHANGE_GOODS)
    {
        $smarty->assign('is_exchange_goods', 1);
    }
    else
    {
        //正常购物流程  清空其他购物流程情况
        $_SESSION['flow_order']['extension_code'] = '';
    }
    
    if($flow_type != CART_EXCHANGE_GOODS){
        //非积分兑换形式的商品
        /* 代码增加_start  By   */
        $sel_cartgoods_count = count($_REQUEST['sel_cartgoods']);
        $_SESSION['sel_cartgoods'] =  $sel_cartgoods_count>0 ? (implode(",", $_REQUEST['sel_cartgoods'])) : $_SESSION['sel_cartgoods'];
        /* 代码增加_end   By   */
        
        //验证购物车中提交过来的商品中参加的活动是否都正常start
        $_REQUEST['sel_goods'] = $_SESSION['sel_cartgoods'];
        $favourable_list = favourable_list($_SESSION['user_rank'],false);
        if($favourable_list){
            $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";
            foreach($favourable_list as $fk=>$fv){
                if(!$fv['available']){
                    $sql = "select count(rec_id) as num from ". $ecs->table('cart') .
                    " WHERE $sql_where " .
                    "AND is_gift = ".$fv['act_id'];
                    if($db->getOne($sql) > 0){
                        $result['error'] = 1;
                        $result['result'] = '购物车中参加['.$fv['act_name'].']活动的商品未满足条件,请重新设置或者将其赠品删除';
                        die($json->encode($result));
                        //show_message('购物车中参加['.$fv['act_name'].']活动的商品未满足条件,请重新设置或者将其赠品删除', '', '', 'warning');
                    }
                }
            }
            unset($sql_where);
        }
    
        //验证购物车中提交过来的商品中参加的活动是否都正常end
    }
 
    /* 检查购物车中是否有商品 */
    /* 代码增加_end  By  */
    $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";
 
    $sql = "SELECT COUNT(*) FROM " . $ecs->table('cart') .
        " WHERE $sql_where " .
        "AND parent_id = 0 AND is_gift = 0 AND rec_type = '$flow_type'";
 
/* 代码增加_end  By  */
    if ($db->getOne($sql) == 0)
    {
        $result['error'] = 1;
        $result['result'] = $_LANG['no_goods_in_cart'];
        die($json->encode($result));
        //show_message($_LANG['no_goods_in_cart'], '', '', 'warning');
    }
 
    /*
     * 检查用户是否已经登录
     * 如果用户已经登录了则检查是否有默认的收货地址
     * 如果没有登录则跳转到登录和注册页面
     */
    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0)
    {
        /* 用户没有登录且没有选定匿名购物,转向到登录页面 */
        $result['error'] = 2;
        $result['result'] = $_LANG['no_goods_in_cart'];
        die($json->encode($result));
        
       // ecs_header("Location: flow.php?step=login\n");
        //exit;
    }
    
    $consignee = get_consignee($_SESSION['user_id']);
    
    //$smarty->assign('consignee', $consignee);
    if(!empty($consignee) && $_SESSION['user_id'] > 0){
        $consignee['address_short_name'] .=  get_region_info($consignee['province'])."-";
        $consignee['address_short_name'] .=  get_region_info($consignee['city'])."-";
        $consignee['address_short_name'] .=  get_region_info($consignee['district'])."&nbsp;";
        $consignee['address_short_name'] .=  sub_str($consignee['address'],16);
        $consignee['address_short_name'] .=  $consignee['zipcode'] ? (",".$consignee['zipcode']) : "";
    }
 
    /* 代码增加_start  By   */
   $smarty->assign('consignee_list', $consignee);
   $smarty->assign('shop_province_list', get_regions(1, $_CFG['shop_country']));
    /* 代码增加_end  By   */
   
   /* 对商品信息赋值 */
    $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
    /*
     * 分供货商显示商品
     */
    $cart_ids = $cart_goods_new = array();
    if(count($cart_goods)>0){
        foreach($cart_goods as $key => $val){
            $cart_goods_new[$val['supplier_id']]['goodlist'][] = $val;
            $cart_ids[] = $val['rec_id'];
        }
    }
    $_SESSION['sel_cartgoods'] = (isset($_SESSION['sel_cartgoods']) && !empty($_SESSION['sel_cartgoods'])) ? $_SESSION['sel_cartgoods'] : implode(',',$cart_ids);//针对app添加
    
    if ($flow_type != CART_EXCHANGE_GOODS && $flow_type != CART_GROUP_BUY_GOODS)
    {
        foreach($cart_goods_new as $k => $v){
            $discount = compute_discount($k);
            if(is_array($discount)){
                $cart_goods_new[$k]['zhekou']['discount'] = $discount['discount'];
                $favour_name = empty($discount['name']) ? '' : join(',', $discount['name']);
                $cart_goods_new[$k]['zhekou']['your_discount'] = sprintf($_LANG['your_discount'], $favour_name, price_format($discount['discount']));
            }
        }
    }
    /* 对是否允许修改购物车赋值 */
    if ($flow_type != CART_GENERAL_GOODS || $_CFG['one_step_buy'] == '1')
    {
        $smarty->assign('allow_edit_cart', 0);
    }
    else
    {
        $smarty->assign('allow_edit_cart', 1);
    }
 
    /*
     * 取得购物流程设置
     */
    $smarty->assign('config', $_CFG);
    /*
     * 取得订单信息
     */
    $order = flow_order_info();
    //file_put_contents('./order.txt',var_export($order,true));
    $smarty->assign('order', $order);
 
    /*
     * 计算订单的费用
     */
    $total = order_fee($order, $cart_goods, $consignee);
 
 
    $smarty->assign('total', $total);
    $smarty->assign('shopping_money', sprintf($_LANG['shopping_money'], $total['formated_goods_price']));
    $smarty->assign('market_price_desc', sprintf($_LANG['than_market_price'], $total['formated_market_price'], $total['formated_saving'], $total['save_rate']));
   
   
   /* 取得配送列表 */
    $region            = array($consignee['country'], $consignee['province'], $consignee['city'], $consignee['district']);
    $shipping_list     = available_shipping_list($region);
    $cart_weight_price = cart_weight_price($flow_type);
    $insure_disabled   = true;
    $cod_disabled      = true;
 
    // 查看购物车中是否全为免运费商品,若是则把运费赋为零
    //$sql = 'SELECT count(*) FROM ' . $ecs->table('cart') . " WHERE `session_id` = '" . SESS_ID. "' AND `extension_code` != 'package_buy' AND `is_shipping` = 0";
    $sql = 'SELECT count(*) FROM ' . $ecs->table('cart') . " WHERE $sql_where AND `extension_code` != 'package_buy' AND `is_shipping` = 0";
    $shipping_count = $db->getOne($sql);
 
    foreach ($shipping_list AS $key => $val)
    {
        $shipping_cfg = unserialize_config($val['configure']);
        $shipping_fee = ($shipping_count == 0 AND $cart_weight_price['free_shipping'] == 1) ? 0 : shipping_fee($val['shipping_code'], unserialize($val['configure']),
        $cart_weight_price['weight'], $cart_weight_price['amount'], $cart_weight_price['number']);
 
        $shipping_list[$key]['format_shipping_fee'] = price_format($shipping_fee, false);
        $shipping_list[$key]['shipping_fee']        = $shipping_fee;
        $shipping_list[$key]['free_money']          = price_format($shipping_cfg['free_money'], false);
        $shipping_list[$key]['insure_formated']     = strpos($val['insure'], '%') === false ?
            price_format($val['insure'], false) : $val['insure'];
 
        /* 当前的配送方式是否支持保价 */
        if ($val['shipping_id'] == $order['shipping_id'])
        {
            $insure_disabled = ($val['insure'] == 0);
            $cod_disabled    = ($val['support_cod'] == 0);
        }
    }
 
    $smarty->assign('shipping_list',   $shipping_list);
    $smarty->assign('insure_disabled', $insure_disabled);
    $smarty->assign('cod_disabled',    $cod_disabled);
    
    /* 取得支付列表 */
    if ($order['shipping_id'] == 0)
    {
        $cod        = true;
        $cod_fee    = 0;
    }
    else
    {
        $shipping = shipping_info($order['shipping_id']);
        $cod = $shipping['support_cod'];
 
        if ($cod)
        {
            /* 如果是团购,且保证金大于0,不能使用货到付款 */
            if ($flow_type == CART_GROUP_BUY_GOODS)
            {
                $group_buy_id = $_SESSION['extension_id'];
                if ($group_buy_id <= 0)
                {
                    //show_message('error group_buy_id');
                         $result['error'] = 1;
                        $result['result'] = 'error group_buy_id';
                        die($json->encode($result));
                }
                $group_buy = group_buy_info($group_buy_id);
                if (empty($group_buy))
                {
                    //show_message('group buy not exists: ' . $group_buy_id);
                    $result['error'] = 1;
                        $result['result'] = 'group buy not exists: ' . $group_buy_id;
                        die($json->encode($result));
                }
 
                if ($group_buy['deposit'] > 0)
                {
                    $cod = false;
                    $cod_fee = 0;
 
                    /* 赋值保证金 */
                    $smarty->assign('gb_deposit', $group_buy['deposit']);
                }
            }
 
            if ($cod)
            {
                $shipping_area_info = shipping_area_info($order['shipping_id'], $region);
                $cod_fee            = $shipping_area_info['pay_fee'];
            }
        }
        else
        {
            $cod_fee = 0;
        }
    }
 
    // 给货到付款的手续费加<span id>,以便改变配送的时候动态显示
    $payment_list = available_payment_list(1, $cod_fee);
    if(isset($payment_list))
    {
        foreach ($payment_list as $key => $payment)
        {
            if ($payment['is_cod'] == '1')
            {
                $payment_list[$key]['format_pay_fee'] = '<span id="ECS_CODFEE">' . $payment['format_pay_fee'] . '</span>';
            }
            /* 如果有易宝神州行支付 如果订单金额大于300 则不显示 */
            if ($payment['pay_code'] == 'yeepayszx' && $total['amount'] > 300)
            {
                unset($payment_list[$key]);
            }
            /* 如果有余额支付 */
            if ($payment['pay_code'] == 'balance')
            {
                /* 如果未登录,不显示 */
                if ($_SESSION['user_id'] == 0)
                {
                    unset($payment_list[$key]);
                }
                else
                {
                    if ($_SESSION['flow_order']['pay_id'] == $payment['pay_id'])
                    {
                        $smarty->assign('disable_surplus', 1);
                    }
                }
            }
        }
    }
    $smarty->assign('payment_list', $payment_list);
    
 
    $user_info = user_info($_SESSION['user_id']);
   
 
    /* 如果使用余额,取得用户余额 */
    if ((!isset($_CFG['use_surplus']) || $_CFG['use_surplus'] == '1')
        && $_SESSION['user_id'] > 0
        && $user_info['user_money'] > 0)
    {
        // 能使用余额
        $smarty->assign('allow_use_surplus', 1);
        $smarty->assign('your_surplus', $user_info['user_money']);
    }
 
    /* 如果使用积分,取得用户可用积分及本订单最多可以使用的积分 */
    if ((!isset($_CFG['use_integral']) || $_CFG['use_integral'] == '1')
        && $_SESSION['user_id'] > 0
        && $user_info['pay_points'] > 0
        && ($flow_type != CART_GROUP_BUY_GOODS && $flow_type != CART_EXCHANGE_GOODS))
    {
        // 能使用积分
        $keyong = flow_available_points();// 可用积分
        foreach($keyong as $k=>$v){
            $cart_goods_new[$k]['jifen'] = $v;
        }
        
        $smarty->assign('allow_use_integral', 1);
        //$smarty->assign('order_max_integral', $keyong);  
        $smarty->assign('your_integral',      $user_info['pay_points']); // 用户积分
    }
 
    /* 如果使用红包,取得用户可以使用的红包及用户选择的红包 */
    if ((!isset($_CFG['use_bonus']) || $_CFG['use_bonus'] == '1')
        && ($flow_type != CART_GROUP_BUY_GOODS && $flow_type != CART_EXCHANGE_GOODS))
    {
        // 取得用户可用红包
        $user_bonus = user_bonus($_SESSION['user_id'], $total['goods_price']);
        if (!empty($user_bonus))
        {
            foreach ($user_bonus AS $key => $val)
            {
                $user_bonus[$key]['bonus_money_formated'] = price_format($val['type_money'], false);
                if(isset($cart_goods_new[$val['supplier_id']])){
                    $cart_goods_new[$val['supplier_id']]['redbag'][] = $user_bonus[$key];
                }
            }
            //file_put_contents('./goodslist.txt',var_export($cart_goods_new,true));   
            
            $smarty->assign('bonus_list', $user_bonus);
        }
 
        // 能使用红包
        $smarty->assign('allow_use_bonus', 1);
    }
    //jx   添加发票插件
     /* 如果能开发票,取得发票内容列表 */
    if ((!isset($_CFG['can_invoice']) || $_CFG['can_invoice'] == '1')
        && isset($_CFG['invoice_content'])
        && trim($_CFG['invoice_content']) != '' && $flow_type != CART_EXCHANGE_GOODS)
    {
        $inv_content_list = explode("\n", str_replace("\r", '', $_CFG['invoice_content']));
        $smarty->assign('inv_content_list', $inv_content_list);
 
        $inv_type_list = array();
        foreach ($_CFG['invoice_type']['type'] as $key => $type)
        {
        /*增值税发票_更改_START_*/
            if (!empty($type)&&$_CFG['invoice_type']['enable'][$key]=='1')
            {
                $inv_type_list[$type] = $_LANG[$type] . ' [' . floatval($_CFG['invoice_type']['rate'][$key]) . '%]';
            }
        /*增值税发票_更改_START_*/
        }
        $smarty->assign('inv_type_list', $inv_type_list);
        $smarty->assign('province_list', get_regions(1, $_CFG['shop_country']));
    }
    //jx   结束
    $smarty->assign('goods_list', $cart_goods_new);
 
    /* 如果使用缺货处理,取得缺货处理列表 */
    if (!isset($_CFG['use_how_oos']) || $_CFG['use_how_oos'] == '1')
    {
        if (is_array($GLOBALS['_LANG']['oos']) && !empty($GLOBALS['_LANG']['oos']))
        {
            $smarty->assign('how_oos_list', $GLOBALS['_LANG']['oos']);
        }
    }
 
    /* 保存 session */
    $_SESSION['flow_order'] = $order;
    
    
    $res['result'] =  $smarty->fetch("checkout_app.lbi");
    $res['shipping_id'] = intval($order['shipping_id']);
    die($json->encode($res));
}
 
elseif ($_REQUEST['step'] == 'select_shipping')
{
    /*------------------------------------------------------ */
    //-- 改变配送方式
    /*------------------------------------------------------ */
    $result = array('error' => 0,'message'=>'', 'content' => '', 'need_insure' => 0);
 
    /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
    /* 获得收货人信息 */
    $consignee = get_consignee($_SESSION['user_id']);
 
    /* 对商品信息赋值 */
    $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
    if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
    {
        $result['error'] = 1;
        $result['message'] = $_LANG['no_goods_in_cart'];
    }
    else
    {
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);
 
        /* 取得订单信息 */
        $order = flow_order_info();
 
        $order['shipping_id'] = intval($_REQUEST['shipping']);
        $regions = array($consignee['country'], $consignee['province'], $consignee['city'], $consignee['district']);
        $shipping_info = shipping_area_info($order['shipping_id'], $regions);
 
 
        /* 计算订单的费用 */
        $total = order_fee($order, $cart_goods, $consignee);
        $smarty->assign('total', $total);
 
        /* 取得可以得到的积分和红包 */
        $smarty->assign('total_integral', cart_amount(false, $flow_type) - $total['bonus'] - $total['integral_money']);
        $smarty->assign('total_bonus',    price_format(get_total_bonus(), false));
 
        /* 团购标志 */
        if ($flow_type == CART_GROUP_BUY_GOODS)
        {
            $smarty->assign('is_group_buy', 1);
        }
 
        $result['cod_fee']     = $shipping_info['pay_fee'];
        $result['support_cod'] = $shipping_info['support_cod'];
        if (strpos($result['cod_fee'], '%') === false)
        {
            $result['cod_fee'] = price_format($result['cod_fee'], false);
        }
        
        $result['need_insure'] = ($shipping_info['insure'] > 0) ? 1 : 0;
        $result['content']     = $smarty->fetch('app/order_total_app.lbi');
 
        /* 代码增加_start  By   */
        $result['supplier_shipping']     = $smarty->fetch('app/order_supplier_shipping_app.lbi');
        /* 代码增加_end   By   */
        $result['pickup_content']     =  '';
        if(intval($_REQUEST['pickup']) > 0){//目前没有此功能app端
            $sql = 'select * from ' . $ecs->table('pickup_point') .
                ' where city_id=' . $consignee['city'];
            $pickup_point_list = $db->getAll($sql);
                $smarty->assign('pickup_point_list',      $pickup_point_list);
                $result['pickup_content']     = $smarty->fetch('library/pickup.lbi');
        }
    }
 
    echo $json->encode($result);
    exit;
}
 
 
elseif ($_REQUEST['step'] == 'select_insure')
{
    /*------------------------------------------------------ */
    //-- 选定/取消配送的保价
    /*------------------------------------------------------ */
    $result = array('error' => 0,'message'=>'', 'content' => '');
 
    $_GET['insure'] = !empty($_GET['insure']) ? urldecode($_GET['insure']) : '';
    /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
    /* 获得收货人信息 */
    $consignee = get_consignee($_SESSION['user_id']);
 
    /* 对商品信息赋值 */
    $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
    if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
    {
        $result['error'] = $_LANG['no_goods_in_cart'];
    }
    else
    {
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);
 
        /* 取得订单信息 */
        $order = flow_order_info();
 
        $order['need_insure'] = intval($_REQUEST['insure']);
 
 
    }
     /* 保存 session */
 
 
 $_SESSION['flow_order'] = $order;
 $total = order_fee($order, $cart_goods, $consignee);
 $smarty->assign('total', $total);
$result['content'] = $smarty->fetch('app/order_total_app.lbi');
 
    die($json->encode($result));
}elseif ($_REQUEST['step'] == 'change_needinv')
{
    /*------------------------------------------------------ */
    //-- 改变发票的设置
    /*------------------------------------------------------ */
    $result = array('error' => '', 'content' => '');
    $json = new JSON();
    $_GET['inv_type'] = !empty($_GET['inv_type']) ? json_str_iconv(urldecode($_GET['inv_type'])) : '';
    $_GET['invPayee'] = !empty($_GET['invPayee']) ? json_str_iconv(urldecode($_GET['invPayee'])) : '';
    $_GET['inv_content'] = !empty($_GET['inv_content']) ? json_str_iconv(urldecode($_GET['inv_content'])) : '';
 
    /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
    /* 获得收货人信息 */
    $consignee = get_consignee($_SESSION['user_id']);
 
    /* 对商品信息赋值 */
    $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
    if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
    {
        $result['error'] = $_LANG['no_goods_in_cart'];
        die($json->encode($result));
    }
    else
    {
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);
 
        /* 取得订单信息 */
        $order = flow_order_info();
 
        if (isset($_GET['need_inv']) && intval($_GET['need_inv']) == 1)
        {
            $order['need_inv']    = 1;
            $order['inv_type']    = trim(stripslashes($_GET['inv_type']));
            $order['inv_payee']   = trim(stripslashes($_GET['inv_payee']));
            $order['inv_content'] = trim(stripslashes($_GET['inv_content']));
        }
        else
        {
            $order['need_inv']    = 0;
            $order['inv_type']    = '';
            $order['inv_payee']   = '';
            $order['inv_content'] = '';
        }
 
        /* 计算订单的费用 */
        $total = order_fee($order, $cart_goods, $consignee);
        $smarty->assign('total', $total);
 
        /* 团购标志 */
        if ($flow_type == CART_GROUP_BUY_GOODS)
        {
            $smarty->assign('is_group_buy', 1);
        }
 
       $result['content'] = $smarty->fetch('app/order_total_app.lbi');
    }
      die($json->encode($result));
}
 
elseif ($_REQUEST['step'] == 'select_payment')
{
    /*------------------------------------------------------ */
    //-- 改变支付方式
    /*------------------------------------------------------ */
    $result = array('error' => '', 'content' => '', 'need_insure' => 0, 'payment' => 1);
 
    /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
    /* 获得收货人信息 */
    $consignee = get_consignee($_SESSION['user_id']);
 
    /* 对商品信息赋值 */
    $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
    if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
    {
        $result['error'] = $_LANG['no_goods_in_cart'];
    }
    else
    {
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);
 
        /* 取得订单信息 */
        $order = flow_order_info();
 
        $order['pay_id'] = intval($_REQUEST['payment']);
        $payment_info = payment_info($order['pay_id']);
        $result['pay_code'] = $payment_info['pay_code'];
 
        /* 保存 session */
        $_SESSION['flow_order'] = $order;
 
        /* 计算订单的费用 */
        $total = order_fee($order, $cart_goods, $consignee);
        $smarty->assign('total', $total);
 
        /* 取得可以得到的积分和红包 */
        $smarty->assign('total_integral', cart_amount(false, $flow_type) - $total['bonus'] - $total['integral_money']);
        $smarty->assign('total_bonus',    price_format(get_total_bonus(), false));
 
        /* 团购标志 */
        if ($flow_type == CART_GROUP_BUY_GOODS)
        {
            $smarty->assign('is_group_buy', 1);
        }
 
        $result['content'] = $smarty->fetch('library/order_total_app.lbi');
    }
 
    echo $json->encode($result);
    exit;
}
 
elseif ($_REQUEST['step'] == 'change_bonus')
{
    /*------------------------------------------------------ */
    //-- 改变红包
    /*------------------------------------------------------ */
    $result = array('error' => 0,'message'=>'', 'content' => '');
 
    /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
    /* 获得收货人信息 */
    $consignee = get_consignee($_SESSION['user_id']);
 
    /* 对商品信息赋值 */
    $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
    if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
    {
        $result['error'] = 1;
        $result['message'] = $_LANG['no_goods_in_cart'];
        die($json->encode($result));
    }
    else
    {
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);
 
        $result['suppid'] = $suppid = intval($_GET['suppid']);
 
        /* 取得订单信息 */
        $order = flow_order_info();
 
        $bonus = bonus_info(intval($_GET['bonus']));
 
        if ((!empty($bonus) && $bonus['user_id'] == $_SESSION['user_id'] && $bonus['supplier_id'] == $suppid) || intval($_GET['bonus']) == 0)
        {
            $bonus_info = (isset($order['bonus_id_info'])) ? $order['bonus_id_info'] : array();
            if(intval($_GET['bonus']) == 0){
                unset($bonus_info[$suppid]);
            }else{
                $bonus_info[$suppid] = $_GET['bonus'];
            }
            $order['bonus_id_info'] = $bonus_info = array_filter($bonus_info);
            $order['bonus_id'] = implode(',',$bonus_info);//intval($_GET['bonus']);
 
            $bonus_sn_info = (isset($order['bonus_sn_info'])) ? $order['bonus_sn_info'] : array();
            unset($bonus_sn_info[$suppid]);
            $order['bonus_sn_info'] = $bonus_sn_info = array_filter($bonus_sn_info);
            $order['bonus_sn'] = implode(',',$bonus_sn_info);
        }
        else
        {
            $order['bonus_id'] = 0;
            $result['error'] = 1;
            $result['message'] = $_LANG['invalid_bonus'];
        }
 
        /* 计算订单的费用 */
        $total = order_fee($order, $cart_goods, $consignee);
        $smarty->assign('total', $total);
 
        /* 团购标志 */
        if ($flow_type == CART_GROUP_BUY_GOODS)
        {
            $smarty->assign('is_group_buy', 1);
        }
 
        $result['content'] = $smarty->fetch('app/order_total_app.lbi');
    }
 
    die($json->encode($result));
}
 
/* 验证红包序列号 */
elseif ($_REQUEST['step'] == 'validate_bonus')
{
 
    $result = array('error' => 0,'message'=>'', 'content' => '');
 
    $result['suppid'] = $suppid = intval($_GET['suppid']);
 
    $bonus_sn = intval($_REQUEST['bonus_sn']);
    if (is_numeric($bonus_sn) && $bonus_sn>0)
    {
        $bonus = bonus_info(0, $bonus_sn, $suppid);
    }
    else
    {
        $bonus = array();
    }
 
    /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
    /* 获得收货人信息 */
    $consignee = get_consignee($_SESSION['user_id']);
 
    /* 对商品信息赋值 */
    $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
    if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
    {
        $result['error'] = 1;
        $result['message'] = $_LANG['no_goods_in_cart'];
    }
    else
    {
        
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);
 
        /* 取得订单信息 */
        $order = flow_order_info();
        $bonus_info = (isset($order['bonus_sn_info'])) ? $order['bonus_sn_info'] : array();
        $bonus_id_info = (isset($order['bonus_id_info'])) ? $order['bonus_id_info'] : array();
        
 
        if (((!empty($bonus) && $bonus['user_id'] == $_SESSION['user_id']) || ($bonus['type_money'] > 0 && empty($bonus['user_id']))) && $bonus['supplier_id'] == $suppid && $bonus['order_id'] <= 0)
        {
            
            //$order['bonus_kill'] = $bonus['type_money'];
            $now = gmtime();
            if ($now > $bonus['use_end_date'])
            {
                
                $order['bonus_sn'] = '';
                //$order['bonus_sn'] = implode(',',$bonus_info);//$bonus_sn;
                $result['error'] = 1;
                $result['message']=$_LANG['bonus_use_expire'];
            }
            else
            {
                $bonus_info[$suppid] = $bonus_sn;
                $order['bonus_sn_info'] = $bonus_info = array_filter($bonus_info);
                $order['bonus_sn'] = implode(',',$bonus_info);//$bonus_sn;
                unset($bonus_id_info[$suppid]);
                $order['bonus_id_info'] = $bonus_id_info = array_filter($bonus_id_info);
                $order['bonus_id'] = implode(',',$bonus_id_info);
 
                 /* 计算订单的费用 */
                $total = order_fee($order, $cart_goods, $consignee);
 
                if($total['goods_price']<$bonus['min_goods_amount'])
                {
                 $order['bonus_id'] = '';
                 /* 重新计算订单 */
                 $total = order_fee($order, $cart_goods, $consignee);
                 $result['error'] = 1;
                 $result['message'] = sprintf($_LANG['bonus_min_amount_error'], price_format($bonus['min_goods_amount'], false));
                }
 
                $smarty->assign('total', $total);
 
                /* 团购标志 */
                if ($flow_type == CART_GROUP_BUY_GOODS)
                {
                    $smarty->assign('is_group_buy', 1);
                }
 
                $result['content'] = $smarty->fetch('app/order_total_app.lbi');
                
            }
        }
        else
        {
            $order['bonus_sn'] = '';//$bonus_sn;
            $result['error'] = 1;
            $result['message']=$_LANG['bonus_not_exist'];
        }
        //echo "<pre>";
        //print_r($order);
       
    }
    die($json->encode($result));
}
 
elseif ($_REQUEST['step'] == 'change_integral')
{
    /*------------------------------------------------------ */
    //-- 改变积分
    /*------------------------------------------------------ */
    
    $result = array('error' => 0,'message'=>'', 'content' => '');
 
    $points    = floatval($_GET['points']);
    $result['suppid'] = $suppid        = intval($_GET['suppid']);
    $user_info = user_info($_SESSION['user_id']);
    
    
 
    /* 取得订单信息 */
    $order = flow_order_info();
    
    $integral_info = (isset($order['integral_info'])) ? $order['integral_info'] : array();
    $integral_info[$suppid] = $points;
    
    $order['integral_info'] = $integral_info;
 
    $flow_points = flow_available_points();  // 该订单允许使用的积分
    $user_points = $user_info['pay_points']; // 用户的积分总数
    
    //所有订单的总积分
    $points_all = array_sum($integral_info);
 
    if ($points_all > $user_points)
    {
        $result['error'] = 1;
        $result['message'] = $_LANG['integral_not_enough'];
    }
    elseif ($points > $flow_points[$suppid])
    {
        $result['error'] = 1;
        $result['message'] = sprintf($_LANG['integral_too_much'], $flow_points[$suppid]);
    }
    else
    {
        /* 取得购物类型 */
        $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
        $order['integral'] = $points_all;
 
        /* 获得收货人信息 */
        $consignee = get_consignee($_SESSION['user_id']);
 
        /* 对商品信息赋值 */
        $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
        if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
        {
            $result['error'] = 1;
            $result['message'] = $_LANG['no_goods_in_cart'];
        }
        else
        {
            /* 计算订单的费用 */
            $total = order_fee($order, $cart_goods, $consignee);
            $smarty->assign('total',  $total);
            $smarty->assign('config', $_CFG);
 
            /* 团购标志 */
            if ($flow_type == CART_GROUP_BUY_GOODS)
            {
                $smarty->assign('is_group_buy', 1);
            }
 
            $result['content'] = $smarty->fetch('app/order_total_app.lbi');
        }
    }
    die($json->encode($result));
}
 
elseif ($_REQUEST['step'] == 'change_surplus')
{
    /*------------------------------------------------------ */
    //-- 改变余额
    /*------------------------------------------------------ */
 
    $result = array('error' => 0,'message'=>'', 'content' => '');
    
    $surplus   = floatval($_GET['surplus']);
    $result['suppid'] = $suppid        = intval($_GET['suppid']);
    $user_info = user_info($_SESSION['user_id']);
    
    /* 取得订单信息 */
    $order = flow_order_info();
    $surplus_info = (isset($order['surplus_info'])) ? $order['surplus_info'] : array();
    $surplus_info[$suppid] = $surplus;
 
    if ($user_info['user_money'] + $user_info['credit_line'] < array_sum($surplus_info))
    {
        $result['error'] = 1;
        $result['message'] = $_LANG['surplus_not_enough'];
    }
    else
    {
        /* 取得购物类型 */
        $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
 
        /* 取得购物流程设置 */
        $smarty->assign('config', $_CFG);
 
        /* 获得收货人信息 */
        $consignee = get_consignee($_SESSION['user_id']);
 
        /* 对商品信息赋值 */
        $cart_goods = cart_goods($flow_type); // 取得商品列表,计算合计
 
        if (empty($cart_goods) || !check_consignee_info($consignee, $flow_type))
        {
            $result['error'] = 1;
            $result['message'] = $_LANG['no_goods_in_cart'];
        }
        else
        {
            
            
            $order['surplus_info'] = $surplus_info;
            $order['surplus'] = array_sum($surplus_info);//$surplus;
 
            /* 计算订单的费用 */
            $total = order_fee($order, $cart_goods, $consignee);
            $smarty->assign('total', $total);
 
            /* 团购标志 */
            if ($flow_type == CART_GROUP_BUY_GOODS)
            {
                $smarty->assign('is_group_buy', 1);
            }
 
            $result['content'] = $smarty->fetch('app/order_total_app.lbi');
        }
    }
    die($json->encode($result));
}
 
elseif ($_REQUEST['step'] == 'done')
{
 
    $suppids = explode(',',$_POST['suppids']);
    if(isset($_POST['bonus'])){
        $_POST['bonus'] = array_combine($suppids, explode(',',$_POST['bonus']));
    }
    if(isset($_POST['bonus_sn'])){
        $_POST['bonus_sn'] = array_combine($suppids, explode(',',$_POST['bonus_sn']));
    }
    if(isset($_POST['integral'])){
        $_POST['integral'] = array_combine($suppids, explode(',',$_POST['integral']));
    }
    if(isset($_POST['surplus'])){
        $_POST['surplus'] = array_combine($suppids, explode(',',$_POST['surplus']));
    }
    include_once('../includes/lib_clips.php');
    include_once('../includes/lib_payment.php'); 
   
    $result = array('error'=>0,'result'=>'');
    /* 代码增加_start  By  */
    $id_ext ="";
    if ($_SESSION['sel_cartgoods'])
    {
        $id_ext = " AND rec_id in (". $_SESSION['sel_cartgoods'] .") ";
    }
    /* 代码增加_end  By  */
 
    /* 取得购物类型 */
    $flow_type = isset($_SESSION['flow_type']) ? intval($_SESSION['flow_type']) : CART_GENERAL_GOODS;
    
    /* 代码增加_end  By  */
    $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";
 
 
    /* 检查购物车中是否有商品 */
    $sql = "SELECT COUNT(*) FROM " . $ecs->table('cart') .
        " WHERE $sql_where " .
        "AND parent_id = 0 AND is_gift = 0 AND rec_type = '$flow_type'";
    /* 代码增加_end  By  */
    if ($db->getOne($sql) == 0)
    {
        //show_message($_LANG['no_goods_in_cart'], '', '', 'warning');
        $result['error'] = 11;
        $result['result'] = $_LANG['no_goods_in_cart'];
        die($json->encode($result));
    }
 
    /* 检查商品库存 */
    /* 如果使用库存,且下订单时减库存,则减少库存 */
    if ($_CFG['use_storage'] == '1' && $_CFG['stock_dec_time'] == SDT_PLACE)
    {
        $cart_goods_stock = get_cart_goods();
        $_cart_goods_stock = array();
        foreach ($cart_goods_stock['goods_list'] as $value)
        {
            $_cart_goods_stock[$value['rec_id']] = $value['goods_number'];
        }
        flow_cart_stock($_cart_goods_stock);
        unset($cart_goods_stock, $_cart_goods_stock);
    }
 
    /*
     * 检查用户是否已经登录
     * 如果用户已经登录了则检查是否有默认的收货地址
     * 如果没有登录则跳转到登录和注册页面
     */
    if (empty($_SESSION['direct_shopping']) && $_SESSION['user_id'] == 0)
    {
        $result['error'] = 2;
        $result['result'] = '请先登陆!';
        die($json->encode($result));
        /* 用户没有登录且没有选定匿名购物,转向到登录页面 */
        ecs_header("Location: flow.php?step=login\n");
        exit;
    }
 
    $consignee = get_consignee($_SESSION['user_id']);
 
    /* 检查收货人信息是否完整 */
    if (!check_consignee_info($consignee, $flow_type))
    {
        /* 如果不完整则转向到收货人信息填写界面 */
        $result['error'] = 3;
        $result['result'] = '请先设置收货地址';
        die($json->encode($result));
        ecs_header("Location: flow.php?step=consignee\n");
        exit;
    }
 
    /* 订单中的商品 */
    $cart_goods = cart_goods($flow_type);
    
    $cart_goods_new = array();
    if(count($cart_goods)>0){
        foreach($cart_goods as $key => $val){
            $cart_goods_new[$val['supplier_id']]['goodlist'][$val['rec_id']] = $val;
            $cart_goods_new[$val['supplier_id']]['referer'] = $val['seller'];
        }
    }
    //echo "<pre>";
    //print_r($cart_goods);
    //print_r($cart_goods_new);
 
    if (empty($cart_goods))
    {
        //show_message($_LANG['no_goods_in_cart'], $_LANG['back_home'], './', 'warning');
        $result['error'] = 21;
        $result['result'] = $_LANG['no_goods_in_cart'];
        die($json->encode($result));
    }
 
    /* 检查商品总额是否达到最低限购金额 */
    if ($flow_type == CART_GENERAL_GOODS && cart_amount(true, CART_GENERAL_GOODS) < $_CFG['min_goods_amount'])
    {
        //show_message(sprintf($_LANG['goods_amount_not_enough'], price_format($_CFG['min_goods_amount'], false)));
        $result['error'] = 31;
        $result['result'] = sprintf($_LANG['goods_amount_not_enough'], price_format($_CFG['min_goods_amount'], false));
        die($json->encode($result));
    }
 
    
    //此订单拆分订单后的订单信息
    $order_info = array();
    //组装拆分的子订单数组信息start
    foreach ($cart_goods_new as $ckey=>$cval){
        
        $cart_goods = $cval['goodlist'];
 
        $_POST['how_oos'] = isset($_POST['how_oos']) ? intval($_POST['how_oos']) : 0;
        $_POST['card_message'] = isset($_POST['card_message']) ? compile_str($_POST['card_message']) : '';
        $_POST['inv_type'] = !empty($_POST['inv_type']) ? compile_str($_POST['inv_type']) : '';
        $_POST['inv_payee'] = isset($_POST['inv_payee']) ? compile_str($_POST['inv_payee']) : '';
        $_POST['inv_content'] = isset($_POST['inv_content']) ? compile_str($_POST['inv_content']) : '';
        $_POST['postscript'] = isset($_POST['postscript']) ? compile_str($_POST['postscript']) : '';
        
        $order_integral = isset($_POST['integral']) ? $_POST['integral'] : array();
        $order_bonus_id = isset($_POST['bonus']) ? $_POST['bonus'] : array();
        $order_bonus_sn = isset($_POST['bonus_sn']) ? $_POST['bonus_sn'] : array();
        $order_surplus = isset($_POST['surplus']) ? $_POST['surplus'] : array();
        
        $_POST['vat_invoice'] = isset($_POST['vat_invoice']) ? compile_str($_POST['vat_invoice']) : '';
        $_POST['normal_invoice'] = isset($_POST['normal_invoice']) ? compile_str($_POST['normal_invoice']) : '';
        //$_POST['danwei'] = isset($_POST['danwei']) ? compile_str($_POST['danwei']) : '';
        
        $order = array(
            'shipping_id'     => intval($_POST['shipping']),
            'pay_id'          => intval($_POST['payment']),
            'pack_id'         => isset($_POST['pack']) ? intval($_POST['pack']) : 0,
            'card_id'         => isset($_POST['card']) ? intval($_POST['card']) : 0,
            'card_message'    => trim($_POST['card_message']),
            'surplus'         => isset($order_surplus[$ckey]) ? floatval($order_surplus[$ckey]) : 0.00,
            'integral'        => isset($order_integral[$ckey]) ? intval($order_integral[$ckey]) : 0,
            'bonus_id'        => isset($order_bonus_id[$ckey]) ? intval($order_bonus_id[$ckey]) : 0,
            'need_inv'        => empty($_POST['need_inv']) ? 0 : 1,
            //'inv_type'        => $_POST['inv_type'],
           // 'inv_payee'       => trim($_POST['inv_payee']),
           //s 'inv_content'     => $_POST['inv_content'],
            'postscript'      => trim($_POST['postscript']),
            'how_oos'         => isset($_LANG['oos'][$_POST['how_oos']]) ? addslashes($_LANG['oos'][$_POST['how_oos']]) : '',
            'need_insure'     => isset($_POST['need_insure']) ? intval($_POST['need_insure']) : 0,
            'user_id'         => $_SESSION['user_id'],
            'add_time'        => gmtime(),
            'order_status'    => OS_UNCONFIRMED,
            'shipping_status' => SS_UNSHIPPED,
            'pay_status'      => PS_UNPAYED,
            'agency_id'       => get_agency_by_regions(array($consignee['country'], $consignee['province'], $consignee['city'], $consignee['district'])),
            'supplier_id'      => $ckey,
              'need_inv'        => 1,//empty($_POST['need_inv']) ? 0 : 1,//   jx   发票是否开启
            //'inv_type' => isset($_POST['normal_invoice']) ? intval($_POST['vat_invoice']) : ,
            'inv_content' => $_POST['cont'],
            'vat_inv_company_name' => $_POST['vat_inv_company_name'],
            'vat_inv_taxpayer_id' => $_POST['vat_inv_taxpayer_id'],
            'vat_inv_registration_address' => $_POST['vat_inv_registration_address'],
            'vat_inv_registration_phone' => $_POST['vat_inv_registration_phone'],
            'vat_inv_deposit_bank' => $_POST['vat_inv_deposit_bank'],
            'vat_inv_bank_account' => $_POST['vat_inv_bank_account'],
            'inv_consignee_name' => $_POST['inv_consignee_name'],
            'inv_consignee_phone' => $_POST['inv_consignee_phone'],
            'inv_consignee_country     ' => '1',
            'inv_consignee_province' => $_POST['inv_consignee_province'],
            'inv_consignee_city' => $_POST['selCities'],
            'inv_consignee_district' => $_POST['selDistricts'],
            'inv_consignee_address' => $_POST['inv_consignee_address']
            );
        if($_POST['normal_invoice'])
        {
            $order['inv_type'] = $_POST['normal_invoice'];
            if($_POST['danwei'])
            {
                $order['inv_payee'] = $_POST['danwei'];
            }else
            {
                $order['inv_payee'] = '个人';
            }
            $order['inv_payee_type'] = $_POST['oadio'];
            $order['inv_payee_type'] = 'individual';
            
        }else
        {
            $order['inv_type'] = $_POST['vat_invoice'];
        }
        
    
        /* 扩展信息 */
        if (isset($_SESSION['flow_type']) && intval($_SESSION['flow_type']) != CART_GENERAL_GOODS)
        {
            $order['extension_code'] = $_SESSION['extension_code'];
            $order['extension_id'] = $_SESSION['extension_id'];
        }
        else
        {
            $order['extension_code'] = '';
            $order['extension_id'] = 0;
        }
    
        /* 检查积分余额是否合法 */
        $user_id = $_SESSION['user_id'];
        if ($user_id > 0)
        {
            $user_info = user_info($user_id);
    
            $order['surplus'] = min($order['surplus'], $user_info['user_money'] + $user_info['credit_line']);
            if ($order['surplus'] < 0)
            {
                $order['surplus'] = 0;
            }
    
            // 查询用户有多少积分
            $flow_points = flow_available_points();  // 该订单允许使用的积分
            $user_points = $user_info['pay_points']; // 用户的积分总数
    
            $order['integral'] = min($order['integral'], $user_points, $flow_points[$ckey]);
            if ($order['integral'] < 0)
            {
                $order['integral'] = 0;
            }
        }
        else
        {
            $order['surplus']  = 0;
            $order['integral'] = 0;
        }
    
        /* 检查红包是否存在 */
        if ($order['bonus_id'] > 0)
        {
            $bonus = bonus_info($order['bonus_id']);
    
            if (empty($bonus) || $bonus['user_id'] != $user_id || $bonus['order_id'] > 0 || $bonus['min_goods_amount'] > cart_amount_new(array_keys($cart_goods),true, $flow_type))
            {
                $order['bonus_id'] = 0;
            }else{
                
            }
        }
        elseif (isset($_POST['bonus_sn'][$ckey]))
        {
            $bonus_sn = intval($_POST['bonus_sn'][$ckey]);
            $bonus = bonus_info(0, $bonus_sn);
            $now = gmtime();
            if (empty($bonus) || $bonus['user_id'] > 0 || $bonus['order_id'] > 0 || $bonus['min_goods_amount'] > cart_amount_new(array_keys($cart_goods),true, $flow_type) || $now > $bonus['use_end_date'])
            {
            }
            else
            {
                if ($user_id > 0)
                {
                    $sql = "UPDATE " . $ecs->table('user_bonus') . " SET user_id = '$user_id' WHERE bonus_id = '$bonus[bonus_id]' LIMIT 1";
                    $db->query($sql);
                }
                $order['bonus_id'] = '';//$bonus['bonus_id'];
                $order['bonus_sn'] = $bonus_sn;
            }
        }
        
        /* 判断是不是实体商品 */
        foreach ($cart_goods AS $val)
        {
            /* 统计实体商品的个数 */
            if ($val['is_real'])
            {
                $is_real_good=1;
            }
        }
        if(isset($is_real_good))
        {
            $sql="SELECT shipping_id FROM " . $ecs->table('shipping') . " WHERE shipping_id=".$order['shipping_id'] ." AND enabled =1"; 
            if(!$db->getOne($sql))
            {
               show_message($_LANG['flow_no_shipping']);
            }
        }
    
        /* 收货人信息 */
        foreach ($consignee as $key => $value)
        {
            $order[$key] = addslashes($value);
        }
        
        /* 代码增加_start  By   */
        $order['best_time'] = isset($_POST['best_time']) ? trim($_POST['best_time']) : '';
        /* 代码增加_end  By   */
    
       
        /* 订单中的总额 */
        $total = order_fee($order, $cart_goods, $consignee);
        
        $order['bonus']        = $total['bonus'];
        $order['goods_amount'] = $total['goods_price'];
        $order['discount']     = $total['discount'];
        $order['surplus']      = $total['surplus'];
        $order['tax']          = $total['tax'];
    
        // 购物车中的商品能享受红包支付的总额
        $discount_amout = compute_discount_amount($ckey);
        // 红包和积分最多能支付的金额为商品总额
        $temp_amout = $order['goods_amount'] - $discount_amout;
        if ($temp_amout <= 0)
        {
            $order['bonus_id'] = 0;
        }
    
        /* 配送方式 */
        if ($order['shipping_id'] > 0)
        {
            $shipping = shipping_info($order['shipping_id']);
            $order['shipping_name'] = addslashes($shipping['shipping_name']);
        }
        $order['shipping_fee'] = $total['shipping_fee'];
        $order['insure_fee']   = $total['shipping_insure'];
    
        /* 支付方式 */
        if ($order['pay_id'] > 0)
        {
            $payment = payment_info($order['pay_id']);
            $order['pay_name'] = addslashes($payment['pay_name']);
        }
        $order['pay_fee'] = $total['pay_fee'];
        $order['cod_fee'] = $total['cod_fee'];
    
        /* 商品包装 */
        if ($order['pack_id'] > 0)
        {
            $pack               = pack_info($order['pack_id']);
            $order['pack_name'] = addslashes($pack['pack_name']);
        }
        $order['pack_fee'] = $total['pack_fee'];
    
        /* 祝福贺卡 */
        if ($order['card_id'] > 0)
        {
            $card               = card_info($order['card_id']);
            $order['card_name'] = addslashes($card['card_name']);
        }
        $order['card_fee']      = $total['card_fee'];
    
        $order['order_amount']  = number_format($total['amount'], 2, '.', '');
        /*发票金额*/
        $order['inv_money'] =  $order['order_amount'] ;
        /*增值税发票_添加_END_*/
        /* 如果全部使用余额支付,检查余额是否足够 */
        if ($payment['pay_code'] == 'balance' && $order['order_amount'] > 0)
        {
            if($order['surplus'] >0) //余额支付里如果输入了一个金额
            {
                $order['order_amount'] = $order['order_amount'] + $order['surplus'];
                $order['surplus'] = 0;
            }
            if ($order['order_amount'] > ($user_info['user_money'] + $user_info['credit_line']))
            {
                //show_message($_LANG['balance_not_enough']);
                $result['error'] = 41;
                $result['result'] = $_LANG['balance_not_enough'];
                die($json->encode($result));
            }
            else
            {
                $order['surplus'] = $order['order_amount'];
                $order['order_amount'] = 0;
            }
        }
    
        /* 如果订单金额为0(使用余额或积分或红包支付),修改订单状态为已确认、已付款 */
        if ($order['order_amount'] <= 0)
        {
            $order['order_status'] = OS_CONFIRMED;
            $order['confirm_time'] = gmtime();
            $order['pay_status']   = PS_PAYED;
            $order['pay_time']     = gmtime();
            $order['order_amount'] = 0;
        }
    
        $order['integral_money']   = $total['integral_money'];
        $order['integral']         = $total['integral'];
    
        if ($order['extension_code'] == 'exchange_goods')
        {
            $order['integral_money']   = 0;
            $order['integral']         = $total['exchange_integral'];
        }
    
        $order['from_ad']          = !empty($_SESSION['from_ad']) ? $_SESSION['from_ad'] : '0';
        //$order['referer']          = !empty($_SESSION['referer']) ? addslashes($_SESSION['referer']) : '';
        $order['referer']          = $cval['referer'];
    
        /* 记录扩展信息 */
        if ($flow_type != CART_GENERAL_GOODS)
        {
            $order['extension_code'] = $_SESSION['extension_code'];
            $order['extension_id'] = $_SESSION['extension_id'];
        }
    
        $affiliate = unserialize($_CFG['affiliate']);
        if(isset($affiliate['on']) && $affiliate['on'] == 1 && $affiliate['config']['separate_by'] == 1)
        {
            //推荐订单分成
            $parent_id = get_affiliate();
            if($user_id == $parent_id)
            {
                $parent_id = 0;
            }
        }
        elseif(isset($affiliate['on']) && $affiliate['on'] == 1 && $affiliate['config']['separate_by'] == 0)
        {
            //推荐注册分成
            $parent_id = 0;
        }
        else
        {
            //分成功能关闭
            $parent_id = 0;
        }
        $order['parent_id'] = $parent_id;
        
            /* 代码增加_start   By  */
        /*  自提功能
            获取订单确认页选择的自提点
        */
        $pickup_point = isset($_POST['pickup_point']) ? $_POST['pickup_point'] : 0;
        if($pickup_point > 0)
            $order['is_pickup'] = 1;
        else
            $order['is_pickup'] = 0;
        $order['pickup_point'] = $pickup_point;
        /* 代码增加_end   By  */
        //$order['order_sn'] = get_order_sn();
        
        if(count($order)>0){
            $order_info[$ckey] = $order;
        }
        unset($order);
    }
    //组装拆分的子订单数组信息end
    
    
    //判断是否拆分为多个订单,多个订单就生成父订单id号
    $del_patent_id = 0;
    if(count($order_info)>1){
        $error_no = 0;
        do
        {
            $save['order_sn'] = get_order_sn(); //获取新订单号
            $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('order_info'), $save, 'INSERT');
            $error_no = $GLOBALS['db']->errno();
    
            if ($error_no > 0 && $error_no != 1062)
            {
                die($GLOBALS['db']->errorMsg());
            }
        }
        while ($error_no == 1062); //如果是订单号重复则重新提交数据
        $del_patent_id = $parent_order_id = $db->insert_id();
    }else{
        $parent_order_id = 0;
    }
    
    $all_order_amount = 0;//记录订单所需支付的总金额
    //用来展示用的数组数据
    $split_order = array();
    $split_order['sub_order_count'] = count($order_info);
    //生成订单
    foreach($order_info as $ok=>$order){
        
        $cart_goods = $cart_goods_new[$ok]['goodlist']; 
        
        if($cart_goods){
            $id_ext_new = " AND rec_id in (". implode(',',array_keys($cart_goods)) .") ";
        }
        
        //获取佣金id
        $order['rebate_id'] = get_order_rebate($ok);
        
        //下单来源
        $order['froms'] = 'app';
        
        $order['parent_order_id'] = $parent_order_id;
         /* 插入订单表 */
        $error_no = 0;
        do
        {
            $order['order_sn'] = get_order_sn(); //获取新订单号
            $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('order_info'), $order, 'INSERT');
    
            $error_no = $GLOBALS['db']->errno();
    
            if ($error_no > 0 && $error_no != 1062)
            {
                die($GLOBALS['db']->errorMsg());
            }
        }
        while ($error_no == 1062); //如果是订单号重复则重新提交数据
    
        $new_order_id = $db->insert_id();
        $order['order_id'] = $new_order_id;
        
        $parent_order_id = ($parent_order_id>0) ? $parent_order_id : $new_order_id;
    
        /* 插入订单商品 下面这个SQL有修改 by  注意末尾那个字段 */
        /* 代码增加_start  By  */
        $sql = "INSERT INTO " . $ecs->table('order_goods') . "( " .
                    "order_id, goods_id, goods_name, goods_sn, product_id, goods_number, market_price, ".
                    "goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id, package_attr_id) ".
                " SELECT '$new_order_id', goods_id, goods_name, goods_sn, product_id, goods_number, market_price, ".
                    "goods_price, goods_attr, is_real, extension_code, parent_id, is_gift, goods_attr_id, package_attr_id ".
                " FROM " .$ecs->table('cart') .
                " WHERE $sql_where AND rec_type = '$flow_type' "; //$id_ext_new 
        /* 代码增加_end  By  */
        $db->query($sql);
        /* 修改拍卖活动状态 */
        if ($order['extension_code']=='auction')
        {
            $sql = "UPDATE ". $ecs->table('goods_activity') ." SET is_finished='2' WHERE act_id=".$order['extension_id'];
            $db->query($sql);
        }
    
        /* 处理余额、积分、红包 */
        if ($order['user_id'] > 0 && $order['surplus'] > 0)
        {
            log_account_change($order['user_id'], $order['surplus'] * (-1), 0, 0, 0, sprintf($_LANG['pay_order'], $order['order_sn']));
        }
        if ($order['user_id'] > 0 && $order['integral'] > 0)
        {
            log_account_change($order['user_id'], 0, 0, 0, $order['integral'] * (-1), sprintf($_LANG['pay_order'], $order['order_sn']));
        }
    
    
        if ($order['bonus_id'] > 0 && $temp_amout > 0)
        {
            use_bonus($order['bonus_id'], $new_order_id);
        }
        
        $split_order['suborder_list'][$ok]['order_sn'] = $order['order_sn'];
        $split_order['suborder_list'][$ok]['order_amount_formated'] = price_format($order['order_amount']);
        
    
        /* 如果使用库存,且下订单时减库存,则减少库存 */
        if ($_CFG['use_storage'] == '1' && $_CFG['stock_dec_time'] == SDT_PLACE)
        {
            change_order_goods_storage($order['order_id'], true, SDT_PLACE);
        }
    
        /* 给商家发邮件 */
        /* 增加是否给客服发送邮件选项 */
        if ($_CFG['send_service_email'] && $_CFG['service_email'] != '')
        {
            $tpl = get_mail_template('remind_of_new_order');
            $smarty->assign('order', $order);
            $smarty->assign('goods_list', $cart_goods);
            $smarty->assign('shop_name', $_CFG['shop_name']);
            $smarty->assign('send_date', date($_CFG['time_format']));
            $content = $smarty->fetch('str:' . $tpl['template_content']);
            send_mail($_CFG['shop_name'], $_CFG['service_email'], $tpl['template_subject'], $content, $tpl['is_html']);
        }
    
    /* 如果需要,发短信 */
    if ($_CFG['sms_order_placed'] == '1' && $_CFG['sms_shop_mobile'] != '')
    {
       // include_once('includes/cls_sms.php');
        //$sms = new sms();
        //$msg = $order['pay_status'] == PS_UNPAYED ?
        //    $_LANG['order_placed_sms'] : $_LANG['order_placed_sms'] . '[' . $_LANG['sms_paid'] . ']';
       // $sms->send($_CFG['sms_shop_mobile'], sprintf($msg, $order['consignee'], $order['tel']),'', 13,1);
    }
        /* 如果订单金额为0 处理虚拟卡 */
        if ($order['order_amount'] <= 0)
        {
            /* 代码增加_start  By  */
            $sql = "SELECT goods_id, goods_name, goods_number AS num FROM ".
                   $GLOBALS['ecs']->table('cart') .
                    " WHERE is_real = 0 AND extension_code = 'virtual_card'".
                    " AND $sql_where AND rec_type = '$flow_type'";
            /* 代码增加_end  By  */
            $res = $GLOBALS['db']->getAll($sql);
    
            $virtual_goods = array();
            foreach ($res AS $row)
            {
                $virtual_goods['virtual_card'][] = array('goods_id' => $row['goods_id'], 'goods_name' => $row['goods_name'], 'num' => $row['num']);
            }
    
            if ($virtual_goods AND $flow_type != CART_GROUP_BUY_GOODS)
            {
                /* 虚拟卡发货 */
                if (virtual_goods_ship($virtual_goods,$msg, $order['order_sn'], true))
                {
                    /* 如果没有实体商品,修改发货状态,送积分和红包 */
                    $sql = "SELECT COUNT(*)" .
                            " FROM " . $ecs->table('order_goods') .
                            " WHERE order_id = '$order[order_id]' " .
                            " AND is_real = 1";
                    if ($db->getOne($sql) <= 0)
                    {
                        /* 修改订单状态 */
                        update_order($order['order_id'], array('shipping_status' => SS_SHIPPED, 'shipping_time' => gmtime()));
    
                        /* 如果订单用户不为空,计算积分,并发给用户;发红包 */
                        if ($order['user_id'] > 0)
                        {
                            /* 取得用户信息 */
                            $user = user_info($order['user_id']);
    
                            /* 计算并发放积分 */
                            $integral = integral_to_give($order);
                            log_account_change($order['user_id'], 0, 0, intval($integral['rank_points']), intval($integral['custom_points']), sprintf($_LANG['order_gift_integral'], $order['order_sn']));
    
                            /* 发放红包 */
                            send_order_bonus($order['order_id']);
                        }
                    }
                }
            }
    
        }
        
        $all_order_amount += $order['order_amount'];
        //user_uc_call('add_feed', array($order['order_id'], BUY_GOODS)); //推送feed到uc
    }
    /* 清空购物车 */
    clear_cart($flow_type);
    /* 清除缓存,否则买了商品,但是前台页面读取缓存,商品数量不减少 */
    clear_all_files();
    
    //删除父订单记录
    if($del_patent_id > 0){
        $sql="delete from ".$GLOBALS['ecs']->table('order_info')." where order_id='$del_patent_id' ";
        $GLOBALS['db']->query($sql);
    }
    /* 代码增加_start  By   */
    //$split_order = split_order($new_order_id);
    $smarty->assign('split_order',      $split_order);
    /* 如果需要,发短信 */
    if(count($split_order['suborder_list']) > 0){
        foreach($split_order['suborder_list'] as $key => $val){
            $supplier_ids[$key] = $val['order_sn'];
        }
    }
    
    
    //$supplier_ids = array_keys();
    include_once('../send.php');
    
    send_sms($supplier_ids,'您有一条新订单,订单号为:ordersn请注意查看。【shopname】',1);
    
    /* 取得支付信息,生成支付代码 */
    if ($split_order['sub_order_count'] >1)
    {
        $order['order_sn'] = $parent_order_id; 
        /* 插入支付日志 */
        $order['log_id'] = insert_pay_log($order['order_sn'], $order['order_amount'], PAY_ORDER);
    }else{
        /* 插入支付日志 */
        $order['log_id'] = insert_pay_log($order['order_id'], $order['order_amount'], PAY_ORDER);
    }
    $order['order_amount'] = $all_order_amount; //替换为总金额去支付
    
    
 
    
    
    if ($order['order_amount'] > 0)
    {
        $result['result']['order_sn'] = $order['order_sn'];
        $result['result']['allmoney'] = $order['order_amount'];
        
        $payment = payment_info($order['pay_id']);
 
        include_once('../includes/modules/payment/' . $payment['pay_code'] . '.php');
 
        $pay_obj    = new $payment['pay_code'];
 
        $pay_online = $pay_obj->get_code($order, unserialize_config($payment['pay_config']));
 
        $order['pay_desc'] = $payment['pay_desc'];
        $result['result']['pay_code'] = $payment['pay_code'];
        $result['result']['pay_online'] = $pay_online;
 
        //$smarty->assign('pay_online', $pay_online);
    }
    if(!empty($order['shipping_name']))
    {
        $order['shipping_name']=trim(stripcslashes($order['shipping_name']));
    }
 
    /* 订单信息 */
    $smarty->assign('order',      $order);
    $smarty->assign('total',      $total);
    
    unset($_SESSION['flow_consignee']); // 清除session中保存的收货人信息
    unset($_SESSION['flow_order']);
    unset($_SESSION['direct_shopping']);
    die($json->encode($result));
}
 
/**
 * 取得某用户等级当前时间可以享受的优惠活动
 * @param   int     $user_rank      用户等级id,0表示非会员  
 * @param   int     $is_have        是否判断已经选择赠品
 * @return  array
 */
function favourable_list($user_rank,$is_have=true)
{
    /* 购物车中已有的优惠活动及数量 */
    $used_list = cart_favourable();
 
    /* 当前用户可享受的优惠活动 */
    $favourable_list = array();
    $user_rank = ',' . $user_rank . ',';
    $now = gmtime();
    if(isset($_REQUEST['suppid'])){
        $tj = " AND supplier_id=".$_REQUEST['suppid'];
    }else{
        $tj = '';
    }
    $sql = "SELECT * " .
            "FROM " . $GLOBALS['ecs']->table('favourable_activity') .
            " WHERE CONCAT(',', user_rank, ',') LIKE '%" . $user_rank . "%'" .
            " AND start_time <= '$now' AND end_time >= '$now'" .$tj.
            " AND act_type = '" . FAT_GOODS . "'" .
            " ORDER BY sort_order";
    $res = $GLOBALS['db']->query($sql);
    while ($favourable = $GLOBALS['db']->fetchRow($res))
    {
        $favourable['start_time'] = local_date($GLOBALS['_CFG']['time_format'], $favourable['start_time']);
        $favourable['end_time']   = local_date($GLOBALS['_CFG']['time_format'], $favourable['end_time']);
        $favourable['formated_min_amount'] = price_format($favourable['min_amount'], false);
        $favourable['formated_max_amount'] = price_format($favourable['max_amount'], false);
        $favourable['gift']       = unserialize($favourable['gift']);
        $_REQUEST['suppid'] = $favourable['supplier_id'] = $favourable['supplier_id'];
 
        foreach ($favourable['gift'] as $key => $value)
        {
            $favourable['gift'][$key]['formated_price'] = price_format($value['price'], false);
            $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('goods') . " WHERE is_on_sale = 1 AND goods_id = ".$value['id'];
            $is_sale = $GLOBALS['db']->getOne($sql);
            if(!$is_sale)
            {
                unset($favourable['gift'][$key]);
            }
            $goods_thumb = $GLOBALS['db']->getOne("SELECT `goods_thumb` FROM " . $GLOBALS['ecs']->table('goods') . " WHERE `goods_id`='{$value['id']}'");
            $favourable['gift'][$key]['goods_thumb'] = get_image_path($value['id'], $goods_thumb, true);
        }
 
        $favourable['act_range_desc'] = act_range_desc($favourable);
        $favourable['act_type_desc'] = sprintf($GLOBALS['_LANG']['fat_ext'][$favourable['act_type']], $favourable['act_type_ext']);
 
        /* 是否能享受 */
        $favourable['available'] = favourable_available($favourable);
        if ($favourable['available'] && $is_have)
        {
            /* 是否尚未享受 */
            $favourable['available'] = !favourable_used($favourable, $used_list);
        }
        $favourable_list[] = $favourable;
    }
    
    
 
    return $favourable_list;
}
 
/**
 * 取得购物车中已有的优惠活动及数量
 * @return  array
 */
function cart_favourable()
{
    $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";//添加 
    $list = array();
    /* 代码修改_start  By    将这块替换掉*/
    $sql = "SELECT is_gift, COUNT(*) AS num " .
            "FROM " . $GLOBALS['ecs']->table('cart') .
            " WHERE $sql_where " .
            " AND rec_type = '" . CART_GENERAL_GOODS . "'" .
            " AND is_gift > 0" .
            " GROUP BY is_gift";
    /* 代码修改_end  By    */
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $list[$row['is_gift']] = $row['num'];
    }
 
    return $list;
}
 
/**
 * 根据购物车判断是否可以享受某优惠活动
 * @param   array   $favourable     优惠活动信息
 * @return  bool
 */
function favourable_available($favourable)
{
    /* 会员等级是否符合 */
    $user_rank = $_SESSION['user_rank'];
    if (strpos(',' . $favourable['user_rank'] . ',', ',' . $user_rank . ',') === false)
    {
        return false;
    }
 
    /* 优惠范围内的商品总额 */
    $amount = cart_favourable_amount($favourable);
 
    /* 金额上限为0表示没有上限 */
    return $amount >= $favourable['min_amount'] &&
        ($amount <= $favourable['max_amount'] || $favourable['max_amount'] == 0);
}
 
/**
 * 购物车中是否已经有某优惠
 * @param   array   $favourable     优惠活动
 * @param   array   $cart_favourable购物车中已有的优惠活动及数量
 */
function favourable_used($favourable, $cart_favourable)
{
    if ($favourable['act_type'] == FAT_GOODS)
    {
        return isset($cart_favourable[$favourable['act_id']]) &&
            $cart_favourable[$favourable['act_id']] >= $favourable['act_type_ext'] &&
            $favourable['act_type_ext'] > 0;
    }
    else
    {
        return isset($cart_favourable[$favourable['act_id']]);
    }
}
 
/**
 * 取得优惠范围描述
 * @param   array   $favourable     优惠活动
 * @return  string
 */
function act_range_desc($favourable)
{
    if ($favourable['act_range'] == FAR_BRAND)
    {
        $sql = "SELECT brand_name FROM " . $GLOBALS['ecs']->table('brand') .
                " WHERE brand_id " . db_create_in($favourable['act_range_ext']);
        return join(',', $GLOBALS['db']->getCol($sql));
    }
    elseif ($favourable['act_range'] == FAR_CATEGORY)
    {
        $sql = "SELECT cat_name FROM " . $GLOBALS['ecs']->table('category') .
                " WHERE cat_id " . db_create_in($favourable['act_range_ext']);
        return join(',', $GLOBALS['db']->getCol($sql));
    }
    elseif ($favourable['act_range'] == FAR_GOODS)
    {
        $sql = "SELECT goods_name FROM " . $GLOBALS['ecs']->table('goods') .
                " WHERE goods_id " . db_create_in($favourable['act_range_ext']);
        return join(',', $GLOBALS['db']->getCol($sql));
    }
    else
    {
        return '';
    }
}
 
/**
 * 取得购物车中某优惠活动范围内的总金额
 * @param   array   $favourable     优惠活动
 * @return  float
 */
function cart_favourable_amount($favourable)
{
    $sql_where = $_SESSION['user_id']>0 ? "c.user_id='". $_SESSION['user_id'] ."' " : "c.session_id = '" . SESS_ID . "' AND c.user_id=0 ";//添加 
    /* 查询优惠范围内商品总额的sql */
    /* 代码修改_start  By    将这块替换掉*/
    $sql = "SELECT SUM(c.goods_price * c.goods_number) " .
            "FROM " . $GLOBALS['ecs']->table('cart') . " AS c, " . $GLOBALS['ecs']->table('goods') . " AS g " .
            "WHERE c.goods_id = g.goods_id " .
            "AND $sql_where " .
            "AND c.rec_type = '" . CART_GENERAL_GOODS . "' " .
            "AND g.supplier_id=".$favourable['supplier_id']." ".
            "AND c.is_gift = 0 " .
            "AND c.goods_id > 0 ";
    /* 代码修改_end  By    */
 
    /* 根据优惠范围修正sql */
    if ($favourable['act_range'] == FAR_ALL)
    {
        // sql do not change
    }
    elseif ($favourable['act_range'] == FAR_CATEGORY)
    {
        /* 取得优惠范围分类的所有下级分类 */
        $id_list = array();
        $cat_list = explode(',', $favourable['act_range_ext']);
        foreach ($cat_list as $id)
        {
            $id_list = array_merge($id_list, array_keys(cat_list(intval($id), 0, false)));
        }
 
        $sql .= "AND g.cat_id " . db_create_in($id_list);
    }
    elseif ($favourable['act_range'] == FAR_BRAND)
    {
        $id_list = explode(',', $favourable['act_range_ext']);
 
        $sql .= "AND g.brand_id " . db_create_in($id_list);
    }
    else
    {
        $id_list = explode(',', $favourable['act_range_ext']);
 
        $sql .= "AND g.goods_id " . db_create_in($id_list);
    }
    
    $sql .= (isset($_REQUEST['sel_goods']) && !empty($_REQUEST['sel_goods'])) ? " AND c.rec_id in (". $_REQUEST['sel_goods'] .") " : "";
    
    //计算某个店铺的商品总额
    if(isset($_REQUEST['suppid'])){
        $sql .= " AND g.supplier_id=".intval($_REQUEST['suppid']);
    }
    //echo $sql;
 
    /* 优惠范围内的商品总额 */
    return $GLOBALS['db']->getOne($sql);
}
 
/**
 * 添加优惠活动(赠品)到购物车
 * @param   int     $act_id     优惠活动id
 * @param   int     $id         赠品id
 * @param   float   $price      赠品价格
 */
function add_gift_to_cart($act_id, $id, $price)
{
    $sql = "INSERT INTO " . $GLOBALS['ecs']->table('cart') . " (" .
                "user_id, session_id, goods_id, goods_sn, goods_name, market_price, goods_price, ".
                "goods_number, is_real, extension_code, parent_id, is_gift, rec_type ) ".
            "SELECT '$_SESSION[user_id]', '" . SESS_ID . "', goods_id, goods_sn, goods_name, market_price, ".
                "'$price', 1, is_real, extension_code, 0, '$act_id', '" . CART_GENERAL_GOODS . "' " .
            "FROM " . $GLOBALS['ecs']->table('goods') .
            " WHERE goods_id = '$id'";
    $GLOBALS['db']->query($sql);
}
 
/**
 * 删除购物车中的商品
 *
 * @access  public
 * @param   integer $id
 * @return  void
 */
function flow_drop_cart_goods($id)
{
    /* 取得商品id */
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('cart'). " WHERE rec_id = '$id'";
    $row = $GLOBALS['db']->getRow($sql);
    if ($row)
    {
        $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";//添加 
        //如果是超值礼包
        if ($row['extension_code'] == 'package_buy')
        {
/* 代码修改_start  By    将这块替换掉*/
            
            $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') .
                    " WHERE $sql_where " .
                    "AND rec_id = '$id' LIMIT 1";
/* 代码修改_end  By    */
        }
 
        //如果是普通商品,同时删除所有赠品及其配件
        elseif ($row['parent_id'] == 0 && $row['is_gift'] == 0)
        {
            /* 检查购物车中该普通商品的不可单独销售的配件并删除 */
            $sql = "SELECT c.rec_id
                    FROM " . $GLOBALS['ecs']->table('cart') . " AS c, " . $GLOBALS['ecs']->table('group_goods') . " AS gg, " . $GLOBALS['ecs']->table('goods'). " AS g
                    WHERE gg.parent_id = '" . $row['goods_id'] . "'
                    AND c.goods_id = gg.goods_id
                    AND c.parent_id = '" . $row['goods_id'] . "'
                    AND c.extension_code <> 'package_buy'
                    AND gg.goods_id = g.goods_id
                    AND g.is_alone_sale = 0";
            $res = $GLOBALS['db']->query($sql);
            $_del_str = $id . ',';
            while ($id_alone_sale_goods = $GLOBALS['db']->fetchRow($res))
            {
                $_del_str .= $id_alone_sale_goods['rec_id'] . ',';
            }
            $_del_str = trim($_del_str, ',');
 
            /* 代码修改_start  By    将这块替换掉*/
            if($_del_str){
                $sql_plus = " rec_id IN ($_del_str) OR ";
            }
            $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') .
                    " WHERE $sql_where " .
                    "AND ('$sql_plus' parent_id = '$row[goods_id]' OR is_gift <> 0)";
            /* 代码修改_end  By    */
        }
 
        //如果不是普通商品,只删除该商品即可
        else
        {
            /* 代码修改_start  By    将这块替换掉*/
            $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') .
                    " WHERE $sql_where " .
                    "AND rec_id = '$id' LIMIT 1";
            /* 代码修改_end  By    */
        }
 
        $GLOBALS['db']->query($sql);
    }
 
    flow_clear_cart_alone();
}
 
/**
 * 删除购物车中不能单独销售的商品
 *
 * @access  public
 * @return  void
 */
function flow_clear_cart_alone()
{
    /* 查询:购物车中所有不可以单独销售的配件 */
    $sql_where = $_SESSION['user_id']>0 ? "c.user_id='". $_SESSION['user_id'] ."' " : "c.session_id = '" . SESS_ID . "' AND c.user_id=0 ";//添加 
 
/* 代码修改_start  By    将这块替换掉*/
    $sql = "SELECT c.rec_id, gg.parent_id
            FROM " . $GLOBALS['ecs']->table('cart') . " AS c
                LEFT JOIN " . $GLOBALS['ecs']->table('group_goods') . " AS gg ON c.goods_id = gg.goods_id
                LEFT JOIN" . $GLOBALS['ecs']->table('goods') . " AS g ON c.goods_id = g.goods_id
            WHERE $sql_where 
            AND c.extension_code <> 'package_buy'
            AND gg.parent_id > 0
            AND g.is_alone_sale = 0";
/* 代码修改_end  By    */
    $res = $GLOBALS['db']->query($sql);
    $rec_id = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $rec_id[$row['rec_id']][] = $row['parent_id'];
    }
 
    if (empty($rec_id))
    {
        return;
    }
$sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";//添加 
 
 
    /* 查询:购物车中所有商品 */
    /* 代码修改_start  By    将这块替换掉*/
    $sql = "SELECT DISTINCT goods_id
            FROM " . $GLOBALS['ecs']->table('cart') . "
            WHERE $sql_where 
            AND extension_code <> 'package_buy'";
    /* 代码修改_end  By    */
    $res = $GLOBALS['db']->query($sql);
    $cart_good = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $cart_good[] = $row['goods_id'];
    }
 
    if (empty($cart_good))
    {
        return;
    }
 
    /* 如果购物车中不可以单独销售配件的基本件不存在则删除该配件 */
    $del_rec_id = '';
    foreach ($rec_id as $key => $value)
    {
        foreach ($value as $v)
        {
            if (in_array($v, $cart_good))
            {
                continue 2;
            }
        }
 
        $del_rec_id = $key . ',';
    }
    $del_rec_id = trim($del_rec_id, ',');
 
    if ($del_rec_id == '')
    {
        return;
    }
 
    /* 删除 */
    /* 代码修改_start  By    将这块替换掉*/
    if($del_rec_id){
        $sql_plus = " AND rec_id IN ($del_rec_id) ";
    }
    $sql = "DELETE FROM " . $GLOBALS['ecs']->table('cart') ."
            WHERE $sql_where 
            ".$sql_plus;
    /* 代码修改_end  By    */
    $GLOBALS['db']->query($sql);
}
 
/**
 * 获得用户的可用积分
 *
 * @access  private
 * @return  integral
 */
function flow_available_points()
{
   /* 代码修改_start  By    将这块替换掉*/
    $sql_where = $_SESSION['user_id']>0 ? "c.user_id='". $_SESSION['user_id'] ."' " : "c.session_id = '" . SESS_ID . "' AND c.user_id=0 ";
    $sql = "SELECT SUM(g.integral * c.goods_number) as integral,g.supplier_id ".
            "FROM " . $GLOBALS['ecs']->table('cart') . " AS c, " . $GLOBALS['ecs']->table('goods') . " AS g " .
            "WHERE $sql_where AND c.goods_id = g.goods_id AND c.is_gift = 0 AND g.integral > 0 " .
            "AND c.rec_type = '" . CART_GENERAL_GOODS . "' GROUP BY g.supplier_id";
   /* 代码修改_end  By   */
    $info = $GLOBALS['db']->getAll($sql);
    $ret = array();
    foreach($info as $key => $val){
        $ret[$val['supplier_id']] = integral_of_value(intval($val['integral']));
    }
    return $ret;
}
 
/**
 * 检查订单中商品库存
 *
 * @access  public
 * @param   array   $arr
 *
 * @return  void
 */
function flow_cart_stock($arr)
{
    foreach ($arr AS $key => $val)
    {
        $val = intval(make_semiangle($val));
        if ($val <= 0 || !is_numeric($key))
        {
            continue;
        }
/* 代码修改_start  By    将这块替换掉*/
        $sql_where = $_SESSION['user_id']>0 ? "user_id='". $_SESSION['user_id'] ."' " : "session_id = '" . SESS_ID . "' AND user_id=0 ";
        $sql = "SELECT `goods_id`, `goods_attr_id`, `extension_code` FROM" .$GLOBALS['ecs']->table('cart').
               " WHERE rec_id='$key' AND $sql_where";
        $goods = $GLOBALS['db']->getRow($sql);
/* 代码修改_end  By    */
 
        $sql = "SELECT g.goods_name, g.goods_number, c.product_id ".
                "FROM " .$GLOBALS['ecs']->table('goods'). " AS g, ".
                    $GLOBALS['ecs']->table('cart'). " AS c ".
                "WHERE g.goods_id = c.goods_id AND c.rec_id = '$key'";
        $row = $GLOBALS['db']->getRow($sql);
 
        //系统启用了库存,检查输入的商品数量是否有效
        if (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] != 'package_buy')
        {
            if ($row['goods_number'] < $val)
            {
                show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],
                $row['goods_number'], $row['goods_number']));
                exit;
            }
 
            /* 是货品 */
            $row['product_id'] = trim($row['product_id']);
            if (!empty($row['product_id']))
            {
                $sql = "SELECT product_number FROM " .$GLOBALS['ecs']->table('products'). " WHERE goods_id = '" . $goods['goods_id'] . "' AND product_id = '" . $row['product_id'] . "'";
                $product_number = $GLOBALS['db']->getOne($sql);
                if ($product_number < $val)
                {
                    show_message(sprintf($GLOBALS['_LANG']['stock_insufficiency'], $row['goods_name'],
                    $row['goods_number'], $row['goods_number']));
                    exit;
                }
            }
        }
        elseif (intval($GLOBALS['_CFG']['use_storage']) > 0 && $goods['extension_code'] == 'package_buy')
        {
            if (judge_package_stock($goods['goods_id'], $val))
            {
                show_message($GLOBALS['_LANG']['package_stock_insufficiency']);
                exit;
            }
        }
    }
 
}
/*
 *判断商品最后的价格
 * goods_id   商品id
 *number      商品数量
 *rec_id      购物车ID
 *  jx   2015-04-02
 **/
function get_min_price($goods_id,$number,$rec_id)
{
    $sql = "SELECT * FROM ".$GLOBALS['ecs']->table('cart')." WHERE goods_id = '$goods_id' AND  rec_id = '$rec_id'";
    $res = $GLOBALS['db']->getRow($sql);
    $attr_id = $res['goods_attr_id'];
    //取得属性价格  开始
    if(empty($attr_id))
    {
        $atr_price=0;
    }else
    {
         $goods_att=explode(',',$attr_id);
        foreach ($goods_att as $value)
        {
            if(!empty($value))
            {
                $sql="SELECT `attr_price` FROM " .$GLOBALS['ecs']->table('goods_attr')." WHERE goods_id='$goods_id' AND `goods_attr_id`='$value'";
                $atr_price+=$GLOBALS['db']->getOne($sql);
            }
            
        }
    }
    $sql = "SELECT volume_price FROM ".$GLOBALS['ecs']->table('volume_price')."WHERE goods_id = '$goods_id' AND volume_number <= '$number'";
    $volume = $GLOBALS['db']->getOne($sql);//优惠价格
    //属性价格   结束
    $user_id = $res['user_id'];
    if($user_id == 0)//用户不存在
    {
        if(!empty($atr_price))//存在属性价格
        {
            $sql = " SELECT * FROM ".$GLOBALS['ecs']->table('goods')."WHERE goods_id = '$goods_id'";
            $res = $GLOBALS['db']->getROW($sql);
            if($res['is_promote'] == 1 && $res['promote_start_date'] < gmtime() && $res['promote_end_date'] > gmtime())//是促销商品
            {
                if(empty($volume))//不存在优惠价格
                {
                    if($res['shop_price'] > $res['promote_price'])//本店价  > 促销价格
                    {
                        $min_price = ($res['promote_price'] + $atr_price);
                        return $min_price;
                    }else
                    {
                        $min_price = ($res['shop_price'] + $atr_price);
                        return $min_price;
                    }
                }else//存在优惠价格
                {
                    $min = min($volume,$res['shop_price'],$res['promote_price']);//返回优惠  促销 本店 最小的价格
                    $min_price = ($min + $atr_price);
                    return $min_price;
                }
            }else//不是促销商品
            {
                if(empty($volume))//不存在优惠价格
                {
                    $min_price = ($res['shop_price'] + $atr_price);
                    //return $min_price;
                }else//存在优惠价格
                {
                    $min = min($volume,$res['shop_price']);//返回优惠  促销 本店 最小的价格
                    $min_price = ($min + $atr_price);
                }
                return $min_price;
            }
        }else
        {
            $sql = " SELECT * FROM ".$GLOBALS['ecs']->table('goods')."WHERE goods_id = '$goods_id'";
            $res = $GLOBALS['db']->getROW($sql);
            if($res['is_promote'] == 1 && $res['promote_start_date'] < gmtime() && $res['promote_end_date'] > gmtime())//是促销商品
            {
                if(empty($volume))//不存在优惠价格
                {
                    if($res['shop_price'] > $res['promote_price'])//本店价  > 促销价格
                    {
                        $min_price = $res['promote_price'];
                    }else
                    {
                        $min_price = $res['shop_price'];
                    }
                    return $min_price;
                }else//存在优惠价格
                {
                    $min = min($volume,$res['shop_price'],$res['promote_price']);//返回优惠  促销 本店 最小的价格
                    $min_price = $min;
                    return $min_price;
                }
            }else//不是促销商品
            {
                if(empty($volume))//不存在优惠价格
                {
                    $min_price = $res['shop_price'];
                    return $min_price;
                }else//存在优惠价格
                {
                    $min = min($volume,$res['shop_price']);//返回优惠  促销 本店 最小的价格
                    $min_price = $min;
                    return $min_price;
                }
            }
        }
        
    }else//用户存在
    {
        $row = $GLOBALS['db'] -> getOne("SELECT rank_points FROM ".$GLOBALS['ecs']->table('users')." WHERE `user_id`='$user_id'");
        $sql = "SELECT rank_id FROM " . $GLOBALS['ecs']->table('user_rank') . " WHERE max_points > '$row' ORDER BY max_points ASC LIMIT 1";
        $rank_id = $GLOBALS['db']->getOne($sql);//取得会员等级
 
        if(empty($rank_id))
        {
            if(!empty($atr_price) && $atr_price != 0)//存在属性价格
            {
                $sql = " SELECT * FROM ".$GLOBALS['ecs']->table('goods')."WHERE goods_id = '$goods_id'";
                $res = $GLOBALS['db']->getROW($sql);
                if($res['is_promote'] == 1 && $res['promote_start_date'] < gmtime() && $res['promote_end_date'] > gmtime())//是促销商品
                {
                    if(empty($volume))//不存在优惠价格
                    {
                        if($res['shop_price'] > $res['promote_price'])//本店价  > 促销价格
                        {
                            $min_price= ($res['promote_price'] + $atr_price);
                        }else
                        {
                            $min_price = ($res['shop_price'] + $atr_price);
                        }
                        return $min_price;
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price'],$res['promote_price']);//返回优惠  促销 本店 最小的价格
                        $min_price = ($min + $atr_price);
                        return $min_price;
                    }
                }else//不是促销商品
                {
                    if(empty($volume))//不存在优惠价格
                    {
                        $min_price = ($res['shop_price'] + $atr_price);
                        return $min_price;
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price']);//返回优惠  促销 本店 最小的价格
                        $min_price = ($min + $atr_price);
                        return $min_price;
                    }
                }
            }else
            {
                $sql = " SELECT * FROM ".$GLOBALS['ecs']->table('goods')."WHERE goods_id = '$goods_id'";
                $res = $GLOBALS['db']->getROW($sql);
                if($res['is_promote'] == 1 && $res['promote_start_date'] < gmtime() && $res['promote_end_date'] > gmtime())//是促销商品
                {
                    if(empty($volume))//不存在优惠价格
                    {
                        if($res['shop_price'] > $res['promote_price'])//本店价  > 促销价格
                        {
                            $min_price = $res['promote_price'];
                        }else
                        {
                            $min_price = $res['shop_price'];
                        }
                        return $min_price; 
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price'],$res['promote_price']);//返回优惠  促销 本店 最小的价格
                        $min_price = $min;
                        return $min_price;
                    }
                }else//不是促销商品
                {
                    if(empty($volume))//不存在优惠价格
                    {
                        $min_price = $res['shop_price'];
                        return $min_price;
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price']);//返回优惠  促销 本店 最小的价格
                        $min_price = $min;
                        return $min_price;
                    }
                }
            }
        }else
        {
            if(!empty($atr_price) && $atr_price != 0)//存在属性价格
            {
                $sql = " SELECT * FROM ".$GLOBALS['ecs']->table('goods')."WHERE goods_id = '$goods_id'";
                $res = $GLOBALS['db']->getROW($sql);
                
                $rank_price = rank_price($rank_id,$res['shop_price']);
                if($res['is_promote'] == 1 && $res['promote_start_date'] < gmtime() && $res['promote_end_date'] > gmtime())//是促销商品
                {
                    if(empty($volume))//不存在优惠价格
                    {
                        if($res['shop_price'] > $res['promote_price'])//本店价  > 促销价格
                        {
                            if($res['promote_price'] > $rank_price)
                            {
                                $min_price= ($rank_price + $atr_price);
                            }else
                            {
                                $min_price= ($res['promote_price'] + $atr_price);
                            }
                            return $min_price;
                        }else
                        {
                            if($res['shop_price'] > $rank_price)
                            {
                                $min_price= ($rank_price + $atr_price);
                            }else
                            {
                                $min_price = ($res['shop_price'] + $atr_price);
                            }
                            return $min_price;
                        }
                        
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price'],$res['promote_price'],$rank_price);//返回优惠  促销 本店 最小的价格
                        $min_price = ($min + $atr_price);
                        return $min_price;
                    }
                }else//不是促销商品
                {
                    if(empty($volume))//不存在优惠价格
                    {
                        if($res['shop_price'] > $rank_price)
                        {
                            $min_price = ($rank_price + $atr_price);
                        }else
                        {
                            $min_price = ($res['shop_price'] + $atr_price);
                        }
                        return $min_price;
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price']);//返回优惠  促销 本店 最小的价格
                        $min_price = ($min + $atr_price);
                        return $min_price;
                    }
                }
            }else
            {
                $sql = " SELECT * FROM ".$GLOBALS['ecs']->table('goods')."WHERE goods_id = '$goods_id'";
                $res = $GLOBALS['db']->getROW($sql);
                $rank_price = rank_price($rank_id,$res['shop_price']);
                if($res['is_promote'] == 1 && $res['promote_start_date'] < gmtime() && $res['promote_end_date'] > gmtime())//是促销商品
                {
                    if(empty($volume))//不存在优惠价格
                    {
                        if($res['shop_price'] > $res['promote_price'])//本店价  > 促销价格
                        {
                            if($res['promote_price'] > $rank_price)
                            {
                                $min_price = $rank_price;
                            }else
                            {
                                $min_price= $res['promote_price'];
                            }
                            return $min_price;
                        }else
                        {
                            if($res['shop_price'] > $rank_price)
                            {
                                $min_price = $rank_price;
                            }else
                            {
                                $min_price = $res['shop_price'];
                            }
                            return $min_price;
                        }
                        
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price'],$res['promote_price'],$rank_price);//返回优惠  促销 本店 最小的价格
                        $min_price = $min;
                        return $min_price;
                    }
                }else//不是促销商品
                {
                    if($volume == '' && empty($volume))//不存在优惠价格
                    {
                        if($res['shop_price'] > $rank_price)
                        {
                            $min_price = $rank_price;
                        }else
                        {
                            $min_price = $res['shop_price'];
                        }
                        return $min_price;
                    }else//存在优惠价格
                    {
                        $min = min($volume,$res['shop_price'],$rank_price);//返回优惠  促销 本店 最小的价格
                        $min_price = $min;
                        return $min_price;
                    }
                }
            }
        }
        
    }
}
/*
 *
 *获取对应会员等级的优惠价格
 *
 **/
function rank_price($rank,$price)
{
    $sql = "SELECT  IFNULL(mp.user_price, r.discount * $price / 100) AS price " .
            '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.rank_id = '$rank'";
        $rank_price = $GLOBALS['db']->getOne($sql);//取得会员等级价格
        return $rank_price;
}
 
?>