Blame view

application/api/controller/kuerp/third/Sales.php 127 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
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
  <?php

  

  namespace app\api\controller\kuerp\third;

  

  use think\Db;

  use Exception;

  class Sales extends Thirds

  {

      protected $noNeedLogin = '*';

      protected $noNeedRight = '*';

      protected $web_site    = '';

  

      protected $serial_status;

      protected $batch_status;

      protected $taxes_status;

      protected $price_taxes;

      protected $taxes_rate;

      protected $auxiliary;

      protected $price_decimal;

      protected $number_decimal;

      protected $examine_status;

  

      public function _initialize()

      {

          parent::_initialize();

          $this->web_site = $this->request->server('HTTP_ORIGIN');

  

          $this->serial_status= Db::name('kuerp_sets')->where(['name'=>'serial'])->value('is_checked');//是否开启序列号

          $this->batch_status= Db::name('kuerp_sets')->where(['name'=>'batch'])->value('is_checked');//是否开启批次

          $this->auxiliary = Db::name('kuerp_sets')->where(['name'=>'auxiliary'])->value('is_checked');//是否启用属性

          $this->taxes_status = Db::name('kuerp_sets')->where(['name'=>'taxes'])->value('is_checked');//是否启用税金

          $this->price_taxes = Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked');//商品价格是否含税

          $this->taxes_rate = Db::name('kuerp_sets')->where(['name'=>'taxes'])->value('value');//税率

          $this->price_decimal = Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value');

          $this->number_decimal = Db::name('kuerp_sets')->where(['name'=>'number_decimal'])->value('value');

          $this->examine_status = Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');

      }

  

      /**

       * 获取销货单列表

       * @ApiMethod (POST)

       */

      public function getSalesList()

      {

          $data = input();

          $page = isset($data['page'])?$data['page']:1;

          $page = empty($page)?1:$page;

          $limit = isset($data['limit'])?$data['limit']:10;

          $limit = empty($limit)?10:$limit;

  

          $where = [];

          $keyword = isset($data['keyword'])?$data['keyword']:'';

          if(!empty($keyword)){

              $where['s.purchase_code'] = ['like','%'.$keyword.'%'];

          }

          $order_num = isset($data['order_num'])?$data['order_num']:'';

          if(!empty($order_num)){

              $where['g.order_num'] = ['like','%'.$order_num.'%'];

          }

          $third_account = $this->third_account;

          $where['g.third_id'] = $third_account['id'];

  

          $list = Db::name('kuerp_goods_sales')->alias('s')

              ->join('kuerp_goods_salethird g','s.id=g.purchase_id')

              ->join('kuerp_customers c','s.customers_id=c.id')

              ->where($where)

              ->page($page,$limit)

              ->field('s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address,

              s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime')

              ->select();

  

          foreach ($list as $list_k=>$list_v){

              $list[$list_k]['purchase_time_text'] = date('Y-m-d',$list_v['purchase_time']);

              $list[$list_k]['createtime_text'] = date('Y-m-d H:i:s',$list_v['createtime']);

          }

          $count = Db::name('kuerp_goods_sales')->alias('s')

              ->join('kuerp_goods_salethird g','s.id=g.purchase_id')

              ->join('kuerp_customers c','s.customers_id=c.id')

              ->where($where)->count();

  

          $result['list'] = $list;$result['count'] = $count;

          $this->success('查询成功',$result,200);

      }

  

      /**

       * 添加销货单

       * @ApiMethod (POST)

       */

      public function sales()

      {

          $detail = input();

          $check = $this->checkParameter($detail,[

              'order_num|外部订单号'=>'require',

              'mobile|客户'=>'require',

              'goods|商品'=>'require',

          ]);

          if(isset($check['status'])){

              $this->error($check['msg'],'',$check['status']);

          }else{

              $purchase_code = $this->getCodes('',11);

              if(empty($purchase_code)){

                  $this->error('销货单单号获取失败','',27);

              }

              $purchase_time = strtotime(date('Y-m-d'));

  

              $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime');

              $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate');

              if($purchase_time<=$checktime){

                  $this->error('不可添加小于结账日期'.$checkdate.'的数据');

              }

  

              //验证客户是否可用

              $third_account = $this->third_account;

              $customer = Db::name('kuerp_customers')

                  ->where(['customer_mobile'=>$detail['mobile'],'third_id'=>$third_account['id']])

                  ->find();

              if(empty($customer)){

                  $this->error('此手机号未注册','',27);

              }elseif ($customer['status']!=1){

                  $this->error('客户状态异常','',27);

              }

  

              $salethird = Db::name('kuerp_goods_salethird')

                  ->where(['order_num'=>$detail['order_num'],'third_id' => $third_account['id'],])

                  ->find();

              if($salethird){

                  $this->error('此订单号已创建销货单','',27);

              }

  

              $account = Db::name('kuerp_accounts')->where(['is_default'=>'1'])->value('id');

              if(empty($account)){

                  $account = Db::name('kuerp_accounts')->value('id');

                  if(empty($account)){

                      $this->error('未查询到可用账户','',27);

                  }

              }

  

              $data = [];

              $number = 0;$all_money = 0;$tax_money=0;$money_tax_price=0;

  

              //验证商品数据

              $goods_data = html_entity_decode($detail['goods']);

              $goods_data = json_decode($goods_data,true);

              if(empty($goods_data)){

                  $this->error('商品数据解析失败','',27);

              }else{

  

                  foreach ($goods_data as $goods_data_k=>$goods_data_v){

                      $check = $this->checkParameter($goods_data_v,[

                          'goods_code|商品编号'=>'require',

                          'goods_unit|商品单位'=>'require',

                          'goods_attr|商品属性'=>'requireNone',

                          'number|数量'=>'require',

                          'price|商品价格'=>'require',

                      ]);

                      if(isset($check['status'])){

                          $this->error($check['msg'],'',$check['status']);

                      }

  

                      $status = 0;

                      foreach ($goods_data as $goods_data_k2=>$goods_data_v2){

                          if($goods_data_v['goods_code']==$goods_data_v2['goods_code'] && $goods_data_v['goods_unit']==$goods_data_v2['goods_unit'] && $goods_data_v['goods_attr']==$goods_data_v2['goods_attr']){

                              $status += 1;

                          }

                      }

                      if($status>1){

                          $this->error('同一笔销货单不可存在相同单位,属性的商品','',27);

                      }

  

                      $goods = Db::name('kuerp_goods')->where(['goods_code'=>$goods_data_v['goods_code']])->find();

                      if(empty($goods)){

                          $this->error('商品数据错误','',27);

                      }

                      if($goods['auxiliary']==1 && $this->auxiliary==1 && empty($goods_data_v['goods_attr'])){

                          $this->error($goods['goods_name'].'必须选择商品属性','',27);

                      }

                  }

  

                  $data = [];

                  $goods_data = $this->setGoodsData($goods_data);

                  if($goods_data['status']!=1){

                      $this->error($goods_data['msg'],'',27);

                  }else{

                      foreach ($goods_data['data'] as $goods_data_k=>$goods_data_v){

                          $goods_data_v['purchase_code'] = $purchase_code;

                          $goods_data_v['purchase_type'] = 1;

                          $goods_data_v['purchase_time'] = $purchase_time;

                          $goods_data_v['customers_id'] = $customer['id'];

                          $goods_data_v['sale_user'] = 0;

                          $goods_data_v['receiving_address'] = isset($detail['address'])?$detail['address']:$customer['getgoods_address'];

                          $data[] = $goods_data_v;

  

                          $number += (float)$goods_data_v['number'];

                          $all_money += (float)$goods_data_v['all_money'];

                          $tax_money += (float)$goods_data_v['tax_money'];

                          $money_tax_price += (float)$goods_data_v['money_tax_price'];

                      }

                  }

              }

  

              $preferential_rate = ((float)$detail['preferential_money'])/$money_tax_price;

              $row = [

                  'purchase_code'     =>  $purchase_code,

                  'purchase_type'     =>  1,

                  'purchase_time'     =>  $purchase_time,

                  'customers_id'      =>  $customer['id'],//客户

                  'sale_user'         =>  0,//销售员

                  'receiving_address' =>  isset($detail['address'])?$detail['address']:$customer['getgoods_address'],

                  'customers_money'   =>  0,

                  'number'            =>  $number,

                  'all_money'         =>  $all_money,

                  'tax_money'         =>  $tax_money,//税

                  'money_tax_price'   =>  $money_tax_price,//价税合计

                  'preferential_rate' =>  $preferential_rate,

                  'preferential_money'=>  (float)$detail['preferential_money'],

                  'real_money'        =>  (float)($money_tax_price-(float)$detail['preferential_money']),

                  'purchase_money'    =>  0,

                  'note'              =>  isset($detail['note'])?$detail['note']:'',

                  'arrears'           =>  0,

                  'all_arrears'       =>  0,

                  'pay_status'        =>  1,

                  'pay_money'         =>  (float)($money_tax_price-(float)$detail['preferential_money']),

                  'account'           =>  $account,

                  'cost_data'         =>  null,

                  'third_id'          => $third_account['id'],

                  'createtime'        => time(),

                  'updatetime'        => time(),

              ];

  

              $result = false;

              Db::startTrans();

              try {

                  if(!empty($row)){

                      $result = Db::name('kuerp_goods_sales')->insertGetId($row);

                  }

  

                  Db::name('kuerp_goods_salethird')->insert([

                      'order_num' => $detail['order_num'],

                      'purchase_id' => $result,

                      'third_id' => $third_account['id'],

                      'createtime' => time(),

                      'updatetime' => time(),

                  ]);

  

                  Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>$row['purchase_code']])->delete();

                  foreach ($data as $data_k=>$data_v){

                      $goods_serial = $data_v['serial_code'];

                      unset($data_v['serial_code']);

  

                      $goods_batch_info = $data_v['goods_batch_info'];

                      unset($data_v['goods_batch_info']);

  

                      $purchase_id = Db::name('kuerp_goods_sales_info')->insertGetId($data_v);

  

                      //批次

                      if($goods_batch_info){

                          $goods_batch_info['purchase_info_id'] = $purchase_id;

                          Db::name('kuerp_goods_batch_sale_info')->insert($goods_batch_info);

                      }

  

                      if(!empty($goods_serial)){

                          Db::name('kuerp_goods_serial')->where(['serial'=>['in',$goods_serial]])->update([

                              'status'=>'3',

                              'sale_id' => $purchase_id,

                              'sale_code' => $data_v['purchase_code']

                          ]);

                      }

                  }

  

                  Db::commit();

              } catch (Exception $e) {

                  Db::rollback();

                  $this->error($e->getMessage());

              }

              if ($result !== false) {

                  if($this->examine_status!=1){

                      try {

                          $exa = $this->examine($result,1);

                      } catch (Exception $e) {

                      }

                  }

                  $this->success('添加成功',['id'=>$result,'purchase_code'=>$row['purchase_code']],1);

              } else {

                  $this->error('添加失败','',27);

              }

          }

      }

  

      /**

       * 循环解析商品信息

       */

      function setGoodsData($goods_data){

          $serial_status = $this->serial_status;//是否开启序列号

          $batch_status = $this->batch_status;//是否开启批次

          $taxes_status = $this->taxes_status;//是否启用税金

          $price_taxes = $this->price_taxes;//商品价格是否含税

          $taxes_rate = $this->taxes_rate;//税率

          $account = Db::name('kuerp_accounts')->where(['is_default'=>'1'])->value('id');

          if(empty($account)){

              $account = Db::name('kuerp_accounts')->value('id');

              if(empty($account)){

                  return ['status'=>0,'msg'=>'未查询到可用账户'];

              }

          }

  

          $goods_arr = [];

          foreach ($goods_data as $goods_data_k=>$goods_data_v){

              $goods = Db::name('kuerp_goods')->where(['goods_code'=>$goods_data_v['goods_code']])->find();

  

              $number = (float)$goods_data_v['number'];

              if($taxes_status==1){//启用税金

                  $unit_price = $goods_data_v['price'];

  

                  $tax_included = $unit_price;//默认接口提交的金额是价税合计,若不是则使用注释代码

                  $unit_price = $tax_included*100/(100+$taxes_rate);//单价

                  //if($price_taxes==1){//商品价格是否含税

                  //$tax_included = $unit_price;

                  //$unit_price = $tax_included*100/(100+$taxes_rate);//单价

                  //}else{

                  //$tax_included = $unit_price*(100+$taxes_rate)/100;

                  //}

              }else{

                  $unit_price = $goods_data_v['price'];

                  $tax_included = $goods_data_v['price'];

              }

              $all_money = $unit_price*$number;

              $money_tax_price = $tax_included*$number;

              $tax_money = $money_tax_price-$all_money;

  

  

              //商品库存

              $where = [];

              $where['goods_code'] = $goods['goods_code'];

              if($goods_data_v['goods_attr']>0){

                  $where['attribute_id'] = $goods_data_v['goods_attr'];

              }

              $stock = Db::name('kuerp_goods_stock')->where($where)->select();//通过库存去查询可用仓库

              if(empty($stock)){

                  return ['status'=>0,'msg'=>$goods['goods_name'].'商品库存不足'];

              }

              $allow_stock = Db::name('kuerp_goods_stock')->where($where)->sum('stock');

              $need_number = $number;

              $stock_arr = [];

              foreach ($stock as $stock_k=>$stock_v){

                  if($need_number<=0){

                      break;

                  }else{

                      if($need_number<$stock_v['stock']){

                          $stock_arr[] = [

                              'stores_id' => $stock_v['stores_id'],

                              'number'    => $need_number,

                          ];

                          $need_number = 0;

                      }else{

                          $stock_arr[] = [

                              'stores_id' => $stock_v['stores_id'],

                              'number'    => $stock_v['stock'],

                          ];

                          $need_number -= $stock_v['stock'];

                      }

                  }

              }

  

              if($goods['batch']==1 && $batch_status==1){//开启批次

                  $stock_batch_arr = [];

                  foreach ($stock_arr as $stock_arr_k=>$stock_arr_v){

                      $where = [];

                      $where['goods_id'] = $goods['id'];

                      if($goods_data_v['goods_attr']>0){

                          $where['attribute_id'] = $goods_data_v['goods_attr'];

                      }

                      $where['stores_id'] = $stock_arr_v['stores_id'];

                      $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->select();

                      if(empty($goods_batch_stock)){

                          return ['status'=>0,'msg'=>'批次信息查询失败'];

                      }

  

                      $need_number = $stock_arr_v['number'];

                      foreach ($goods_batch_stock as $stock_k=>$stock_v){

                          if($need_number<=0){

                              break;

                          }else{

                              $batch = Db::name('kuerp_goods_batch')->where(['id'=>$stock_v['batch_id']])->find();

                              if(empty($batch)){

                                  return ['status'=>0,'msg'=>'批次信息查询失败'];

                              }

                              if($need_number<$stock_v['stock']){

                                  $stock_batch_arr[] = [

                                      'stores_id' => $stock_v['stores_id'],

                                      'batch_id'  => $stock_v['batch_id'],

                                      'number'    => $need_number,

                                      'batch_name'  => $batch['batch_name'],

                                      'manufacture' => $batch['manufacture'],

                                      'guarantee'   => $batch['guarantee'],

                                      'validity'    => $batch['validity'],

                                  ];

                                  $need_number = 0;

                              }else{

                                  $stock_batch_arr[] = [

                                      'stores_id' => $stock_v['stores_id'],

                                      'batch_id'  => $stock_v['batch_id'],

                                      'number'    => $stock_v['stock'],

                                      'batch_name'  => $batch['batch_name'],

                                      'manufacture' => $batch['manufacture'],

                                      'guarantee'   => $batch['guarantee'],

                                      'validity'    => $batch['validity'],

                                  ];

                                  $need_number -= $stock_v['stock'];

                              }

                          }

                      }

                  }

                  $stock_arr = $stock_batch_arr;

              }

  

              if($goods['serial']==1 && $serial_status==1){//开启序列号

                  foreach ($stock_arr as $stock_arr_k=>$stock_arr_v){

                      $where = [];

                      $where['goods_id'] = $goods['id'];

                      if($goods_data_v['goods_attr']>0){

                          $where['attribute_id'] = $goods_data_v['goods_attr'];

                      }

                      $where['stores_id'] = $stock_arr_v['stores_id'];

                      $where['store_status'] = '1';

                      $serial_id = Db::name('kuerp_goods_serial')->where($where)->limit($stock_arr_v['number'])->column('id');

                      if(sizeof($serial_id)!=$stock_arr_v['number']){

                          return ['status'=>0,'msg'=>'商品可用序列号不足'];

                      }

                      $stock_arr[$stock_arr_k]['serial_id'] = $serial_id;

  

                      $period_period = [];$serial_code = [];

                      $serial = Db::name('kuerp_goods_serial')->where($where)->limit($stock_arr_v['number'])->select();

                      foreach ($serial as $serial_k=>$serial_v){

                          $period_period[] = [

                              'serial' => $serial_v['serial'],

                              'remark' => $serial_v['remark'],

                          ];

                          $serial_code[] = $serial_v['serial'];

                      }

                      $stock_arr[$stock_arr_k]['period_period'] = serialize($period_period);

                      $stock_arr[$stock_arr_k]['serial_code'] = $serial_code;

                  }

              }

  

              //多单位换算基础数量

              $advance_rate = 1;

              if($goods['more_unit']==1){

                  $main_unit = $goods['more_unit_id'];

                  $unit_list = Db::name('kuerp_unit_conversion')

                      ->where(['main_unit'=>$main_unit])

                      ->order('advance_rate','ASC')

                      ->field('vice_unit,advance_rate')

                      ->select();

                  $unit_id = [$main_unit];

                  foreach ($unit_list as $unit_list_k=>$unit_list_v){

                      $unit_id[] = $unit_list_v['vice_unit'];

                  }

  

                  $unit_list_id = Db::name('kuerp_unit_conversion')

                      ->where(['main_unit'=>$main_unit])

                      ->column('advance_rate','vice_unit');

                  $unit_list_id[$goods['unit_id']] = 1;

                  $advance_rate = isset($unit_list_id[$goods_data_v['goods_unit']])?$unit_list_id[$goods_data_v['goods_unit']]:$advance_rate;

              }

              foreach ($stock_arr as $stock_arr_k=>$stock_arr_v){

                  $goods_batch_info = [];

                  if(isset($stock_arr_v['batch_id'])){

                      $goods_batch_info = [

                          'batch_id'    => isset($stock_arr_v['batch_id'])?$stock_arr_v['batch_id']:0,

                          'goods_id'    => $goods['id'],

                          'goods_code'  => $goods['goods_code'],

                          'attribute_id'=> $goods_data_v['goods_attr'],

                          'goods_unit'  => $goods_data_v['goods_unit'],

                          'stores_id'   => $stock_arr_v['stores_id'],

                          'stock'       => $stock_arr_v['number'],

                          'number'      => $stock_arr_v['number'],

                          'batch_name'  => isset($stock_arr_v['batch_name'])?$stock_arr_v['batch_name']:'',

                          'manufacture' => isset($stock_arr_v['manufacture'])?$stock_arr_v['manufacture']:null,

                          'guarantee'   => isset($stock_arr_v['guarantee'])?$stock_arr_v['guarantee']:null,

                          'validity'    => isset($stock_arr_v['validity'])?$stock_arr_v['validity']:null,

                          'status'      => '0',

                          'type'        => 2,

                          'createtime'  => time(),

                          'updatetime'  => time(),

                      ];

                  }

  

                  $goods_arr[] = [

                      'goods_code'    => $goods_data_v['goods_code'],

                      'goods_note'    => $goods['note'],

                      'goods_unit'    => $goods_data_v['goods_unit'],

                      'stores_id'     => $stock_arr_v['stores_id'],

                      'allow_stock'   => $allow_stock,

                      'number'        => $stock_arr_v['number'],

                      'default_unit'  => $goods['unit_id'],

                      'basic_nmber'   => $stock_arr_v['number']*$advance_rate,

                      'unit_price'    => $unit_price,

                      'cost_price'    => 0,

                      'discount'      => 0,

                      'dis_money'     => 0,

                      'all_money'     => $all_money,

                      'remark'        => '',

                      'goods_attr'    => $goods_data_v['goods_attr'],

                      'tax_included'  => $tax_included,

                      'tax_rate'      => $taxes_rate,

                      'tax_money'     => $tax_money,

  

                      'money_tax_price'=> $money_tax_price,

                      'batch_id'      => isset($stock_arr_v['batch_id'])?$stock_arr_v['batch_id']:0,

                      'batch_name'    => isset($stock_arr_v['batch_name'])?$stock_arr_v['batch_name']:'',

                      'manufacture'   => isset($stock_arr_v['manufacture'])?$stock_arr_v['manufacture']:null,

                      'guarantee'     => isset($stock_arr_v['guarantee'])?$stock_arr_v['guarantee']:null,

                      'validity'      => isset($stock_arr_v['validity'])?$stock_arr_v['validity']:null,

                      'preferential_rate' => 0,

                      'preferential_money' => 0,

                      'real_money'    => $money_tax_price,

                      'purchase_money'=> 0,

                      'pay_status'    => 1,

                      'pay_money'     => $money_tax_price,

                      'account'       => $account,

                      'createtime'    => time(),

                      'updatetime'    => time(),

                      'goods_batch_info'  => $goods_batch_info,

                      'period'        => isset($stock_arr_v['period_period'])?$stock_arr_v['period_period']:'',

                      'serial_code'   => isset($stock_arr_v['serial_code'])?$stock_arr_v['serial_code']:[],

                  ];

              }

          }

          return ['status'=>1,'data'=>$goods_arr];

      }

  

      //审核/反审核

      function examine($ids = null,$examine=1){

          $examine_set= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');

          if(empty($ids)){

              if($examine_set!=1){

                  return ['status'=>0,'msg'=>'系统错误'];

              }

              $this->error('请选择要操作的数据');

          }

          if($examine==1){

              $list = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0'])->select();

              $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime');

              $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate');

              foreach ($list as $list_k=>$list_v){

                  if($list_v['purchase_time']<=$checktime){

                      $this->error('不可操作小于结账日期'.$checkdate.'的数据');

                  }

              }

              $set_discount = \think\Db::name('kuerp_sets')->where(['name'=>'discount'])->value('is_checked');

  

              $purchase_code = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0'])->column('purchase_code');

              if(empty($list)){

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'系统错误'];

                  }

                  $this->error('请选择需要审核的数据');

              }

              $account_list_obj = Db::name('kuerp_accounts')->field('id,account_coding,account_name,balance')->select();

              $account_list = [];

              foreach ($account_list_obj as $account_list_obj_k=>$account_list_obj_v){

                  $account_list[$account_list_obj_v['id']] = $account_list_obj_v;

              }

              $examine_ids = [];

              $accountData = [];

              $arrears = [];

              $log = [];

              $payment_log = [];$invoicing = [];

              foreach ($list as $list_k=>$list_v){

                  $examine_ids[] = $list_v['id'];

                  if($list_v['arrears']>0){

                      $suppliers = Db::name('kuerp_customers')->where(['id'=>$list_v['customers_id']])->field('id,need_receive_account')->find();

                      if($suppliers){

                          $status = false;

                          foreach ($arrears as $arrears_k=>$arrears_v){

                              if($arrears_v['id']==$suppliers['id']){

                                  $arrears[$arrears_k]['need_receive_account'] = $list_v['arrears']+$arrears_v['need_receive_account'];

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $arrears[] = [

                                  'id'  => $suppliers['id'],

                                  'need_receive_account'  => $list_v['arrears']+$suppliers['need_receive_account'],

                              ];

                          }

                      }

                  }

                  if($list_v['pay_money']>0){

                      $accountData[] = [

                          'balance'  => $list_v['pay_money'],

                          'account'  => $list_v['account'],

                      ];

                      $log[] = [

                          'account_coding'    => $account_list[$list_v['account']]['account_coding'],

                          'account_name'      => $account_list[$list_v['account']]['account_name'],

                          'balance'           => $account_list[$list_v['account']]['balance']-$list_v['pay_money'],

                          'expenditure'       => $list_v['pay_money'],

                          'pay_time'          => $list_v['purchase_time'],

                          'bill_code'         => $list_v['purchase_code'],

                          'business_type'     => 5,

                          'customers_id'      => $list_v['customers_id'],

                          'remark'            => $list_v['note'],

                          'createtime'        => time(),

                          'updatetime'        => time(),

                      ];

                  }

                  $payment_log[] = [

                      'customers_id'   => $list_v['customers_id'],

                      'sale_user'      => $list_v['sale_user'],

                      'business_type'  => 1,

                      'bill_time'      => $list_v['purchase_time'],

                      'bill_code'      => $list_v['purchase_code'],

                      'bill_codes'     => $list_v['purchase_code'],

                      'purchase_money' => $list_v['all_money'],

                      'discount_money' => $list_v['preferential_money'],

                      'real_money'     => $list_v['real_money'],

                      'money_tax_price'=> $list_v['money_tax_price'],

                      'pay_money'      => $list_v['pay_money'],

                      'give_money'     => $list_v['arrears'],

                      'reamrk'         => $list_v['note'],

                      'createtime'     => time(),

                  ];

                  if($set_discount==1){

                      $money = $list_v['real_money'];

                  }else{

                      $money = $list_v['money_tax_price'];

                  }

                  $invoicing[] = [

                      'customers_id'   => $list_v['customers_id'],

                      'status'         => $list_v['pay_status'],

                      'bill_time'      => $list_v['purchase_time'],

                      'bill_code'      => $list_v['purchase_code'],

                      'bill_codes'     => $list_v['purchase_code'],

                      'money'          => $list_v['real_money'],

                      'tax'            => $list_v['tax_money'],

                      'real_money'     => $money,

                      'pay_money'      => 0,

                      'give_money'     => $money,

                      'createtime'     => time(),

                  ];

              }

              $editstock = [];$addstock = [];$goods_stock = [];

              $info_list = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>['in',$purchase_code]])->select();

              foreach ($info_list as $info_list_k=>$info_list_v){

                  try {

                      unset($info_list[$info_list_k]['period']);

                  } catch (Exception $e) {

                  }

              }

  

              $purchase_info_id = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>['in',$purchase_code]])->column('id');

  

              $purchase_info = [];

              $goods_stock_log = [];//收发详情

              $serial_log = [];//序列号跟踪

              foreach ($info_list as $list_k=>$list_v){

                  //序列号跟踪

                  $goods_serial = Db::name('kuerp_goods_serial')->where(['sale_id'=>$list_v['id']])->field('goods_id,serial,stores_id,attribute_id')->select();

                  foreach ($goods_serial as $goods_serial_k=>$goods_serial_v){

                      $serial_log[] = [

                          'goods_id'=>$goods_serial_v['goods_id'],

                          'stores_id'=>$goods_serial_v['stores_id'],

                          'attribute_id'=>$goods_serial_v['attribute_id'],

                          'serial'=>$goods_serial_v['serial'],

                          'business_type'=>'4',

                          'store_status'=>'2',

                          'remarks' => $list_v['remark'],

                          'purchase_id'=>$list_v['id'],

                          'customer_id'=>$list_v['customers_id'],

                          'purchase_code'=>$list_v['purchase_code'],

                          'purchase_time' => $list_v['purchase_time'],

                          'createtime'  => time(),

                          'updatetime'  => time(),

                      ];

                  }

                  //序列号跟踪

  

                  $info_list[$list_k]['sales_order_id']    = $list_v['id'];

                  $info_list[$list_k]['createtime']        = time();

                  $info_list[$list_k]['updatetime']        = time();

                  $stock_number = $list_v['basic_nmber'];

                  $goods = Db::name('kuerp_goods')->where(['goods_code'=>$list_v['goods_code']])->field('id,attribute_id,inventory,unit_id')->find();

                  $status = false;

                  foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){

                      if($goods_stock_v['id']==$goods['id']){

                          $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']-$stock_number;

                          $status = true;

                      }

                  }

                  if(!$status){

                      $goods_stock[] = [

                          'id'=>$goods['id'],

                          'inventory'=>$goods['inventory']-$stock_number,

                      ];

                  }

                  //商品库存

                  $where = [];

                  $where['goods_code'] = $list_v['goods_code'];

                  $where['stores_id'] = $list_v['stores_id'];

                  if($list_v['goods_attr']>0){

                      $goods_attr = $list_v['goods_attr'];

                      if($goods_attr>0){

                          $where['attribute_id'] = $goods_attr;

                      }

                  }

  

                  //获取成本单价

                  $balance_cost=0;$balance_unit=0;

                  $goodsInfo = json_decode($this->getUnit($list_v['goods_code'],$list_v['stores_id'],$list_v['goods_attr']),true);

                  $unitData = isset($goodsInfo['unit'])?$goodsInfo['unit']:[];

                  if(empty($unitData)){

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>'商品信息获取错误'];

                      }

                      $this->error('商品信息获取错误');

                  }else{

                      foreach ($unitData as $unitData_k=>$unitData_v){

                          if($unitData_v['id']==$goods['unit_id']){

                              $balance_unit = $unitData_v['purchase'];

                          }

                      }

                  }

                  //获取成本单价结束

  

                  $stock = Db::name('kuerp_goods_stock')->where($where)->field('stock,id')->find();

                  if($stock){

                      $status = false;

                      foreach ($editstock as $editstock_k=>$editstock_v){

                          if($editstock_v['id']==$stock['id']){

                              $editstock[$editstock_k]['stock'] -= $stock_number;

                              $status = true;

                              $balance_cost   = (round($balance_unit*$stock_number,4));

                          }

                      }

                      if(!$status){

                          $editstock[] = [

                              'id'=>$stock['id'],

                              'stock'=>(round($stock['stock']-$stock_number,4))

                          ];

                          $balance_cost   = (round($balance_unit*$stock_number,4));

                      }

                  }else{

                      $store = Db::name('kuerp_stores')->where(['id'=>$list_v['stores_id']])->value('name');

                      if($examine!=1){

                          if($examine_set!=1){

                              return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败'];

                          }

                          return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败'];

                      }

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>'商品'.$store.'库存不足'];

                      }

                      $this->error('商品'.$store.'库存不足');

                  }

                  $purchase_info[] = $list_v['id'];

  

                  //收发详情

                  if($goods){

                      $goods_stock_log[] = [

                          'purchase_code' => $list_v['purchase_code'],

                          'purchase_id'   => $list_v['id'],

                          'purchase_time' => $list_v['purchase_time'],

                          'business_type' => '4',

                          'customer_id'   => $list_v['customers_id'],

                          'goods_id'      => $goods['id'],

                          'goods_code'    => $list_v['goods_code'],

                          'attribute_id'  => (float)$list_v['goods_attr'],

                          'goods_unit'    => (float)$list_v['goods_unit'],

                          'stores_id'     => (float)$list_v['stores_id'],

                          'remark'        => $list_v['remark'],

                          'out_number'    => (float)$list_v['number'],

                          'out_basics'    => (float)$list_v['basic_nmber'],

                          'out_unit'      => $balance_unit,

                          'out_cost'      => $balance_cost,

                          'createtime'    => time(),

                          'updatetime'    => time(),

                      ];

                  }

                  //收发详情结束

              }

              foreach ($editstock as $editstock_k=>$editstock_v){

                  if($editstock_v['stock']<0){

                      $store = Db::name('kuerp_stores')->where(['id'=>$list_v['stores_id']])->value('name');

                      if($examine_set!=1){

                          return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败'];

                      }

                      $this->error($store.'库存不足,操作失败');

                  }

              }

  

              $order_count = [];

              $code_id = Db::name('kuerp_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->column('order_id');

              $purchase_order_info = Db::name('kuerp_goods_sales_order_info')

                  ->field('id,purchase_code')

                  ->where(['id'=>['in',$code_id]])

                  ->select();

              foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){

                  $purord_info = Db::name('kuerp_goods_saleord_info')

                      ->where(['order_id'=>$purchase_order_info_v['id'],'purchase_id'=>['in',$purchase_info]])->field('sum(number) as number,sum(money) as money,sum(all_money) as all_money')->find();

                  $purchase_order_info[$purchase_order_info_k]['pay_count'] = isset($purord_info['number'])?$purord_info['number']:0;

                  $purchase_order_info[$purchase_order_info_k]['pay_money'] = isset($purord_info['money'])?$purord_info['money']:0;

                  $purchase_order_info[$purchase_order_info_k]['all_money'] = isset($purord_info['all_money'])?$purord_info['all_money']:0;

              }

              foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){

                  $status = false;

                  foreach ($order_count as $order_count_k=>$order_count_v){

                      if($order_count_v['purchase_code']==$purchase_order_info_v['purchase_code']){

                          $order_count[$order_count_k]['store_number'] += $purchase_order_info_v['pay_count'];

                          $status = true;

                      }

                  }

                  if(!$status){

                      $order_count[] = [

                          'purchase_code'  => $purchase_order_info_v['purchase_code'],

                          'store_number'  => $purchase_order_info_v['pay_count'],

                      ];

                  }

              }

              //修改状态

              if(!empty($order_count)){

                  foreach ($order_count as $order_count_k=>$order_count_v){

                      $number = Db::name('kuerp_goods_sales_order')->where(['purchase_code'=>$order_count_v['purchase_code']])->field('id,number,store_number')->find();

                      if(($number['number']-$number['store_number'])<=$order_count_v['store_number']){

                          $order_count[$order_count_k]['status'] = 1;

                      }else{

                          $order_count[$order_count_k]['status'] = 2;

                      }

                      $order_count[$order_count_k]['id'] = $number['id'];

                  }

              }

  

              //批次库存数据

              //获取设置

              $set= \think\Db::name('kuerp_sets')->field('type,name,value,is_checked,set')->select();

              $setList = [];

              foreach ($set as $set_k=>$set_v){

                  if(!empty($set_v['set'])){

                      $set_v['set'] = unserialize($set_v['set']);

                  }

                  $setList[$set_v['name']] = $set_v;

              }

              //数量价格格式化

              $number_decimal = $setList['number_decimal']['value'];

              $goods_serial = Db::name('kuerp_goods_sales_info')->where(['id'=>['in',$purchase_info_id]])->select();

  

              $goods_batch_add = [];$goods_batch_edit = [];$goods_batch_log = [];

              foreach ($goods_serial as $k=>$v){

                  $goods = Db::name('kuerp_goods')->where(['goods_code'=>$v['goods_code']])->field('id,goods_name,attribute_id,inventory,unit_id,batch')->find();

                  if($goods['batch']==1){

                      //先通过单位计算需修改多单位商品的库存数量

                      if($goods['unit_id']==$v['goods_unit']){

                          $stock_number = $v['number'];

                      }else{

                          $unit_conversion = Db::name('kuerp_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$v['goods_unit']])->value('advance_rate');

                          if(empty($unit_conversion)){

                              if($examine_set!=1){

                                  return ['status'=>0,'msg'=>'商品单位信息查询失败'];

                              }

                              $this->error('商品单位信息查询失败');

                          }else{

                              $stock_number = round(($v['number']*$unit_conversion),$number_decimal);

                          }

                      }

  

                      $where = [];

                      $where['goods_id'] = $goods['id'];

                      if($v['goods_attr']){

                          $where['attribute_id'] = $v['goods_attr'];

                      }

                      $where['stores_id'] = $v['stores_id'];

                      $where['batch_id'] = $v['batch_id'];

                      $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find();

                      if(empty($goods_batch_stock)){

                          $status = false;

                          foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){

                              if($goods_batch_add_v['batch_id']==$v['batch_id'] && $goods_batch_add_v['goods_id']==$v['goods_id'] && $goods_batch_add_v['attribute_id']==$v['attribute_id'] && $goods_batch_add_v['stores_id']==$v['stores_id']){

                                  $goods_batch_add[$goods_batch_add_k]['stock'] -= $stock_number;

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $goods_batch_add[] = [

                                  'batch_id' => $v['batch_id'],

                                  'goods_id' => $goods['id'],

                                  'goods_code' => $v['goods_code'],

                                  'attribute_id' => $v['goods_attr'],

                                  'stores_id' => $v['stores_id'],

                                  'stock' => $stock_number,

                                  'createtime' => time(),

                                  'updatetime' => time(),

                              ];

                          }

                      }else{

                          if(isset($goods_batch_edit[$goods_batch_stock['id']])){

                              $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number;

                          }else{

                              $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']-$stock_number;

                          }

                      }

                      foreach ($goods_batch_edit as $k2=>$v2){

                          if($v2<0){

                              if($examine_set!=1){

                                  return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];

                              }

                              $this->error('商品'.$goods['goods_name'].'库存不足');

                          }

                      }

  

                      //商品批次跟踪

                      $goods_batch_log[] = [

                          'goods_id'      => $goods['id'],

                          'goods_unit'    => $v['goods_unit'],

                          'stores_id'     => $v['stores_id'],

                          'batch_id'      => (float)$v['batch_id'],

                          'number'        => (float)$v['number'],

                          'purchase_code' => $v['purchase_code'],

                          'purchase_time' => $v['purchase_time'],

                          'business_type' => '4',

                          'remarks'       => $v['remark'],

                          'order_id'      => $v['id'],

                          'unit_price'    => $v['unit_price'],

                          'tax_included'  => $v['tax_included'],

                          'all_money'     => $v['all_money'],

                          'tax_money'     => $v['tax_money'],

                          'money_tax_price' => $v['money_tax_price'],

                          'customer_id'   => $v['customers_id'],

                          'createtime'    => time(),

                          'updatetime'    => time(),

                      ];

                  }

                  //商品批次跟踪结束

              }

              //批次库存数据结束

  

              //应收账款明细表

              $ex_list = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0','arrears'=>['>','0']])->select();

              $accounts_receivable = [];

              foreach ($ex_list as $ex_list_k=>$ex_list_v){

                  $accounts_receivable[] = [

                      'customers_id'      => $ex_list_v['customers_id'],

                      'purchase_time'     => $ex_list_v['purchase_time'],

                      'purchase_code'     => $ex_list_v['purchase_code'],

                      'business_type'     => '6',

                      'receivable'        => $ex_list_v['arrears'],

                      'staff_member_id'   => $ex_list_v['sale_user'],

                      'remark'            => $ex_list_v['note'],

                      'sale_money'        => $ex_list_v['money_tax_price'],

                      'receivable_money'  => round(($ex_list_v['real_money']+$ex_list_v['customers_money']),4),

                      'amount'            => $ex_list_v['preferential_money'],

                      'cost'              => $ex_list_v['customers_money'],

                      'real_money'        => $ex_list_v['pay_money'],

                      'createtime'        => time(),

                      'updatetime'        => time(),

                  ];

              }

  

              //采购销售费用

              $cost_data = [];

              $suppliers = Db::name('kuerp_suppliers')->column('id','supplyer_code');

              foreach ($list as $list_k=>$list_v){

                  if(!empty($list_v['cost_data'])){

                      $cost_datas = unserialize($list_v['cost_data']);

                      foreach ($cost_datas as $cost_datas_k=>$cost_datas_v){

                          $list_v['supplyer_code'] = isset($list_v['supplyer_code'])?$list_v['supplyer_code']:'';

                          $cost_data[] = [

                              'purchase_time'     => time(),

                              'purchase_code'     => (new Goods())->getCodes('',27),

                              'business_type'     => '2',

                              'suppliers_ids'     => isset($suppliers[$list_v['supplyer_code']])?$suppliers[$list_v['supplyer_code']]:null,

                              'suppliers_id'      => $cost_datas_v['suppliers_id'],

                              'expend_type_id'    => $cost_datas_v['expend_type_id'],

                              'purchase_number'   => $list_v['purchase_code'],

                              'purchase_times'    => $list_v['purchase_time'],

                              'money'             => $cost_datas_v['money'],

                              'pay_money'         => $cost_datas_v['money'],

                              'remark'            => $cost_datas_v['remark'],

                              'preparation_id'    => 0,

                              'operation_id'      => 0,

                              'createtime'        => time(),

                              'updatetime'        => time(),

                          ];

                      }

                  }

              }

  

              //销售利润表

              $profit = [];

              foreach ($list as $list_k=>$list_v){

                  $amount = null;

                  $amounts = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>$list_v['purchase_code']])->sum('dis_money');

                  if($amounts>0){

                      $amount = $amounts;

                  }

                  $profit[] = [

                      'purchase_type' => '1',

                      'purchase_code' => $list_v['purchase_code'],

                      'purchase_time' => $list_v['purchase_time'],

                      'customers_id'  => $list_v['customers_id'],

                      'sale_user'     => $list_v['sale_user'],

                      'preparation_id'=> $list_v['preparation_id'],

                      'examine_id'    => 0,

                      'number'        => $list_v['number'],

                      'amount'        => $amount,

                      'all_money'     => $list_v['all_money'],

                      'tax_money'     => $list_v['tax_money'],

                      'money_tax_price'=> $list_v['money_tax_price'],

                      'preferential_money'=> $list_v['preferential_money'],

                      'purchase_money'=> $list_v['purchase_money'],

                      'real_money'    => $list_v['real_money'],

                      'customers_money'=> $list_v['customers_money'],

                      'need_money'    => $list_v['real_money']+$list_v['customers_money'],

                      'pay_money'     => $list_v['pay_money'],

                      'remark'        => $list_v['note'],

                  ];

              }

  

              //核销单源单

              $writeoff_source = [];

              foreach ($list as $list_k=>$list_v){

                  if($list_v['arrears']>0){

                      $writeoff_source[] = [

                          'purchase_type' => 3,

                          'type' => 1,

                          'customers_id' => $list_v['customers_id'],

                          'purchase_code' => $list_v['purchase_code'],

                          'purchase_time' => $list_v['purchase_time'],

                          'price' => $list_v['real_money'],

                          'money' => $list_v['pay_money'],

                          'write_off' => $list_v['arrears'],

                          'remark' => $list_v['note'],

                      ];

                  }

              }

  

              Db::startTrans();

              try {

  

                  $orderinfo_id = Db::name('kuerp_user_ordersaleinfo')->where(['sales_id'=>['in',$ids]])->column('orderinfo_id');

                  $orderinfo = Db::name('kuerp_user_orderinfo')->where(['id'=>['in',$orderinfo_id]])->field('goods_id,number')->select();

                  foreach ($orderinfo as $orderinfo_k=>$orderinfo_v){

                      Db::name('kuerp_goods')->where(['id'=>$orderinfo_v['goods_id']])->inc('sale_number',$orderinfo_v['number'])->update();

                  }

  

                  if(!empty($writeoff_source)){

                      Db::name('kuerp_writeoff_source')->insertAll($writeoff_source);

                  }

  

                  if(!empty($profit)){

                      Db::name('kuerp_goods_sales_profit')->insertAll($profit);

                  }

                  if(!empty($cost_data)){

                      Db::name('kuerp_cost')->insertAll($cost_data);

                  }

                  if(!empty($accounts_receivable)){

                      Db::name('kuerp_accounts_receivable')->insertAll($accounts_receivable);

                  }

  

                  //序列号跟踪

                  if(!empty($serial_log)){

                      Db::name('kuerp_goods_serial_log')->insertAll($serial_log);

                  }

                  if(!empty($goods_stock_log)){

                      Db::name('kuerp_goods_log')->insertAll($goods_stock_log);

                  }

                  //批次库存数据

                  if(!empty($goods_batch_log)){

                      Db::name('kuerp_goods_batch_log')->insertAll($goods_batch_log);

                  }

                  if(!empty($goods_batch_add)){

                      Db::name('kuerp_goods_batch_stock')->insertAll($goods_batch_add);

                  }

                  if(!empty($goods_batch_edit)){

                      foreach ($goods_batch_edit as $k=>$v){

                          Db::name('kuerp_goods_batch_stock')->where(['id'=>$k])->update(['stock'=>$v]);

                      }

                  }

                  //批次库存数据结束

  

                  if(!empty($purchase_info_id)){

                      Db::name('kuerp_goods_serial')->where(['sale_id'=>['in',$purchase_info_id]])->update(['store_status'=>'2']);

                      Db::name('kuerp_goods_batch_sale_info')->where(['purchase_info_id'=>['in',$purchase_info_id]])->update(['status'=>'1']);

                  }

                  if(!empty($order_count)){

                      $goodsSalesOrder = (new \app\admin\model\kuerp\GoodsSalesOrder());

                      foreach ($order_count as $order_count_k=>$order_count_v){

                          $goodsSalesOrder->where(['id'=>$order_count_v['id']])->inc('store_number',$order_count_v['store_number'])->update();

                          unset($order_count[$order_count_k]['store_number']);

                      }

                      $goodsSalesOrder->saveAll($order_count);

                  }

                  if(!empty($payment_log)){

                      (new \app\admin\model\kuerp\SalesPaymentLog())->saveAll($payment_log);

                  }

                  if(!empty($invoicing)){

                      (new \app\admin\model\kuerp\SalesInvoicing())->saveAll($invoicing);

                  }

                  $result = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0'])->update(['examine_status'=>1,'examine_id'=>0,'examine_time'=>time(),'operation_id'=>0]);

                  if(!empty($log)){

                      Db::name('kuerp_accounts_log')->insertAll($log);

                  }

                  if(!empty($editstock)){

                      (new \app\admin\model\kuerp\GoodsStock())->saveAll($editstock);

                  }

                  if(!empty($goods_stock)){

                      (new \app\admin\model\kuerp\Goods())->saveAll($goods_stock);

                  }

                  if(!empty($arrears)){

                      (new \app\admin\model\kuerp\Customers())->saveAll($arrears);

                  }

                  Db::name('kuerp_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->update(['status'=>'1','times'=>time()]);

                  foreach ($info_list as $info_list_k=>$info_list_v){

                      if(isset($info_list_v['id'])){

                          unset($info_list[$info_list_k]['id']);

                      }

                  }

                  Db::name('kuerp_goods_sales_summary')->insertAll($info_list);

                  if(!empty($accountData)){

                      foreach ($accountData as $accountData_k=>$accountData_v){

                          Db::name('kuerp_accounts')->where(['id'=>$accountData_v['account']])->inc('balance',$accountData_v['balance'])->update();

                      }

                  }

                  Db::commit();

              } catch (Exception $e) {

                  Db::rollback();

                  $this->error($e->getMessage());

              }

              if(isset($result) && $result !== false){

                  if($examine_set!=1){

                      return ['status'=>1,'msg'=>'操作成功'];

                  }

                  $this->success(sizeof($list).'条数据审核成功');

              }else{

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'操作失败'];

                  }

                  $this->error('审核失败');

              }

          }

      }

  

      //获取单位价格信息

      public function getUnit($goods_code='',$store='',$attr='',$supplyer_code=''){

          $set= Db::name('kuerp_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find();

          $taxes= Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked');

  

          if($goods_code){

              $row = Db::name('kuerp_goods')->where(['goods_code'=>$goods_code])->field('batch,serial,id,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life')->find();

              $goods_id = isset($row['id'])?$row['id']:0;

          }else{

              $goods_id = isset($_POST['goods_id'])?$_POST['goods_id']:0;

              $row = Db::name('kuerp_goods')->where(['id'=>$goods_id])->field('batch,serial,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life')->find();

          }

          if(empty($row)){

              return [];

          }

          if($supplyer_code){

              $suppliers = Db::name('kuerp_suppliers')->where(['supplyer_code'=>$supplyer_code])->value('vat_rate');

          }

  

          $auxiliary = Db::name('kuerp_auxiliary')->where(['status'=>1])->column('name','id');

          $goods_attr = Db::name('kuerp_goods_attr')->where(['goods_id'=>$goods_id])->select();

          foreach ($goods_attr as $goods_attr_k=>$goods_attr_v){

              $aux_combination = Db::name('kuerp_aux_combination')->where(['id'=>$goods_attr_v['attribute_id']])->value('attr_ids');

              $goods_attr[$goods_attr_k]['attr_ids'] = [];

              $goods_attr[$goods_attr_k]['attr_name'] = '';

              if($aux_combination){

                  $goods_attr[$goods_attr_k]['attr_id'] = $aux_combination;

                  $aux_combination = explode('&',$aux_combination);

                  $goods_attr[$goods_attr_k]['attr_ids'] = $aux_combination;

                  foreach ($aux_combination as $aux_combination_k=>$aux_combination_v){

                      $aux_combination[$aux_combination_k] = $auxiliary[$aux_combination_v];

                  }

                  $aux_combination = implode('/',$aux_combination);

                  $goods_attr[$goods_attr_k]['attr_name'] = $aux_combination;

              }

          }

  

  

          $where = [];

          $where['goods_id'] = $goods_id;

          if($store){

              $where['stores_id'] = $store;

          }else{

              $where['stores_id'] = $row['manager_warehouse_id'];

          }

          if($attr){

              //$attribute_id = Db::name('kuerp_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->value('attribute_id');

              $where['attribute_id'] = $attr;

          }

          $stock = Db::name('kuerp_goods_stock')->where($where)->value('stock');

          if($row['more_unit']==1){

              $unit = Db::name('kuerp_unit_conversion')->alias('uc')

                  ->join('kuerp_unit u','u.id=uc.vice_unit')

                  ->where(['uc.main_unit'=>$row['unit_id']])

                  ->order('uc.advance_rate','ASC')

                  ->field('uc.vice_unit,u.name,u.id,uc.advance_rate')

                  ->select();

              $unit_ids = [$row['unit_id']];

              foreach ($unit as $unit_k=>$unit_v){

                  $unit_ids[] = $unit_v['id'];

              }

              $goods_price = Db::name('kuerp_goods_price')->where(['unit'=>['in',$unit_ids],'goods_id'=>$goods_id])->select();

              foreach ($goods_price as $goods_price_k=>$goods_price_v){

                  foreach ($unit as $unit_k=>$unit_v){

                      if($goods_price_v['unit']==$row['unit_id']){

                          $goods_price[$goods_price_k]['advance_rate'] = 1;

                      }else if($goods_price_v['unit']==$unit_v['id']){

                          $goods_price[$goods_price_k]['advance_rate'] = $unit_v['advance_rate'];

                      }

                  }

              }

          }else{

              $unit_ids = [$row['unit_id']];

          }

          $unit = Db::name('kuerp_unit')->where(['id'=>['in',$unit_ids]])->field('id,name')->select();

          foreach ($unit as $unit_k=>$unit_v){

              if(isset($goods_price)){

                  foreach ($goods_price as $goods_price_k=>$goods_price_v){

                      if($unit_v['id']==$goods_price_v['unit']){

                          $unit[$unit_k]['is_default'] = $goods_price_v['is_default'];

                          //$unit[$unit_k]['purchase'] = $goods_price_v['purchase']*$goods_price_v['advance_rate'];

                          $unit[$unit_k]['purchase'] = $goods_price_v['purchase'];

                          if($goods_price_v['is_default']!=1){

                              $unit[$unit_k]['basic_nmber'] = $goods_price_v['advance_rate'];

                          }else{

                              $unit[$unit_k]['basic_nmber'] = 1;

                          }

                      }

                  }

              }else{

                  if($row['auxiliary']==1 && !empty($attr)){

                      $purchase = Db::name('kuerp_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->value('purchase');

                  }else{

                      $purchase = $row['purchase'];

                  }

                  if($unit_v['id']==$row['unit_id']){

                      $unit[$unit_k]['is_default'] = 1;

                      $unit[$unit_k]['basic_nmber'] = 1;

                      $unit[$unit_k]['purchase'] = $purchase;

                  }

              }

          }

          foreach ($unit as $unit_k=>$unit_v){

              if(isset($suppliers) && $suppliers!==''){

                  $tax = $suppliers;

              }else{

                  $tax = $set['value'];

              }

              if(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1){

                  $unit[$unit_k]['tax'] = 1;

                  $unit[$unit_k]['tax_rate'] = $tax;

              }else{

                  $unit[$unit_k]['tax'] = 0;

                  $unit[$unit_k]['tax_rate'] = 0;

              }

              if(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1 && $taxes==1){

                  $unit[$unit_k]['purchase'] = round(($unit_v['purchase']/(100+$tax)*100),4);

                  $unit[$unit_k]['tax_included'] = $unit_v['purchase'];

              }elseif(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1 && $taxes!=1){

                  $unit[$unit_k]['purchase'] = $unit_v['purchase'];

                  $unit[$unit_k]['tax_included'] = round(($unit_v['purchase']*(100+$tax)/100),4);

              }else{

                  $unit[$unit_k]['purchase'] = $unit_v['purchase'];

                  $unit[$unit_k]['tax_included'] = $unit_v['purchase'];

              }

          }

          $data = [];

          foreach ($unit_ids as $unit_ids_k=>$unit_ids_v){

              foreach ($unit as $unit_k=>$unit_v){

                  if($unit_ids_v==$unit_v['id']){

                      if($unit_v['is_default']==1){

                          $unit_v['stock'] = is_null($stock)?0:$stock;

                      }else{

                          $unit_v['stock'] = round($stock/$unit_v['basic_nmber'],2);

                      }

                      $unit_v['tax_money'] = round(($unit_v['tax_included']-$unit_v['purchase']),4);;

                      $data[] = $unit_v;

                  }

              }

          }

          return json_encode(['unit'=>$data,'attr'=>$goods_attr,'goods'=>$row]);

      }

  

      /**

       * 销货单详情

       * @ApiMethod (POST)

       */

      public function salesInfo()

      {

          $detail = input();

          $check = $this->checkParameter($detail,[

              'purchase_code|销货单编号'=>'require',

          ]);

          if(isset($check['status'])){

              $this->error($check['msg'],'',$check['status']);

          }else{

              $where = [];

              $third_account = $this->third_account;

              $where['g.third_id'] = $third_account['id'];

              $where['s.purchase_code'] = $detail['purchase_code'];

  

              $list = Db::name('kuerp_goods_sales')->alias('s')

                  ->join('kuerp_goods_salethird g','s.id=g.purchase_id')

                  ->join('kuerp_customers c','s.customers_id=c.id')

                  ->where($where)

                  ->field('s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address,

              s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime')

                  ->find();

              if(empty($list)){

                  $this->error('未查询到相关数据','',27);

              }

  

              $list['purchase_time_text'] = date('Y-m-d',$list['purchase_time']);

              $list['createtime_text'] = date('Y-m-d H:i:s',$list['createtime']);

  

              $purchase_info = Db::name('kuerp_goods_sales_info')->alias('gs')

                  ->join('kuerp_goods g','gs.goods_code=g.goods_code')

                  ->join('kuerp_unit u','u.id=gs.goods_unit')

                  ->where(['gs.purchase_code'=>$list['purchase_code']])

                  ->field('gs.goods_code,gs.goods_note,gs.goods_attr,gs.goods_unit,u.name as unit_name,gs.number,gs.unit_price,gs.tax_included,gs.all_money

                  ,gs.tax_money,gs.money_tax_price')

                  ->select();

              foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){

                  $attribute_id = Db::name('kuerp_goods_attr')->where(['id'=>$purchase_info_v['goods_attr']])->value('attribute_id');

                  $attribute_ids = Db::name('kuerp_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');

                  $attribute_name = '';

                  if($attribute_ids){

                      $attribute_ids = explode('&',$attribute_ids);

                      $attribute = Db::name('kuerp_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');

                      $attribute_name = implode('/',$attribute);

                  }

                  $purchase_info[$purchase_info_k]['attr_name'] = $attribute_name;

              }

              $list['info_list'] = $purchase_info;

              $this->success('查询成功',$list,1);

          }

      }

  

      /**

       * 添加退货单

       * @ApiMethod (POST)

       */

      public function returnSales()

      {

          $detail = input();

          $check = $this->checkParameter($detail,[

              'order_num|外部订单号'=>'require',

              'mobile|客户'=>'require',

              'goods|商品'=>'require',

          ]);

          if(isset($check['status'])){

              $this->error($check['msg'],'',$check['status']);

          }else{

              $serial_status = $this->serial_status;//是否开启序列号

              $batch_status = $this->batch_status;//是否开启批次

              $taxes_status = $this->taxes_status;//是否启用税金

              $price_taxes = $this->price_taxes;//商品价格是否含税

              $taxes_rate = $this->taxes_rate;//税率

  

  

              $where = [];

              $third_account = $this->third_account;

              $where['g.third_id'] = $third_account['id'];

              $where['g.order_num'] = $detail['order_num'];

  

              $list = Db::name('kuerp_goods_sales')->alias('s')

                  ->join('kuerp_goods_salethird g','s.id=g.purchase_id')

                  ->join('kuerp_customers c','s.customers_id=c.id')

                  ->where($where)

                  ->field('s.id,s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address,

              s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime,s.examine_status')

                  ->find();

              if(empty($list)){

                  $this->error('未查询到相关数据','',27);

              }elseif ($list['examine_status']!=1){

                  $this->error('此销货单未审核,不可退货','',27);

              }

  

              $purchase_info = Db::name('kuerp_goods_sales_info')->alias('gs')

                  ->where(['gs.purchase_code'=>$list['purchase_code']])

                  ->select();

  

              $purchase_code = $this->getCodes('',12);

              if(empty($purchase_code)){

                  $this->error('销货退货单单号获取失败','',27);

              }

              $purchase_time = strtotime(date('Y-m-d'));

  

              $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime');

              $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate');

              if($purchase_time<=$checktime){

                  $this->error('不可添加小于结账日期'.$checkdate.'的数据');

              }

  

              //验证客户是否可用

              $third_account = $this->third_account;

              $customer = Db::name('kuerp_customers')

                  ->where(['customer_mobile'=>$detail['mobile'],'third_id'=>$third_account['id']])

                  ->find();

              if(empty($customer)){

                  $this->error('此手机号未注册','',27);

              }elseif ($customer['status']!=1){

                  $this->error('客户状态异常','',27);

              }

  

              $salethird = Db::name('kuerp_goods_exchangethird')

                  ->where(['order_num'=>$detail['order_num'],'third_id' => $third_account['id'],])

                  ->find();

              if($salethird){

                  $this->error('此订单号已创建销货退货单','',27);

              }

  

              $account = Db::name('kuerp_accounts')->where(['is_default'=>'1'])->value('id');

              if(empty($account)){

                  $account = Db::name('kuerp_accounts')->value('id');

                  if(empty($account)){

                      $this->error('未查询到可用账户','',27);

                  }

              }

  

              $data = [];

              //验证商品数据

              $goods_data = html_entity_decode($detail['goods']);

              $goods_data = json_decode($goods_data,true);

              if(empty($goods_data)){

                  $this->error('商品数据解析失败','',27);

              }else{

  

                  $data = [];

                  foreach ($goods_data as $goods_data_k=>$goods_data_v){

                      $check = $this->checkParameter($goods_data_v,[

                          'goods_code|商品编号'=>'require',

                          'goods_unit|商品单位'=>'require',

                          'goods_attr|商品属性'=>'requireNone',

                          'number|数量'=>'require',

                          'price|商品价格'=>'require',

                      ]);

                      if(isset($check['status'])){

                          $this->error($check['msg'],'',$check['status']);

                      }

  

                      $status = 0;

                      foreach ($goods_data as $goods_data_k2=>$goods_data_v2){

                          if($goods_data_v['goods_code']==$goods_data_v2['goods_code'] && $goods_data_v['goods_unit']==$goods_data_v2['goods_unit'] && $goods_data_v['goods_attr']==$goods_data_v2['goods_attr']){

                              $status += 1;

                          }

                      }

                      if($status>1){

                          $this->error('同一笔销货单不可存在相同单位,属性的商品','',27);

                      }

  

                      $goods = Db::name('kuerp_goods')->where(['goods_code'=>$goods_data_v['goods_code']])->find();

                      if($goods['auxiliary']==1 && $this->auxiliary==1 && empty($goods_data_v['goods_attr'])){

                          $this->error($goods['goods_name'].'必须选择商品属性','',27);

                      }

  

                      $status = false;

                      foreach ($purchase_info as $k=>$v){

                          if($v['goods_code']==$goods_data_v['goods_code'] && $v['goods_unit']==$goods_data_v['goods_unit']){

                              if(!empty($v['goods_attr']) && $v['goods_attr']==$goods_data_v['goods_attr']){

                                  $status = true;

                              }elseif (empty($v['goods_attr'])){

                                  $status = true;

                              }

                              if($status){

                                  $goods_batch_info = [];

                                  if($goods['batch']==1 && $batch_status==1){

                                      if(empty($v['batch_id'])){

                                          $this->error('销货单批次信息错误,不可退货','',27);

                                      }

                                      $batch = Db::name('kuerp_goods_batch')->where(['id'=>$v['batch_id']])->find();

                                      if(empty($batch)){

                                          $this->error('批次信息查询失败','',27);

                                      }

                                      $goods_batch_info = [

                                          'batch_id'    => isset($v['batch_id'])?$v['batch_id']:'',

                                          'goods_id'    => $goods['id'],

                                          'goods_code'  => $goods['goods_code'],

                                          'attribute_id'=> isset($goods_data_v['goods_attr'])?$goods_data_v['goods_attr']:0,

                                          'goods_unit'  => (int)$goods_data_v['goods_unit'],

                                          'stores_id'   => $v['stores_id'],

                                          'stock'       => (float)$goods_data_v['number'],

                                          'number'      => (float)$goods_data_v['number'],

                                          'batch_name'  => isset($v['batch_name'])?$v['batch_name']:'',

                                          'manufacture' => $v['manufacture'],

                                          'guarantee'   => $v['guarantee'],

                                          'validity'    => $v['validity'],

                                          'status'      => '0',

                                          'type'        => 2,

                                          'createtime'  => time(),

                                          'updatetime'  => time(),

                                      ];

                                      $v['goods_batch_info'] = $goods_batch_info;

                                  }

  

                                  if($goods['serial']==1 && $serial_status==1){//开启序列号

                                      $serial = unserialize($v['period']);

  

                                      $serial_code = [];

                                      foreach ($serial as $serial_k=>$serial_v){

                                          $serial_code[] = $serial_v['serial'];

                                      }

  

                                      $goods_serials = Db::name('kuerp_goods_serial')->where(['store_status'=>'1','serial'=>['in',$serial_code]])->limit($goods_data_v['number'])->field('serial,remark')->select();

                                      if(sizeof($goods_serials)!=$goods_data_v['number']){

                                          $this->error('商品可用序列号不足,退货失败','',27);

                                      }

  

                                      $period_period = [];$serial_code = [];

                                      foreach ($goods_serials as $serial_k=>$serial_v){

                                          $period_period[] = [

                                              'serial' => $serial_v['serial'],

                                              'remark' => $serial_v['remark'],

                                          ];

                                          $serial_code[] = $serial_v['serial'];

                                      }

                                      $v['period_period'] = serialize($period_period);

                                      $v['serial_code'] = $serial_code;

                                  }

  

                                  //多单位换算基础数量

                                  $advance_rate = 1;

                                  if($goods['more_unit']==1){

                                      $main_unit = $goods['more_unit_id'];

                                      $unit_list = Db::name('kuerp_unit_conversion')

                                          ->where(['main_unit'=>$main_unit])

                                          ->order('advance_rate','ASC')

                                          ->field('vice_unit,advance_rate')

                                          ->select();

                                      $unit_id = [$main_unit];

                                      foreach ($unit_list as $unit_list_k=>$unit_list_v){

                                          $unit_id[] = $unit_list_v['vice_unit'];

                                      }

  

                                      $unit_list_id = Db::name('kuerp_unit_conversion')

                                          ->where(['main_unit'=>$main_unit])

                                          ->column('advance_rate','vice_unit');

                                      $unit_list_id[$goods['unit_id']] = 1;

                                      $advance_rate = isset($unit_list_id[$goods_data_v['goods_unit']])?$unit_list_id[$goods_data_v['goods_unit']]:$advance_rate;

                                  }

  

                                  $number = (float)$goods_data_v['number'];

                                  if($taxes_status==1){//启用税金

                                      $unit_price = $goods_data_v['price'];

                                      $tax_included = $unit_price;//默认接口提交的金额是价税合计,若不是则使用注释代码

                                      $unit_price = $tax_included*100/(100+$taxes_rate);//单价

                                      //if($price_taxes==1){//商品价格是否含税

                                      //$tax_included = $unit_price;

                                      //$unit_price = $tax_included*100/(100+$taxes_rate);//单价

                                      //}else{

                                      //$tax_included = $unit_price*(100+$taxes_rate)/100;

                                      //}

                                  }else{

                                      $unit_price = $goods_data_v['price'];

                                      $tax_included = $goods_data_v['price'];

                                  }

                                  $all_money = $unit_price*$number;

                                  $money_tax_price = $tax_included*$number;

                                  $tax_money = $money_tax_price-$all_money;

  

                                  $data[] = [

                                      'purchase_code'     => $purchase_code,

                                      'purchase_type'     => 2,

                                      'purchase_time'     => $purchase_time,

                                      'customers_id'      => $customer['id'],

                                      'sale_user'   => 0,

                                      'receiving_address' => isset($detail['address'])?$detail['address']:$customer['getgoods_address'],

  

                                      'goods_code'    => $v['goods_code'],

                                      'goods_note'    => $v['remark'],

                                      'goods_unit'    => $v['goods_unit'],

                                      'stores_id'     => $v['stores_id'],

                                      'allow_stock'   => $v['number'],

                                      'number'        => $goods_data_v['number'],

                                      'default_unit'  => $v['default_unit'],

                                      'basic_nmber'   => $goods_data_v['number']*$advance_rate,

                                      'unit_price'    => $unit_price,

                                      'cost_price'    => 0,

                                      'discount'      => 0,

                                      'dis_money'     => 0,

                                      'all_money'     => $all_money,

                                      'remark'        => '',

                                      'goods_attr'    => $goods_data_v['goods_attr'],

                                      'tax_included'  => $tax_included,

                                      'tax_rate'      => $taxes_rate,

                                      'tax_money'     => $tax_money,

  

                                      'money_tax_price'=> $money_tax_price,

                                      'batch_id'      => $v['batch_id'],

                                      'batch_name'    => $v['batch_name'],

                                      'manufacture'   => $v['manufacture'],

                                      'guarantee'     => $v['guarantee'],

                                      'validity'      => $v['validity'],

                                      'preferential_rate' => 0,

                                      'preferential_money' => 0,

                                      'real_money'    => $money_tax_price,

                                      'purchase_money'=> 0,

  

                                      'purchase_info_id'  =>  $v['id'],

                                      'purchase_id'       =>  $list['purchase_code'],

  

                                      'retreat_number'    =>  (float)$goods_data_v['number'],

                                      'retreat_status'    =>  1,

                                      'retreat_money'     =>  $money_tax_price,

  

                                      'account'       => $account,

                                      'createtime'    => time(),

                                      'updatetime'    => time(),

                                      'goods_batch_info'  => $goods_batch_info,

                                      'period'        => isset($v['period_period'])?$v['period_period']:'',

                                      'serial_code'   => isset($v['serial_code'])?$v['serial_code']:[],

                                  ];

                              }

                          }

                      }

                      if(!$status){

                          $this->error('退货单商品数据与购货单商品数据不符,退货单创建失败','',1);

                      }

                  }

              }

  

              $number = 0;$all_money = 0;$tax_money=0;$money_tax_price=0;

              foreach ($data as $goods_data_k=>$goods_data_v){

                  $number += (float)$goods_data_v['number'];

                  $all_money += (float)$goods_data_v['all_money'];

                  $tax_money += (float)$goods_data_v['tax_money'];

                  $money_tax_price += (float)$goods_data_v['money_tax_price'];

              }

  

              $preferential_rate = ((float)$detail['preferential_money'])/$money_tax_price;

              $row = [

                  'purchase_code'     =>  $purchase_code,

                  'purchase_type'     =>  2,

                  'purchase_time'     =>  $purchase_time,

                  'customers_id'      =>  $customer['id'],//客户

                  'sale_user'         =>  0,//销售员

                  'receiving_address' =>  isset($detail['address'])?$detail['address']:$customer['getgoods_address'],

                  'customers_money'   =>  0,

                  'number'            =>  $list['number'],

                  'all_money'         =>  $all_money,

                  'tax_money'         =>  $tax_money,//税

                  'money_tax_price'   =>  $money_tax_price,//价税合计

                  'preferential_rate' =>  $preferential_rate,

                  'preferential_money'=>  (float)$detail['preferential_money'],

                  'real_money'        =>  (float)($money_tax_price-(float)$detail['preferential_money']),

                  'purchase_money'    =>  0,

                  'note'              =>  isset($detail['note'])?$detail['note']:'',

                  'arrears'           =>  0,

                  'all_arrears'       =>  0,

                  'pay_status'        =>  1,

                  'pay_money'         =>  (float)($money_tax_price-(float)$detail['preferential_money']),

                  'purchase_id'       =>  $list['purchase_code'],

                  'retreat_number'    =>  $number,

                  'retreat_status'    =>  1,

                  'retreat_money'     =>  (float)($money_tax_price-(float)$detail['preferential_money']),

  

                  'account'           =>  $account,

                  'cost_data'         =>  null,

                  'third_id'          => $third_account['id'],

                  'createtime'        =>  time(),

                  'updatetime'        =>  time(),

              ];

  

              $result = false;

              Db::startTrans();

              try {

                  if(!empty($row)){

                      $result = Db::name('kuerp_goods_sales_exchange')->insertGetId($row);

                  }

                  if($result){

                      Db::name('kuerp_goods_saleexc')->insert(['purchase_id'=>$list['id'],'exchange_id'=>$result]);

                  }

  

                  Db::name('kuerp_goods_exchangethird')->insert([

                      'order_num' => $detail['order_num'],

                      'purchase_id' => $result,

                      'third_id' => $third_account['id'],

                      'createtime' => time(),

                      'updatetime' => time(),

                  ]);

  

                  Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>$row['purchase_code']])->delete();

                  foreach ($data as $data_k=>$data_v){

                      $goods_serial = $data_v['serial_code'];

                      unset($data_v['serial_code']);

  

                      $goods_batch_info = $data_v['goods_batch_info'];

                      unset($data_v['goods_batch_info']);

  

                      $purchase_id = Db::name('kuerp_goods_sales_exchange_info')->insertGetId($data_v);

  

                      //批次

                      if($goods_batch_info){

                          $goods_batch_info['purchase_info_id'] = $purchase_id;

                          Db::name('kuerp_goods_batch_salee_info')->insert($goods_batch_info);

                      }

  

                      if(!empty($goods_serial)){

                          Db::name('kuerp_goods_serial')->where(['serial'=>['in',$goods_serial]])->update([

                              'status'=>'4',

                              'purchase_exchange_id' => $purchase_id,

                              'purchase_exchange_code' => $data_v['purchase_code']

                          ]);

                      }

  

                      if(!empty($data_v['purchase_info_id'])){

                          Db::name('kuerp_goods_saleexc_info')->insert([

                              'purchase_id'=>$data_v['purchase_info_id'],

                              'exchange_id'=>$purchase_id,

                              'type' => $data_v['purchase_type'],

                              'number'=>$data_v['retreat_number'],

                              'money'=>$data_v['retreat_money'],

                              'all_money'=>$data_v['all_money'],

                              'tax_rate'=>$data_v['tax_rate'],

                              'tax_money'=>$data_v['tax_money'],

                              'money_tax_price'=>$data_v['money_tax_price']

                          ]);

                      }

                  }

  

                  Db::commit();

              } catch (Exception $e) {

                  Db::rollback();

                  $this->error($e->getMessage());

              }

              if ($result !== false) {

                  if($this->examine_status!=1){

                      try {

                          $exa = $this->examiner($result,1);

                      } catch (Exception $e) {

                      }

                  }

                  $this->success('添加成功',['id'=>$result,'purchase_code'=>$row['purchase_code']],1);

              } else {

                  $this->error('添加失败','',27);

              }

          }

      }

  

      /**

       * 退货单详情

       * @ApiMethod (POST)

       */

      public function returnSalesInfo()

      {

          $detail = input();

          $check = $this->checkParameter($detail,[

              'purchase_code|销货退货单编号'=>'require',

          ]);

          if(isset($check['status'])){

              $this->error($check['msg'],'',$check['status']);

          }else{

              $where = [];

              $third_account = $this->third_account;

              $where['g.third_id'] = $third_account['id'];

              $where['s.purchase_code'] = $detail['purchase_code'];

  

              $list = Db::name('kuerp_goods_sales_exchange')->alias('s')

                  ->join('kuerp_goods_exchangethird g','s.id=g.purchase_id')

                  ->join('kuerp_customers c','s.customers_id=c.id')

                  ->where($where)

                  ->field('s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address,

              s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime')

                  ->find();

              if(empty($list)){

                  $this->error('未查询到相关数据','',27);

              }

  

              $list['purchase_time_text'] = date('Y-m-d',$list['purchase_time']);

              $list['createtime_text'] = date('Y-m-d H:i:s',$list['createtime']);

  

              $purchase_info = Db::name('kuerp_goods_sales_exchange_info')->alias('gs')

                  ->join('kuerp_goods g','gs.goods_code=g.goods_code')

                  ->join('kuerp_unit u','u.id=gs.goods_unit')

                  ->where(['gs.purchase_code'=>$list['purchase_code']])

                  ->field('gs.goods_code,gs.goods_note,gs.goods_attr,gs.goods_unit,u.name as unit_name,gs.number,gs.unit_price,gs.tax_included,gs.all_money

                  ,gs.tax_money,gs.money_tax_price')

                  ->select();

              foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){

                  $attribute_id = Db::name('kuerp_goods_attr')->where(['id'=>$purchase_info_v['goods_attr']])->value('attribute_id');

                  $attribute_ids = Db::name('kuerp_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');

                  $attribute_name = '';

                  if($attribute_ids){

                      $attribute_ids = explode('&',$attribute_ids);

                      $attribute = Db::name('kuerp_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');

                      $attribute_name = implode('/',$attribute);

                  }

                  $purchase_info[$purchase_info_k]['attr_name'] = $attribute_name;

              }

              $list['info_list'] = $purchase_info;

              $this->success('查询成功',$list,1);

          }

      }

  

      //审核/反审核

      function examiner($ids = null,$examine=1){

          $examine_set= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked');

          if(empty($ids)){

              if($examine_set!=1){

                  return ['status'=>0,'msg'=>'系统错误'];

              }

              $this->error('请选择要操作的数据');

          }

          if($examine==1){

              $list = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0'])->select();

              $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime');

              $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate');

              foreach ($list as $list_k=>$list_v){

                  if($list_v['purchase_time']<=$checktime){

                      $this->error('不可操作小于结账日期'.$checkdate.'的数据');

                  }

              }

              $set_discount = Db::name('kuerp_sets')->where(['name'=>'discount'])->value('is_checked');

  

              $purchase_code = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0'])->column('purchase_code');

              if(empty($list)){

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'系统错误'];

                  }

                  $this->error('请选择需要审核的数据');

              }

              $account_list_obj = Db::name('kuerp_accounts')->field('id,account_coding,account_name,balance')->select();

              $account_list = [];

              foreach ($account_list_obj as $account_list_obj_k=>$account_list_obj_v){

                  $account_list[$account_list_obj_v['id']] = $account_list_obj_v;

              }

  

              $examine_ids = [];

              $accountData = [];

              $log = [];

              $suppliers = [];

              $payment_log = [];$invoicing = [];

              $editstock = [];$goods_stock = [];$goods_exchange_info = [];$exchange_info = [];

              $goods_stock_log = [];//收发详情

              $serial_log = [];//序列号跟踪

  

              $order_count = [];

              foreach ($list as $list_k=>$list_v){

  

                  $info_list = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>$list_v['purchase_code']])->select();

                  foreach ($info_list as $info_list_k=>$info_list_v){

  

                      if($list_v['purchase_type']==1 && !empty($info_list_v['purchase_order_id'])){

                          $status = false;

                          foreach ($order_count as $order_count_k=>$order_count_v){

                              if($order_count_v['purchase_code']==$info_list_v['purchase_order_id']){

                                  $order_count[$order_count_k]['store_number'] += $info_list_v['retreat_number'];

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $order_count[] = [

                                  'purchase_code'  => $info_list_v['purchase_order_id'],

                                  'store_number'  => $info_list_v['retreat_number'],

                              ];

                          }

                          //修改状态

                          if(!empty($order_count)){

                              foreach ($order_count as $order_count_k=>$order_count_v){

                                  $number = Db::name('kuerp_goods_sales_order')->where(['purchase_code'=>$order_count_v['purchase_code']])->field('id,number,store_number')->find();

                                  if(($number['number']-$number['store_number'])<=$order_count_v['store_number']){

                                      $order_count[$order_count_k]['status'] = 1;

                                  }else{

                                      $order_count[$order_count_k]['status'] = 2;

                                  }

                                  $order_count[$order_count_k]['id'] = $number['id'];

                              }

                          }

                      }

  

                      //序列号跟踪

                      $goods_serial = Db::name('kuerp_goods_serial')->where(['sale_id'=>$info_list_v['id']])->field('goods_id,serial,stores_id,attribute_id')->select();

                      foreach ($goods_serial as $goods_serial_k=>$goods_serial_v){

                          $serial_log[] = [

                              'goods_id'=>$goods_serial_v['goods_id'],

                              'stores_id'=>$goods_serial_v['stores_id'],

                              'attribute_id'=>$goods_serial_v['attribute_id'],

                              'serial'=>$goods_serial_v['serial'],

                              'business_type'=>'5',

                              'store_status'=>'1',

                              'remarks' => $info_list_v['remark'],

                              'purchase_id'=>$info_list_v['id'],

                              'customer_id'=>$info_list_v['customers_id'],

                              'purchase_code'=>$info_list_v['purchase_code'],

                              'purchase_time' => $info_list_v['purchase_time'],

                              'createtime'  => time(),

                              'updatetime'  => time(),

                          ];

                      }

                      //序列号跟踪

  

                      $goods_exchange_info[] = $info_list_v;

                      //商品总库存

                      $goods = Db::name('kuerp_goods')->where(['goods_code'=>$info_list_v['goods_code']])->field('id,attribute_id,inventory,unit_id')->find();

                      //换算单位为基础单位

                      $now_unit = Db::name('kuerp_unit')->where(['id'=>$info_list_v['goods_unit']])->find();

                      if(empty($now_unit)){

                          if($examine_set!=1){

                              return ['status'=>0,'msg'=>'商品单位错误'];

                          }

                          $this->error('商品单位错误');

                      }else if($now_unit['type']==2 && $now_unit['is_main']!=1){

                          $unit_conversion = Db::name('kuerp_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$now_unit['id']])->find();

                          if(empty($unit_conversion)){

                              if($examine_set!=1){

                                  return ['status'=>0,'msg'=>'商品单位信息获取错误错误'];

                              }

                              $this->error('商品单位信息获取错误错误');

                          }else{

                              $info_list_v['retreat_number'] = $info_list_v['retreat_number']*$unit_conversion['advance_rate'];

                              $goods_exchange[] = $info_list_v;

                          }

                      }

                      $status = false;

                      foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){

                          if($goods_stock_v['id']==$goods['id']){

                              $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']+$info_list_v['retreat_number'];

                              $status = true;

                          }

                      }

                      if(!$status){

                          $goods_stock[] = [

                              'id'=>$goods['id'],

                              'inventory'=>$goods['inventory']+$info_list_v['retreat_number'],

                          ];

                      }

  

                      //获取成本单价

                      $balance_cost=0;$balance_unit=0;

                      $goodsInfo = json_decode($this->getUnit($info_list_v['goods_code'],$info_list_v['stores_id'],$info_list_v['goods_attr']),true);

                      $unitData = isset($goodsInfo['unit'])?$goodsInfo['unit']:[];

                      if(empty($unitData)){

                          if($examine_set!=1){

                              return ['status'=>0,'msg'=>'商品信息获取错误'];

                          }

                          $this->error('商品信息获取错误');

                      }else{

                          foreach ($unitData as $unitData_k=>$unitData_v){

                              if($unitData_v['id']==$goods['unit_id']){

                                  $balance_unit = $unitData_v['purchase'];

                              }

                          }

                      }

                      //获取成本单价结束

  

                      //商品库存

                      $where = [];

                      $where['goods_code'] = $info_list_v['goods_code'];

                      $where['stores_id'] = $info_list_v['stores_id'];

                      if($info_list_v['goods_attr']>0){

                          $goods_attr = Db::name('kuerp_goods_attr')->where(['id'=>$info_list_v['goods_attr']])->value('attribute_id');

                          if($goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

                      }

                      $stock = Db::name('kuerp_goods_stock')->where($where)->field('stock,id')->find();

                      if(empty($stock)){

                          if($examine_set!=1){

                              return ['status'=>0,'msg'=>'商品'.$info_list_v['goods_code'].'库存不足'];

                          }

                          $this->error('商品'.$info_list_v['goods_code'].'库存不足');

                      }

                      $status = false;

                      foreach ($editstock as $editstock_k=>$editstock_v){

                          if($editstock_v['id']==$stock['id']){

                              $editstock[$editstock_k]['stock'] += $info_list_v['retreat_number'];

                              $status = true;

                              $balance_cost   = (round($balance_unit*$info_list_v['retreat_number'],4));

                          }

                      }

                      if(!$status){

                          $editstock[] = [

                              'id'=>$stock['id'],

                              'stock'=>(round($stock['stock']+$info_list_v['retreat_number'],2))

                          ];

                          $balance_cost   = (round($balance_unit*$info_list_v['retreat_number'],4));

                      }

  

                      //收发详情

                      if($goods){

                          $goods_stock_log[] = [

                              'purchase_code' => $info_list_v['purchase_code'],

                              'purchase_id'   => $info_list_v['id'],

                              'purchase_time' => $info_list_v['purchase_time'],

                              'business_type' => '3',

                              'customer_id'   => $info_list_v['customers_id'],

                              'goods_id'      => $goods['id'],

                              'goods_code'    => $info_list_v['goods_code'],

                              'attribute_id'  => (float)$info_list_v['goods_attr'],

                              'goods_unit'    => (float)$info_list_v['goods_unit'],

                              'stores_id'     => (float)$info_list_v['stores_id'],

                              'remark'        => $info_list_v['remark'],

                              'out_number'    => (float)$info_list_v['number'],

                              'out_basics'    => (float)$info_list_v['basic_nmber'],

                              'out_unit'      => $balance_unit,

                              'out_cost'      => $balance_cost,

                              'createtime'    => time(),

                              'updatetime'    => time(),

                          ];

                      }

                      //收发详情结束

                  }

  

                  if ($list_v['retreat_money']>0){

                      $status = false;

                      foreach ($accountData as $accountData_k=>$accountData_v){

                          if($accountData_v['id']==$list_v['account']){

                              $accountData[$accountData_k]['balance'] -= $list_v['retreat_money'];

                              $status = true;

                          }

                      }

                      if(!$status){

                          $accountData[] = [

                              'balance'  => $list_v['retreat_money'],

                              'id'  => $list_v['account'],

                          ];

                      }

                  }

                  $need_give_account = $list_v['real_money']+$list_v['customers_money']-$list_v['retreat_money'];

                  if ($need_give_account>0){

                      $status = false;

                      foreach ($suppliers as $suppliers_k=>$suppliers_v){

                          if($suppliers_v['id']==$list_v['customers_id']){

                              $suppliers[$suppliers_k]['need_receive_account'] -= $need_give_account;

                              $status = true;

                          }

                      }

                      if(!$status){

                          $suppliers[] = [

                              'need_receive_account'  => $need_give_account,

                              'id'  => $list_v['customers_id'],

                          ];

                      }

                  }

  

                  $examine_ids[] = $list_v['id'];

                  if($list_v['pay_money']>0){

                      $log[] = [

                          'account_coding'    => $account_list[$list_v['account']]['account_coding'],

                          'account_name'      => $account_list[$list_v['account']]['account_name'],

                          'balance'           => $account_list[$list_v['account']]['balance']-$list_v['pay_money'],

                          'expenditure'       => $list_v['pay_money'],

                          'pay_time'          => $list_v['purchase_time'],

                          'bill_code'         => $list_v['purchase_code'],

                          'business_type'     => 6,

                          'customers_id'      => $list_v['customers_id'],

                          'remark'            => $list_v['note'],

                          'createtime'        => time(),

                          'updatetime'        => time(),

                      ];

                  }

  

                  $payment_log[] = [

                      'customers_id'   => $list_v['customers_id'],

                      'sale_user'      => $list_v['sale_user'],

                      'business_type'  => 2,

                      'bill_time'      => $list_v['purchase_time'],

                      'bill_code'      => $list_v['purchase_code'],

                      'bill_codes'     => $list_v['purchase_code'],

                      'purchase_money' => -$list_v['all_money'],

                      'discount_money' => $list_v['preferential_money'],

                      'real_money'     => -$list_v['real_money'],

                      'money_tax_price'=> -$list_v['money_tax_price'],

                      'pay_money'      => -$list_v['retreat_money'],

                      'give_money'     => -$list_v['arrears'],

                      'reamrk'         => $list_v['note'],

                      'createtime'     => time(),

                  ];

                  if($set_discount==1){

                      $money = $list_v['real_money'];

                  }else{

                      $money = $list_v['money_tax_price'];

                  }

                  $invoicing[] = [

                      'customers_id'   => $list_v['customers_id'],

                      'status'         => $list_v['retreat_status']+3,

                      'bill_time'      => $list_v['purchase_time'],

                      'bill_code'      => $list_v['purchase_code'],

                      'bill_codes'     => $list_v['purchase_code'],

                      'money'          => $list_v['real_money'],

                      'tax'            => $list_v['tax_money'],

                      'real_money'     => $money,

                      'pay_money'      => 0,

                      'give_money'     => $money,

                      'createtime'     => time(),

                  ];

              }

  

              $info_list_sum = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>['in',$purchase_code]])->select();

              foreach ($info_list_sum as $list_k=>$list_v){

                  $info_list_sum[$list_k]['returns_order_id'] = $list_v['id'];

                  unset($info_list_sum[$list_k]['id']);

                  $info_list_sum[$list_k]['createtime']        = time();

                  $info_list_sum[$list_k]['updatetime']        = time();

              }

              $info_list_id = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>['in',$purchase_code]])->column('id');

  

              //批次库存数据

              //获取设置

              $set= \think\Db::name('kuerp_sets')->field('type,name,value,is_checked,set')->select();

              $setList = [];

              foreach ($set as $set_k=>$set_v){

                  if(!empty($set_v['set'])){

                      $set_v['set'] = unserialize($set_v['set']);

                  }

                  $setList[$set_v['name']] = $set_v;

              }

              //数量价格格式化

              $number_decimal = $setList['number_decimal']['value'];

              $goods_serial = Db::name('kuerp_goods_sales_exchange_info')->where(['id'=>['in',$info_list_id]])->select();

              $goods_batch_add = [];$goods_batch_edit = [];$goods_batch_log = [];

              foreach ($goods_serial as $k=>$v){

                  $goods = Db::name('kuerp_goods')->where(['goods_code'=>$v['goods_code']])->field('id,goods_name,attribute_id,inventory,unit_id,batch')->find();

                  if($goods['batch']==1){

                      //先通过单位计算需修改多单位商品的库存数量

                      if($goods['unit_id']==$v['goods_unit']){

                          $stock_number = $v['number'];

                      }else{

                          $unit_conversion = Db::name('kuerp_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$v['goods_unit']])->value('advance_rate');

                          if(empty($unit_conversion)){

                              if($examine_set!=1){

                                  return ['status'=>0,'msg'=>'商品单位信息查询失败'];

                              }

                              $this->error('商品单位信息查询失败');

                          }else{

                              $stock_number = round(($v['number']*$unit_conversion),$number_decimal);

                          }

                      }

  

                      $where = [];

                      $where['goods_id'] = $goods['id'];

                      if($v['goods_attr']){

                          $where['attribute_id'] = $v['goods_attr'];

                      }

                      $where['stores_id'] = $v['stores_id'];

                      $where['batch_id'] = $v['batch_id'];

                      $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find();

                      if(empty($goods_batch_stock)){

                          $status = false;

                          foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){

                              if($goods_batch_add_v['batch_id']==$v['batch_id'] && $goods_batch_add_v['goods_code']==$v['goods_code'] && $goods_batch_add_v['attribute_id']==$v['goods_attr'] && $goods_batch_add_v['stores_id']==$v['stores_id']){

                                  $goods_batch_add[$goods_batch_add_k]['stock'] += $stock_number;

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $goods_batch_add[] = [

                                  'batch_id' => $v['batch_id'],

                                  'goods_id' => $goods['id'],

                                  'goods_code' => $v['goods_code'],

                                  'attribute_id' => $v['goods_attr'],

                                  'stores_id' => $v['stores_id'],

                                  'stock' => $stock_number,

                                  'createtime' => time(),

                                  'updatetime' => time(),

                              ];

                          }

                      }else{

                          if(isset($goods_batch_edit[$goods_batch_stock['id']])){

                              $goods_batch_edit[$goods_batch_stock['id']] += $stock_number;

                          }else{

                              $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']+$stock_number;

                          }

                      }

                      foreach ($goods_batch_edit as $k2=>$v2){

                          if($v2<0){

                              if($examine_set!=1){

                                  return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足'];

                              }

                              $this->error('商品'.$goods['goods_name'].'库存不足');

                          }

                      }

  

                      //商品批次跟踪

                      $goods_batch_log[] = [

                          'goods_id'      => $goods['id'],

                          'goods_unit'    => $v['goods_unit'],

                          'stores_id'     => $v['stores_id'],

                          'batch_id'      => (float)$v['batch_id'],

                          'number'        => (float)$v['number'],

                          'purchase_code' => $v['purchase_code'],

                          'purchase_time' => $v['purchase_time'],

                          'business_type' => '5',

                          'remarks'       => $v['remark'],

                          'order_id'      => $v['id'],

                          'unit_price'    => $v['unit_price'],

                          'tax_included'  => $v['tax_included'],

                          'all_money'     => $v['all_money'],

                          'tax_money'     => $v['tax_money'],

                          'money_tax_price' => $v['money_tax_price'],

                          'customer_id'   => $v['customers_id'],

                          'createtime'    => time(),

                          'updatetime'    => time(),

                      ];

                      //商品批次跟踪结束

                  }

              }

              //批次库存数据结束

  

              //应收账款明细表

              $ex_list = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0','arrears'=>['>','0']])->select();

              $accounts_receivable = [];

              foreach ($ex_list as $ex_list_k=>$ex_list_v){

                  $accounts_receivable[] = [

                      'customers_id'      => $ex_list_v['customers_id'],

                      'purchase_time'     => $ex_list_v['purchase_time'],

                      'purchase_code'     => $ex_list_v['purchase_code'],

                      'business_type'     => '7',

                      'receivable'        => -$ex_list_v['arrears'],

                      'remark'            => $ex_list_v['note'],

                      'sale_money'        => $ex_list_v['money_tax_price'],

                      'receivable_money'  => round(($ex_list_v['real_money']+$ex_list_v['customers_money']),4),

                      'amount'            => $ex_list_v['preferential_money'],

                      'cost'              => $ex_list_v['customers_money'],

                      'real_money'        => $ex_list_v['pay_money'],

                      'createtime'        => time(),

                      'updatetime'        => time(),

                  ];

              }

  

              //采购销售费用

              $cost_data = [];

              $now_suppliers = Db::name('kuerp_suppliers')->column('id','supplyer_code');

              foreach ($list as $list_k=>$list_v){

                  if(!empty($list_v['cost_data'])){

                      $cost_datas = unserialize($list_v['cost_data']);

                      foreach ($cost_datas as $cost_datas_k=>$cost_datas_v){

                          $list_v['supplyer_code'] = isset($list_v['supplyer_code'])?$list_v['supplyer_code']:'';

                          $cost_data[] = [

                              'purchase_time'     => time(),

                              'purchase_code'     => (new Goods())->getCodes('',27),

                              'business_type'     => '4',

                              'suppliers_ids'     => isset($now_suppliers[$list_v['supplyer_code']])?$now_suppliers[$list_v['supplyer_code']]:null,

                              'suppliers_id'      => $cost_datas_v['suppliers_id'],

                              'expend_type_id'    => $cost_datas_v['expend_type_id'],

                              'purchase_number'   => $list_v['purchase_code'],

                              'purchase_times'    => $list_v['purchase_time'],

                              'money'             => $cost_datas_v['money'],

                              'pay_money'         => $cost_datas_v['money'],

                              'remark'            => $cost_datas_v['remark'],

                              'preparation_id'    => 0,

                              'operation_id'      => 0,

                              'createtime'        => time(),

                              'updatetime'        => time(),

                          ];

                      }

                  }

              }

  

              //销售利润表

              $profit = [];

              foreach ($list as $list_k=>$list_v){

                  $amount = null;

                  $amounts = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>$list_v['purchase_code']])->sum('dis_money');

                  if($amounts>0){

                      $amount = $amounts;

                  }

                  $profit[] = [

                      'purchase_type' => '2',

                      'purchase_code' => $list_v['purchase_code'],

                      'purchase_time' => $list_v['purchase_time'],

                      'customers_id'  => $list_v['customers_id'],

                      'sale_user'     => $list_v['sale_user'],

                      'preparation_id'=> $list_v['preparation_id'],

                      'examine_id'    => 0,

                      'number'        => -$list_v['number'],

                      'amount'        => -$amount,

                      'all_money'     => -$list_v['all_money'],

                      'tax_money'     => -$list_v['tax_money'],

                      'money_tax_price'=> -$list_v['money_tax_price'],

                      'preferential_money'=> -$list_v['preferential_money'],

                      'purchase_money'=> -$list_v['purchase_money'],

                      'real_money'    => -$list_v['real_money'],

                      'need_money'    => -$list_v['real_money']-$list_v['customers_money'],

                      'pay_money'     => -$list_v['pay_money'],

                      'remark'        => $list_v['note'],

                  ];

              }

  

              //核销单源单

              $writeoff_source = [];

              foreach ($list as $list_k=>$list_v){

                  if($list_v['arrears']>0){

                      $writeoff_source[] = [

                          'purchase_type' => 3,

                          'type' => 2,

                          'customers_id' => $list_v['customers_id'],

                          'purchase_code' => $list_v['purchase_code'],

                          'purchase_time' => $list_v['purchase_time'],

                          'price' => -$list_v['real_money'],

                          'money' => -$list_v['pay_money'],

                          'write_off' => -$list_v['arrears'],

                          'remark' => $list_v['note'],

                      ];

                  }

              }

              Db::startTrans();

              try {

                  if(!empty($writeoff_source)){

                      Db::name('kuerp_writeoff_source')->insertAll($writeoff_source);

                  }

  

                  if(!empty($profit)){

                      Db::name('kuerp_goods_sales_profit')->insertAll($profit);

                  }

  

                  if(!empty($cost_data)){

                      Db::name('kuerp_cost')->insertAll($cost_data);

                  }

  

                  if(!empty($accounts_receivable)){

                      Db::name('kuerp_accounts_receivable')->insertAll($accounts_receivable);

                  }

  

                  //序列号跟踪

                  if(!empty($serial_log)){

                      Db::name('kuerp_goods_serial_log')->insertAll($serial_log);

                  }

                  if(!empty($goods_stock_log)){

                      Db::name('kuerp_goods_log')->insertAll($goods_stock_log);

                  }

                  //批次库存数据

                  if(!empty($goods_batch_log)){

                      Db::name('kuerp_goods_batch_log')->insertAll($goods_batch_log);

                  }

                  if(!empty($goods_batch_add)){

                      Db::name('kuerp_goods_batch_stock')->insertAll($goods_batch_add);

                  }

                  if(!empty($goods_batch_edit)){

                      foreach ($goods_batch_edit as $k=>$v){

                          Db::name('kuerp_goods_batch_stock')->where(['id'=>$k])->update(['stock'=>$v]);

                      }

                  }

                  //批次库存数据结束

  

                  if(!empty($info_list_id)){

                      Db::name('kuerp_goods_batch_salee_info')->where(['purchase_info_id'=>['in',$info_list_id]])->update(['status'=>'1']);

                      Db::name('kuerp_goods_serial')->where(['sale_exchange_id'=>['in',$info_list_id]])->update(['store_status'=>'1']);

                      Db::name('kuerp_goods_saleexc_info')->where(['exchange_id'=>['in',$info_list_id]])->update(['status'=>'1','times'=>time()]);

                  }

  

                  //销售记录

                  $info_list = $goods_exchange_info;

                  foreach ($info_list as $info_list_k=>$info_list_v){

                      $info_list[$info_list_k]['number'] = $info_list_v['retreat_number'];

                      unset($info_list[$info_list_k]['id']);

                      unset($info_list[$info_list_k]['purchase_id']);

                      unset($info_list[$info_list_k]['purchase_info_id']);

                      unset($info_list[$info_list_k]['retreat_status']);

                      unset($info_list[$info_list_k]['retreat_number']);

                      unset($info_list[$info_list_k]['retreat_money']);

                      $info_list[$info_list_k]['createtime'] = time();

                      $info_list[$info_list_k]['updatetime'] = time();

                  }

                  foreach ($info_list as $info_list_k=>$info_list_v){

                      $info_list[$info_list_k]['purchase_type'] = 2;

                  }

                  Db::name('kuerp_goods_sales_info')->insertAll($info_list);

  

                  if(!empty($info_list_sum)){

                      foreach ($info_list_sum as $info_list_k=>$info_list_v){

                          unset($info_list_sum[$info_list_k]['purchase_id']);

                          unset($info_list_sum[$info_list_k]['purchase_info_id']);

                          unset($info_list_sum[$info_list_k]['retreat_status']);

                          unset($info_list_sum[$info_list_k]['retreat_number']);

                          unset($info_list_sum[$info_list_k]['retreat_money']);

                          $info_list_sum[$info_list_k]['purchase_type'] = 2;

                          $info_list_sum[$info_list_k]['number'] = -$info_list_v['retreat_number'];

                          $info_list_sum[$info_list_k]['basic_nmber'] = -$info_list_v['basic_nmber'];

                          $info_list_sum[$info_list_k]['all_money'] = -$info_list_v['all_money'];

                          $info_list_sum[$info_list_k]['tax_money'] = -$info_list_v['tax_money'];

                          $info_list_sum[$info_list_k]['money_tax_price'] = -$info_list_v['money_tax_price'];

                          $info_list_sum[$info_list_k]['real_money'] = -$info_list_v['real_money'];

                          $info_list_sum[$info_list_k]['pay_money'] = -$info_list_v['pay_money'];

                      }

                      Db::name('kuerp_goods_sales_summary')->insertAll($info_list_sum);

                  }

  

                  //销售记录

                  foreach ($exchange_info as $exchange_info_k=>$exchange_info_v){

                      $info_ids = Db::name('kuerp_goods_saleexc_info')->where(['exchange_id'=>$exchange_info_v['id']])->value('purchase_id');

                      if($info_ids){

                          $where = [];

                          $where['id'] = $info_ids;

                          $purchase_summary = Db::name('kuerp_goods_sales_summary')->where($where)->find();

                          if($purchase_summary){

                              Db::name('kuerp_goods_sales_summary')->where($where)->update([

                                  'number' => ($purchase_summary['number']-$exchange_info_v['retreat_number']),

                                  'basic_nmber' => ($purchase_summary['basic_nmber']-$exchange_info_v['retreat_number']),

                                  'all_money' => ($purchase_summary['all_money']-$exchange_info_v['all_money']),

                                  'money_tax_price' => ($purchase_summary['money_tax_price']-$exchange_info_v['money_tax_price']),

                                  'tax_money' => ($purchase_summary['tax_money']-$exchange_info_v['tax_money']),

                                  'real_money' => ($purchase_summary['real_money']-$exchange_info_v['real_money']),

                              ]);

                          }

                      }

                  }

                  Db::name('kuerp_goods_excpur_info')->where(['exchange_id'=>['in',$info_list_id]])->update(['status'=>'1','times'=>time()]);

                  if(!empty($editstock)){

                      (new \app\admin\model\kuerp\GoodsStock())->saveAll($editstock);

                  }

                  if(!empty($goods_stock)){

                      (new \app\admin\model\kuerp\Goods())->saveAll($goods_stock);

                  }

  

                  $result = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0'])->update(['examine_status'=>1,'examine_id'=>0,'examine_time'=>time(),'operation_id'=>0]);

                  if(!empty($log)){

                      Db::name('kuerp_accounts_log')->insertAll($log);

                  }

                  if(!empty($accountData)){

                      foreach ($accountData as $accountData_k=>$accountData_v){

                          Db::name('kuerp_accounts')->where(['id'=>$accountData_v['id']])->dec('balance',$accountData_v['balance'])->update();

                      }

                  }

  

                  if(!empty($suppliers)){

                      foreach ($suppliers as $suppliers_k=>$suppliers_v){

                          Db::name('kuerp_customers')->where(['id'=>$suppliers_v['id']])->dec('need_receive_account',$suppliers_v['need_receive_account'])->update();

                      }

                  }

                  if(!empty($payment_log)){

                      (new \app\admin\model\kuerp\SalesPaymentLog())->saveAll($payment_log);

                  }

                  if(!empty($invoicing)){

                      (new \app\admin\model\kuerp\SalesInvoicing())->saveAll($invoicing);

                  }

                  Db::commit();

              } catch (Exception $e) {

                  Db::rollback();

                  $this->error($e->getMessage());

              }

              if(isset($result) && $result !== false){

                  if($examine_set!=1){

                      return ['status'=>1,'msg'=>'操作成功'];

                  }

                  $this->success(sizeof($list).'条数据审核成功');

              }else{

                  if($examine_set!=1){

                      return ['status'=>0,'msg'=>'操作失败'];

                  }

                  $this->error('审核失败');

              }

          }

      }

  }