Blame view

application/admin/controller/kuerp/Split.php 120 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
  <?php

  

  namespace app\admin\controller\kuerp;

  

  use app\common\controller\Backend;

  use think\Db;

  use Exception;

  use think\Session;

  /**

   * 拆卸单

   *

   * @icon fa fa-circle-o

   */

  class Split extends Backend

  {

      protected $noNeedRight = ['savetemp'];

      /**

       * Split模型对象

       * @var \app\admin\model\kuerp\Split

       */

      protected $model = null;

  

      public function _initialize()

      {

          parent::_initialize();

          $setData = (new Goods())->getSetData();

          $this->assign('set', $setData['setList']);

          $this->assignconfig('set',$setData['setList']);

          $this->assign('nowmenu', $setData['nowmenu']);

  

          $this->model = new \app\admin\model\kuerp\Split;

          $this->view->assign("businessTypeList", $this->model->getBusinessTypeList());

          $this->view->assign("examineStatusList", $this->model->getExamineStatusList());

  

          $this->view->assign("storesList", $this->model->getStoresList());

          $this->assignconfig("storesList", $this->model->getStoresList());

  

          $this->view->assign("goodsList", $this->model->getGoodsList());

          $this->assignconfig("goodsList", $this->model->getGoodsList());

      }

  

      public function import()

      {

          parent::import();

      }

  

      /**

       * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法

       * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑

       * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改

       */

  

  

      /**

       * 查看

       */

      public function index()

      {

          //当前是否为关联查询

          $this->relationSearch = true;

          //设置过滤方法

          $this->request->filter(['strip_tags', 'trim']);

          if ($this->request->isAjax()) {

              //如果发送的来源是Selectpage,则转发到Selectpage

              if ($this->request->request('keyField')) {

                  return $this->selectpage();

              }

              list($where, $sort, $order, $offset, $limit) = $this->buildparams();

  

              $list = $this->model

                  ->with(['admin'])

                  ->where($where)

                  ->order($sort, $order)

                  ->paginate($limit);

  

              foreach ($list as $row) {

  

                  $row->getRelation('admin')->visible(['username']);

              }

              $lists = [];

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

                  if(isset($list_v['print_num']) && empty($list_v['print_num'])){

                      $list_v['print_num'] = 0;

                  }

                  $p_info = Db::name('kuerp_split_info')->alias('a')

                      ->join('kuerp_goods g','g.id=a.goods_id')

                      ->where((new Goods())->setWhere(['a.purchase_code'=>$list_v['purchase_code'],'a.type'=>'1'],'a'))

                      ->field('a.number,g.goods_name,a.goods_attr,a.unit_cost,a.cost_money,a.goods_unit')->find();

                  $p_info['attr_name'] = '';

                  if(!empty($p_info['goods_attr'])){

                      $attribute_id = Db::name('kuerp_goods_attr')->where((new Goods())->setWhere(['id'=>$p_info['goods_attr']]))->value('attribute_id');

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

                      if($attribute_ids){

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

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

                          $p_info['attr_name'] = implode('/',$attribute);

                      }

                  }

                  $p_info['unit_name'] = Db::name('kuerp_unit')->where((new Goods())->setWhere(['id'=>$p_info['goods_unit']]))->value('name');

  

                  $preparation = Db::name('admin')->where((new Goods())->setWhere(['id'=>$list_v['preparation_id']]))->value('username');

                  if($list_v['examine_id']){

                      $examine = Db::name('admin')->where((new Goods())->setWhere(['id'=>$list_v['examine_id']]))->value('username');

                      $examine_time = date('Y-m-d',$list_v['examine_time']);

                  }else{

                      $examine = '';

                      $examine_time = '';

                  }

  

  

                  $info = Db::name('kuerp_split_info')->alias('a')

                      ->join('kuerp_goods g','g.id=a.goods_id')

                      ->where((new Goods())->setWhere(['a.purchase_code'=>$list_v['purchase_code'],'a.type'=>'2'],'a'))

                      ->field('a.number,g.goods_name,a.goods_attr,a.unit_cost,a.cost_money,a.goods_unit')

                      ->select();

                  foreach ($info as $info_k=>$info_v){

                      $row = [];

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

                      $row['preparation'] = $preparation;

                      $row['examine'] = $examine;

                      $row['examine_time'] = $examine_time;

                      $row['remark'] = $list_v['remark'];

                      $row['print_num'] = $list_v['print_num'];

  

                      $row['purchase_code'] = $list_v['purchase_code'];

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

                      $row['pnumber'] = $p_info['number'];

                      $row['pname'] = $p_info['goods_name'].' '.$p_info['attr_name'];

                      $row['unit_name'] = $p_info['unit_name'];

                      $row['money'] = $list_v['money'];

                      $row['punit_cost'] = $p_info['unit_cost'];

                      $row['pcost_money'] = $p_info['cost_money'];

  

                      $info_v['attr_name'] = '';

                      if(!empty($info_v['goods_attr'])){

                          $attribute_id = Db::name('kuerp_goods_attr')->where((new Goods())->setWhere(['id'=>$info_v['goods_attr']]))->value('attribute_id');

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

                          if($attribute_ids){

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

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

                              $info_v['attr_name'] = implode('/',$attribute);

                          }

                      }

                      $info_v['unit_name'] = Db::name('kuerp_unit')->where((new Goods())->setWhere(['id'=>$info_v['goods_unit']]))->value('name');

  

                      $row['znumber'] = $info_v['number'];

                      $row['zname'] = $info_v['goods_name'].' '.$info_v['attr_name'];

                      $row['zunit_cost'] = $info_v['unit_cost'];

                      $row['zcost_money'] = $info_v['cost_money'];

                      $row['zunit_name'] = $info_v['unit_name'];

  

                      $lists[] = $row;

                  }

              }

              $result = array("total" => $list->total(), "rows" => $lists);

  

              return json($result);

          }

          return $this->view->fetch();

      }

  

      public function add($ids = null)

      {

          if ($this->request->isPost()) {

              $params = $this->request->post("row/a");

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

              if ($params) {

                  (new \app\admin\controller\kuerp\Checkout())->checkOutTime($params['purchase_time']);

                  $params = $this->preExcludeFields($params);

                  if(empty($params['purchase_code'])){

                      $this->error('请填写单据编号');

                  }

                  $admin = Session::get('admin');

                  $serial_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'serial']))->value('is_checked');//序列号设置

                  $batch_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'batch']))->value('is_checked');//批次设置

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

                  $data = [];

                  $goods_serial_all = [];//全部序列号,重复判断使用

                  $p = 2;$z = 2;

                  foreach ($params['goods_code'] as $k=>$v){

                      if(!empty($v)){//有产品编号

                          if(!isset($params['goods_unit'][$k]) || empty($params['goods_unit'][$k])){

                              $this->error('请选择单位');

                          }

                          if(!isset($params['stores_id'][$k]) || empty($params['stores_id'][$k])){

                              $this->error('请选择仓库');

                          }

  

                          $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$v]))->find();

                          if ($goods['auxiliary']==1 && empty($params['goods_attr'][$k]) && $auxiliary_status==1){

                              return ['status'=>500,'msg'=>'请选择商品'.$v.'属性'];

                          }

                          $goods_unit = $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k];

                          $advance_rate = 1;//换算进率

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

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

                                  //所选单位不等于商品默认单位,需查询进率换算基础数量

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

                                      ->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$goods_unit]))

                                      ->field('id,advance_rate')

                                      ->find();

                                  if(empty($more_unit)){

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

                                  }else{

                                      $advance_rate = $more_unit['advance_rate'];

                                  }

                              }

                          }

                          $basic_number = round(($advance_rate*$params['number'][$k]),4);

  

                          if($k>0){

                              //子件

                              if($v==$params['goods_code'][0]){

                                  $this->error('子件不能和组合件为同一商品');

                              }

                              //批次

                              $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();//已有批次

                              $goods_batch = [];$goods_batch_info = [];

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

                                  if(empty($params['batch'][$k])){

                                      if($batch_data){

                                          $this->error('请选择生产批次');

                                      }else{

                                          $this->error('请输入生产批次');

                                      }

                                  }

                                  if(empty($params['manufacture'][$k])){

                                      $this->error('请输入生产日期');

                                  }

                                  if($params['guarantee'][$k]===''){

                                      $this->error('请输入保质期');

                                  }

                                  if ($params['guarantee'][$k]<0){

                                      $this->error('保质期天数不可小于0');

                                  }

                                  if(empty($params['validity'][$k])){

                                      $this->error('请输入过期时间');

                                  }

                                  if(empty($batch_data)){//新增生产批次

                                      $goods_batch = (new Goods())->setDataInfo([

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

                                          'goods_code'  => $params['goods_code'][$k],

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

                                          'stores_id'   => $params['stores_id'][$k],

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

                                          'manufacture' => strtotime($params['manufacture'][$k]),

                                          'guarantee'   => $params['guarantee'][$k],

                                          'validity'    => strtotime($params['validity'][$k]),

                                          'createtime'  => time(),

                                          'updatetime'  => time(),

                                      ]);

                                  }

                                  $goods_batch_info = (new Goods())->setDataInfo([

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

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

                                      'goods_code'  => $params['goods_code'][$k],

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

                                      'goods_unit'  => (int)$params['goods_unit'][$k],

                                      'stores_id'   => $params['stores_id'][$k],

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

                                      'number'      => (float)$params['number'][$k],

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

                                      'manufacture' => strtotime($params['manufacture'][$k]),

                                      'guarantee'   => $params['guarantee'][$k],

                                      'validity'    => strtotime($params['validity'][$k]),

                                      'status'      => '0',

                                      'type'        => 2,

                                      'createtime'  => time(),

                                      'updatetime'  => time(),

                                  ]);

                              }

                              //序列号

                              $goods_serial='';

                              if(isset($params['period_period'][$k]) && $goods['serial']==1 && $serial_set==1){

                                  if(empty($params['period_period'][$k])){

                                      $this->error('请输入商品序列号');

                                  }

                                  $goods_serial = $params['period_period'][$k];

                                  $period_period = unserialize($params['period_period'][$k]);

                                  $serial_code = [];

                                  foreach ($period_period as $period_period_k=>$period_period_v){

                                      if(in_array($period_period_v['serial'],$goods_serial_all)){

                                          $this->error('商品序列号'.$period_period_v['serial'].'重复');

                                      }else{

                                          $goods_serial_all[] = $period_period_v['serial'];

                                      }

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

                                  }

                                  $serial = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>['in',$serial_code]]))->column('serial');

                                  if($serial){

                                      $this->error('商品序列号'.$serial[0].'已占用');

                                  }

                                  $basic_number = (float)$params['number'][$k];

                              }

  

                              $data[] = (new Goods())->setDataInfo([

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

                                  'purchase_time'     =>  strtotime($params['purchase_time']),

                                  'business_type'     =>  '1',

                                  'type'              =>  '2',

  

                                  'goods_id'          =>  $goods['id'],//商品信息

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

                                  'goods_attr'        =>  $params['goods_attr'][$k]===''?null:(float)$params['goods_attr'][$k],

                                  'goods_unit'        =>  $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k],

  

                                  'stores_id'         =>  $params['stores_id'][$k]===''?null:(float)$params['stores_id'][$k],

                                  'allow_stock'       =>  $params['allow_stock'][$k]===''?null:(float)$params['allow_stock'][$k],

                                  'number'            =>  $params['number'][$k]===''?null:(float)$params['number'][$k],

                                  'period'            =>  $goods_serial,

                                  'basic_number'      =>  $basic_number,

                                  'unit_cost'         =>  $params['unit_cost'][$k]===''?null:(float)$params['unit_cost'][$k],

                                  'cost_money'        =>  $params['cost_money'][$k]===''?null:(float)$params['cost_money'][$k],

                                  'remark'            =>  $params['remark'][$k]===''?null:$params['remark'][$k],

  

                                  'batch'             =>  $params['batch'][$k]===''?null:$params['batch'][$k],

                                  'batch_id'          =>  $params['batch_id'][$k]===''?null:(float)$params['batch_id'][$k],

                                  'manufacture'       =>  $params['manufacture'][$k]===''?null:strtotime($params['manufacture'][$k]),

                                  'guarantee'         =>  $params['guarantee'][$k]===''?null:(float)$params['guarantee'][$k],

                                  'validity'          =>  $params['validity'][$k]===''?null:strtotime($params['validity'][$k]),

  

                                  'preparation_id'    =>  $admin['id'],

                                  'operation_id'      =>  $admin['id'],

  

                                  'createtime'        => time(),

                                  'updatetime'        => time(),

                                  'goods_batch'       => $goods_batch,

                                  'goods_batch_info'  => $goods_batch_info

                              ]);

                              $p = 1;

                          }else{

                              //组合件

                              //批次

                              $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();//已有批次

                              $goods_batch = [];$goods_batch_info = [];

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

                                  if(empty($params['batch'][$k])){

                                      $this->error('请选择生产批次');

                                  }

                                  if(empty($params['manufacture'][$k])){

                                      $this->error('请输入生产日期');

                                  }

                                  if($params['guarantee'][$k]===''){

                                      $this->error('请输入保质期');

                                  }

                                  if ($params['guarantee'][$k]<0){

                                      $this->error('保质期天数不可小于0');

                                  }

                                  if(empty($params['validity'][$k])){

                                      $this->error('请输入过期时间');

                                  }

                                  if(empty($batch_data)){

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

                                  }

                                  $goods_batch_info = (new Goods())->setDataInfo([

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

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

                                      'goods_code'  => $params['goods_code'][$k],

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

                                      'goods_unit'  => (int)$params['goods_unit'][$k],

                                      'stores_id'   => $params['stores_id'][$k],

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

                                      'number'      => (float)$params['number'][$k],

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

                                      'manufacture' => strtotime($params['manufacture'][$k]),

                                      'guarantee'   => $params['guarantee'][$k],

                                      'validity'    => strtotime($params['validity'][$k]),

                                      'status'      => '0',

                                      'type'        => 2,

                                      'createtime'  => time(),

                                      'updatetime'  => time(),

                                  ]);

                              }

                              //序列号

                              $goods_serial='';

                              if(isset($params['period_period'][$k]) && $goods['serial']==1 && $serial_set==1){

                                  if(empty($params['period_period'][$k])){

                                      $this->error('请输入商品序列号');

                                  }

                                  $goods_serial = $params['period_period'][$k];

                                  $period_period = unserialize($params['period_period'][$k]);

                                  $serial_code = [];

                                  foreach ($period_period as $period_period_k=>$period_period_v){

                                      if(in_array($period_period_v['serial'],$goods_serial_all)){

                                          $this->error('商品序列号'.$period_period_v['serial'].'重复');

                                      }else{

                                          $goods_serial_all[] = $period_period_v['serial'];

                                      }

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

                                  }

                                  $serial = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>['in',$serial_code],'store_status'=>'1']))->column('serial');

                                  if(sizeof($serial)!=sizeof($serial_code)){

                                      foreach ($serial_code as $serial_code_k=>$serial_code_v){

                                          if(in_array($serial_code_v,$serial)){

                                              $this->error('商品序列号'.$serial_code_v.'不在库,不可出库');

                                          }

                                      }

                                      $this->error('商品序列号不在库,不可出库');

                                  }

                                  $basic_number = (float)$params['number'][$k];

                              }

  

                              $data[] = (new Goods())->setDataInfo([

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

                                  'purchase_time'     =>  strtotime($params['purchase_time']),

                                  'business_type'     =>  '1',

                                  'type'              =>  '1',

  

                                  'goods_id'          =>  $goods['id'],//商品信息

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

                                  'goods_attr'        =>  $params['goods_attr'][$k]===''?null:(float)$params['goods_attr'][$k],

                                  'goods_unit'        =>  $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k],

  

                                  'stores_id'         =>  $params['stores_id'][$k]===''?null:(float)$params['stores_id'][$k],

                                  'allow_stock'       =>  $params['allow_stock'][$k]===''?null:(float)$params['allow_stock'][$k],

                                  'number'            =>  $params['number'][$k]===''?null:(float)$params['number'][$k],

                                  'period'            =>  $goods_serial,

                                  'basic_number'      =>  $basic_number,

                                  'unit_cost'         =>  $params['unit_cost'][$k]===''?null:(float)$params['unit_cost'][$k],

                                  'cost_money'        =>  $params['cost_money'][$k]===''?null:(float)$params['cost_money'][$k],

                                  'remark'            =>  $params['remark'][$k]===''?null:$params['remark'][$k],

  

                                  'batch'             =>  $params['batch'][$k]===''?null:$params['batch'][$k],

                                  'batch_id'          =>  $params['batch_id'][$k]===''?null:(float)$params['batch_id'][$k],

                                  'manufacture'       =>  $params['manufacture'][$k]===''?null:strtotime($params['manufacture'][$k]),

                                  'guarantee'         =>  $params['guarantee'][$k]===''?null:(float)$params['guarantee'][$k],

                                  'validity'          =>  $params['validity'][$k]===''?null:strtotime($params['validity'][$k]),

  

                                  'preparation_id'    =>  $admin['id'],

                                  'operation_id'      =>  $admin['id'],

  

                                  'createtime'        => time(),

                                  'updatetime'        => time(),

                                  'goods_batch'       => $goods_batch,

                                  'goods_batch_info'  => $goods_batch_info

                              ]);

                              $z = 1;

                          }

                      }

                  }

                  if($z==2){

                      $this->error('组合件信息不能为空');

                  }

                  if($p==2){

                      $this->error('组合子件信息不能为空');

                  }

                  if(!empty($data)){

                      $row = (new Goods())->setDataInfo([

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

                          'purchase_time'     =>  strtotime($params['purchase_time']),

                          'business_type'     =>  '1',

                          'money'             =>  $params['costs']===''?null:(float)$params['costs'],

                          'remark'            =>  $params['note']===''?null:$params['note'],

                          'preparation_id'    =>  $admin['id'],

                          'operation_id'      =>  $admin['id'],

                          'createtime'        => time(),

                          'updatetime'        => time(),

                      ]);

                  }else{

                      $this->error('组件信息不能为空');

                  }

  

                  if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code'])]))->find()){

                      $this->error('单据编号已占用');

                  }

                  $result = false;

                  Db::startTrans();

                  try {

                      if(!empty($row)){

                          $result = $this->model->insertGetId($row);

                      }

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

                          $goods_batch = $data_v['goods_batch'];

                          unset($data_v['goods_batch']);

                          $goods_batch_info = $data_v['goods_batch_info'];

                          unset($data_v['goods_batch_info']);

                          $goods_batch_id = 0;

                          if(!empty($goods_batch)){

                              $goods_batch_id = Db::name('kuerp_goods_batch')->insertGetId($goods_batch);

                          }

                          if($goods_batch_id>0){

                              $goods_batch_info['batch_id'] = $goods_batch_id;

                              $goods_batch_info['batch_name'] = $goods_batch['batch_name'];

  

                              $data_v['batch_id'] = $goods_batch_id;

                              $data_v['batch'] = $goods_batch['batch_name'];

                          }

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

                          //批次

                          if($goods_batch_info){

                              $goods_batch_info['split_id'] = $purchase_id;

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

                          }

                      }

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

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

                  }

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

                      if($examine!=1){

                          try {

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

                          } catch (Exception $e) {

                          }

                          if(!isset($exa) || $exa['status']!=1){

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

                              try {

                                  $this->del($result);

                              } catch (Exception $e) {

                              }

                              $this->error($exa['msg']);

                          }

                      }

                      $this->success();

                  } else {

                      $this->error(__('No rows were inserted'));

                  }

              }

              $this->error(__('Parameter %s can not be empty', ''));

          }

          $code = (new Goods())->getCodes('',20);

          $this->view->assign("code", $code);

          return $this->view->fetch();

      }

  

      public function edit($ids = null)

      {

          $row = $this->model->where((new Goods())->setWhere(['id'=>$ids]))->find();

          if (!$row) {

              $this->error(__('No Results were found'));

          }

          $adminIds = $this->getDataLimitAdminIds();

          if (is_array($adminIds)) {

              if (!in_array($row[$this->dataLimitField], $adminIds)) {

                  $this->error(__('You have no permission'));

              }

          }

          if ($this->request->isPost()) {

              $params = $this->request->post("row/a");

              $old_purchase_code = $params['old_purchase_code'];unset($params['old_purchase_code']);

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

              if ($params) {

                  (new \app\admin\controller\kuerp\Checkout())->checkOutTime($row['purchase_time']);

                  $params = $this->preExcludeFields($params);

                  if(empty($params['purchase_code'])){

                      $this->error('请填写单据编号');

                  }

                  $admin = Session::get('admin');

                  $serial_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'serial']))->value('is_checked');//序列号设置

                  $batch_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'batch']))->value('is_checked');//批次设置

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

                  $data = [];

                  $goods_serial_all = [];//全部序列号,重复判断使用

                  $p = 2;$z = 2;

                  foreach ($params['goods_code'] as $k=>$v){

                      if(!empty($v)){//有产品编号

                          if(!isset($params['goods_unit'][$k]) || empty($params['goods_unit'][$k])){

                              $this->error('请选择单位');

                          }

                          if(!isset($params['stores_id'][$k]) || empty($params['stores_id'][$k])){

                              $this->error('请选择仓库');

                          }

  

                          $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$v]))->find();

                          if ($goods['auxiliary']==1 && empty($params['goods_attr'][$k]) && $auxiliary_status==1){

                              return ['status'=>500,'msg'=>'请选择商品'.$v.'属性'];

                          }

                          $goods_unit = $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k];

                          $advance_rate = 1;//换算进率

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

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

                                  //所选单位不等于商品默认单位,需查询进率换算基础数量

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

                                      ->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$goods_unit]))

                                      ->field('id,advance_rate')

                                      ->find();

                                  if(empty($more_unit)){

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

                                  }else{

                                      $advance_rate = $more_unit['advance_rate'];

                                  }

                              }

                          }

                          $basic_number = round(($advance_rate*$params['number'][$k]),4);

  

                          if($k>0){

                              //子件

                              if($v==$params['goods_code'][0]){

                                  $this->error('子件不能和组合件为同一商品');

                              }

                              //批次

                              $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();//已有批次

                              $goods_batch = [];$goods_batch_info = [];

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

                                  if(empty($params['batch'][$k])){

                                      if($batch_data){

                                          $this->error('请选择生产批次');

                                      }else{

                                          $this->error('请输入生产批次');

                                      }

                                  }

                                  if(empty($params['manufacture'][$k])){

                                      $this->error('请输入生产日期');

                                  }

                                  if($params['guarantee'][$k]===''){

                                      $this->error('请输入保质期');

                                  }

                                  if ($params['guarantee'][$k]<0){

                                      $this->error('保质期天数不可小于0');

                                  }

                                  if(empty($params['validity'][$k])){

                                      $this->error('请输入过期时间');

                                  }

                                  if(empty($batch_data)){//新增生产批次

                                      $goods_batch = (new Goods())->setDataInfo([

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

                                          'goods_code'  => $params['goods_code'][$k],

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

                                          'stores_id'   => $params['stores_id'][$k],

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

                                          'manufacture' => strtotime($params['manufacture'][$k]),

                                          'guarantee'   => $params['guarantee'][$k],

                                          'validity'    => strtotime($params['validity'][$k]),

                                          'createtime'  => time(),

                                          'updatetime'  => time(),

                                      ]);

                                  }

                                  $goods_batch_info = (new Goods())->setDataInfo([

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

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

                                      'goods_code'  => $params['goods_code'][$k],

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

                                      'goods_unit'  => (int)$params['goods_unit'][$k],

                                      'stores_id'   => $params['stores_id'][$k],

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

                                      'number'      => (float)$params['number'][$k],

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

                                      'manufacture' => strtotime($params['manufacture'][$k]),

                                      'guarantee'   => $params['guarantee'][$k],

                                      'validity'    => strtotime($params['validity'][$k]),

                                      'status'      => '0',

                                      'type'        => '2',

                                      'createtime'  => time(),

                                      'updatetime'  => time(),

                                  ]);

                              }

                              //序列号

                              $goods_serial='';

                              if(isset($params['period_period'][$k]) && $goods['serial']==1 && $serial_set==1){

                                  if(empty($params['period_period'][$k])){

                                      $this->error('请输入商品序列号');

                                  }

                                  $goods_serial = $params['period_period'][$k];

                                  $period_period = unserialize($params['period_period'][$k]);

                                  $serial_code = [];

                                  foreach ($period_period as $period_period_k=>$period_period_v){

                                      if(in_array($period_period_v['serial'],$goods_serial_all)){

                                          $this->error('商品序列号'.$period_period_v['serial'].'重复');

                                      }else{

                                          $goods_serial_all[] = $period_period_v['serial'];

                                      }

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

                                  }

                                  $serial = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>['in',$serial_code]]))->column('serial');

                                  if($serial){

                                      $this->error('商品序列号'.$serial[0].'已占用');

                                  }

                                  $basic_number = (float)$params['number'][$k];

                              }

  

                              $data[] = (new Goods())->setDataInfo([

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

                                  'purchase_time'     =>  strtotime($params['purchase_time']),

                                  'business_type'     =>  '1',

                                  'type'              =>  '2',

  

                                  'goods_id'          =>  $goods['id'],//商品信息

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

                                  'goods_attr'        =>  $params['goods_attr'][$k]===''?null:(float)$params['goods_attr'][$k],

                                  'goods_unit'        =>  $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k],

  

                                  'stores_id'         =>  $params['stores_id'][$k]===''?null:(float)$params['stores_id'][$k],

                                  'allow_stock'       =>  $params['allow_stock'][$k]===''?null:(float)$params['allow_stock'][$k],

                                  'number'            =>  $params['number'][$k]===''?null:(float)$params['number'][$k],

                                  'period'            =>  $goods_serial,

                                  'basic_number'      =>  $basic_number,

                                  'unit_cost'         =>  $params['unit_cost'][$k]===''?null:(float)$params['unit_cost'][$k],

                                  'cost_money'        =>  $params['cost_money'][$k]===''?null:(float)$params['cost_money'][$k],

                                  'remark'            =>  $params['remark'][$k]===''?null:$params['remark'][$k],

  

                                  'batch'             =>  $params['batch'][$k]===''?null:$params['batch'][$k],

                                  'batch_id'          =>  $params['batch_id'][$k]===''?null:(float)$params['batch_id'][$k],

                                  'manufacture'       =>  $params['manufacture'][$k]===''?null:strtotime($params['manufacture'][$k]),

                                  'guarantee'         =>  $params['guarantee'][$k]===''?null:(float)$params['guarantee'][$k],

                                  'validity'          =>  $params['validity'][$k]===''?null:strtotime($params['validity'][$k]),

  

                                  'preparation_id'    =>  $admin['id'],

                                  'operation_id'      =>  $admin['id'],

  

                                  'createtime'        => time(),

                                  'updatetime'        => time(),

                                  'goods_batch'       => $goods_batch,

                                  'goods_batch_info'  => $goods_batch_info

                              ]);

                              $p = 1;

                          }else{

                              //组合件

                              //批次

                              $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();//已有批次

                              $goods_batch = [];$goods_batch_info = [];

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

                                  if(empty($params['batch'][$k])){

                                      $this->error('请选择生产批次');

                                  }

                                  if(empty($params['manufacture'][$k])){

                                      $this->error('请输入生产日期');

                                  }

                                  if($params['guarantee'][$k]===''){

                                      $this->error('请输入保质期');

                                  }

                                  if ($params['guarantee'][$k]<0){

                                      $this->error('保质期天数不可小于0');

                                  }

                                  if(empty($params['validity'][$k])){

                                      $this->error('请输入过期时间');

                                  }

                                  if(empty($batch_data)){

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

                                  }

                                  $goods_batch_info = (new Goods())->setDataInfo([

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

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

                                      'goods_code'  => $params['goods_code'][$k],

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

                                      'goods_unit'  => (int)$params['goods_unit'][$k],

                                      'stores_id'   => $params['stores_id'][$k],

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

                                      'number'      => (float)$params['number'][$k],

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

                                      'manufacture' => strtotime($params['manufacture'][$k]),

                                      'guarantee'   => $params['guarantee'][$k],

                                      'validity'    => strtotime($params['validity'][$k]),

                                      'status'      => '0',

                                      'type'        => '2',

                                      'createtime'  => time(),

                                      'updatetime'  => time(),

                                  ]);

                              }

                              //序列号

                              $goods_serial='';

                              if(isset($params['period_period'][$k]) && $goods['serial']==1 && $serial_set==1){

                                  if(empty($params['period_period'][$k])){

                                      $this->error('请输入商品序列号');

                                  }

                                  $goods_serial = $params['period_period'][$k];

                                  $period_period = unserialize($params['period_period'][$k]);

                                  $serial_code = [];

                                  foreach ($period_period as $period_period_k=>$period_period_v){

                                      if(in_array($period_period_v['serial'],$goods_serial_all)){

                                          $this->error('商品序列号'.$period_period_v['serial'].'重复');

                                      }else{

                                          $goods_serial_all[] = $period_period_v['serial'];

                                      }

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

                                  }

                                  $serial = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>['in',$serial_code],'store_status'=>'1']))->column('serial');

                                  if(sizeof($serial)!=sizeof($serial_code)){

                                      foreach ($serial_code as $serial_code_k=>$serial_code_v){

                                          if(in_array($serial_code_v,$serial)){

                                              $this->error('商品序列号'.$serial_code_v.'不在库,不可出库');

                                          }

                                      }

                                      $this->error('商品序列号不在库,不可出库');

                                  }

                                  $basic_number = (float)$params['number'][$k];

                              }

  

                              $data[] = (new Goods())->setDataInfo([

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

                                  'purchase_time'     =>  strtotime($params['purchase_time']),

                                  'business_type'     =>  '1',

                                  'type'              =>  '1',

  

                                  'goods_id'          =>  $goods['id'],//商品信息

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

                                  'goods_attr'        =>  $params['goods_attr'][$k]===''?null:(float)$params['goods_attr'][$k],

                                  'goods_unit'        =>  $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k],

  

                                  'stores_id'         =>  $params['stores_id'][$k]===''?null:(float)$params['stores_id'][$k],

                                  'allow_stock'       =>  $params['allow_stock'][$k]===''?null:(float)$params['allow_stock'][$k],

                                  'number'            =>  $params['number'][$k]===''?null:(float)$params['number'][$k],

                                  'period'            =>  $goods_serial,

                                  'basic_number'      =>  $basic_number,

                                  'unit_cost'         =>  $params['unit_cost'][$k]===''?null:(float)$params['unit_cost'][$k],

                                  'cost_money'        =>  $params['cost_money'][$k]===''?null:(float)$params['cost_money'][$k],

                                  'remark'            =>  $params['remark'][$k]===''?null:$params['remark'][$k],

  

                                  'batch'             =>  $params['batch'][$k]===''?null:$params['batch'][$k],

                                  'batch_id'          =>  $params['batch_id'][$k]===''?null:(float)$params['batch_id'][$k],

                                  'manufacture'       =>  $params['manufacture'][$k]===''?null:strtotime($params['manufacture'][$k]),

                                  'guarantee'         =>  $params['guarantee'][$k]===''?null:(float)$params['guarantee'][$k],

                                  'validity'          =>  $params['validity'][$k]===''?null:strtotime($params['validity'][$k]),

  

                                  'preparation_id'    =>  $admin['id'],

                                  'operation_id'      =>  $admin['id'],

  

                                  'createtime'        => time(),

                                  'updatetime'        => time(),

                                  'goods_batch'       => $goods_batch,

                                  'goods_batch_info'  => $goods_batch_info

                              ]);

                              $z = 1;

                          }

                      }

                  }

                  if($z==2){

                      $this->error('组合件信息不能为空');

                  }

                  if($p==2){

                      $this->error('组合子件信息不能为空');

                  }

                  if(!empty($data)){

                      $rowdata = (new Goods())->setDataInfo([

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

                          'purchase_time'     =>  strtotime($params['purchase_time']),

                          'business_type'     =>  '1',

                          'money'             =>  $params['costs']===''?null:(float)$params['costs'],

                          'remark'            =>  $params['note']===''?null:$params['note'],

                          'preparation_id'    =>  $admin['id'],

                          'operation_id'      =>  $admin['id'],

                          'updatetime'        => time(),

                      ]);

                  }else{

                      $this->error('组件信息不能为空');

                  }

                  if($this->model->where((new Goods())->setWhere(['purchase_code'=>trim($params['purchase_code']),'id'=>['<>',$ids]]))->find()){

                      $this->error('单据编号已占用');

                  }

                  $goods_purchase_info = Db::name('kuerp_split_info')->where(['purchase_code'=>$old_purchase_code])->column('id');

                  $result = false;

                  Db::startTrans();

                  try {

                      if($examine!=1 && $row['examine_status']==1){

                          $this->examine($ids,2);

                      }

                      if(!empty($row)){

                          $result = $this->model->where((new Goods())->setWhere(['id'=>$ids]))->update($rowdata);

                      }

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

                      Db::name('kuerp_goods_batch_info')->where(['split_id'=>['in',$goods_purchase_info]])->delete();

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

                          $goods_batch = $data_v['goods_batch'];

                          unset($data_v['goods_batch']);

                          $goods_batch_info = $data_v['goods_batch_info'];

                          unset($data_v['goods_batch_info']);

                          $goods_batch_id = 0;

                          if(!empty($goods_batch)){

                              $goods_batch_id = Db::name('kuerp_goods_batch')->insertGetId($goods_batch);

                          }

                          if($goods_batch_id>0){

                              $goods_batch_info['batch_id'] = $goods_batch_id;

                              $goods_batch_info['batch_name'] = $goods_batch['batch_name'];

  

                              $data_v['batch_id'] = $goods_batch_id;

                              $data_v['batch'] = $goods_batch['batch_name'];

                          }

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

  

                          //批次

                          if($goods_batch_info){

                              $goods_batch_info['split_id'] = $purchase_id;

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

                          }

                      }

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

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

                  }

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

                      if($examine!=1){

                          try {

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

                          } catch (Exception $e) {

                          }

                          if(!isset($exa) || $exa['status']!=1){

                              try {

                                  $this->del($result);

                              } catch (Exception $e) {

                              }

                              $this->error('添加失败,请刷新页面重试');

                          }

                      }

                      $this->success();

                  } else {

                      $this->error(__('No rows were inserted'));

                  }

              }

              $this->error(__('Parameter %s can not be empty', ''));

          }

          $this->view->assign("row", $row);

  

          $info = Db::name('kuerp_split_info')->alias('a')

              ->join('kuerp_goods g','g.id=a.goods_id')

              ->where((new Goods())->setWhere(['a.purchase_code'=>$row['purchase_code']],'a'))

              ->field('a.*,g.goods_name')

              ->select();

          foreach ($info as $info_k=>$info_v){

              $info[$info_k]['unit'] = json_decode((new GoodsPurchase())->getUnit($info_v['goods_code'],'',''),true);

          }

          $this->view->assign("info_list", $info);

  

          //审核禁止输入

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

          if($examine_set==1 && $row['examine_status']==1){

              $this->assign('exclass','hiddens');

          }

          $this->assignconfig("examine_status", $row['examine_status']);

  

          return $this->view->fetch();

      }

      /**

       * 删除

       */

      public function del($ids = "")

      {

          if (!$this->request->isPost()) {

              $this->error(__("Invalid parameters"));

          }

          $ids = $ids ? $ids : $this->request->post("ids");

          if ($ids) {

              $pk = $this->model->getPk();

              $adminIds = $this->getDataLimitAdminIds();

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

              if (is_array($adminIds)) {

                  $this->model->where($this->dataLimitField, 'in', $adminIds);

              }

              $list = $this->model->where($pk, 'in', $ids)->select();

              $goods_purchase_id = [];$goods_purchase_code = [];

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

                  if($list_v['examine_status']==1){

                      (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);

                  }

                  if($list_v['examine_status']==1 && $examine==1){

                      $this->error('不能删除已审核的数据');

                  }elseif ($list_v['examine_status']==1 && $examine!=1){

                      try {

                          $this->examine($list_v['id'],2);

                      } catch (Exception $e) {

                      }

                  }

                  if(!in_array($list_v['id'],$goods_purchase_id)){

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

                  }

                  if(!in_array($list_v['purchase_code'],$goods_purchase_code)){

                      $goods_purchase_code[] = $list_v['purchase_code'];

                  }

              }

              $goods_purchase_info = Db::name('kuerp_split_info')->where(['purchase_code'=>['in',$goods_purchase_code]])->column('id');

  

              $count = 0;

              Db::startTrans();

              try {

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

                      $count += $v->delete();

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

                  }

                  Db::name('kuerp_goods_batch_info')->where(['split_id'=>['in',$goods_purchase_info]])->delete();

                  Db::commit();

              }catch (Exception $e) {

                  Db::rollback();

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

              }

              if ($count) {

                  $this->success();

              } else {

                  $this->error(__('No rows were deleted'));

              }

          }

          $this->error(__('Parameter %s can not be empty', 'ids'));

      }

      //审核/反审核

      public 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('请选择要操作的数据');

          }

  

          $serial_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'serial']))->value('is_checked');//序列号设置

          $batch_set= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'batch']))->value('is_checked');//批次设置

  

          if($examine==1){

              $list = $this->model->where(['id'=>['in',$ids],'examine_status'=>'0'])->select();

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

                  (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);

              }

              $purchase_code = $this->model->where(['id'=>['in',$ids],'examine_status'=>'0'])->column('purchase_code');

              if(empty($list)){

                  if($examine_set!=1){

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

                  }

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

              }

  

              $editstock = [];//库存表修改库存

              $addstock = [];//库存表添加库存

              $goods_stock = [];//商品列表库存

              $goods_stock_log = [];//商品收发明细

              $goods_batch_add = [];//批次库存新增

              $goods_batch_edit = [];//批次库存更新

              $goods_batch_log = [];//批次记录

              $per_code = [];//序列号

              $goods_serial = [];//序列号添加

              $edit_serial = [];//序列号更新

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

              $info_list = Db::name('kuerp_split_info')->where((new Goods())->setWhere(['purchase_code'=>['in',$purchase_code]]))->select();

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

                  $goods = Db::name('kuerp_goods')->where(['id'=>$info_list_v['goods_id']])->find();

                  $stock_number = $info_list_v['basic_number'];//基础数量

                  if($info_list_v['type']==2){//子件

                      //商品表库存

                      $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'] = $info_list_v['goods_code'];

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

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

                          $goods_attr = $info_list_v['goods_attr'];

                          if($goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

                      }

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

                      if($stock){

                          $status = false;

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

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

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

                                  $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']+$info_list_v['cost_money'],4));

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $editstock[] = [

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

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

                                  'cost_money'=>(round($stock['cost_money']+$info_list_v['cost_money'],4))

                              ];

                          }

                      }else{

                          $addstock[] = (new Goods())->setDataInfo([

                              'goods_id'  => isset($goods['id'])?$goods['id']:'',

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

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

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

                              'stock'  => $stock_number,

                              'cost_money'=>(round($info_list_v['cost_money'],4)),

                              'createtime'  => time(),

                              'updatetime'  => time(),

                          ]);

                      }

                      //商品库存表库存结束

  

                      //收发详情

                      $goods_stock_log[] = (new Goods())->setDataInfo([

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

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

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

                          'business_type' => '14',

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

                          'goods_code'    => $goods['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'],

                          'enter_number'  => (float)$info_list_v['number'],

                          'enter_basics'  => (float)$stock_number,

                          'enter_unit'    => round($info_list_v['unit_cost'],4),

                          'enter_cost'    => (float)$info_list_v['cost_money'],

                          'out_number'  => null,

                          'out_basics'  => null,

                          'out_unit'    => null,

                          'out_cost'    => null,

                          'createtime'    => time(),

                          'updatetime'    => time(),

                      ]);

                      //收发详情结束

  

                      //批次跟踪

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

                          $where = [];

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

                          if(isset($goods_attr) && $goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

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

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

                          $where = (new Goods())->setWhere($where);

                          $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']==$info_list_v['batch_id'] && $goods_batch_add_v['goods_id']==$info_list_v['goods_id'] && $goods_batch_add_v['attribute_id']==$goods_attr && $goods_batch_add_v['stores_id']==$info_list_v['stores_id']){

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

                                      $status = true;

                                  }

                              }

                              if(!$status){

                                  $batch_id = $info_list_v['batch_id'];

                                  if(empty($batch_id)){

                                      $batch_id = Db::name('kuerp_goods_batch')->insertGetId((new Goods())->setDataInfo([

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

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

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

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

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

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

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

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

                                          'createtime' => time(),

                                          'updatetime' => time(),

                                      ]));

                                  }

                                  $edit_batch[] = ['id'=>$info_list_v['id'],'batch_id'=>$batch_id];

                                  $goods_batch_add[] = (new Goods())->setDataInfo([

                                      'batch_id' => $batch_id,

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

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

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

                                      'stores_id' => $info_list_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;

                              }

                          }

                          //商品批次跟踪

                          $goods_batch_log[] = (new Goods())->setDataInfo([

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

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

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

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

                              'number'        => (float)$info_list_v['basic_number'],

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

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

                              'business_type' => 13,

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

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

                              'unit_price'    => $info_list_v['unit_cost'],

                              'tax_included'  => $info_list_v['unit_cost'],

                              'all_money'     => $info_list_v['cost_money'],

                              'tax_money'     => 0,

                              'money_tax_price' => $info_list_v['cost_money'],

                              'createtime'    => time(),

                              'updatetime'    => time(),

                          ]);

                          //商品批次跟踪结束

                      }

  

                      //序列号跟踪

                      if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){

                          $period = unserialize($info_list_v['period']);

                          foreach ($period as $period_k=>$period_v){

  

                              $per_s = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>$period_v['serial']]))->field('serial,status,store_status')->find();//序列号

                              if($per_s){//仓库有记录

                                  $edit_serial[] = ['serial'=>$per_s['serial'],'split_id'=>$info_list_v['id'],'type'=>13,'store_status'=>1,'old_status'=>$per_s['status'],'old_store'=>$per_s['store_status']];

                              }else{

                                  if(!in_array($period_v['serial'],$per_code)){

                                      $per_code[] = $period_v['serial'];

                                  }

                                  $goods_serial[] = (new Goods())->setDataInfo([

                                      'split_id' => $info_list_v['id'],

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

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

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

                                      'attribute_id'=>isset($goods_attr)?$goods_attr:null,

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

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

                                      'status'=> '13',

                                      'store_status'=>'1',

                                      'split_status'=>'1',

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

                                      'createtime'  => time(),

                                      'updatetime'  => time(),

                                  ]);

                              }

                              $serial_log[] = (new Goods())->setDataInfo([

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

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

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

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

                                  'business_type'=>'13',

                                  'store_status'=>'1',

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

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

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

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

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

                                  'createtime'  => time(),

                                  'updatetime'  => time(),

                              ]);

                          }

                      }

                      //序列号跟踪

                  }else{

                      //组合件

                      //商品表库存

                      $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,

                          ];

                      }

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

                          if($goods_stock_v['inventory']<0){

                              if($examine_set!=1){

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

                              }

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

                          }

                      }

                      //商品表库存结束

  

                      //商品库存表库存

                      $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 = $info_list_v['goods_attr'];

                          if($goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

                      }

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

                      if($stock){

                          $status = false;

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

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

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

                                  $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']-$info_list_v['cost_money'],4));

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $editstock[] = [

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

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

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

                              ];

                          }

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

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

                                  if($examine_set!=1){

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

                                  }

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

                              }

                          }

                      }else{

                          if($examine_set!=1){

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

                          }

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

                      }

                      //商品库存表库存结束

  

                      //收发详情

                      $goods_stock_log[] = (new Goods())->setDataInfo([

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

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

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

                          'business_type' => '15',

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

                          'goods_code'    => $goods['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'],

                          'enter_number'  => null,

                          'enter_basics'  => null,

                          'enter_unit'    => null,

                          'enter_cost'    => null,

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

                          'out_basics'  => (float)$stock_number,

                          'out_unit'    => round($info_list_v['unit_cost'],4),

                          'out_cost'    => (float)$info_list_v['cost_money'],

                          'createtime'    => time(),

                          'updatetime'    => time(),

                      ]);

                      //收发详情结束

  

                      //批次跟踪

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

                          $where = [];

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

                          if($goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

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

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

                          $where = (new Goods())->setWhere($where);

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

                          if(empty($goods_batch_stock)){

                              if($examine_set!=1){

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

                              }

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

                          }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 $goods_batch_edit_k=>$goods_batch_edit_v){

                                  if($goods_batch_edit_v<0){

                                      if($examine_set!=1){

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

                                      }

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

                                  }

                              }

                          }

                          //商品批次跟踪

                          $goods_batch_log[] = (new Goods())->setDataInfo([

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

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

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

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

                              'number'        => (float)$info_list_v['basic_number'],

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

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

                              'business_type' => '14',

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

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

                              'unit_price'    => $info_list_v['unit_cost'],

                              'tax_included'  => $info_list_v['unit_cost'],

                              'all_money'     => $info_list_v['cost_money'],

                              'tax_money'     => '0',

                              'money_tax_price' => $info_list_v['cost_money'],

                              'createtime'    => time(),

                              'updatetime'    => time(),

                          ]);

                          //商品批次跟踪结束

                      }

  

                      //序列号跟踪

                      if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){

                          $period = unserialize($info_list_v['period']);

                          foreach ($period as $period_k=>$period_v){

  

                              $per_s = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>$period_v['serial']]))->field('serial,status,store_status')->find();//序列号

                              if($per_s){//仓库有记录

                                  $edit_serial[] = ['serial'=>$per_s['serial'],'split_id'=>$info_list_v['id'],'type'=>14,'store_status'=>'2','old_status'=>$per_s['status'],'old_store'=>$per_s['store_status']];

                              }else{

                                  if($examine_set!=1){

                                      return ['status'=>0,'msg'=>$goods['goods_name'].'序列号'.$period_v['serial'].'不存在'];

                                  }

                                  $this->error($goods['goods_name'].'序列号'.$period_v['serial'].'不存在');

                              }

                              $serial_log[] = (new Goods())->setDataInfo([

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

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

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

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

                                  'business_type'=>'14',

                                  'store_status'=>'2',

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

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

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

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

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

                                  'createtime'  => time(),

                                  'updatetime'  => time(),

                              ]);

                          }

                      }

                      //序列号跟踪

                  }

              }

              $admin = Session::get('admin');

              Db::startTrans();

              try {

                  //库存表修改库存

                  if(!empty($editstock)){

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

                  }

                  //库存表添加库存

                  if(!empty($addstock)){

                      foreach ($addstock as $addstock_k=>$addstock_v){

                          $where = [];

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

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

                          if($addstock_v['attribute_id']>0){

                              $where['attribute_id'] = $addstock_v['attribute_id'];

                          }

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

                          if($stock){

                              Db::name('kuerp_goods_stock')->where(['id'=>$stock['id']])->inc('stock',$addstock_v['stock'])->update();

                          }else{

                              Db::name('kuerp_goods_stock')->insert($addstock_v);

                          }

                      }

                  }

                  //商品列表库存

                  if(!empty($goods_stock)){

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

                  }

                  //商品收发明细

                  if(!empty($goods_stock_log)){

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

                  }

                  //批次库存新增

                  if(!empty($goods_batch_add)){

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

                          $where = [];

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

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

                          if($goods_batch_add_v['attribute_id']>0){

                              $where['attribute_id'] = $goods_batch_add_v['attribute_id'];

                          }

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

                          if($stock){

                              Db::name('kuerp_goods_batch_stock')->where(['id'=>$stock['id']])->inc('stock',$goods_batch_add_v['stock'])->update();

                          }else{

                              Db::name('kuerp_goods_batch_stock')->insert($goods_batch_add_v);

                          }

                      }

                  }

                  //批次库存更新

                  if(!empty($goods_batch_edit)){

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

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

                      }

                  }

                  //批次记录

                  if(!empty($goods_batch_log)){

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

                  }

                  //序列号添加

                  if(!empty($goods_serial)){

                      Db::name('kuerp_goods_serial')->insertAll($goods_serial);

                  }

                  //序列号更新

                  if(!empty($edit_serial)){

                      foreach ($edit_serial as $edit_serial_k=>$edit_serial_v){

                          Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>$edit_serial_v['serial']]))->update([

                              'split_id'=>$edit_serial_v['split_id'],

                              'status'=>$edit_serial_v['type'],

                              'store_status'=>$edit_serial_v['store_status'],

                              'old_status'=>$edit_serial_v['old_status'],

                              'old_store'=>$edit_serial_v['old_store']

                          ]);

                      }

                  }

                  //序列号跟踪

                  if(!empty($serial_log)){

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

                  }

                  $result = $this->model->where((new Goods())->setWhere(['id'=>['in',$ids],'examine_status'=>'0']))->update(['examine_status'=>1,'examine_id'=>$admin['id'],'examine_time'=>time(),'operation_id'=>$admin['id']]);

                  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('审核失败');

              }

          }else{

              $list = $this->model->where(['id'=>['in',$ids],'examine_status'=>'1'])->select();

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

                  (new \app\admin\controller\kuerp\Checkout())->checkOutTime($list_v['purchase_time']);

              }

              $purchase_code = $this->model->where(['id'=>['in',$ids],'examine_status'=>'1'])->column('purchase_code');

              if(empty($list)){

                  if($examine_set!=1){

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

                  }

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

              }

  

              $editstock = [];//库存表修改库存

              $goods_stock = [];//商品列表库存

              $goods_batch_edit = [];//批次库存更新

              $info_list = Db::name('kuerp_split_info')->where((new Goods())->setWhere(['purchase_code'=>['in',$purchase_code]]))->select();

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

                  $goods = Db::name('kuerp_goods')->where(['id'=>$info_list_v['goods_id']])->find();

                  $stock_number = $info_list_v['basic_number'];//基础数量

                  if($info_list_v['type']==2){

                      //字件

                      //商品表库存

                      $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,

                          ];

                      }

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

                          if($goods_stock_v['inventory']<0){

                              if($examine_set!=1){

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

                              }

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

                          }

                      }

                      //商品表库存结束

  

                      //商品库存表库存

                      $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 = $info_list_v['goods_attr'];

                          if($goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

                      }

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

                      if($stock){

                          $status = false;

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

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

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

                                  $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']-$info_list_v['cost_money'],4));

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $editstock[] = [

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

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

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

                              ];

                          }

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

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

                                  if($examine_set!=1){

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

                                  }

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

                              }

                          }

                      }else{

                          if($examine_set!=1){

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

                          }

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

                      }

                      //商品库存表库存结束

  

                      //批次跟踪

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

                          $where = [];

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

                          if(isset($goods_attr) && $goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

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

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

                          $where = (new Goods())->setWhere($where);

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

                          if(empty($goods_batch_stock)){

                              if($examine_set!=1){

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

                              }

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

                          }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 $goods_batch_edit_k=>$goods_batch_edit_v){

                                  if($goods_batch_edit_v<0){

                                      if($examine_set!=1){

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

                                      }

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

                                  }

                              }

                          }

                      }

  

                      //序列号跟踪

                      if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){

                          $period = unserialize($info_list_v['period']);

                          foreach ($period as $period_k=>$period_v){

                              $per_s = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>$period_v['serial']]))->find();//序列号

                              if($per_s){//仓库有记录

                                  $edit_serial[] = ['serial'=>$per_s['serial'],'split_id'=>$info_list_v['id'],'type'=>$per_s['old_status'],'store_status'=>$per_s['old_store']];

                              }else{

                                  if($examine_set!=1){

                                      return ['status'=>0,'msg'=>$goods['goods_name'].'序列号'.$period_v['serial'].'不存在'];

                                  }

                                  $this->error($goods['goods_name'].'序列号'.$period_v['serial'].'不存在');

                              }

                          }

                      }

                      //序列号跟踪

                  }else{

                      //组合件

                      //商品表库存

                      $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'] = $info_list_v['goods_code'];

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

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

                          $goods_attr = $info_list_v['goods_attr'];

                          if($goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

                      }

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

                      if($stock){

                          $status = false;

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

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

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

                                  $editstock[$editstock_k]['cost_money'] = (round($editstock_v['cost_money']+$info_list_v['cost_money'],4));

                                  $status = true;

                              }

                          }

                          if(!$status){

                              $editstock[] = [

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

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

                                  'cost_money'=>(round($stock['cost_money']+$info_list_v['cost_money'],4))

                              ];

                          }

                      }

                      //商品库存表库存结束

  

                      //批次跟踪

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

                          $where = [];

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

                          if(isset($goods_attr) && $goods_attr>0){

                              $where['attribute_id'] = $goods_attr;

                          }

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

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

                          $where = (new Goods())->setWhere($where);

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

                          if(!empty($goods_batch_stock)){

                              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;

                              }

                          }

                      }

  

                      //序列号跟踪

                      if($goods && $goods['serial']==1 && $serial_set==1 && !empty($info_list_v['period'])){

                          $period = unserialize($info_list_v['period']);

                          foreach ($period as $period_k=>$period_v){

                              $per_s = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>$period_v['serial']]))->find();//序列号

                              if($per_s){//仓库有记录

                                  $edit_serial[] = ['serial'=>$per_s['serial'],'split_id'=>$info_list_v['id'],'type'=>$per_s['old_status'],'store_status'=>$per_s['old_store']];

                              }else{

                                  if($examine_set!=1){

                                      return ['status'=>0,'msg'=>$goods['goods_name'].'序列号'.$period_v['serial'].'不存在'];

                                  }

                                  $this->error($goods['goods_name'].'序列号'.$period_v['serial'].'不存在');

                              }

                          }

                      }

                      //序列号跟踪

                  }

              }

  

              $info_id = Db::name('kuerp_split_info')->where((new Goods())->setWhere(['purchase_code'=>['in',$purchase_code]]))->column('id');

              $admin = Session::get('admin');

              Db::startTrans();

              try {

                  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($goods_batch_edit)){

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

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

                      }

                  }

  

                  Db::name('kuerp_goods_log')->where((new Goods())->setWhere(['business_type'=>['in',[15,14]],'purchase_id'=>['in',$info_id]]))->delete();

  

                  Db::name('kuerp_goods_batch_log')->where((new Goods())->setWhere(['business_type'=>['in',[13,14]],'order_id'=>['in',$info_id]]))->delete();

  

                  Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['split_id'=>['in',$info_id],'split_status'=>'1']))->delete();

                  $serial = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['split_id'=>['in',$info_id],'split_status'=>'2']))->select();

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

                      Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['id'=>$serial_v['id']]))->update(['status'=>$serial_v['old_status'],'store_status'=>$serial_v['old_store']]);

                  }

                  Db::name('kuerp_goods_serial_log')->where((new Goods())->setWhere(['business_type'=>['in',[13,14]],'order_id'=>['in',$info_id]]))->delete();

  

                  $result = $this->model->where(['id'=>['in',$ids],'examine_status'=>'1'])->update(['examine_status'=>0,'examine_id'=>0,'examine_time'=>'','operation_id'=>$admin['id']]);

                  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 savetemp($ids = null)

      {

          if ($this->request->isPost()) {

              $data = input();

              $team_name = isset($data['temp_name'])?trim($data['temp_name']):'';

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

              if(empty($team_name)){

                  $this->error('请输入模板名称');

              }

              if ($params) {

                  $team = Db::name('kuerp_split_temp')->where((new Goods())->setWhere(['temp_name'=>$team_name]))->find();

                  if($team){

                      $this->error('模板名称不能重复');

                  }

                  $params = $this->preExcludeFields($params);

                  if(empty($params['purchase_code'])){

                      $this->error('请填写单据编号');

                  }

                  $admin = Session::get('admin');

                  $serial= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'serial']))->value('is_checked');//序列号设置

                  $batch= Db::name('kuerp_sets')->where((new Goods())->setWhere(['name'=>'batch']))->value('is_checked');//批次设置

  

                  $data = [];

                  $goods_serial_all = [];//全部序列号,重复判断使用

                  $p = 2;$z = 2;

                  foreach ($params['goods_code'] as $k=>$v){

                      if(!empty($v)){//有产品编号

                          if(!isset($params['goods_unit'][$k]) || empty($params['goods_unit'][$k])){

                              $this->error('请选择单位');

                          }

                          if(!isset($params['stores_id'][$k]) || empty($params['stores_id'][$k])){

                              $this->error('请选择仓库');

                          }

  

                          $goods = Db::name('kuerp_goods')->where((new Goods())->setWhere(['goods_code'=>$v]))->find();

                          $goods_unit = $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k];

                          $advance_rate = 1;//换算进率

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

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

                                  //所选单位不等于商品默认单位,需查询进率换算基础数量

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

                                      ->where((new Goods())->setWhere(['main_unit'=>$goods['unit_id'],'vice_unit'=>$goods_unit]))

                                      ->field('id,advance_rate')

                                      ->find();

                                  if(empty($more_unit)){

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

                                  }else{

                                      $advance_rate = $more_unit['advance_rate'];

                                  }

                              }

                          }

                          $basic_number = round(($advance_rate*$params['number'][$k]),4);

  

                          if($k==0){

                              //组合件

                              //批次

                              $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();//已有批次

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

                                  if(empty($params['batch'][$k])){

                                      $this->error('请选择生产批次');

                                  }

                                  if(empty($params['manufacture'][$k])){

                                      $this->error('请输入生产日期');

                                  }

                                  if($params['guarantee'][$k]===''){

                                      $this->error('请输入保质期');

                                  }

                                  if ($params['guarantee'][$k]<0){

                                      $this->error('保质期天数不可小于0');

                                  }

                                  if(empty($params['validity'][$k])){

                                      $this->error('请输入过期时间');

                                  }

                                  if(empty($batch_data)){

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

                                  }

                              }

                              //序列号

                              $goods_serial='';

                              if(isset($params['period_period'][$k]) && $goods['serial']==1 && $serial==1){

                                  if(empty($params['period_period'][$k])){

                                      $this->error('请输入商品序列号');

                                  }

                                  $goods_serial = $params['period_period'][$k];

                                  $period_period = unserialize($params['period_period'][$k]);

                                  $serial_code = [];

                                  foreach ($period_period as $period_period_k=>$period_period_v){

                                      if(in_array($period_period_v['serial'],$goods_serial_all)){

                                          $this->error('商品序列号'.$period_period_v['serial'].'重复');

                                      }else{

                                          $goods_serial_all[] = $period_period_v['serial'];

                                      }

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

                                  }

                                  $serial = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>['in',$serial_code],'store_status'=>'1']))->column('serial');

                                  if(sizeof($serial)!=sizeof($serial_code)){

                                      foreach ($serial_code as $serial_code_k=>$serial_code_v){

                                          if(in_array($serial_code_v,$serial)){

                                              $this->error('商品序列号'.$serial_code_v.'不在库,不可出库');

                                          }

                                      }

                                      $this->error('商品序列号不在库,不可出库');

                                  }

                                  $basic_number = (float)$params['number'][$k];

                              }

  

                              $data[] = (new Goods())->setDataInfo([

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

                                  'purchase_time'     =>  strtotime($params['purchase_time']),

                                  'business_type'     =>  1,

                                  'type'              =>  1,

  

                                  'goods_id'          =>  $goods['id'],//商品信息

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

                                  'goods_attr'        =>  $params['goods_attr'][$k]===''?null:(float)$params['goods_attr'][$k],

                                  'goods_unit'        =>  $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k],

  

                                  'stores_id'         =>  $params['stores_id'][$k]===''?null:(float)$params['stores_id'][$k],

                                  'allow_stock'       =>  $params['allow_stock'][$k]===''?null:(float)$params['allow_stock'][$k],

                                  'number'            =>  $params['number'][$k]===''?null:(float)$params['number'][$k],

                                  'period'            =>  $goods_serial,

                                  'basic_number'      =>  $basic_number,

                                  'unit_cost'         =>  $params['unit_cost'][$k]===''?null:(float)$params['unit_cost'][$k],

                                  'cost_money'        =>  $params['cost_money'][$k]===''?null:(float)$params['cost_money'][$k],

                                  'remark'            =>  $params['remark'][$k]===''?null:$params['remark'][$k],

  

                                  'batch'             =>  $params['batch'][$k]===''?null:$params['batch'][$k],

                                  'batch_id'          =>  $params['batch_id'][$k]===''?null:(float)$params['batch_id'][$k],

                                  'manufacture'       =>  $params['manufacture'][$k]===''?null:strtotime($params['manufacture'][$k]),

                                  'guarantee'         =>  $params['guarantee'][$k]===''?null:(float)$params['guarantee'][$k],

                                  'validity'          =>  $params['validity'][$k]===''?null:strtotime($params['validity'][$k]),

  

                                  'preparation_id'    =>  $admin['id'],

                                  'operation_id'      =>  $admin['id'],

  

                                  'createtime'        => time(),

                                  'updatetime'        => time(),

                              ]);

                              $p = 1;

                          }else{

                              //子件

                              //批次

                              $batch_data = Db::name('kuerp_goods_batch')->where((new Goods())->setWhere(['id'=>$params['batch_id'][$k]]))->find();//已有批次

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

                                  if(empty($params['batch'][$k])){

                                      if($batch_data){

                                          $this->error('请选择生产批次');

                                      }else{

                                          $this->error('请输入生产批次');

                                      }

                                  }

                                  if(empty($params['manufacture'][$k])){

                                      $this->error('请输入生产日期');

                                  }

                                  if($params['guarantee'][$k]===''){

                                      $this->error('请输入保质期');

                                  }

                                  if ($params['guarantee'][$k]<0){

                                      $this->error('保质期天数不可小于0');

                                  }

                                  if(empty($params['validity'][$k])){

                                      $this->error('请输入过期时间');

                                  }

                              }

                              //序列号

                              $goods_serial='';

                              if(isset($params['period_period'][$k]) && $goods['serial']==1 && $serial==1){

                                  if(empty($params['period_period'][$k])){

                                      $this->error('请输入商品序列号');

                                  }

                                  $goods_serial = $params['period_period'][$k];

                                  $period_period = unserialize($params['period_period'][$k]);

                                  $serial_code = [];

                                  foreach ($period_period as $period_period_k=>$period_period_v){

                                      if(in_array($period_period_v['serial'],$goods_serial_all)){

                                          $this->error('商品序列号'.$period_period_v['serial'].'重复');

                                      }else{

                                          $goods_serial_all[] = $period_period_v['serial'];

                                      }

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

                                  }

                                  $serial = Db::name('kuerp_goods_serial')->where((new Goods())->setWhere(['serial'=>['in',$serial_code]]))->column('serial');

                                  if($serial){

                                      $this->error('商品序列号'.$serial[0].'已占用');

                                  }

                                  $basic_number = (float)$params['number'][$k];

                              }

  

                              $data[] = (new Goods())->setDataInfo([

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

                                  'purchase_time'     =>  strtotime($params['purchase_time']),

                                  'business_type'     =>  1,

                                  'type'              =>  2,

  

                                  'goods_id'          =>  $goods['id'],//商品信息

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

                                  'goods_attr'        =>  $params['goods_attr'][$k]===''?null:(float)$params['goods_attr'][$k],

                                  'goods_unit'        =>  $params['goods_unit'][$k]===''?null:(float)$params['goods_unit'][$k],

  

                                  'stores_id'         =>  $params['stores_id'][$k]===''?null:(float)$params['stores_id'][$k],

                                  'allow_stock'       =>  $params['allow_stock'][$k]===''?null:(float)$params['allow_stock'][$k],

                                  'number'            =>  $params['number'][$k]===''?null:(float)$params['number'][$k],

                                  'period'            =>  $goods_serial,

                                  'basic_number'      =>  $basic_number,

                                  'unit_cost'         =>  $params['unit_cost'][$k]===''?null:(float)$params['unit_cost'][$k],

                                  'cost_money'        =>  $params['cost_money'][$k]===''?null:(float)$params['cost_money'][$k],

                                  'remark'            =>  $params['remark'][$k]===''?null:$params['remark'][$k],

  

                                  'batch'             =>  $params['batch'][$k]===''?null:$params['batch'][$k],

                                  'batch_id'          =>  $params['batch_id'][$k]===''?null:(float)$params['batch_id'][$k],

                                  'manufacture'       =>  $params['manufacture'][$k]===''?null:strtotime($params['manufacture'][$k]),

                                  'guarantee'         =>  $params['guarantee'][$k]===''?null:(float)$params['guarantee'][$k],

                                  'validity'          =>  $params['validity'][$k]===''?null:strtotime($params['validity'][$k]),

  

                                  'preparation_id'    =>  $admin['id'],

                                  'operation_id'      =>  $admin['id'],

  

                                  'createtime'        => time(),

                                  'updatetime'        => time(),

                              ]);

                              $z = 1;

                          }

                      }

                  }

                  if($p==2){

                      $this->error('组合件信息不能为空');

                  }

                  if($z==2){

                      $this->error('组合子件信息不能为空');

                  }

                  if(!empty($data)){

                      $row = (new Goods())->setDataInfo([

                          'temp_name'         =>  $team_name,

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

                          'purchase_time'     =>  strtotime($params['purchase_time']),

                          'business_type'     =>  1,

                          'money'             =>  $params['costs']===''?null:(float)$params['costs'],

                          'remark'            =>  $params['note']===''?null:$params['note'],

                          'preparation_id'    =>  $admin['id'],

                          'operation_id'      =>  $admin['id'],

                          'createtime'        => time(),

                          'updatetime'        => time(),

                      ]);

                  }else{

                      $this->error('组件信息不能为空');

                  }

  

                  $result = false;

                  Db::startTrans();

                  try {

                      if(!empty($row)){

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

                      }

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

                          $data_v['purchase_id'] = $result;

                          Db::name('kuerp_split_info_temp')->insertGetId($data_v);

                      }

                      Db::commit();

                  } catch (Exception $e) {

                      Db::rollback();

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

                  }

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

                      $this->success($result);

                  } else {

                      $this->error(__('No rows were inserted'));

                  }

              }

              $this->error(__('Parameter %s can not be empty', ''));

          }

      }

  }