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
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 WebAPI.Models;
using Newtonsoft.Json;
using DBUtility;
 
namespace WebAPI.Controllers.SCGL
{
    public class Sc_ComplementGoodBillController : ApiController
    {
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        public WebServer webserver = new WebServer();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        public ClsXt_BaseBillMain omodel = new ClsXt_BaseBillMain();
        public JIT_CallGoodsBillSub oSub = new JIT_CallGoodsBillSub();
 
        #region 生产叫料平台列表
 
        //创建临时类 查询条件子段
        public class ComplementGoodBill
        {
            public string HSeOrderBillNo { get; set; }//销售订单
            public string DepartmentName { get; set; }//生产车间
            public string Organization { get; set; }//组织
            public string MaterialNumber { get; set; }//物料代码
            public string MaterialName { get; set; }//物料名称
            public string MaterialModel { get; set; }//规格型号
            public string CPNumber { get; set; }//产品代码
            public string CPName { get; set; }//产品名称
            public string CPModel { get; set; }//规格型号
            public string HICMOBillNo { get; set; }//生产订单号
            public DateTime HBeginDate { get; set; }//开始时间
            public DateTime HEndDate { get; set; }//结束时间
            public int ps { get; set; }//仅显示未完全配送
            public string user { get; set; }//当前登录人
            public string Arbitrarily { get; set; }//任意参数
        }
 
        [Route("Sc_ComplementGoodBill/ComplementGoodBillList")]
        [HttpGet]
        public object ComplementGoodBillList(string sWhere, string user)
        {
            try
            {
                //查看权限
                if (!DBUtility.ClsPub.Security_Log("JIT_ComplementGoodBillMain_Query", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无查看权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                if (sWhere == null || sWhere.Equals(""))
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = new DataTable();
                    return objJsonResult;
                }
                //反序列化传递的值
                ComplementGoodBill com = JsonConvert.DeserializeObject<ComplementGoodBill>(sWhere.ToString());
 
                ds = oCN.RunProcReturn($"exec h_p_JIT_CallGoodsPlatForm_Query '{com.HSeOrderBillNo}','{com.DepartmentName}','{com.MaterialNumber}','{com.MaterialName}'," +
                    $"'{com.MaterialModel}','{com.HICMOBillNo}','{com.CPNumber}','{com.CPName}','{com.CPModel}'," +
                    $"{com.ps},{com.Organization},'{com.user}' ", "h_p_JIT_CallGoodsPlatForm_Query");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产叫料平台  叫料新增
 
        //临时表  叫料单据字段
        public class CallGoodsBill
        {
            public static readonly string ModName = "4601";
            public long HSouceInterID = 0;//HSourceInterIDCol
            public long HSourceEntryID = 0;//HSourceEntryIDCol
            public int? 叫料数量 = 0;//HBHGQtyCol
            public int? 配套数量 = 0;
            public int? 已叫料数量 = 0;//HBHGQtyCol
            public int? 已配送数量 = 0;//HBHGQtyCol
            public double? 退料数量 = 0;//退料数量
            public long HSTOCKID = 0;//HSTOCKID
            public string 退料原因 { get; set; }//退料原因
            public int HMaterialID = 0; //HMaterialIDCol
            public string 物料代码 = ""; 
            public string HSourceBillNo { get; set; }//HSourceBillNoCol
            public int 调出仓库可用库存数量 = 0;//HKFQtyKYCol
            public int 调出仓库库存数量 = 0;//HKFQtySCol
            public double 计划发料数量 = 0;//HPlanQtyCol 
            public int HWHID = 0;//HWHIDCol
            public int? 调出仓库id = 0;//调出仓库id
            public int? 调入仓库id = 0;//调入仓库id
            public string HMaterialNumberCol { get; set; }
            public string HMaterialModelCol { get; set; }
            public string HProcID { get; set; }//HProcIDCol
            public string HProcNameCol { get; set; }//工段
            public int HOutOrgID = 0;//HOutOrgID
            public string HICMOBillNo { get; set; }// HICMOBillNoCol
            public long HICMOInterID = 0;// HICMOInterIDCol
            public long HICMOEntryID = 0;// HICMOEntryIDCol
            public long HDeptID = 0;//HDeptID
            public long 库存数量 = 0;
            public double HBHGQtyCol = 0;
            public string 销售订单号 { get; set; }//HSeOrderNOCol
            public DateTime? 计划开工日期 = null;//HBPlanDateCol
        }
 
        //临时表  叫料子表
        public class JIT_CallGoodsBillSub: ClsXt_BaseBillSub
        {
            public double HQty;
            public long HMaterID;
            public string HMaterNumber;
            public string HProcName;
            public string HModel;
            public string HProcID;
            public string HICMOBillNo;
            public string HSeOrderNOCol;
            public string HSeOrderNo;
            public string HSeOrderBillNo;
            public int HWHID = 0;
            public int HSCWHID = 0;
            public long HOutOrgID = 0;
            public long HICMOInterID = 0;
            public long HICMOEntryID = 0;
            public long HSeOrderInterID = 0;
            public long HSeOrderEntryID = 0;
            public long HDeptID = 0;
            public double HPlanQty = 0;
            public int HSPID = 0;
            public DateTime? HBPlanDate = null;
        }
 
        [Route("Sc_ComplementGoodBill/InsertCallGoodsBill")]
        [HttpPost]
        public object InsertCallGoodsBill([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string[] sArray = _value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
            omodel.HPRDORGID = long.Parse(sArray[2].ToString());
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Cj_CallGoodsBill_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                List<CallGoodsBill> listCa = new List<CallGoodsBill>();
                //获取表格数据集合
                listCa = JsonConvert.DeserializeObject<List<CallGoodsBill>>(msg2.ToString());
 
                int j = 0;
                var flag = false;
                var txt = "";
 
                for (int i = 0; i < listCa.Count; i++)
                {
                    if (listCa[i].HMaterialID != 0 && listCa[i].叫料数量 > 0)
                    {
                        var Hqty = 0.0;
                        var TuiHqty = 0.0;
                        //当前单据已叫料 和 已退料数量
                        ds = oCN.RunProcReturn($@"
                                               select isnull(a.HQty,0) 已叫料数量,isnull(b.HQty,0) 已退料申请数量 from (
                                                select sum(b.HQty) HQty,c.HNumber ,b.HSourceBillNo
                                                from JIT_CallGoodsBillMain a
                                                join JIT_CallGoodsBillSub b on a.HInterID=b.HInterID
                                                left join Gy_Material c on b.HMaterID=c.HItemID
                                                where b.HSourceBillNo='{listCa[i].HSourceBillNo}'
                                                group by c.HNumber,b.HSourceBillNo
                                                ) a
                                                left join (
                                                select sum(b.HQty) HQty,c.HNumber,b.HSourceBillNo
                                                from JIT_CallGoodsBackRequestBillMain a
                                                inner join JIT_CallGoodsBackRequestBillSub b on a.HInterID=b.HInterID
                                                left join Gy_Material c on b.HMaterID=c.HItemID
                                                where b.HSourceBillNo='{listCa[i].HSourceBillNo}'
                                                group by c.HNumber,b.HSourceBillNo
                                                ) b  on a.HNumber=b.HNumber and a.HSourceBillNo=b.HSourceBillNo where a.HNumber='{listCa[i].物料代码}'", "JIT_CallGoodsBillMain");
 
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            Hqty = double.Parse(ds.Tables[0].Rows[0]["已叫料数量"].ToString());
                            TuiHqty = double.Parse(ds.Tables[0].Rows[0]["已退料申请数量"].ToString());
                        }
 
                        if (listCa[i].叫料数量 > listCa[i].计划发料数量)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{(i + 1)}行叫料数量大于计划发料数量!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        if((listCa[i].叫料数量 + Hqty - TuiHqty)> listCa[i].计划发料数量)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{(i + 1)}行叫料总数量大于计划发料数量!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        ds = oCN.RunProcReturn("select * from h_v_JIT_WarehouseList where 仓库ID=" + listCa[i].调出仓库id + "  and  物料编码 = '" + listCa[i].物料代码 + "' and 库存组织=" + listCa[i].HOutOrgID, "h_v_JIT_WarehouseList");
 
                        var HSCWHIDCount= double.Parse(ds.Tables[0].Rows[0]["调出仓库可用库存数量"].ToString());
 
                        if (HSCWHIDCount == 0 && listCa[i].叫料数量 > 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{(i + 1)}行库存数量为0,无法叫料!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        if (listCa[i].叫料数量 > HSCWHIDCount)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{(i + 1)}行库存数量小于叫料数量,无法叫料!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        if (listCa[i].叫料数量 > 0 && listCa[i].调入仓库id == 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{(i + 1)}行请选择调入仓库!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        if (listCa[i].叫料数量 > 0 && listCa[i].调出仓库id == 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{(i + 1)}行请选择调出仓库!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        if (listCa[i].调出仓库id == listCa[i].调入仓库id)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{(i + 1)}行调出仓库和调入仓库不能一样!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        j = 1;
 
                        var txtHBillNo = DBUtility.ClsPub.CreateBillCode_Prod(CallGoodsBill.ModName, ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号
                        omodel.HInterID = DBUtility.ClsPub.CreateBillID_Prod(CallGoodsBill.ModName, ref DBUtility.ClsPub.sExeReturnInfo);
                        omodel.HYear = DateTime.Now.Year;
                        omodel.HPeriod = DateTime.Now.Month;
                        //固定赋值=================================
                        omodel.HBillNo = txtHBillNo.Trim();  //在赋值类前就处理好字符串和数字
                        omodel.HDate = DateTime.Now;
                        omodel.HMaker = user;
                        omodel.HMakeDate = DateTime.Today.ToString();
                        omodel.HUpDater = "";
                        omodel.HUpDateDate = "";
                        omodel.HCloseType = false;
                        //明细类赋值
 
                        //固定赋值========================================
                        oSub.HEntryID = i + 1;
                        oSub.HSourceInterID =listCa[i].HSouceInterID;
                        oSub.HSourceEntryID = listCa[i].HSourceEntryID;
                        oSub.HSourceBillType = "88";
                        oSub.HSourceBillNo = listCa[i].HSourceBillNo;
                        oSub.HQty =double.Parse(listCa[i].叫料数量.ToString());
 
                        oSub.HMaterID = listCa[i].HMaterialID;
                        oSub.HMaterNumber = listCa[i].HMaterialNumberCol; 
                        oSub.HModel = listCa[i].HMaterialModelCol;
                        oSub.HWHID = int.Parse(listCa[i].调入仓库id.ToString());
                        oSub.HSCWHID = int.Parse(listCa[i].调出仓库id.ToString());
                        oSub.HProcName = listCa[i].HProcNameCol; 
                        oSub.HProcID = listCa[i].HProcID;
                        oSub.HOutOrgID = listCa[i].HOutOrgID;
                        oSub.HICMOBillNo = listCa[i].HICMOBillNo;
                        oSub.HICMOInterID = listCa[i].HICMOInterID;
                        oSub.HICMOEntryID = listCa[i].HICMOEntryID; 
                        oSub.HDeptID = listCa[i].HDeptID; 
                        oSub.HPlanQty = listCa[i].计划发料数量;
                        oSub.HSeOrderNo = listCa[i].销售订单号; 
                        oSub.HBPlanDate = listCa[i].计划开工日期;
 
 
                        flag = AddBill(ref DBUtility.ClsPub.sExeReturnInfo);
 
                        if (flag == false)
                        {
                            txt = txt + DBUtility.ClsPub.isStrNull(i + 1) + "、";
                        }
                    }
                }
                if (txt != "")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"第{txt}行叫料失败,重新叫料";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    if (flag == true)
                    {
                        objJsonResult.code = "1";
                        objJsonResult.count = 1;
                        objJsonResult.Message = "叫料成功!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
                if (j == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无任何叫料数量或叫料数量小于0,无法叫料!";
                    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 bool AddBill(ref string sReturn)
        {
            try
            {
                //插入子表
                oCN.RunProc("Insert into JIT_CallGoodsBillSub " +
                      " (HInterID,HEntryID,HRemark" +
                      ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                      ",HSeOrderInterID,HSeOrderEntryID,HSeOrderBillNo,HICMOInterID,HICMOEntryID,HICMOBillNo" +
                      ",HDeptID,HMaterID,HQty,HProcID,HWHID,HSCWHID,HSPID,HBillNo_bak,HOutOrgID,HStockOutOrgID,HStockInOrgID" +
                      ") values("
                      + omodel.HInterID.ToString() + "," + oSub.HEntryID.ToString() + ",'" + oSub.HRemark + "'" +
                      "," + oSub.HSourceInterID.ToString() + "," + oSub.HSourceEntryID.ToString() + ",'" + oSub.HSourceBillNo + "','" + oSub.HSourceBillType + "'," + oSub.HRelationQty.ToString() + "," + oSub.HRelationMoney.ToString() +
                      "," + oSub.HSeOrderInterID.ToString() + "," + oSub.HSeOrderEntryID.ToString() + ",'" + oSub.HSeOrderBillNo + "'," + oSub.HICMOInterID.ToString() + "," + oSub.HICMOEntryID.ToString() + ",'" + oSub.HICMOBillNo + "'" +
                      "," + oSub.HDeptID.ToString() + "," + oSub.HMaterID.ToString() + "," + oSub.HQty.ToString() + ",'" + oSub.HProcID + "'," + oSub.HWHID.ToString() + ", " + oSub.HSCWHID.ToString()+"," + oSub.HSPID.ToString() +",'',"+oSub.HOutOrgID+ ","+oSub.HOutOrgID+ "," + omodel.HPRDORGID +") ", ref DBUtility.ClsPub.sExeReturnInfo);
 
                //主表
 
                oCN.RunProc("Insert Into JIT_CallGoodsBillMain   " +
                "(HBillType,HBillSubType,HInterID,HBillNo,HDate" +
                ",HYear,HPeriod,HRemark,HMaker,HMakeDate" +
                ",HBillStatus,HCheckItemNowID,HCheckItemNextID,HCheckFlowID" +
                ",HMainSourceBillType,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo" +
                ",HBacker,HBackDate,HBackRemark,HChecker,HCheckDate,HUpDater,HUpDateDate " +
                ",HCloseMan,HCloseDate,HCloseType,HDeleteMan,HDeleteDate " +
                ",HPrintQty,HCallManID,HSendManID,HCallType,HWareManID,HOrgID) " +
                " values('" + CallGoodsBill.ModName + "','" + CallGoodsBill.ModName + "'," + omodel.HInterID.ToString() + ",'" + omodel.HBillNo + "','" + omodel.HDate + "'" +
                ", " + omodel.HYear.ToString() + "," + omodel.HPeriod.ToString() + ",'" + omodel.HRemark + "','" + omodel.HMaker + "',getdate()" +
                ",'" + omodel.HBillStatus + "'," + omodel.HCheckItemNowID.ToString() + "," + omodel.HCheckItemNextID.ToString() + ", " + omodel.HCheckFlowID.ToString() +
                ",'" + omodel.HMainSourceBillType + "'," + omodel.HMainSourceInterID.ToString() + "," + omodel.HMainSourceEntryID.ToString() + ", '" + omodel.HMainSourceBillNo + "'" +
                ",'" + omodel.HBacker + "','" + omodel.HBackDate + "','" + omodel.HBackRemark + "', '" + omodel.HChecker + "','" + omodel.HCheckDate + "', '" + omodel.HUpDater + "',''" +
                ",'" + omodel.HCloseMan + "','" + omodel.HCloseDate + "','" + omodel.HCloseType + "', '" + omodel.HDeleteMan + "','" + omodel.HDeleteDate + "'" +
                ",0,0,0,'普通',0," + omodel.HPRDORGID + ") ", ref DBUtility.ClsPub.sExeReturnInfo);
 
                sReturn = "新增单据成功!";
                //oCn.Commit();
                return true;
            }
            catch (Exception e)
            {
                sReturn = e.Message;
                oCN.RollBack();
                throw (e);
            }
        }
        #endregion
 
        #region 生产叫料平台  不良品退料申请单新增
        [Route("Sc_ComplementGoodBill/Insert_BLP_ICStockTuiBill")]
        [HttpPost]
        public object Insert_BLP_ICStockTuiBill([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string[] sArray = _value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            DBUtility.ClsPub.CurUserName = sArray[1].ToString();
            string HOrgID = sArray[2].ToString();
            json flag =new json();
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("JIT_BLP_ComplementGoods_Edit", 1, false, DBUtility.ClsPub.CurUserName))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                List<CallGoodsBill> listCa = new List<CallGoodsBill>();
                //获取表格数据集合
                listCa = JsonConvert.DeserializeObject<List<CallGoodsBill>>(msg2.ToString());
 
                int j=0;
                for (int i = 0; i < listCa.Count; i++)
                {
                    if (listCa[i].退料数量 > 0)
                    {
                        flag = (json)AddICStockTuiBill(listCa[i], "666601", "不良品退料", HOrgID);
                        j = 1;
 
                        if (flag.code == "0")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = objJsonResult.Message;
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                }
                if (j == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无任何退料数量或退料数量小于0,无法退料!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                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;
            }
        }
 
        //新增
        public object AddICStockTuiBill(CallGoodsBill listCa,string HBillType,string HBackType,string HOrgID)
        {
            try
            {
              
 
                if (listCa.退料数量 > listCa.已配送数量 && listCa.已配送数量 > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "退料数量大于已配送数量!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (listCa.调入仓库id == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "请选择调入仓库!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                if (listCa.调出仓库id == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "请选择调出仓库!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                var THqty = 0.0;
                ds = oCN.RunProcReturn($@"
                                    select sum(b.HQty) HQty,c.HNumber,b.HSourceBillNo
                                    from JIT_CallGoodsBackRequestBillMain a
                                    inner join JIT_CallGoodsBackRequestBillSub b on a.HInterID=b.HInterID
                                    left join Gy_Material c on b.HMaterID=c.HItemID
                                    where c.HNumber='{listCa.物料代码}' and b.HSourceBillNo='{listCa.HSourceBillNo}'
                                    group by c.HNumber,b.HSourceBillNo
                                  ", "JIT_CallGoodsBackRequestBillMain");
 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    THqty = double.Parse(ds.Tables[0].Rows[0]["HQty"].ToString());
                }
 
                if ((listCa.退料数量 + THqty) > listCa.已配送数量)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "累计退料数量大于已配送数量!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                Int64 lngBillKey = listCa.HSouceInterID == 0 ? 0 : listCa.HSouceInterID;
                Int64 lngBillKeyEntry = listCa.HSourceEntryID == 0 ? 0 : listCa.HSourceEntryID;
                double HQty = (double)(listCa.退料数量 == 0 ? 0 : listCa.退料数量);
                long HDeptWHID = int.Parse(listCa.调入仓库id.ToString()) == 0 ? 0 : int.Parse(listCa.调入仓库id.ToString());
                string HTuiResult = listCa.退料原因 == "" ? "" : listCa.退料原因;
                var HBillNo = DBUtility.ClsPub.CreateBillCode_Prod("6666", ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号
                var HBillInterID = DBUtility.ClsPub.CreateBillID_Prod("6666", ref DBUtility.ClsPub.sExeReturnInfo);
 
                oCN.BeginTran();
                //插入子表
                oCN.RunProc("Insert into JIT_CallGoodsBackRequestBillSub ( HInterID,HEntryID,HSourceInterID,HSourceEntryID," +
                        "HSourceBillNo,HSourceBillType, HMaterID, HQty, HSCWHID," +
                        " HWHID, HSPID, HResult, HDeptID, HProcID,HBackReason) " +
                        $"values({HBillInterID},1,{lngBillKey},{ lngBillKeyEntry}," +
                        $"'{listCa.HSourceBillNo}',88,{listCa.HMaterialID},{HQty},{(listCa.调出仓库id==null?0:listCa.调出仓库id)}," +
                        $"{HDeptWHID},'','',{listCa.HDeptID},'','{HTuiResult}')");
 
                    //插入主表
                    oCN.RunProc("insert into JIT_CallGoodsBackRequestBillMain(HYear,HPeriod,HInterid,HBillSubType,HBillStatus," +
                        "HBillType,HDate,HBillNo,HOrgID,HMaker,HMakeDate,HRemark,HBackRemark,HMainSourceBillType , HMainSourceInterID, HMainSourceEntryID, HMainSourceBillNo," +
                        " HItemMainID, HSendManID, HWareManID, HStockOutOrgID, HStockInOrgID, HBackType)" +
                       $"values({DateTime.Now.Year},{DateTime.Now.Month},{HBillInterID},'{HBillType}',1," +
                       $" '{HBillType}','{DateTime.Now}','{HBillNo}',{HOrgID},'{ DBUtility.ClsPub.CurUserName}','{DateTime.Now}','','',0,0,0,''," +
                       $" 0, 0, {listCa.HOutOrgID}, {HOrgID}, 0,'{HBackType}')");
                 
                oCN.Commit();
 
                objJsonResult.code = "1";
                objJsonResult.Message = "新增单据成功!";
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.Message = e.Message; ;
                return objJsonResult;
                throw (e);
            }
        }
        #endregion
 
        #region 生产叫料平台  余量退料申请单新增
        [Route("Sc_ComplementGoodBill/Insert_YL_ICStockTuiBill")]
        [HttpPost]
        public object Insert_YL_ICStockTuiBill([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string[] sArray = _value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            DBUtility.ClsPub.CurUserName = sArray[1].ToString();
            string HOrgID = sArray[2].ToString();
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("JIT_YL_ComplementGoods_Edit", 1, false, DBUtility.ClsPub.CurUserName))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                List<CallGoodsBill> listCa = new List<CallGoodsBill>();
                //获取表格数据集合
                listCa = JsonConvert.DeserializeObject<List<CallGoodsBill>>(msg2.ToString());
                json flag = new json();
 
                int j = 0;
                for (int i = 0; i < listCa.Count; i++)
                {
                    if (listCa[i].退料数量 > 0)
                    {
                        flag =(json)AddICStockTuiBill(listCa[i], "666602", " 余量退料", HOrgID);
                        j = 1;
                        if (flag.code == "0")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = objJsonResult.Message;
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                }
                if (j == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无任何退料数量,无法退料!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                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
 
        #region 生产叫料平台  报废退料申请单新增
        [Route("Sc_ComplementGoodBill/Insert_BF_ICStockTuiBill")]
        [HttpPost]
        public object Insert_BF_ICStockTuiBill([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string[] sArray = _value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            DBUtility.ClsPub.CurUserName = sArray[1].ToString();
            string HOrgID = sArray[2].ToString();
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("JIT_BF_ComplementGoods_Edit", 1, false, DBUtility.ClsPub.CurUserName))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                List<CallGoodsBill> listCa = new List<CallGoodsBill>();
                //获取表格数据集合
                listCa = JsonConvert.DeserializeObject<List<CallGoodsBill>>(msg2.ToString());
 
                json flag = new json();
                int j = 0;
                for (int i = 0; i < listCa.Count; i++)
                {
                    if (listCa[i].退料数量 > 0)
                    {
                        flag =(json) AddICStockTuiBill(listCa[i], "666603", "报废退料", HOrgID);
                        j = 1;
                        if (flag.code == "0")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = objJsonResult.Message;
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                    }
                }
                if (j == 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无任何退料数量,无法退料!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                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
 
        #region 生产叫料平台  叫料拣料信息查询  上查/下查
        //查询条件
        public class HlpBill
        {
            public int HSouceInterID = 0;
            public int HSourceEntryID = 0;
            public int Type = 0;
            public string user { get; set; }
        }
        [Route("Sc_ComplementGoodBill/HlpBillList")]
        [HttpGet]
        public object HlpBillList(string sWhere)
        {
            try
            {
                List<object> columnNameList = new List<object>();
                //反序列化传递的值
                HlpBill com = JsonConvert.DeserializeObject<HlpBill>(sWhere.ToString());
 
                if (com.Type == 1)
                {
                    //生产叫料平台  下查
                    ds = oCN.RunProcReturn($"exec h_p_JIT_CallGoodsPlatForm_DownQuery 0,0,'',0,0,{com.HSouceInterID},{com.HSourceEntryID}" +
                       $",'{com.user}'", "h_p_JIT_CallGoodsPlatForm_DownQuery");
                }
                else if (com.Type == 2)
                {
                    //拣料配送 上查
                    ds = oCN.RunProcReturn($"exec h_p_JIT_ComplementGoodsBillList_UpQuery '{com.HSouceInterID}',{com.HSourceEntryID}", "h_p_JIT_ComplementGoodsBillList_UpQuery");
                }
                else if (com.Type == 3)
                {
                    //拣料配送 下查
                    ds = oCN.RunProcReturn($"exec h_p_JIT_ComplementGoodsBillList_DownQuery '{com.HSouceInterID}',{com.HSourceEntryID}", "h_p_JIT_ComplementGoodsBillList_DownQuery");
                }
                else if (com.Type == 3772)
                {
                    //生产订单 下查 工序流转卡 
                    ds = oCN.RunProcReturn($"select * from h_v_Sc_ProcessExchangeBillQuery where HICMOInterID='{com.HSouceInterID}' and HICMOEntryID={com.HSourceEntryID} and HBillSubType<>'SUB'", "h_v_Sc_ProcessExchangeBillQuery");
                }
 
                //添加列名
                foreach (DataColumn col in ds.Tables[0].Columns)
                {
                    Type dataType = col.DataType;
                    string ColmString = "{\"ColmCols\":\"" + col.ColumnName + "\",\"ColmType\":\"" + dataType.Name + "\"}";
                    columnNameList.Add(JsonConvert.DeserializeObject(ColmString));//获取到DataColumn列对象的列名
                }
 
                //type==1 存储过程里面查询了两次  所有需要判断两张表都有没有数据
                if (ds.Tables[0].Rows.Count != 0)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = ds.Tables[0];
                    objJsonResult.list = columnNameList;
                    return objJsonResult;
                }
                else if ((com.Type == 1 ? ds.Tables[1].Rows.Count : ds.Tables[0].Rows.Count) != 0)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = ds.Tables[1];
                    objJsonResult.list = columnNameList;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = ds.Tables[0];
                    objJsonResult.list = columnNameList;
                    return objJsonResult;
                }
 
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产叫料平台  拣料单新增
        [Route("Sc_ComplementGoodBill/Insert_JL_ICStockTuiBill")]
        [HttpPost]
        public object Insert_JL_ICStockTuiBill([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string[] sArray = _value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
            string HOrgID = sArray[2].ToString();
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Cj_ComplementGoodsBill_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                ds = oCN.RunProcReturn("select * from h_v_IF_JIT_CallGoodsBillList ", "h_v_IF_JIT_CallGoodsBillList ");
 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    ds.Clear();
                    long HInterID = DBUtility.ClsPub.CreateBillID_Prod("4602", ref DBUtility.ClsPub.sExeReturnInfo);
                    string HBillNo = DBUtility.ClsPub.CreateBillCode_Prod("4602", ref DBUtility.ClsPub.sExeReturnInfo, true);
 
                   ds= oCN.RunProcReturn($"exec h_p_JIT_ComplementGoods_Insert {HInterID},'{HBillNo}',{HOrgID},'{user}',0,0", "h_p_JIT_ComplementGoods_Insert");
 
                    objJsonResult.code = ds.Tables[0].Rows[0][0].ToString();
                    objJsonResult.count = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                    objJsonResult.Message = ds.Tables[0].Rows[0][1].ToString();
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    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
 
        #region 生产叫料平台  退料单新增
        [Route("Sc_ComplementGoodBill/Insert_TL_ICStockTuiBill")]
        [HttpPost]
        public object Insert_TL_ICStockTuiBill([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string[] sArray = _value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
            string HOrgID = sArray[2].ToString();
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Cj_CallGoodsBillBack_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                ds = oCN.RunProcReturn("select * from JIT_CallGoodsBackRequestBillMain where HISCheck=0 ", "JIT_CallGoodsBackRequestBillMain ");
 
                if (ds.Tables[0].Rows.Count > 0)
                {
 
                    long HInterID = DBUtility.ClsPub.CreateBillID_Prod("6666", ref DBUtility.ClsPub.sExeReturnInfo);
                    string HBillNo = DBUtility.ClsPub.CreateBillCode_Prod("6666", ref DBUtility.ClsPub.sExeReturnInfo, true);
                    //long HOutOrgID = int.Parse(listCa[i].HOutOrgID.ToString());
                    //long HWHID = int.Parse(listCa[i].HWHID.ToString());
 
                   ds= oCN.RunProcReturn($"exec h_p_JIT_GetCallGoodsBillCount_Back {HInterID},'{HBillNo}',{HOrgID},'{user}'", "h_p_JIT_GetCallGoodsBillCount_Back");
 
                    objJsonResult.code = ds.Tables[0].Rows[0][0].ToString();
                    objJsonResult.count = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                    objJsonResult.Message = ds.Tables[0].Rows[0][1].ToString();
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    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
 
        #region 生产叫料平台  仓库库存列表
 
        [Route("Sc_ComplementGoodBill/GetWarehouseList_JIT")]
        [HttpGet]
        public object GetWarehouseList_JIT(string sWhere)
        {
            try
            {
                ds = oCN.RunProcReturn("select * from h_v_JIT_WarehouseList where 1=1 "+ sWhere, "h_v_JIT_WarehouseList"); 
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产叫料平台  未生成拣料申请单  未生成退料单
        [Route("Sc_ComplementGoodBill/NotGeneratedMaterialList")]
        [HttpGet]
        public object NotGeneratedMaterialList(string sWhere, string user)
        {
            try
            {
                //查询叫料单 未生成 拣料单 的条数
                //查询退料申请单  未生成 退料单的 条数
                ds = oCN.RunProcReturn($@"select count(*) 未生成    from JIT_CallGoodsBackRequestBillMain as a
                                        inner join JIT_CallGoodsBackRequestBillSub as b on a.HInterID=b.HInterID
                                         where  a.HISCheck =0   and a.HOrgID={sWhere} 
                                        and a.HMaker = '{user}'
                                            union all
                                        select count(*) 未生成  from JIT_CallGoodsBillMain a
                                        join JIT_CallGoodsBillSub b on a.HInterID=b.HInterID
                                         where b.HPSQty=0  and a.HOrgID={sWhere} 
                                        and a.HMaker = '{user}'", "JIT_CallGoodsBackRequestBillMain");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产叫料平台  配套叫料查询
 
        [Route("Sc_ComplementGoodBill/JIT_ComplementGoodPT")]
        [HttpGet]
        public object JIT_ComplementGoodPT(string sWhere,string user)
        {
            try
            {
                ds = oCN.RunProcReturn("exec h_p_JIT_ComplementGoodPT " + sWhere, "h_p_JIT_ComplementGoodPT");
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "Sucess!";
                objJsonResult.data = ds.Tables[0];
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 生产叫料平台  配套叫料新增
        [Route("Sc_ComplementGoodBill/InsertCallGoodsBill_PTJL")]
        [HttpPost]
        public object InsertCallGoodsBill_PTJL([FromBody] JObject msg)
        {
            var _value = msg["msg"].ToString();
            string[] sArray = _value.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
            omodel.HPRDORGID = long.Parse(sArray[2].ToString());
 
            try
            {
                if (!DBUtility.ClsPub.Security_Log("Cj_CallGoodsBill_Edit", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无保存权限";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                List<CallGoodsBill> listCa = new List<CallGoodsBill>();
                //获取表格数据集合
                listCa = JsonConvert.DeserializeObject<List<CallGoodsBill>>(msg2.ToString());
 
                var flag = false;
                var txt = "";
 
                for (int i = 0; i < listCa.Count; i++)
                {
                    if (listCa[i].配套数量 <= 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i + 1}行配套数量小于0,无法叫料!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    ds = oCN.RunProcReturn("select * from h_v_JIT_WarehouseList where 仓库ID=" + listCa[i].调出仓库id + "  and  物料编码 = '" + listCa[i].物料代码 + "' and 库存组织=" + listCa[i].HOutOrgID, "h_v_JIT_WarehouseList");
 
                    var HSCWHIDCount = double.Parse(ds.Tables[0].Rows[0]["调出仓库可用库存数量"].ToString());
 
                    if (HSCWHIDCount == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i + 1}行库存数量为0,无法叫料!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    if (listCa[i].配套数量 > HSCWHIDCount)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i+1}行库存数量小于叫料数量,无法叫料!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    if (listCa[i].调入仓库id == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i + 1}行请选择调入仓库!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    if (listCa[i].调出仓库id == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i + 1}行请选择调出仓库!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    if (listCa[i].调出仓库id == listCa[i].调入仓库id)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i + 1}行调出仓库和调入仓库不能一样!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    var Hqty = 0.0;
                    var TuiHqty = 0.0;
                    //当前单据已叫料 和 已退料数量
                    ds = oCN.RunProcReturn($@"
                                               select isnull(a.HQty,0) 已叫料数量,isnull(b.HQty,0) 已退料申请数量 from (
                                                select sum(b.HQty) HQty,c.HNumber ,b.HSourceBillNo
                                                from JIT_CallGoodsBillMain a
                                                join JIT_CallGoodsBillSub b on a.HInterID=b.HInterID
                                                left join Gy_Material c on b.HMaterID=c.HItemID
                                                where b.HSourceBillNo='{listCa[i].HSourceBillNo}'
                                                group by c.HNumber,b.HSourceBillNo
                                                ) a
                                                left join (
                                                select sum(b.HQty) HQty,c.HNumber,b.HSourceBillNo
                                                from JIT_CallGoodsBackRequestBillMain a
                                                inner join JIT_CallGoodsBackRequestBillSub b on a.HInterID=b.HInterID
                                                left join Gy_Material c on b.HMaterID=c.HItemID
                                                where b.HSourceBillNo='{listCa[i].HSourceBillNo}'
                                                group by c.HNumber,b.HSourceBillNo
                                                ) b  on a.HNumber=b.HNumber and a.HSourceBillNo=b.HSourceBillNo where a.HNumber='{listCa[i].物料代码}'", "JIT_CallGoodsBillMain");
 
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Hqty = double.Parse(ds.Tables[0].Rows[0]["已叫料数量"].ToString());
                        TuiHqty = double.Parse(ds.Tables[0].Rows[0]["已退料申请数量"].ToString());
                    }
 
                    if (listCa[i].配套数量 > listCa[i].计划发料数量)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i + 1}行叫料数量大于计划发料数量!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
                    if ((listCa[i].配套数量 + Hqty - TuiHqty) > listCa[i].计划发料数量)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = $"第{i + 1}行叫料总数量大于计划发料数量!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
 
 
                    var txtHBillNo = DBUtility.ClsPub.CreateBillCode_Prod(CallGoodsBill.ModName, ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号
                        omodel.HInterID = DBUtility.ClsPub.CreateBillID_Prod(CallGoodsBill.ModName, ref DBUtility.ClsPub.sExeReturnInfo);
                        omodel.HYear = DateTime.Now.Year;
                        omodel.HPeriod = DateTime.Now.Month;
                        //固定赋值=================================
                        omodel.HBillNo = txtHBillNo.Trim();  //在赋值类前就处理好字符串和数字
                        omodel.HDate = DateTime.Now;
                        omodel.HMaker = user;
                        omodel.HMakeDate = DateTime.Today.ToString();
                        omodel.HUpDater = "";
                        omodel.HUpDateDate = "";
                        omodel.HCloseType = false;
                        //明细类赋值
 
                        //固定赋值========================================
                        oSub.HEntryID = i + 1;
                        oSub.HSourceInterID = listCa[i].HSouceInterID;
                        oSub.HSourceEntryID = listCa[i].HSourceEntryID;
                        oSub.HSourceBillType = "88";
                        oSub.HSourceBillNo = listCa[i].HSourceBillNo;
                        oSub.HQty = double.Parse(listCa[i].配套数量.ToString());
 
                        oSub.HMaterID = listCa[i].HMaterialID;
                        oSub.HMaterNumber = listCa[i].HMaterialNumberCol;
                        oSub.HModel = listCa[i].HMaterialModelCol;
                        oSub.HWHID = int.Parse(listCa[i].调入仓库id.ToString());
                        oSub.HSCWHID = int.Parse(listCa[i].调出仓库id.ToString());
                        oSub.HProcName = listCa[i].HProcNameCol;
                        oSub.HProcID = listCa[i].HProcID;
                        oSub.HOutOrgID = listCa[i].HOutOrgID;
                        oSub.HICMOBillNo = listCa[i].HICMOBillNo;
                        oSub.HICMOInterID = listCa[i].HICMOInterID;
                        oSub.HICMOEntryID = listCa[i].HICMOEntryID;
                        oSub.HDeptID = listCa[i].HDeptID;
                        oSub.HPlanQty = listCa[i].计划发料数量;
                        oSub.HSeOrderNo = listCa[i].销售订单号;
                        oSub.HBPlanDate = listCa[i].计划开工日期;
 
 
                    flag = AddBill(ref DBUtility.ClsPub.sExeReturnInfo);
 
                        if (flag == false)
                        {
                            txt = txt + DBUtility.ClsPub.isStrNull(i + 1) + "、";
                        }
               
                }
                if (txt != "")
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = $"第{txt}行叫料失败,重新叫料";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    if (flag == true)
                    {
                        objJsonResult.code = "1";
                        objJsonResult.count = 1;
                        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;
            }
        }
 
 
        #endregion
    }
}