1
cwjbxqmz
2023-11-17 8a418b4192bff10d005940210681bb406a4c2f6a
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
using Model;
using Model.生产管理;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Pub_Class;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Http;
using System.Windows.Forms;
using WebAPI.Models;
 
namespace WebAPI.Controllers
{
    //生产任务单Controller
    //数据库主表Sc_ICMOBillMain
    //数据库子表Sc_ICMOBillSub
    public class Sc_ICMOBillController : ApiController
    {
        public DBUtility.ClsPub.Enum_BillStatus BillStatus;
        private json objJsonResult = new json();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        DataSet ds;
 
        //获取系统参数
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        public DAL.ClsSc_ICMOBill BillOld = new DAL.ClsSc_ICMOBill();
 
        #region 生产任务单 保存/编辑功能
        [Route("Sc_ICMOBill/ICMOBillEdit")]
        [HttpPost]
        public object ICMOBillEdit([FromBody] JObject sMainSub)
        {
            try
            {
                var _value = sMainSub["sMainSub"].ToString();
                string msg1 = _value.ToString();
                oCN.BeginTran();
                //保存主表
                objJsonResult = AddBillMain(msg1);
                if (objJsonResult.code == "0")
                {
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = objJsonResult.Message;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "单据保存成功!";
                objJsonResult.data = null;
                return objJsonResult;
 
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "保存失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
 
        public json AddBillMain(string msg1)
        {
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString(); //主表数据
            string msg3 = sArray[1].ToString(); //子表数据
            int OperationType = int.Parse(sArray[2].ToString()); // 数据类型 1添加 2复制 3修改 
            string user = sArray[3].ToString();
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Sc_ICMOBill_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                msg2 = "[" + msg2.ToString() + "]";
                List<ClsSc_ICMOBillMain> mainList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsSc_ICMOBillMain>>(msg2);
 
                long HInterID = mainList[0].HInterID;//递入type得到的单据ID
                string HBillNo = mainList[0].HBillNo;//递入type得到的单据号
                long HPRDORGID = mainList[0].HPRDORGID;//组织
                DateTime HDate = mainList[0].HDate;//日期
                string HRemark = mainList[0].HRemark;//备注
                string HSeOrderBillNo = mainList[0].HSeOrderBillNo;//销售订单号
                long HSeOrderInterID = mainList[0].HSeOrderInterID;//销售订主id
                long HSeOrderEntryID = mainList[0].HSeOrderEntryID;//销售订子id
                long HEmpID = mainList[0].HEmpID;//业务员
                long HCusID = mainList[0].HCusID;//客户
                long HCenterID = mainList[0].HCenterID;//工作中心
                double? HPlanQty = mainList[0].HPlanQty == null ? 0 : mainList[0].HPlanQty;//计划数量
                string HMaker = user;//制单人
 
                long HISENTRUST = mainList[0].HISENTRUST == null ? 0 : mainList[0].HISENTRUST;//组织受托加工
                long HISREWORK = mainList[0].HISREWORK == null ? 0 : mainList[0].HISREWORK;//是否返工
 
                ds = oCN.RunProcReturn("select * from h_v_IF_ICMOBillList where hmainid=" + HInterID + " and 单据号='" + HBillNo + "'", "h_v_IF_ICMOBillList");
 
                if ((OperationType == 1 || OperationType == 2) && ds.Tables[0].Rows.Count == 0)//新增
                {
                    //主表
                    oCN.RunProc(@"Insert Into Sc_ICMOBillMain   
                        (HBillType,HBillStatus,HInterID,HBillNo,HDate,HPRDORGID
                        ,HYear,HPeriod,HRemark,HMaker,HMakeDate
                        ,HSeOrderBillNo,HSeOrderInterID,HSeOrderEntryID,HEmpID,HCusID
                        ,HCenterID,HPlanQty,HDeptID,HMaterID,HUnitID,HBomID,HPlanBeginDate,HPlanEndDate,HISENTRUST,HISREWORK)
                        values('3710',1," + HInterID + ",'" + HBillNo + "','" + HDate + "'," + HPRDORGID +
                    "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",'" + HRemark + "','" + HMaker + "',getdate()" +
                    ",'" + HSeOrderBillNo + "'," + HSeOrderInterID + "," + HSeOrderEntryID + "," + HEmpID + "," + HCusID +
                    "," + HCenterID + "," + HPlanQty + ",0,0,0,0,'',''" + "," + HISENTRUST + "," + HISREWORK + ") ");
                }
                else if (OperationType == 3 || ds.Tables[0].Rows.Count != 0)
                {
                    if (ds.Tables[0].Rows[0]["审核人"].ToString() != "")
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据已审核,不允许修改!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    //修改
                    oCN.RunProc("update Sc_ICMOBillMain  set " +
                                "HRemark='" + HRemark + "', HChecker='" + HMaker + "', HCheckDate=getdate()" +
                                ", HSeOrderBillNo='" + HSeOrderBillNo + "', HSeOrderInterID=" + HSeOrderInterID + ", HSeOrderEntryID=" + HSeOrderEntryID + ", HEmpID=" + HEmpID + ", HCusID=" + HCusID + "" +
                                ", HCenterID=" + HCenterID + ", HPlanQty=" + HPlanQty + " where HInterID=" + HInterID);
 
                    //删除子表
                    oCN.RunProc("delete from Sc_ICMOBillSub where HInterID='" + HInterID + "'");
                }
                //保存子表
                objJsonResult = AddBillSub(msg3, HInterID, OperationType);
 
                if (objJsonResult.code == "0")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = objJsonResult.Message;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                //修改字段 HSTOCKINORGID
                oCN.RunProc("update Sc_ICMOBillSub set HSTOCKINORGID=" + HPRDORGID + " where HInterID=" + HInterID);
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = null;
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
 
        public json AddBillSub(string msg3, long HInterID, int OperationType)
        {
            List<ClsSc_ICMOBillSub> DetailColl = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsSc_ICMOBillSub>>(msg3);
 
            string HINSTOCKTYPE = DetailColl[0].HINSTOCKTYPE == null ? "''" : DetailColl[0].HINSTOCKTYPE;      //入库类型,
            long HCHECKPRODUCT = DetailColl[0].HCHECKPRODUCT == null ? 0 : DetailColl[0].HCHECKPRODUCT;        //产品检验,
            string HQAIP = DetailColl[0].HQAIP == null ? "''" : DetailColl[0].HQAIP;    //紧急放行,
            long HISBACKFLUSH = DetailColl[0].HISBACKFLUSH == null ? 0 : DetailColl[0].HISBACKFLUSH;        //倒冲领料,
            string HREQSRC = DetailColl[0].HREQSRC == null ? "''" : DetailColl[0].HREQSRC;  //需求来源,
            double HSTOCKINQUASELAUXQTY = DetailColl[0].HSTOCKINQUASELAUXQTY == null ? 0 : DetailColl[0].HSTOCKINQUASELAUXQTY;  //合格品入库选单数量,
            long HSeOrderEntrySEQ = DetailColl[0].HSeOrderEntrySEQ == null ? 0 : DetailColl[0].HSeOrderEntrySEQ;        //销售订单行号,
            string HPROJECTNO = DetailColl[0].HPROJECTNO == null ? "''" : DetailColl[0].HPROJECTNO;   //项目编号,
            long HPRODUCTTYPE = DetailColl[0].HPRODUCTTYPE == null ? 0 : DetailColl[0].HPRODUCTTYPE;            //产品类型,
            double HCOSTRATE = DetailColl[0].HCOSTRATE == null ? 0 : DetailColl[0].HCOSTRATE;            // 权重,
            long HBASEUNITID = DetailColl[0].HBASEUNITID == null ? 0 : DetailColl[0].HBASEUNITID;       //基本计量单位,
 
            int i = 0;
            foreach (ClsSc_ICMOBillSub oSub in DetailColl)
            {
                i++;
                if (oSub.HQty <= 0 || oSub.HQty == null)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "第" + i + "行,数量不能为0或者小于0";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (oSub.HMaterID == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "第" + i + "行,物料不能为空";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                //if (oSub.HSourceID == 0)
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "第" + i + "行,生产资源不能为空";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
 
                if (oSub.HDeptID == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "第" + i + "行,生产车间不能为空";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (oSub.HUnitID == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "第" + i + "行,计量单位不能为空";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                oCN.RunProc($@"Insert into Sc_ICMOBillSub 
                (HInterID,HENTRYID,HQty
                ,HPlanBeginDate,HPlanEndDate
                ,HBeginDate,HEndDate
                ,HMaterID,HUnitID,HRemark,HSourceID,HDeptID,HSTATUS
                ,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney
                ,HINSTOCKTYPE,HCHECKPRODUCT,HQAIP,HISBACKFLUSH,HREQSRC,HSTOCKINQUASELAUXQTY,HSeOrderEntrySEQ,HPROJECTNO,HPRODUCTTYPE,HCOSTRATE,HBASEUNITID
                ,HSTOCKINORGID) 
                 values({HInterID},{i},{(oSub.HQty == null ? 0 : oSub.HQty)}
                ,'{(oSub.HPlanBeginDate == null ? DateTime.Now.ToString("yyyy-MM-dd") : oSub.HPlanBeginDate.ToString())}','{(oSub.HPlanEndDate == null ? DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") : oSub.HPlanEndDate.ToString())}'
                ,'{(oSub.HPlanBeginDate == null ? DateTime.Now.ToString("yyyy-MM-dd") : oSub.HPlanBeginDate.ToString())}','{(oSub.HPlanEndDate == null ? DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") : oSub.HPlanEndDate.ToString())}'
                ,{oSub.HMaterID},{oSub.HUnitID},'{oSub.HRemark}',{oSub.HSourceID},{oSub.HDeptID},{oSub.HSTATUS}
                        ,0,0,'','',0,0,{HINSTOCKTYPE},{HCHECKPRODUCT},{HQAIP},{HISBACKFLUSH},{HREQSRC},{HSTOCKINQUASELAUXQTY},{HSeOrderEntrySEQ},{HPROJECTNO},{HPRODUCTTYPE},{HCOSTRATE},{HBASEUNITID},{oSub.HSTOCKINORGID})");
            }
 
            objJsonResult.code = "1";
            objJsonResult.count = 1;
            objJsonResult.Message = null;
            objJsonResult.data = null;
            return objJsonResult;
        }
        #endregion
 
        [Route("Sc_ICMOBill/ICMOBillSaveApi")]
        [HttpPost]
        public object ICMOBillSaveApi([FromBody] JObject sMainSub)
        {
            try
            {
                //LogService.Write(sMainSub.ToString());
                var model = sMainSub["model"].ToString();
                var entry = sMainSub["model"]["HENTRY"].ToString();
                var _model = sMainSub["model"]["HPPBOMMAINENTRY"].ToString();
                var _entry = sMainSub["model"]["HPPBOMSUBENTRY"].ToString();
                model = "[" + model.ToString() + "]";
                List<ClsSc_ICMOBillMain> mainList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsSc_ICMOBillMain>>(model);
                List<ClsSc_ICMOBillSub> subList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsSc_ICMOBillSub>>(entry);
                List<ClsSc_PPBomBillMain> _mainList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsSc_PPBomBillMain>>(_model);
                List<ClsSc_PPBomBillSub> _subList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsSc_PPBomBillSub>>(_entry);
                string sql = string.Empty;
                oCN.BeginTran();
                sql = $"delete Sc_ICMOBillMain where HinterID = {mainList[0].HInterID}";
                oCN.RunProc(sql);
                sql = $"delete Sc_ICMOBillSub where HinterID = {mainList[0].HInterID}";
                oCN.RunProc(sql);
                //主表
                oCN.RunProc(@"Insert Into Sc_ICMOBillMain   
                        (HBillType,HInterID,HBillNo,HDate,HPRDORGID
                        ,HYear,HPeriod,HRemark,HMaker,HMakeDate
                        ,HSeOrderBillNo,HSeOrderInterID,HSeOrderEntryID,HEmpID,HCusID
                        ,HCenterID,HPlanQty,HDeptID,HMaterID,HUnitID,HBomID,HPlanBeginDate,HPlanEndDate,HBillStatus
                        ,HOWNERID,HOWNERTYPEID)
                        values('3710'," + mainList[0].HInterID + ",'" + mainList[0].HBillNo + "','" + mainList[0].HDate + "'," + mainList[0].HPRDORGID +
                "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",'" + mainList[0].HRemark + "','" + mainList[0].HMaker + "',getdate()" +
                ",'" + mainList[0].HSeOrderBillNo + "'," + mainList[0].HSeOrderInterID + "," + mainList[0].HSeOrderEntryID + "," + mainList[0].HEmpID + "," + mainList[0].HCusID +
                "," + mainList[0].HCenterID + "," + mainList[0].HPlanQty + ",0,0,0,0,'','',2" +
                ","+ mainList[0].HOWNERID + ",'"+ mainList[0].HOWNERTYPEID + "') ");
                //保存主表
                foreach (var oSub in subList)
                {
                    oCN.RunProc($@"Insert into Sc_ICMOBillSub 
                (HInterID,HENTRYID,HQty
                ,HPlanBeginDate,HPlanEndDate
                ,HBeginDate,HEndDate
                ,HMaterID,HUnitID,HRemark,HSourceID,HDeptID,HSTATUS
                ,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney
                ,HCOSTRATE,HISBACKFLUSH,HBatchNo,HBomID,HCHECKPRODUCT,HSEQ,HSTOCKINORGID) 
                 values({oSub.HInterID},{oSub.HEntryID},{(oSub.HQty == null ? 0 : oSub.HQty)}
                ,'{(oSub.HPlanBeginDate == null ? DateTime.Now.ToString("yyyy-MM-dd") : oSub.HPlanBeginDate.ToString())}','{(oSub.HPlanEndDate == null ? DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") : oSub.HPlanEndDate.ToString())}'
                ,'{(oSub.HPlanBeginDate == null ? DateTime.Now.ToString("yyyy-MM-dd") : oSub.HPlanBeginDate.ToString())}','{(oSub.HPlanEndDate == null ? DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") : oSub.HPlanEndDate.ToString())}'
                ,{oSub.HMaterID},{oSub.HUnitID},'{oSub.HRemark}',{oSub.HSourceID},{oSub.HDeptID},'{oSub.HSTATUS}'
                        ,0,0,'','',0,0
                ,{oSub.HCOSTRATE},{oSub.HISBACKFLUSH},'{oSub.HBatchNo}',{oSub.HBomID},{oSub.HCHECKPRODUCT},{oSub.HSEQ},{oSub.HSTOCKINORGID})");
                }
 
                foreach (var _item in _mainList)
                {
                    //生产用料清单
                    sql = $"delete Sc_PPBomBillMain where HinterID = {_item.HInterID}";
                    oCN.RunProc(sql);
                    sql = $"delete Sc_PPBomBillSub where HinterID = {_item.HInterID}";
                    oCN.RunProc(sql);
                    //主表
                    oCN.RunProc(@"Insert Into Sc_PPBomBillMain   
                        (HInterID,HYear,HPeriod,HBillType,HBillSubType,HDate,HBillNo,HBillStatus,HICMOInterID
                        ,HICMOEntryID,HMaterID,HUnitID,HQty,HDeptID,HMaker,HMakeDate,HChecker,HCheckDate
                        ,HSeOrderBillNo,HSeOrderInterID,HSeOrderEntryID,HPRDORGID,HENTRUSTORGID,HPARENTOWNERID
                        ,HPARENTOWNERTYPEID,HERPInterID,HERPBillType,HSeOrderEntrySEQ,HICMOEntrySEQ,HREQSRC)
                        values(" + _item.HInterID + "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",'" + 3720 + "','" +
                    _item.HBillSubType + "','" + _item.HDate + "','" + _item.HBillNo + "','" + _item.HBillStatus + "'," + _item.HICMOInterID +
                    "," + _item.HICMOEntryID + ",'" + _item.HMaterID + "'," + _item.HUnitID + "," + _item.HQty + "," + _item.HDeptID +
                    ",'" + _item.HMaker + "','" + _item.HMakeDate + "','" + _item.HChecker + "','" + _item.HCheckDate + "','" + _item.HSeOrderBillNo + "'," + _item.HSeOrderInterID + "," + _item.HSeOrderEntryID + "," + _item.HPRDORGID + "," + _item.HENTRUSTORGID + "," + _item.HPARENTOWNERID + ",'" +
                    _item.HPARENTOWNERTYPEID + "'," + _item.HERPInterID + ",'" + _item.HERPBillType + "'," + _item.HSeOrderEntrySEQ + "," + _item.HICMOEntrySEQ + ",'" + _item.HREQSRC + "')");
                }
                foreach (var oSub in _subList)
                {
                    oCN.RunProc($@"Insert into Sc_PPBomBillSub 
                (HInterID,HEntryID,HMaterID,HMaterNumber,HUnitID,HUnitNumber,HSPID,HQty,HQtyMust,HWHID,HRemark
                ,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HICMOInterID,HICMOEntryID,HICMOBillNo
                ,HCHILDSUPPLYORGID,HSUPPLYORGID,HENTRUSTPICKORGID,HSRCTRANSORGID,HGROUPBYOWNERID,HOWNERID,HOWNERTYPEID
                ,HRelationQty,HMoveStockQty,HAuxPropID,HBatchNO,HMTONo,HQtyScrap,HERPInterID,HERPEntryID
                ,HBackRelationQty,HNumerator,HDenominator,HBASEUNITID,HProcessID,HICMOENTRYSEQ,HPROJECTNO
                ,HOPERID,HSEQ,HSTOCKSTATUSID,HLOT,HOPTQUEUE,HRESERVETYPE,HSelPickedQty,HSELPRCDRETURNQTY,HProcName) 
                 values('{oSub.HInterID}','{oSub.HEntryID}','{oSub.HMaterID}','{oSub.HMaterNumber}','{oSub.HUnitID}','{oSub.HUnitNumber}','{oSub.HSPID}','{oSub.HQty}'
                ,'{oSub.HQtyMust}','{oSub.HWHID}','{oSub.HRemark}','{oSub.HSourceInterID}','{oSub.HSourceEntryID}','{oSub.HSourceBillNo}','{oSub.HSourceBillType}','{oSub.HICMOInterID}','{oSub.HICMOEntryID}','{oSub.HICMOBillNo}','{oSub.HCHILDSUPPLYORGID}','{oSub.HSUPPLYORGID}','{oSub.HENTRUSTPICKORGID}','{oSub.HSRCTRANSORGID}','{oSub.HGROUPBYOWNERID}','{oSub.HOWNERID}','{oSub.HOWNERTYPEID}','{oSub.HRelationQty}','{oSub.HMoveStockQty}','{oSub.HAuxPropID}','{oSub.HBatchNO}','{oSub.HMTONo}','{Convert.ToDecimal(oSub.HQtyScrap)}','{oSub.HERPInterID}','{oSub.HERPEntryID}','{oSub.HBackRelationQty}','{oSub.HNumerator}','{oSub.HDenominator}','{oSub.HBASEUNITID}','{oSub.HProcessID}','{oSub.HICMOENTRYSEQ}','{oSub.HPROJECTNO}','{oSub.HOPERID}','{oSub.HSEQ}','{oSub.HSTOCKSTATUSID}','{oSub.HLOT}','{oSub.HOPTQUEUE}','{oSub.HRESERVETYPE}','{oSub.HSelPickedQty}','{oSub.HSELPRCDRETURNQTY}','{oSub.HProcName}')");
                }
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "单据保存成功!";
                objJsonResult.data = null;
                return objJsonResult;
 
            }
            catch (Exception e)
            {
                LogService.Write(e.Message.ToString());
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "保存失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
          
        }
 
 
        #region 生产任务单(无列表) 保存/编辑功能
        [Route("Sc_ICMOBill/ICMOBillEdit_NoTable")]
        [HttpPost]
        public object ICMOBillEdit_NoTable([FromBody] JObject sMainSub)
        {
            try
            {
                var _value = sMainSub["sMainSub"].ToString();
                string msg1 = _value.ToString();
                oCN.BeginTran();
                //保存主表
                objJsonResult = AddBillMain_NoTable(msg1);
                if (objJsonResult.code == "0")
                {
                    oCN.RollBack();
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = objJsonResult.Message;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "单据保存成功!";
                objJsonResult.data = null;
                return objJsonResult;
 
            }
            catch (Exception e)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "保存失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
 
        public json AddBillMain_NoTable(string msg1)
        {
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString(); //主表数据
            int OperationType = int.Parse(sArray[1].ToString()); // 数据类型 1添加 3修改
            string user = sArray[2].ToString();
            int HEntryID = int.Parse(sArray[3].ToString());
            string HComputerName = SystemInformation.ComputerName; //设备名称
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Sc_ICMOBill_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                var msg3 = msg2.ToString();
                msg2 = "[" + msg2.ToString() + "]";
                List<ClsSc_ICMOBillMain> mainList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClsSc_ICMOBillMain>>(msg2);
 
                long HInterID = mainList[0].HInterID;//递入type得到的单据ID
                string HBillNo = mainList[0].HBillNo;//递入type得到的单据号
                long HPRDORGID = mainList[0].HPRDORGID;//组织
                DateTime HDate = mainList[0].HDate;//日期
                string HRemark = mainList[0].HRemark;//备注
                string HSeOrderBillNo = mainList[0].HSeOrderBillNo;//销售订单号
                long HSeOrderInterID = mainList[0].HSeOrderInterID;//销售订主id
                long HSeOrderEntryID = mainList[0].HSeOrderEntryID;//销售订子id
                long HEmpID = mainList[0].HEmpID;//业务员
                long HCusID = mainList[0].HCusID;//客户
                long HCenterID = mainList[0].HCenterID;//工作中心
                long HBomID = mainList[0].HBomID;//bom
                //double? HPlanQty = mainList[0].HPlanQty == null ? 0 : mainList[0].HPlanQty;//计划数量
                string HMaker = user;//制单人
                string HIsStockQty = mainList[0].HIsStockQty;
                string HRemark3 = mainList[0].HRemark3;
              
 
                ds = oCN.RunProcReturn("select * from h_v_IF_ICMOBillList where hmainid=" + HInterID + " and 单据号='" + HBillNo + "'", "h_v_IF_ICMOBillList");
 
                if ((OperationType == 1 || OperationType == 2 || OperationType == 4) && ds.Tables[0].Rows.Count == 0)//新增
                {
                    //主表
                    oCN.RunProc(@"Insert Into Sc_ICMOBillMain   
                        (HBillType,HBillStatus,HInterID,HBillNo,HDate,HPRDORGID
                        ,HYear,HPeriod,HRemark,HMaker,HMakeDate
                        ,HSeOrderBillNo,HSeOrderInterID,HSeOrderEntryID,HEmpID,HCusID
                        ,HCenterID,HPlanQty,HDeptID,HMaterID,HUnitID,HBomID,HPlanBeginDate,HPlanEndDate
                        ,HIsStockQty,HRemark3)
                        values('3710',1," + HInterID + ",'" + HBillNo + "','" + HDate + "'," + HPRDORGID +
                    "," + DateTime.Now.Year + "," + DateTime.Now.Month + ",'" + HRemark + "','" + HMaker + "',getdate()" +
                    ",'" + HSeOrderBillNo + "'," + HSeOrderInterID + "," + HSeOrderEntryID + "," + HEmpID + "," + HCusID +
                    "," + HCenterID + ",0,0,0,0," + HBomID + ",'',''" +
                    ",'"+ HIsStockQty + "','"+ HRemark3 + "') ");
                    
 
 
                    LogService.Write("用户:" + user + ",日期:" + DateTime.Now + ",新增生产订单单据:" + HBillNo);
                    oCN.RunProc("Insert into System_log (GeginDate, userid, WorkstationName, WorkList, SystemName, NetuserName, State) select GETDATE(),'" + user + "','" + HComputerName + "','" + "新增生产订单单据:" + HBillNo + "','LMES-生产订单模块','" + DBUtility.ClsPub.IPAddress + "','新增单据'", ref DBUtility.ClsPub.sExeReturnInfo);
 
                }
                else if (OperationType == 3 || ds.Tables[0].Rows.Count != 0)
                {
                    if (ds.Tables[0].Rows[0]["审核人"].ToString() != "")
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "单据已审核,不允许修改!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    //修改
                    oCN.RunProc("update Sc_ICMOBillMain  set " +
                                "HRemark='" + HRemark + "', HUpDater='" + HMaker + "', HUpDateDate=getdate()" +
                                ", HSeOrderBillNo='" + HSeOrderBillNo + "', HSeOrderInterID=" + HSeOrderInterID + ", HSeOrderEntryID=" + HSeOrderEntryID + ", HEmpID=" + HEmpID + ", HCusID=" + HCusID +
                                ", HCenterID=" + HCenterID + ",HBomID=" + HBomID + ",HIsStockQty='"+ HIsStockQty + "',HRemark3='" + HRemark3 + "' where HInterID=" + HInterID);
 
                    //删除子表
                    oCN.RunProc("delete from Sc_ICMOBillSub where HInterID='" + HInterID + "' and HEntryID='" + HEntryID + "'");
 
                    LogService.Write("用户:" + user + ",日期:" + DateTime.Now + ",修改生产订单单据:" + HBillNo);
                    oCN.RunProc("Insert into System_log (GeginDate, userid, WorkstationName, WorkList, SystemName, NetuserName, State) select GETDATE(),'" + user + "','" + HComputerName + "','" + "修改生产订单单据:" + HBillNo + "','LMES-生产订单模块','" + DBUtility.ClsPub.IPAddress + "','修改单据'", ref DBUtility.ClsPub.sExeReturnInfo);
                }
                //保存子表
                objJsonResult = AddBillSub_NoTable(msg3, HInterID, OperationType, HEntryID);
 
 
                //反写源单-销售订单数据
                if ((OperationType == 1 || OperationType == 2 || OperationType == 4) && ds.Tables[0].Rows.Count == 0)
                {
                    //生产订单新增回填销售订单关联数量
                    oCN.RunProc("exec h_p_Xs_UpDateRelation_SeOrderToICMO_Add " + HInterID);
                }
                else if (OperationType == 3 || ds.Tables[0].Rows.Count != 0)
                {
                    //生产订单删除回填销售订单关联数量
                    oCN.RunProc("exec h_p_Xs_UpDateRelation_SeOrderToICMO_Delete " + HInterID);
                    //生产订单新增回填销售订单关联数量
                    oCN.RunProc("exec h_p_Xs_UpDateRelation_SeOrderToICMO_Add " + HInterID);
                }
 
 
 
                if (objJsonResult.code == "0")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = objJsonResult.Message;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = null;
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
 
        public json AddBillSub_NoTable(string msg3, long HInterID, int OperationType,int HEntryID)
        {
            ClsSc_ICMOBillSub oSub = Newtonsoft.Json.JsonConvert.DeserializeObject<ClsSc_ICMOBillSub>(msg3);
 
 
            if (oSub.HQty <= 0 || oSub.HQty == null)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "数量不能为0或者小于0";
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            if (oSub.HMaterID == 0)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "物料不能为空";
                objJsonResult.data = null;
                return objJsonResult;
            }
 
          
 
            if (oSub.HUnitID == 0)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "计量单位不能为空";
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            //if (oSub.HBomID == 0)
            //{
            //    objJsonResult.code = "0";
            //    objJsonResult.count = 0;
            //    objJsonResult.Message = "BOM不能为空";
            //    objJsonResult.data = null;
            //    return objJsonResult;
            //}
 
            oCN.RunProc($@"Insert into Sc_ICMOBillSub 
                (HInterID,HENTRYID,HQty
                ,HPlanBeginDate,HPlanEndDate
                ,HBeginDate,HEndDate
                ,HMaterID,HUnitID,HRemark,HSourceID,HDeptID,HSTATUS
                ,HBomID,HEntryCusID,HSTOCKINORGID
                ,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney
                ,HAuxQty,HAuxUnit,HWidth,HWeight,HColorRemark,HCusMaterName,HCusModel,HOrderPickRemark) 
                 values({HInterID},{HEntryID},{(oSub.HQty == null ? 0 : oSub.HQty)}
                ,'{(oSub.HPlanBeginDate == null ? DateTime.Now.ToString("yyyy-MM-dd") : oSub.HPlanBeginDate.ToString())}','{(oSub.HPlanEndDate == null ? DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") : oSub.HPlanEndDate.ToString())}'
                ,'{(oSub.HBeginDate == null ? DateTime.Now.ToString("yyyy-MM-dd") : oSub.HBeginDate.ToString())}','{(oSub.HEndDate == null ? DateTime.Now.AddDays(1).ToString("yyyy-MM-dd") : oSub.HEndDate.ToString())}'
                ,{oSub.HMaterID},{oSub.HUnitID},'{oSub.HRemark}',{oSub.HSourceID},{oSub.HDeptID},{oSub.HSTATUS}
                 ,{oSub.HBomID}  ,{oSub.HCusID} ,{oSub.HSTOCKINORGID}
                        ,{oSub.HSourceInterID},{oSub.HSourceEntryID},'{oSub.HSourceBillNo}','{oSub.HSourceBillType}',0,0
                ,{oSub.HAuxQty},{oSub.HAuxUnit},{oSub.HWidth},{oSub.HWeight},'{oSub.HColorRemark}','{oSub.HCusMaterName}','{oSub.HCusModel}','{oSub.HOrderPickRemark}')");
 
            objJsonResult.code = "1";
            objJsonResult.count = 1;
            objJsonResult.Message = null;
            objJsonResult.data = null;
            return objJsonResult;
        }
        #endregion
 
        #region 生产任务单审核/反审核功能
        [Route("Sc_ICMOBill/CheckSc_ICMOReportBill")]
        [HttpGet]
        public object CheckSc_ICMOReportBill(string HInterID,int Type, string user)
        {
            try
            {
                //判断是否有删除权限
                if (!DBUtility.ClsPub.Security_Log("Sc_ICMOBill_Check", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无权限审核!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (string.IsNullOrWhiteSpace(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HInterID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                ClsPub.CurUserName = user;
                oCN.BeginTran();//开始事务
 
                //Type 1 审核  2  反审核
                if (Type == 1)
                {
                    if (!BillOld.CheckBill(int.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "审核失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                {
                    if (BillOld.AbandonCheck(int.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                        DataSet DSet = oCn.RunProcReturn("exec h_p_Sc_ICMOBill_AbandonCheckCtrl " + int.Parse(HInterID), "h_p_Sc_ICMOBill_AbandonCheckCtrl");
                        //if (DBUtility.ClsPub.isInt(DSet.Tables[0].Rows[0]["Hback"]) != 0)
                        //{
                        //    objJsonResult.code = "0";
                        //    objJsonResult.count = 1;
                        //    objJsonResult.Message = "该任务单已下推流转卡,不允许反审核" + DBUtility.ClsPub.isStrNull(DSet.Tables[0].Rows[0]["HBackRemark"]);
                        //    objJsonResult.data = null;
                        //    return objJsonResult;
                        //}
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "审核失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
 
                oCN.Commit();//提交事务
 
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "执行成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
 
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "执行失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产任务单关闭/反关闭功能
        [Route("Sc_ICMOBill/CloseSc_ICMOReportBill")]
        [HttpGet]
        public object CloseSc_ICMOReportBill(string HInterID, int Type, string user)
        {
            try
            {
                //判断是否有删除权限
                if (!DBUtility.ClsPub.Security_Log("Sc_ICMOBill_Close", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无权限关闭!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (string.IsNullOrWhiteSpace(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HInterID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                ClsPub.CurUserName = user;
 
                oCN.BeginTran();//开始事务
 
                //Type 1 关闭  2  反关闭
                if (Type == 1)
                {
                    if (!BillOld.CloseBill(int.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "关闭失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                {
                    if (!BillOld.CancelClose(int.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "反关闭失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
 
                oCN.Commit();//提交事务
 
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "执行成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
 
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "执行失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产任务单作废/反作废功能
        [Route("Sc_ICMOBill/CancellSc_ICMOReportBill")]
        [HttpGet]
        public object CancellSc_ICMOReportBill(string HInterID, int Type, string user)
        {
            try
            {
                //判断是否有删除权限
                if (!DBUtility.ClsPub.Security_Log("Sc_ICMOBill_Delete", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无权限作废!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (string.IsNullOrWhiteSpace(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HInterID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                ClsPub.CurUserName = user;
 
                oCN.BeginTran();//开始事务
 
                //Type 1 作废  2  反作废
                if (Type == 1)
                {
                    if (!BillOld.Cancelltion(int.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "作废失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                else
                {
                    if (!BillOld.AbandonCancelltion(int.Parse(HInterID), ref ClsPub.sExeReturnInfo))
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "反作废失败!原因:" + ClsPub.sExeReturnInfo;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
 
                oCN.Commit();//提交事务
 
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "执行成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
 
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "执行失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
 
        #region 生产任务单删除功能
        [Route("Sc_ICMOBill/DeltetSc_ICMOReportBill")]
        [HttpGet]
        public object DeltetSc_ICMOReportBill(string HInterID, string user)
        {
            try
            {
                string HComputerName = SystemInformation.ComputerName; //设备名称    
 
                //判断是否有删除权限
                if (!DBUtility.ClsPub.Security_Log("Sc_ICMOBill_Drop", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无权限删除!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (string.IsNullOrWhiteSpace(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "HInterID为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                oCN.BeginTran();//开始事务
                ds = oCN.RunProcReturn("select * from Sc_ICMOBillMain where HInterID=" + HInterID, "Sc_ICMOBillMain");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "没有数据,无法删除!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
 
                string sReturn = "";
                if (oSystemParameter.ShowBill(ref sReturn))
                {
                    if (oSystemParameter.omodel.Sc_ICMOBill_DeleterAndMakerMustSame == "Y")
                    {
                        if (ds.Tables[0].Rows[0]["HMaker"].ToString() != user && (user != "admin" && user != "Admin"))
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "只能删除本人的单据!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                }
 
                if (int.Parse(ds.Tables[0].Rows[0]["HBillStatus"].ToString()) > 1)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据不是未审核状态,不允许删除!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
 
               var DataSet = oCN.RunProcReturn("select * from Sc_ProcessExchangeBillMain where HICMOBillNo='" + ds.Tables[0].Rows[0]["HBillNo"].ToString() +"'", "Sc_ProcessExchangeBillMain");
 
                if (DataSet.Tables[0].Rows.Count > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据已下推工序流转卡,不允许删除!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
 
                 DataSet = oCN.RunProcReturn("select * from h_v_IF_ProductInBillList where 源单单号='" + ds.Tables[0].Rows[0]["HBillNo"].ToString() + "'", "h_v_IF_ProductInBillList");
 
                if (DataSet.Tables[0].Rows.Count > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据已下推生产入库单,不允许删除!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
 
                DataSet = oCN.RunProcReturn("select * from h_v_IF_MateOutBillList where 源单单号='" + ds.Tables[0].Rows[0]["HBillNo"].ToString() + "'", "h_v_IF_MateOutBillList");
 
                if (DataSet.Tables[0].Rows.Count > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "单据已下推生产领料单,不允许删除!";
                    objJsonResult.data = null;
                    return objJsonResult; ;
                }
 
                //生产订单删除回填销售订单关联数量
                oCN.RunProc("exec h_p_Xs_UpDateRelation_SeOrderToICMO_Delete " + HInterID);
 
 
                oCN.RunProc("delete from Sc_ICMOBillMain  where HInterID=" + HInterID);
                oCN.RunProc("delete from Sc_ICMOBillSub  where HInterID=" + HInterID);
 
                LogService.Write("用户:" + user + ",日期:" + DateTime.Now + ",删除生产订单单据:" + ds.Tables[0].Rows[0]["HBillNo"].ToString());
                oCN.RunProc("Insert into System_log (GeginDate, userid, WorkstationName, WorkList, SystemName, NetuserName, State) select GETDATE(),'" + user + "','" + HComputerName + "','" + "删除生产订单单据:" + ds.Tables[0].Rows[0]["HBillNo"].ToString() + "','LMES-生产订单模块','" + DBUtility.ClsPub.IPAddress + "','删除单据'", ref DBUtility.ClsPub.sExeReturnInfo);
 
                oCN.Commit();//提交事务
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "* 数据删除成功!";
                objJsonResult.data = null;
                return objJsonResult; ;
 
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产任务单 多行批量下推
        [Route("Sc_ICMOBill/Sc_ICMOReportBill_dh")]
        [HttpGet]
        public object Sc_ICMOReportBill_dh(string HInterID, string user, int OrganizationID)
        {
            try
            {
                //获取单据ID
                string[] HBillNo = HInterID.Split(',');
                string Error = "";
                int i = 0;
                foreach (var item in HBillNo)
                {
                    string err = "";
                    DAL.ClsSc_ProcessExchangeBill oBill = new DAL.ClsSc_ProcessExchangeBill();
                    Model.ClsSc_ProcessExchangeBillMain lsmain = new Model.ClsSc_ProcessExchangeBillMain();
                    List<Model.ClsSc_ProcessExchangeBillSub> ls = new List<Model.ClsSc_ProcessExchangeBillSub>();
                    string HBillNOs = "";
                    string hmainid = item.Split('|')[0];
                    string HEntryID = item.Split('|')[1];
 
                    ds = oCN.RunProcReturn("select * from h_v_IF_ICMOBillList where 1 = 1  and hmainid=" + hmainid + " and  HEntryID=" + HEntryID + " order by 单据号 desc", "h_v_IF_ICMOBillList");
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        err = "无数据! \n";
                    }
                    else
                    {
                        string HNumber = ds.Tables[0].Rows[0]["产品代码"].ToString();
                        HBillNOs = ds.Tables[0].Rows[0]["单据号"].ToString();
 
                        if (ds.Tables[0].Rows[0]["审核人"].ToString() == "")
                        {
                            err = "所选生产订单为未审核状态,不允许下推生成工序流转卡!\n";
                        }
 
                        if (ds.Tables[0].Rows[0]["关闭人"].ToString() != "")
                        {
                            err = "所选生产订单为已关闭状态,不允许下推生成工序流转卡!\n";
                        }
 
                        if (ds.Tables[0].Rows[0]["作废人"].ToString() != "")
                        {
                            err = "所选生产订单为已作废状态,不允许下推生成工序流转卡!\n";
                        }
 
                        ds = oCN.RunProcReturn("select * from h_v_S_Sc_ICMOBillList where hmainid=" + hmainid + " and HEntryID=" + HEntryID + " and HSTOCKINORGID='" + OrganizationID + "'", "h_v_S_Sc_ICMOBillList");
 
                        if (double.Parse(ds.Tables[0].Rows[0]["流转卡数量"].ToString()) == 0)
                        {
                            err = "所选生产订单已全部下推生成工序流转卡,剩余可下推数量为0,不允许下推生成工序流转卡!\n";
                        }
                        lsmain.HMaker = user;  //制单人
                        lsmain.HBillType = "3772";
                        lsmain.HBillSubType = "3772";
                        lsmain.HDate = DBUtility.ClsPub.isDate(DateTime.Now.ToString("yyyy-MM-dd"));
                        lsmain.HYear = DBUtility.ClsPub.isLong(DateTime.Now.Year);
                        lsmain.HPeriod = DBUtility.ClsPub.isLong(DateTime.Now.Month);
                        lsmain.HICMOInterID = long.Parse(ds.Tables[0].Rows[0]["hmainid"].ToString());
                        lsmain.HICMOBillNo = ds.Tables[0].Rows[0]["生产订单号"].ToString();
                        lsmain.HICMOEntryID = long.Parse(ds.Tables[0].Rows[0]["HEntryID"].ToString());
                        lsmain.HOrderProcNO = ds.Tables[0].Rows[0]["订单跟踪号"].ToString();
                        lsmain.HMaterID = long.Parse(ds.Tables[0].Rows[0]["hmaterid"].ToString());
                        lsmain.HMaterID2 = long.Parse(ds.Tables[0].Rows[0]["hmaterid"].ToString());
                        lsmain.HMaterNumber = ds.Tables[0].Rows[0]["物料代码"].ToString();
                        lsmain.HMaterModel = ds.Tables[0].Rows[0]["规格型号"].ToString();
                        lsmain.HBatchNo = ds.Tables[0].Rows[0]["批号"].ToString();
                        lsmain.HSplitNo =1;//都是1
                        lsmain.HUnitID = long.Parse(ds.Tables[0].Rows[0]["hunitid"].ToString());
                        lsmain.HUnitNumber = ds.Tables[0].Rows[0]["计量单位代码"].ToString();
                        lsmain.HPlanQty = double.Parse(ds.Tables[0].Rows[0]["计划生产数量"].ToString());
                        lsmain.HQty = double.Parse(ds.Tables[0].Rows[0]["流转卡数量"].ToString());
                        lsmain.HPlanBeginDate = DateTime.Parse(ds.Tables[0].Rows[0]["计划开工日期"].ToString());
                        lsmain.HPlanEndDate = DateTime.Parse(ds.Tables[0].Rows[0]["计划完工日期"].ToString());
                        lsmain.HWorkShopID = long.Parse(ds.Tables[0].Rows[0]["hdeptid"].ToString());
                        lsmain.HProdMaterCode = ds.Tables[0].Rows[0]["产品CODE"].ToString();
                        lsmain.HSeOrderBillNo = ds.Tables[0].Rows[0]["销售订单号"].ToString();
                        lsmain.HCusShortName = ds.Tables[0].Rows[0]["客户简称"].ToString();
                        lsmain.HCusNeedMaterial = ds.Tables[0].Rows[0]["客户要求材料成分"].ToString();
                        lsmain.HPlanSendGoodsDate = ds.Tables[0].Rows[0]["预计出货日期"].ToString()==""?DateTime.Now.ToString(): ds.Tables[0].Rows[0]["预计出货日期"].ToString();
                        lsmain.HSellDate = DateTime.Now.ToString();
                        lsmain.HPRDORGID = OrganizationID;
                        lsmain.HProdMaterName = ds.Tables[0].Rows[0]["产品名称"].ToString();
                        lsmain.HCusName = ds.Tables[0].Rows[0]["客户名称"].ToString();
                        lsmain.HWorkRemark = ds.Tables[0].Rows[0]["生产备注"].ToString();
                        lsmain.HImportNote = ds.Tables[0].Rows[0]["重要提示"].ToString();
                        lsmain.HPicNumVer = ds.Tables[0].Rows[0]["图号版本"].ToString();
                        lsmain.HPicNumAssemble = ds.Tables[0].Rows[0]["总装图号"].ToString();
                        lsmain.HMaterTexture = ds.Tables[0].Rows[0]["材质"].ToString();
                        lsmain.HProductNum = ds.Tables[0].Rows[0]["成品编号"].ToString();
                        lsmain.HVerNum = ds.Tables[0].Rows[0]["版本"].ToString();
                        //lsmain.HCusNumber = ds.Tables[0].Rows[0]["源单客户编码"].ToString();
                        //lsmain.HPickLabel = ds.Tables[0].Rows[0]["包装标识"].ToString();
                        //lsmain.HPickLabelNumber = ds.Tables[0].Rows[0]["包装标识编码"].ToString();
                        //lsmain.HXTNumber = ds.Tables[0].Rows[0]["芯体物料代码"].ToString();
                        //lsmain.HXTModel = ds.Tables[0].Rows[0]["芯体规格型号"].ToString();
                        lsmain.HWidth = double.Parse(ds.Tables[0].Rows[0]["HWidth"].ToString());
                        lsmain.HWeight = double.Parse(ds.Tables[0].Rows[0]["HWeight"].ToString());
                        lsmain.HAuxUnit = int.Parse(ds.Tables[0].Rows[0]["HAuxUnit"].ToString());
                        lsmain.HRemark2 = ds.Tables[0].Rows[0]["备注"].ToString();
                        lsmain.HEmpID = long.Parse(ds.Tables[0].Rows[0]["HEmpID"].ToString());
                        lsmain.HCusID = long.Parse(ds.Tables[0].Rows[0]["HCusID"].ToString());
                        lsmain.HColorRemark = ds.Tables[0].Rows[0]["染色要求"].ToString();
                        lsmain.HBLFlag = ds.Tables[0].Rows[0]["HBLFlag"].ToString() == "0" ? false : true;
                        lsmain.HAuxQty = 0;
                        lsmain.HAuxUnit = 0;
 
 
                        ds = oCN.RunProcReturn("select top 1000 * from h_v_Gy_RoutingBillList a left join Gy_Process p on a.hprocid = p.HItemID where 物料代码='" + HNumber + "' and 默认工艺=1", "h_v_Gy_RoutingBillList");
 
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            err = "所选生产订单对应物料未设置对应的工艺路线,不允许下推生成工序流转卡!\n";
                        }
                        else
                        {
                            lsmain.HRoutingBillID = ds.Tables[0].Rows[0]["hmainid"].ToString();
                            oBill.omodel = lsmain;
 
                            int j = 0;
                            foreach (DataRow row in ds.Tables[0].Rows)
                            {
                                if (row["HTProcessFlag"].ToString() == "False")
                                {
                                    ClsSc_ProcessExchangeBillSub sub = new ClsSc_ProcessExchangeBillSub();
                                    sub.HEntryID = j + 1;
                                    sub.HEntryCloseDate = DBUtility.ClsPub.isDate(DateTime.Now);
                                    sub.HProcNo = long.Parse(row["工序号"].ToString());
                                    sub.HProcID = long.Parse(row["HProcID"].ToString());
                                    sub.HProcNumber = row["工序代码"].ToString();
                                    sub.HWorkRemark = row["表体备注"].ToString();
                                    sub.HCenterID = long.Parse(row["HCenterID"].ToString());
                                    sub.HSupID = long.Parse(row["HSupID"].ToString());
                                    sub.HSupFlag = row["委外标记"].ToString() == "False" ? false : true;
                                    sub.HQty = lsmain.HQty;
                                    sub.HTechnologyParameter = row["工艺参数"].ToString();
                                    sub.HPicNum = row["图纸编号"].ToString();
                                    sub.HProcCheckNote = row["本工序确认记录"].ToString();
                                    sub.HDeptID = 0;
                                    sub.HDeptNumber = "";
                                    sub.HOutPrice = 0;
                                    sub.HRemark = "";
                                    sub.HRelationQty_In =0;
                                    sub.HRelationQty_Out = 0;
                                    sub.HRelationQty_WWOrder = 0;
                                    sub.HRelationQty_Bad = 0;
                                    sub.HOverRate = 0;
                                    sub.HMaxQty = 0;
                                    sub.HPassRate = 0;
                                    sub.HSumPassRate = 0;
                                    j++;
                                    oBill.DetailColl.Add(sub);
                                }
                            }
                           
                        }
                    }
 
                    bool bResult = false;
                    if (err != "")
                    {
                        Error += "生产订单单据号:" + HBillNOs + "\n" + err;
                    }
                    else
                    {
                        oBill.omodel.HBillNo = DBUtility.ClsPub.CreateBillCode("3772", ref DBUtility.ClsPub.sExeReturnInfo,true);
                        bResult = oBill.AddBill(ref DBUtility.ClsPub.sExeReturnInfo);
                        oBill.DetailColl.Clear();
 
                        if (!bResult)
                        {
                            Error += "保存失败,生产订单单据号:" + HBillNOs + "\n" + err;
                        }
                        else
                        {
                            i++;
                        }
                    }
                }
                Error = "成功:" + i + "行  !!!    " + Error;
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = Error;
                objJsonResult.data = null;
                return objJsonResult; ;
 
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "下推失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产任务单获取车间
        [Route("Sc_ICMOBill/GetHDeptList")]
        [HttpGet]
        public object GetHDeptList(string HOrgID)
        {
            try
            {
                DataSet oDs = new DataSet();
                //==========
                oDs = oCN.RunProcReturn("select HItemID,HName from Gy_Department where HUSEORGID="+ HOrgID, "Gy_Department");
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "获取成功!";
                objJsonResult.data = oDs.Tables[0];
                return objJsonResult; ;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产任务单获取工作中心
        [Route("Sc_ICMOBill/GetHWorkCenterList")]
        [HttpGet]
        public object GetHWorkCenterList()
        {
            try
            {
                DataSet oDs = new DataSet();
                //==========
                oDs = oCN.RunProcReturn("select HItemID,HName from Gy_WorkCenter", "Gy_WorkCenter");
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "获取成功!";
                objJsonResult.data = oDs.Tables[0];
                return objJsonResult; ;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 墙咔装箱单回车事件
        [Route("Sc_ICMOBill/QK_PackingBillByXSBill")]
        [HttpGet]
        public object QK_PackingBillByXSBill(string HBillNo)
        {
            try
            {
                //string sql = string.Format(@"select a.HInterID,a.HBillNo,b.HEntryID,b.HMaterID,m.HNumber HMaterNumber,m.HName HMaterName, b.HUnitID,u.HName HUnitName,b.HQty HPlanQty,b.HQty HSPlanQty,
                //                            a.HCusID, c.HName HCusName,a.HEmpID,e.HName HEmpName,b.HDeptID,d.HName HDeptName,a.HPlanEndDate,0 HMinQty,0 HTotalQty,0 HSpsQty,a.HSeOrderBillNo,a.HRemark
                //                            from Sc_ICMOBillMain a 
                //                            left join Sc_ICMOBillSub b on a.HInterID=b.HInterID
                //                            left join Gy_Material m on b.HMaterID=m.HItemID
                //                            left join Gy_Unit u on b.HUnitID=u.HItemID
                //                            left join Gy_Customer c on a.HCusID=c.HItemID
                //                            left join Gy_Employee e on a.HEmpID=e.HItemID
                //                            left join Gy_Department d on a.HDeptID=d.HItemID
                //                            where b.HInterID=(select HICMOInterID from Sc_PPBomBillMain where HBillNo='" + HBillNo + "')");
                string sql = string.Format(@"select a.HInterID,a.HBillNo,b.HMaterID,b.HEntryID, m.HNumber HMaterNumber, m.HName HMaterName, b.HUnitID,u.HName HUnitName,b.HQty HPlanQty,b.HQty HSPlanQty,a.HCusID, c.HName HCusName,
                    a.HEmpID,e.HName HEmpName,b.HDeptID,d.HName HDeptName,a.HPlanEndDate,0 HMinQty,0 HTotalQty,0 HSpsQty,a.HSeOrderBillNo,a.HRemark,
                     fo.FBILLNO FBillNo,fo.F_ZZZZ_TEXT7 FLXName,fo.FRECEIVEADDRESS FInAddress,fmn.FNAME FMaterName,fo1.F_ZZZZ_TEXT FBZFS,fu.FDATAVALUE FHX,
                    fu1.FDATAVALUE FQK,fo1.F_ZZZZ_WBBZ1 FWBBZ
                    from Sc_ICMOBillMain a 
                    left join Sc_ICMOBillSub b on a.HInterID=b.HInterID
                    left join Gy_Material m on b.HMaterID=m.HItemID
                    left join Gy_Unit u on b.HUnitID=u.HItemID
                    left join Gy_Customer c on a.HCusID=c.HItemID
                    left join Gy_Employee e on a.HEmpID=e.HItemID
                    left join Gy_Department d on a.HDeptID=d.HItemID
                     left join AIS20200908101915zs..T_PRD_MOENTRY f1 on b.HERPEntryID=f1.FENTRYID
                    left join AIS20200908101915zs..T_SAL_ORDERENTRY fo1 on f1.FSaleOrderEntryId=fo1.FENTRYID
                    left join AIS20200908101915zs..T_SAL_ORDER fo on fo1.FID=fo.FID
                    left join AIS20200908101915zs..T_BD_MATERIAL fm1 on fo1.FSUBMATERIALNUMBER=fm1.FMATERIALID
                    left join AIS20200908101915zs..T_BD_MATERIAL_L fmn on fo1.FSUBMATERIALNUMBER=fmn.FMATERIALID
                    LEFT JOIN AIS20200908101915zs..T_BAS_ASSISTANTDATAENTRY_L fu1 on fo1.F_ZZZZ_ASSISTANT1=fu1.FENTRYID
                    LEFT JOIN AIS20200908101915zs..T_BAS_ASSISTANTDATAENTRY_L fu on fo1.F_ZZZZ_ASSISTANT2=fu.FENTRYID
                     where b.HInterID=(select HICMOInterID from Sc_PPBomBillMain where HBillNo='" + HBillNo + "')");
 
                ds = oCN.RunProcReturn(sql, "Sc_ICMOBillMain");
                if (ds.Tables[0].Rows.Count != 0 || ds != null)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "获取成功!";
                    objJsonResult.data = ds.Tables[0];
                    return objJsonResult;
                }
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "暂无对应的销售明细!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "获取失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 墙咔装箱单整箱生成
        [Route("Sc_ICMOBill/QK_PackingBillSavePack")]
        [HttpPost]
        public object QK_PackingBillSavePack([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();//表格数据
            string msg3 = sArray[1].ToString();//登录人
            string msg5 = sArray[2].ToString();//登录组织
            string msg6 = sArray[3].ToString();//标记
            string msg7 = sArray[4].ToString();//当前用料清单
 
            ListModels oListModels = new ListModels();
            DataSet ds = new DataSet();
            ds = oCN.RunProcReturn("select * from Xt_ORGANIZATIONS where HItemID=" + msg5, "Xt_ORGANIZATIONS");
            string OrgNum = ds.Tables[0].Rows[0]["HNumber"].ToString();//组织代码
            DataSet d = oCN.RunProcReturn("select HRemark from Sc_PPBomBillMain where HBillNo='" + msg7 + "'", "Sc_ICMOBillMain");//查找该用料清单上次生成的箱号(HRemark存放箱号)
            DataSet Ds1 = new DataSet();
            try
            {
                //表体数据
                //按 },{来拆分数组 //去掉【和】
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");  //\n
                                                //msg2 = msg2.Replace("'", "’");
                List<Models.ClsQK_PackingBill> ls = new List<Models.ClsQK_PackingBill>();
                ls = oListModels.getObjectByJson_QK_PackingBill(msg2);
 
 
                //获取年月日并拼接成字符串
                string year = DateTime.Now.Year.ToString();
                string month = DateTime.Now.Month.ToString();
                string day = DateTime.Now.Day.ToString();
                string nowDate = year + month + day;
                //string materid = "";
                //long sum = 0;
                //if (msg4 == "ZZ")
                //{
                string FID = "";
                int LSH;
                string LSH2;
                string TM = "";
                int XH = Convert.ToInt32(d.Tables[0].Rows[0]["HRemark"].ToString() == "" ? 0 : Convert.ToInt32(d.Tables[0].Rows[0]["HRemark"].ToString()));
 
                foreach (Models.ClsQK_PackingBill oItemSub in ls)
                {
                    //根据生成条数生成相应数量条码
                    for (int i = 0; i < oItemSub.HTotalQty; i++)
                    {
                        long HInterID = DBUtility.ClsPub.CreateBillID_Prod("85", ref DBUtility.ClsPub.sExeReturnInfo);
                        string sTMNumber = OrgNum + oItemSub.HMaterNumber + nowDate;
                        Ds1 = oCN.RunProcReturn("exec h_p_WMS_GetMaxNo '" + sTMNumber + "'", "h_p_WMS_GetMaxNo");    //获取最大流水号
                        LSH = ClsPub.isInt(Ds1.Tables[0].Rows[0][0]);//
                        LSH = LSH + 1;
                        LSH2 = LSH.ToString();
                        while (LSH2.Length < 6)
                        {
                            LSH2 = "0" + LSH2;
                        }
                        TM = sTMNumber + LSH2;
 
                        if (msg6 == "ZZ")
                        {
                            XH = XH + 1;
                        }
                        else if (msg6 == "PZ" && ls.IndexOf(oItemSub) == 0)
                        {
                            XH = XH + 1;
                        }
 
                        //获取内码
                        oCN.RunProc("insert into Gy_BarCodeBill (HEntryID,HBarCode,HBarCodeType,HMaterID,HUnitID,HQty" +
                                    ",HBatchNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HinitQty" +
                                    ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HEndQty " +
                                    ",HBarcodeQtys,HBarcodeNo,HDeptID,HWhID,HSPID,HRemark " +
                                    ",HCusID,HCusType,HEndDate,HWorkLineName,HBarCodeDate " +
                                    ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID " +
                                    ",HGiveAwayFlag " +
                                    ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo " +
                                    ") values (" + i
                                    + ",'" + TM + "','唯一条码'," + oItemSub.HMaterID.ToString() + "," + oItemSub.HUnitID.ToString() + "," + oItemSub.HMinQty.ToString()
                                    + ",'',0,0,'" + msg3 + "',getdate(),0," + oItemSub.HMinQty.ToString()
                                    + ", " + oItemSub.HInterID.ToString() + "," + oItemSub.HEntryID.ToString() + ",'" + oItemSub.HBillNo + "','3710',''"
                                    + ",1,1," + oItemSub.HDeptID.ToString() + ",0,0,'" + XH + "'"
                                    + ", " + oItemSub.HCusID.ToString() + ",'',getdate(),'',getdate()"
                                    + ", " + msg5.ToString() + "," + OrgNum.ToString() + ",'" + oItemSub.HSeOrderBillNo.ToString() + "'," + HInterID.ToString()
                                    + ",0"
                                    + ",'" + oItemSub.HMaterName + "','','',0,'','')");
 
                        oCN.RunProc("exec h_p_WMS_SetMaxNo '" + sTMNumber + "'");
 
                    }
                    oCN.RunProc("update Sc_ICMOBillSub set HQty=" + oItemSub.HSpsQty + " where HEntryID=" + oItemSub.HEntryID);
                    oCN.RunProc("update Sc_PPBomBillMain set HRemark=" + XH + " where HBillNo='" + msg7 + "'");
                    DataSet Dsn = oCN.RunProcReturn("select top " + oItemSub.HTotalQty + " HItemID from Gy_BarCodeBill order by HItemID desc", "Gy_BarCodeBill");    //获取最大流水号
                    for (int i = 0; i < oItemSub.HTotalQty; i++)
                    {
                        FID = FID + Dsn.Tables[0].Rows[i][0] + ",";
                    }
                }
                objJsonResult.code = FID;
                objJsonResult.count = 1;
                objJsonResult.Message = "整装生成成功!";
                objJsonResult.data = 1;
                return objJsonResult;
                //}
                //else
                //{
                //    var HInterID = DBUtility.ClsPub.CreateBillID("3783", ref DBUtility.ClsPub.sExeReturnInfo);
                //    var HBillNo = DBUtility.ClsPub.CreateBillCode("3783", ref DBUtility.ClsPub.sExeReturnInfo, true);
                //    oCN.BeginTran();
                //    foreach (Models.ClsQK_PackingBill oItemSub in ls)
                //    {
                //        sum += oItemSub.HMinQty;
                //        materid = oItemSub.HMaterNumber;
                //        //获取内码
                //        long HInterID2 = DBUtility.ClsPub.CreateBillID_Prod("85", ref DBUtility.ClsPub.sExeReturnInfo);
                //        //生成唯一条码   条码前缀 = 组织代码 + 物料代码 + 年 + 月 + 日
                //        string sTMNumber = OrgNum + oItemSub.HMaterNumber + nowDate;
                //        Ds1 = oCN.RunProcReturn("exec h_p_WMS_GetMaxNo '" + sTMNumber + "'", "h_p_WMS_GetMaxNo");    //获取最大流水号
                //        LSH = ClsPub.isInt(Ds1.Tables[0].Rows[0][0]);//唯一码
                //        //插入条码档案
                //        oCN.RunProc("insert into Gy_BarCodeBill (HBarCode,HBarCodeType,HMaterID,HUnitID,HQty" +
                //                    ",HBatchNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HinitQty" +
                //                    ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HEndQty " +
                //                    ",HBarcodeQtys,HBarcodeNo,HDeptID,HWhID,HSPID,HRemark " +
                //                    ",HCusID,HCusType,HEndDate,HWorkLineName,HBarCodeDate " +
                //                    ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID " +
                //                    ",HGiveAwayFlag " +
                //                    ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo " +
                //                    ") values ("
                //                    + "'" + LSH + "','唯一条码'," + oItemSub.HMaterID.ToString() + "," + oItemSub.HUnitID.ToString() + "," + oItemSub.HMinQty.ToString()
                //                    + ",'',0,0,'" + msg3 + "',getdate(),0," + oItemSub.HMinQty.ToString()
                //                    + ", " + oItemSub.HInterID.ToString() + "," + oItemSub.HInterID.ToString() + ",'" + oItemSub.HBillNo + "','3710',''"
                //                    + ",1,1," + oItemSub.HDeptID.ToString() + ",0,0,''"
                //                    + ", " + oItemSub.HCusID.ToString() + ",'',getdate(),'',getdate()"
                //                    + ", " + msg5.ToString() + "," + OrgNum.ToString() + ",''," + HInterID2.ToString()
                //                    + ",0"
                //                    + ",'" + oItemSub.HMaterName + "','','',0,'','')");
                //        //插入组托单子表
                //        string sql = string.Format(@"insert into Sc_PackUnionBillSub(HInterID,HEntryID,HCloseMan,HCloseType,HRemark,
                //                                    HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,
                //                                    HRelationQty,HRelationMoney,HMaterID,HUnitID,HQty,HSourceID,HEquipID,HGroupID,HWorkerID,
                //                                 HScanDate,HBarCode,HBarCode_Pack)
                //                                    values(" + HInterID + ",'','',0,'自动绑定'," + oItemSub.HInterID + ",'','" + oItemSub.HBillNo + "','3710'," +
                //                                    "0,0," + oItemSub.HMaterID + "," + oItemSub.HUnitID + "," + oItemSub.HPlanQty + ", 0,0,0,0," +
                //                                    "getdate(),'" + LSH + "','" + HBillNo + "')");
                //        //更改生产订单的数量
                //        string sql1 = string.Format(@"update Sc_ICMOBillSub set HQty=" + oItemSub.HSpsQty + " where HInterID=" + oItemSub.HInterID);
 
                //        oCN.RunProc(sql);
                //        oCN.RunProc(sql1);
                //    }
                //    //生成组托单主表
                //    string sql2 = string.Format(@"Insert Sc_PackUnionBillMain(HYear,HPeriod,HBillType,HInterID,HDate,HBillNo,HBillStatus,HCheckItemNowID,HCheckItemNextID,
                //                                    HRemark,HBacker,HChecker,HMaker,HMakeDate,HUpDater,HCloseMan,HCloseType,HDeleteMan,HICMOInterID,HICMOBillNo,
                //                                    HBarCode_Pack,HMaterID,HUnitID,HWeight,HMWeight,HPWeight,
                //                                    HProdOrgID,HDeptID,HEmpID,HSNum,HPackNum,HBarCode_Cus,HBatchNo,HBillSubType)
                //                                    values('2022',1,'3783'," + HInterID + ",getdate(),'" + HBillNo + "',1,0,0," +
                //                                        "'自动绑定','','', '" + msg3 + "',getdate(),'','',0,'',0,0,'" +
                //                                         HBillNo + "',0,0,0,0,0," +
                //                                        "0,0,0,0,0,'','','')");
                //    oCN.RunProc(sql2);
                //    //再次生成唯一码
                //    //获取内码
                //    long HInterID3 = DBUtility.ClsPub.CreateBillID_Prod("85", ref DBUtility.ClsPub.sExeReturnInfo);
                //    //生成唯一条码   条码前缀 = 组织代码 + 物料代码 + 年 + 月 + 日
                //    string sTMNumber1 = OrgNum + materid + nowDate;
                //    Ds1 = oCN.RunProcReturn("exec h_p_WMS_GetMaxNo '" + sTMNumber1 + "'", "h_p_WMS_GetMaxNo");    //获取最大流水号
                //    int LSH1 = ClsPub.isInt(Ds1.Tables[0].Rows[0][0]);//唯一码
                //    string sql3 = string.Format(@"insert into Gy_BarCodeBill (HBarCode,HBarCodeType,HMaterID,HUnitID,HQty" +
                //                ",HBatchNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HinitQty" +
                //                ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HEndQty " +
                //                ",HBarcodeQtys,HBarcodeNo,HDeptID,HWhID,HSPID,HRemark " +
                //                ",HCusID,HCusType,HEndDate,HWorkLineName,HBarCodeDate " +
                //                ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID " +
                //                ",HGiveAwayFlag " +
                //                ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo " +
                //                ") values ("
                //                + "'" + LSH1 + "','唯一条码','" + materid.ToString() + "',0," + sum.ToString()
                //                + ",'',0,0,'" + msg3 + "',getdate(),0," + sum.ToString()
                //                + ", 0,0,'" + HBillNo + "','3710','',"
                //                + sum.ToString() + "," + HInterID.ToString() + ",0,0,0,''"
                //                + ",0,'',getdate(),'',getdate()"
                //                + ", " + msg5.ToString() + "," + OrgNum.ToString() + ",''," + HInterID3.ToString()
                //                + ",0"
                //                + ",'','','',0,'','')");
 
                //    oCN.RunProc(sql3);
                //    oCN.Commit();
                //    objJsonResult.code = "1";
                //    objJsonResult.count = 1;
                //    objJsonResult.Message = "拼装生成成功";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
 
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "获取失败!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        # region[墙咔装箱点击主表带出从表]
        [Route("Sc_ICMOBillController/QK_GetPackingBillListByMainID")]
        [HttpGet]
        public object QK_GetPackingBillListByMainID(string HInterID)
        {
 
            DataSet ds;
            try
            {
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                //获取保养项目编辑数据
                string sql = string.Format(@"select a.HInterID hmainid,a.HBarCode,a.HBillType,a.HBarCodeType,
                                     a.HMaterID,m.HName HMaterName,a.HUnitID,u.HName HUnitName,
                                     a.HEmpID,e.HName HEmpName,a.HDeptID,d.HItemID HDeptName,P.HBarCode_Pack,
                                    a.HMakeDate 制单日期,a.HMaker 制单人
                                    from Gy_BarCodeBill a 
                                    left join Gy_Material m on a.HMaterID=m.HItemID
                                    left join Gy_Unit u on a.HUnitID=u.HItemID 
                                    left join Gy_Employee e on a.HEmpID=e.HItemID 
                                    left join Gy_Department d on a.HDeptID=d.HItemID
                                    left join Sc_PackUnionBillMain p on a.HSourceInterID=p.HInterID
                                    where a.HBarCode in(select HBarCode from  Sc_PackUnionBillSub where HInterID=
                                    (select HBarcodeNo from Gy_BarCodeBill where HItemID='" + HInterID + "'))");
                ds = oCN.RunProcReturn(sql, "Sc_PackUnionBillSub");
 
                objJsonResult.code = "0";
                objJsonResult.count = 1;
                objJsonResult.Message = "获取信息成功!";
                objJsonResult.data = ds.Tables[0];
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + e.ToString();
                objJsonResult.data = null;
            }
            return objJsonResult;
        }
        #endregion
 
        #region[墙咔装箱列表]
        /// <summary>
        /// 墙咔装箱列表
        /// </summary>
        /// <returns></returns>
        [Route("Sc_ICMOBillController/QK_GetPackingBillList")]
        [HttpGet]
        public object QK_GetPackingBillList(string sWhere)
        {
            try
            {
 
                ds = QK_GetPackingBillList_s(sWhere);
 
                //if (ds.Tables[0].Rows.Count != 0 || ds != null)
                //{
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
                //}
                //else
                //{
                //objJsonResult.code = "0";
                //objJsonResult.count = 0;
                //objJsonResult.Message = "无数据";
                //objJsonResult.data = null;
                //return objJsonResult;
                //}
            }
            catch (Exception ex)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "没有返回任何记录!" + ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
 
        #region sql语句
 
 
        public static DataSet QK_GetPackingBillList_s(string sWhere)
        {
            ;
            if (sWhere == null || sWhere.Equals(""))
            {
                return new SQLHelper.ClsCN().RunProcReturn("select *  from h_v_QK_PackedBillList order by 制单日期 desc", "h_v_QK_PackedBillList");
            }
            else
            {
                string sql1 = "select * from h_v_QK_PackedBillList where HBarCodeType='唯一条码' ";
                string sql = sql1 + sWhere + " order by 制单日期 desc";
                return new SQLHelper.ClsCN().RunProcReturn(sql, "h_v_QK_PackedBillList");
 
            }
            //return new SQLHelper.ClsCN().RunProcReturn("select * from h_v_Sc_MouldMaintainBillList ", "h_v_Sc_MouldMaintainBillList");
        }
        #endregion
 
        #endregion
 
        #region[墙咔装箱列表-删除]
        /// <summary>
        /// 墙咔装箱列表-删除
        /// </summary>
        /// <param name="HInterID">条码档案主表主ID(唯一)</param>
        /// <param name="Flag">标识</param>
        /// <returns></returns>
        [Route("Sc_ICMOBillController/DeleteQK_GetPackingBillList")]
        [HttpGet]
        public object DeleteQK_GetPackingBillList(string HInterID, string Flag)
        {
            try
            {
                oCN.BeginTran();
                if (Flag == "0")
                {
                    DataSet ds = new DataSet();
                    ds = oCN.RunProcReturn("select * from Gy_BarCodeBill where HItemID=" + HInterID, "Gy_BarCodeBill");
                    DataRow dr = ds.Tables[0].Rows[0];
                    string sql = string.Format(@"delete from Gy_BarCodeBill where HItemID=" + HInterID);
                    string sql1 = string.Format(@"update Sc_ICMOBillSub set HQty=HQty+" + dr["HQty"] + " where HEntryID=(select HSourceEntryID from Gy_BarCodeBill where HItemID=" + HInterID + ")");
                    oCN.RunProc(sql1);
                    oCN.RunProc(sql);
                }
                else
                {
                    //通过条码id找到托条码,通过托条码找到组托单子表中的唯一码
                    string sql2 = "select HBarCode from Sc_PackUnionBillSub where HInterID=(select HBarcodeNo from Gy_BarCodeBill where HItemID=" + HInterID + ")";
                    //string sql2 = "select HBarCode from Sc_PackUnionBillSub where HInterID=(select HBarcodeNo from Gy_BarCodeBill where HItemID=3250)";
                    DataSet ds1 = oCN.RunProcReturn(sql2, "Sc_PackUnionBillSub");
                    DataTable dt = ds1.Tables[0];
                    //遍历找到的唯一码删除条码档案里的相关数据,同时通过唯一码的源单找到生产订单更改生产订单的数量
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            DataSet ds = new DataSet();
                            ds = oCN.RunProcReturn("select * from Gy_BarCodeBill where HBarCode='" + dr["HBarCode"].ToString() + "'", "Gy_BarCodeBill");
                            DataRow dr1 = ds.Tables[0].Rows[0];
 
                            string sql = string.Format(@"delete from Gy_BarCodeBill where HBarCode='" + dr["HBarCode"].ToString() + "'");
                            string sql1 = string.Format(@"update Sc_ICMOBillSub set HQty=HQty+" + dr1["HQty"] + " where HEntryID=(select HSourceEntryID from Gy_BarCodeBill where HBarCode='" + dr["HBarCode"].ToString() + "')");
                            oCN.RunProc(sql1);
                            oCN.RunProc(sql);
                        }
                    }
 
                    //遍历完后删除通过条码id找到托条码,通过托条码找到组托单子表的数据,以及主表数据
                    string sql3 = string.Format(@"delect from Sc_PackUnionBillSub where HInterID=(select HBarcodeNo from Gy_BarCodeBill where HItemID=" + HInterID);
                    string sql4 = string.Format(@"delect from Sc_PackUnionBillMain where HInterID=(select HBarcodeNo from Gy_BarCodeBill where HItemID=" + HInterID);
                    //删除最大的合成的唯一码
                    string sql5 = string.Format(@"delete from Gy_BarCodeBill where HItemID=" + HInterID);
                    oCN.RunProc(sql3);
                    oCN.RunProc(sql4);
                    oCN.RunProc(sql5);
                }
                oCN.Commit();
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "删除成功";
                objJsonResult.data = null;
                return objJsonResult;
 
            }
            catch (Exception ex)
            {
                oCN.RollBack();
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "删除失败" + ex.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
 
        #region [同步单据]
        [Route("Sc_ICMOBill/Sc_ICMOBillViewApi")]
        [HttpGet]
        public json Sc_ICMOBillViewApi(string BillNo, string BillType)
        {
            string sql = string.Empty;
            string sReturn = "";
            if (oSystemParameter.ShowBill(ref sReturn) == true)
            {
                //系统参数是否为私有云模式,N为公有云模式,Y为私有云模式
                if (oSystemParameter.omodel.WMS_CloudMode == "Y")
                {
                    #region [私有云模式,直接调用数据库存储过程更新]
                    try
                    {
                        oCN.BeginTran();
                        SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                        DataSet DS = oCn.RunProcReturn("exec h_p_WMS_ERPSourceBillToLocal '" + BillNo + "','" + BillType + "'", "h_p_WMS_ERPSourceBillToLocal");
                        if (DS == null)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "单据同步失败";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        else
                        {
                            if (DBUtility.ClsPub.isStrNull(DS.Tables[0].Rows[0]["HBack"]) == "2")
                            {
                                objJsonResult.code = "0";
                                objJsonResult.count = 0;
                                objJsonResult.Message = "ERP中不存在该单据号";
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                            else
                            {
                                objJsonResult.code = "1";
                                objJsonResult.count = 1;
                                objJsonResult.Message = "单据同步成功";
                                objJsonResult.data = null;
                                return objJsonResult;
                            }
                        }
 
                    }
                    catch (Exception e)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "Exception!" + e.ToString();
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    #endregion
                }
                else
                {
                    #region [公有云模式,调用WEBAPI的方式进行更新]
                    var json = new
                    {
                        CreateOrgId = 0,
                        Number = BillNo,
                        Id = ""
                    };
                    #region [金蝶部分]
                    //登录金蝶
                    var loginRet = InvokeHelper.Login();
                    var isSuccess = JObject.Parse(loginRet)["LoginResultType"].Value<int>();
                    //判断是否登录成功
                    if (isSuccess < 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = loginRet;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //查看 获取数据                    
                    var _result = InvokeHelper.View("PRD_MO", JsonConvert.SerializeObject(json));
                    var _saveObj = JObject.Parse(_result);
                    //判断数据是否获取成功
                    if (_saveObj["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() != "TRUE")
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "金蝶生产任务单同步失败jsonRoot:" + _result;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    #endregion
                    //获取主表数据
                    DataSet Ds;
                    Int64 InterID = 0;
                    Ds = oCN.RunProcReturn("select * from Sc_ICMOBillMain where HBillNo = '" + BillNo + "'", "Sc_ICMOBillMain");
                    if (Ds.Tables[0].Rows.Count != 0 && ClsPub.isLong(Ds.Tables[0].Rows[0]["HInterID"].ToString()) != 0)
                    {
                        InterID = ClsPub.isLong(Ds.Tables[0].Rows[0]["HInterID"].ToString());
                    }
 
                    #region [主表数据赋值]
                    var jsonData = new
                    {
                        HInterID = _saveObj["Result"]["Result"]["Id"],
                        HYear = DateTime.Now.Year,
                        HPeriod = DateTime.Now.Month,
                        HBillType = 3710,
                        HDate = _saveObj["Result"]["Result"]["Date"],
                        HBillNo = _saveObj["Result"]["Result"]["BillNo"],
                        HBillStatus = _saveObj["Result"]["Result"]["ApproverId_Id"].ToString() == Convert.ToString(0) ? 1 : 2,
                        HEMPID = 0,
                        HRoutingInterID = 0,
                        HPlanQty = 1,
                        HRelationQty = 0,
                        HPlanBeginDate = _saveObj["Result"]["Result"]["Date"],
                        HPlanEndDate = DateTime.Now.ToString(),
                        HBeginDate = _saveObj["Result"]["Result"]["Date"],
                        HEndDate = DateTime.Now.ToString(),
                        HMaterID = 0,
                        HUnitID = 0,
                        HBomID = 0,
                        HBatchNo = "",
                        HSourceInterID = 0,
                        HSourceEntryID = 0,
                        HSourceBillNo = "",
                        HSourceBillType = "",
                        HSeOrderInterID = 0,
                        HSeOrderEntryID = 0,
                        HSeOrderBillNo = "",
                        HPRDORGID = _saveObj["Result"]["Result"]["PrdOrgId_Id"],
                        HENTRUSTORGID = _saveObj["Result"]["Result"]["ENTrustOrgId_Id"],
                        HOWNERID = _saveObj["Result"]["Result"]["OwnerId_Id"],
                        HOWNERTYPEID = _saveObj["Result"]["Result"]["OwnerTypeId"],
                        HCusID = 0,
                        HDeptID = _saveObj["Result"]["Result"]["WorkShopID_Id"].ToString() == null ? 0 : _saveObj["Result"]["Result"]["WorkShopID_Id"],
                        HRemark = "CLOUD导入",
                        HMTONo = "",
                        HERPInterID = _saveObj["Result"]["Result"]["Id"],
                        HERPBillType = _saveObj["Result"]["Result"]["BillType_Id"],
                        HMaker = _saveObj["Result"]["Result"]["CreatorId"]["Name"],
                        HMakeDate = _saveObj["Result"]["Result"]["CreateDate"],
                        HChecker = _saveObj["Result"]["Result"]["ApproverId"]["Name"],
                        HCheckDate = _saveObj["Result"]["Result"]["ApproveDate"],
                        HBillSubType = _saveObj["Result"]["Result"]["IsRework"] = true ? 1 : 0,
                        HISENTRUST = _saveObj["Result"]["Result"]["IsEntrust"],
                        HISREWORK = _saveObj["Result"]["Result"]["IsRework"]                       
                    };
                    #endregion
                    // 删除主表对应数据
                    sql = $"delete from Sc_ICMOBillMain where HInterID = " + InterID;
                    oCN.RunProc(sql);
 
                    //插入主表
                    sql = $@"
                insert into Sc_ICMOBillMain
                (HInterID,HYear,HPeriod,HBillType,HDate
                ,HBillNo,HBillStatus,HEMPID,HRoutingInterID
                ,HPlanQty,HRelationQty,HPlanBeginDate,HPlanEndDate,HBeginDate,HEndDate
                ,HMaterID,HUnitID,HBomID,HBatchNo
                ,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HSeOrderInterID,HSeOrderEntryID,HSeOrderBillNo
                ,HPRDORGID,HENTRUSTORGID,HOWNERID,HOWNERTYPEID
                ,HCusID,HDeptID,HRemark,HMTONo,HERPInterID,HERPBillType
                ,HMaker,HMakeDate,HChecker,HCheckDate
                ,HBillSubType,HISENTRUST,HISREWORK
                 )
                values
                ({jsonData.HInterID},{jsonData.HYear},{jsonData.HPeriod},{jsonData.HBillType},'{jsonData.HDate}','{jsonData.HBillNo}',
                {jsonData.HBillStatus},{jsonData.HEMPID},{jsonData.HRoutingInterID},{jsonData.HPlanQty},{jsonData.HRelationQty},
                '{jsonData.HPlanBeginDate}', '{jsonData.HPlanEndDate}','{jsonData.HBeginDate}','{jsonData.HEndDate}',{jsonData.HMaterID},
                {jsonData.HUnitID},{jsonData.HBomID},'{jsonData.HBatchNo}',{jsonData.HSourceInterID},{jsonData.HSourceEntryID},
                '{jsonData.HSourceBillNo}','{jsonData.HSourceBillType}',{jsonData.HSeOrderInterID}, {jsonData.HSeOrderEntryID},'{jsonData.HSeOrderBillNo}',
                {jsonData.HPRDORGID},{jsonData.HENTRUSTORGID},{jsonData.HOWNERID},'{jsonData.HOWNERTYPEID}',{jsonData.HCusID},{jsonData.HDeptID},
                '{jsonData.HRemark}','{jsonData.HMTONo}',{jsonData.HERPInterID},'{jsonData.HERPBillType}','{jsonData.HMaker}','{jsonData.HMakeDate}',
                '{jsonData.HChecker}','{jsonData.HCheckDate}','{jsonData.HBillSubType}','{jsonData.HISENTRUST}','{jsonData.HISREWORK}')";
 
                    oCN.RunProc(sql);
 
                    #region [申请子表变量]
                    var dataArr = _saveObj["Result"]["Result"]["TreeEntity"];
 
                    DataSet Cs;                   
                    double RelationQty = 0;                  
                    #endregion
                    int i = 0;
 
                    // 获取子表数据
                    Cs = oCN.RunProcReturn("select * from Sc_ICMOBillSub where HInterID = " + InterID, "Sc_ICMOBillSub");
                    // 删除子表对应数据
                    sql = $"delete from Sc_ICMOBillSub where HInterID = " + InterID;
                    oCN.RunProc(sql);
 
                    foreach (var oSub in dataArr)
                    {
                        #region [子表数据赋值]
 
                        if (Cs.Tables[0].Rows.Count != 0 && ClsPub.isLong(Cs.Tables[0].Rows[0]["HInterID"].ToString()) != 0)
                        {                           
                            RelationQty = ClsPub.isDoule(Cs.Tables[0].Rows[i]["HRelationQty"].ToString());                          
 
                            i++;
                        }
 
                        var subData = new
                        {
                            HInterID = _saveObj["Result"]["Result"]["Id"],
                            HENTRYID = oSub["Id"],
                            HSEQ = oSub["Seq"],
                            HQty = oSub["Qty"],
                            HQTYMUST = oSub["Qty"],
                            HRelationQty = RelationQty,
                            HRelationMoney = 0,
                            HPlanBeginDate = oSub["IdPlanStartDate"],
                            HPlanEndDate = oSub["PlanFinishDate"],
                            HBeginDate = oSub["StartDate"],
                            HEndDate = oSub["FinishDate"],
                            HBomID = oSub["BomId_Id"],
                            HRemark = "CLOUD导入",
                            HMaterID = oSub["MaterialId_Id"],
                            HUnitID = oSub["UnitId_Id"],
                            HWHID = oSub["StockId_Id"],
                            HSPID = oSub["StockLocId_Id"],
                            HPROCID = oSub["ProcessId_Id"],
                            HDEPTID = oSub["WorkShopID_Id"],
                            HBatchNo = oSub["Lot_Text"],
                            HSourceInterID = oSub["SrcBillId"],
                            HSourceEntryID = oSub["SrcBillEntryId"],
                            HSourceBillNo = oSub["SrcBillNo"],
                            HSourceBillType = oSub["SrcBillType"],
                            HSeOrderInterID = oSub["SaleOrderId"],
                            HSeOrderEntryID = oSub["SaleOrderEntryId"],
                            HSeOrderBillNo = oSub["SaleOrderNo"],
                            HSTOCKINORGID = oSub["StockInOrgId_Id"],
                            HINSTOCKOWNERID = oSub["InStockOwnerId_Id"],
                            HINSTOCKOWNERTYPEID = oSub["InStockOwnerTypeId"],
                            HREQUESTORGID = oSub["RequestOrgId_Id"],
                            HPlanMode = 0,
                            HMTONo = oSub["MTONo"],
                            HERPInterID = _saveObj["Result"]["Result"]["Id"],
                            HERPEntryID = oSub["Id"],
                            HSTATUS = oSub["Status"],
                            HEntryCusID = 0,
                            HICMOReportRelationQty = oSub["RepQuaSelAuxQty"],
                            HAuxPropID = oSub["AuxPropId_Id"],
                            HProdMaterCode = "",
                            HCusShortName = "",
                            HCusNeedMaterial = "",
                            HPlanSendGoodsDate = "",
                            HProdMaterName = "",
                            HWorkRemark = "",
                            HImportNote = "",
                            HCusName = "",
                            HInstockQty_Max = 0,
                            HInstockQty_Min = 0,
                            HPickLabel = "",
                            HPickLabelNumber = oSub["StockInLimitH"],
                            HCusNumber = oSub["StockInLimitL"],
                            HINSTOCKTYPE = oSub["InStockType"],
                            HCHECKPRODUCT = oSub["CheckProduct"],
                            HQAIP = oSub["QAIP"],
                            HISBACKFLUSH = oSub["ISBACKFLUSH"],
                            HREQSRC = oSub["ReqSrc"],
                            HSTOCKINQUASELAUXQTY = oSub["StockInQuaSelAuxQty"],
                            HSeOrderEntrySEQ = oSub["SaleOrderEntrySeq"],
                            HPROJECTNO = oSub["ProjectNo"],
                            HPRODUCTTYPE = oSub["ProductType"],
                            HCOSTRATE = oSub["CostRate"],
                            HBASEUNITID = oSub["BaseUnitId_Id"],
                        };
                        #endregion                       
 
                        //插入子表
                        sql = $@"
                 insert into Sc_ICMOBillSub
                 (HInterID,HENTRYID,HSEQ,HQty,HQTYMUST,HRelationQty,HRelationMoney
                ,HPlanBeginDate,HPlanEndDate,HBeginDate,HEndDate,HBomID,HRemark
                ,HMaterID,HUnitID,HWHID,HSPID,HPROCID,HDEPTID,HBatchNo
                ,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType
                ,HSeOrderInterID,HSeOrderEntryID,HSeOrderBillNo
                ,HSTOCKINORGID,HINSTOCKOWNERID,HINSTOCKOWNERTYPEID,HREQUESTORGID
                ,HPlanMode,HMTONo,HERPInterID,HERPEntryID,HSTATUS
                ,HEntryCusID,HICMOReportRelationQty,HAuxPropID,HProdMaterCode,HCusShortName
                ,HCusNeedMaterial,HPlanSendGoodsDate,HProdMaterName,HWorkRemark,HImportNote,HCusName
                ,HInstockQty_Max,HInstockQty_Min,HPickLabel,HPickLabelNumber,HCusNumber
                ,HINSTOCKTYPE,HCHECKPRODUCT,HQAIP,HISBACKFLUSH,HREQSRC,HSTOCKINQUASELAUXQTY
                ,HSeOrderEntrySEQ,HPROJECTNO,HPRODUCTTYPE,HCOSTRATE,HBASEUNITID
                 )
                 values
                 ({subData.HInterID},{subData.HENTRYID},{subData.HSEQ},{subData.HQty},{subData.HQTYMUST},{subData.HRelationQty},
                  {subData.HRelationMoney},'{subData.HPlanBeginDate}','{subData.HPlanEndDate}','{subData.HBeginDate}','{subData.HEndDate}',{subData.HBomID},
                  '{subData.HRemark}',{subData.HMaterID},{subData.HUnitID},{subData.HWHID},{subData.HSPID},{subData.HPROCID},{subData.HDEPTID},
                  '{subData.HBatchNo}',{subData.HSourceInterID},{subData.HSourceEntryID},'{subData.HSourceBillNo}','{subData.HSourceBillType}',
                  {subData.HSeOrderInterID},{subData.HSeOrderEntryID},'{subData.HSeOrderBillNo}',{subData.HSTOCKINORGID},{subData.HINSTOCKOWNERID},
                  '{subData.HINSTOCKOWNERTYPEID}',{subData.HREQUESTORGID},'{subData.HPlanMode}','{subData.HMTONo}',{subData.HERPInterID},
                  {subData.HERPEntryID},{subData.HSTATUS},{subData.HEntryCusID},{subData.HICMOReportRelationQty},{subData.HAuxPropID},                  
                  '{subData.HProdMaterCode}','{subData.HCusShortName}','{subData.HCusNeedMaterial}','{subData.HPlanSendGoodsDate}',
                  '{subData.HProdMaterName}','{subData.HWorkRemark}','{subData.HImportNote}','{subData.HCusName}',{subData.HInstockQty_Max},
                  {subData.HInstockQty_Min},'{subData.HPickLabel}',{subData.HPickLabelNumber},{subData.HCusNumber},'{subData.HINSTOCKTYPE}',
                  '{subData.HCHECKPRODUCT}','{subData.HQAIP}','{subData.HISBACKFLUSH}','{subData.HREQSRC}',{subData.HSTOCKINQUASELAUXQTY},
                  {subData.HSeOrderEntrySEQ},'{subData.HPROJECTNO}','{subData.HPRODUCTTYPE}',{subData.HCOSTRATE},{subData.HBASEUNITID})";
 
                        oCN.RunProc(sql);
                    }
 
 
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "单据同步成功!";
                    objJsonResult.data = null;
                    return objJsonResult;
 
                    #endregion
                }
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "单据读取失败!";
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
 
        #region 生产任务单订单包装备注 保存功能
        [Route("Sc_ICMOBill/ICMOBillSaveRemark")]
        [HttpGet]
        public object ICMOBillSaveRemark(string HInterID,string HEntryID, string HOrderPickRemark)
        {
            try
            {
                if (string.IsNullOrEmpty(HInterID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "Exception!HInterID不能为空";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
 
                oCN.BeginTran();
                 oCN.RunProc("update Sc_ICMOBillSub set HOrderPickRemark = '" + HOrderPickRemark + "' where HInterID = " + HInterID + " and  HEntryID = " + HEntryID + "");
                oCN.Commit();
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
    }
}