yangle
2022-08-16 5db884aac7beaac546470f9aa91731cd79961fee
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
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_ComplementGoodsController : ApiController
    {
        private json objJsonResult = new json();
        public DataSet ds = new DataSet();
        public WebServer webserver = new WebServer();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
 
        #region 拣料配送单列表
        public class ComplementGoods
        {
            public string MaterialNumber { get; set; }//物料代码
            public string MaterialName { get; set; }//物料名称
            public string MaterialModel { get; set; }//规格型号
            public string HGD { get; set; }//工段
            public string HWHName { get; set; }//仓库
            public string Organization { get; set; }//组织
            public DateTime HBeginDate { get; set; }//开始时间
            public DateTime HEndDate { get; set; }//结束时间
            public string HDeptName { get; set; }//部门
            public string HJLStatus { get; set; }//拣料状态
            public string user { get; set; }//制单人
        }
 
        [Route("Sc_ComplementGoods/ComplementGoodsList")]
        [HttpGet]
        public object ComplementGoodBillList(string sWhere, string user)
        {
            try
            {
                //查看权限
                if (!DBUtility.ClsPub.Security_Log("Cj_ComplementGoodsBill_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;
                }
                //反序列化传递的值
                ComplementGoods com = JsonConvert.DeserializeObject<ComplementGoods>(sWhere.ToString());
 
 
                ds = oCN.RunProcReturn($"exec h_p_JIT_ComplementGoodsBillList_Query '{com.MaterialNumber}','{com.MaterialName}','{com.MaterialModel}','{com.HGD}'," +
                    $"'{com.HWHName}','{com.HDeptName}','{com.Organization}','{com.user}','{com.HJLStatus}','{com.HBeginDate}','{com.HEndDate}'", "h_p_JIT_ComplementGoodsBillList_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 拣料配送单 删除
        [Route("Sc_ComplementGoods/DelComplementGoodsList")]
        [HttpGet]
        public object DelComplementGoodsList(string HInterID, string HEntryID, string HOrgID, string user)
        {
            try
            {
                //删除权限
                if (!DBUtility.ClsPub.Security_Log("Cj_ComplementGoodsBill_Drop", 1, false, user))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "无删除权限!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                ds = oCN.RunProcReturn($"select HISCheck from JIT_SendGoodsBillMain where HMainSourceInterID={HInterID} ", "JIT_SendGoodsBillMain");
 
                if (ds.Tables[0].Rows.Count > 0)
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "当前单据已配送,不能删除!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
 
                oCN.RunProc($"exec Del_ComplementGoodsBillInfo {HInterID},{HEntryID},{HOrgID}");
 
                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_ComplementGoods/ComplementGoodsBill")]
        [HttpGet]
        public object ComplementGoodsBill(string sWhere)
        {
            try
            {
                if (sWhere == null || sWhere.Equals(""))
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = new DataTable();
                    return objJsonResult;
                }
 
                ds = oCN.RunProcReturn($"exec h_p_JIT_ComplementGoods_Load '{sWhere}'", "h_p_JIT_ComplementGoods_Load");
 
                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_ComplementGoods/ComplementGoodsBill_C")]
        [HttpGet]
        public object ComplementGoodsBill_C(string sWhere)
        {
            try
            {
                if (sWhere == null || sWhere.Equals(""))
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "Sucess!";
                    objJsonResult.data = new DataTable();
                    return objJsonResult;
                }
 
                oCN.RunProc($"exec h_p_JIT_ComplementGoodsBill_C_Add '{sWhere}'");
 
                ds = oCN.RunProcReturn("exec h_p_JIT_GetComplementGoodsBill_C  " + sWhere, "h_p_JIT_GetComplementGoodsBill_C");
 
                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 SendGoodsBill
        {
            public int? 未拣料数量 = 0;
            public int? 最大拣料数量 = 0;
            public int? 本次拣料数量 = 0;
            public int? HEntryID = 0;
            public int? HMaterID = 0;
            public int? HWHID = 0;
            public int? HSPID = 0;
            public int? HStockOutOrgID = 0;
            public int? HStockInOrgID = 0;
            public int? HSCWHID = 0;
        }
 
        [Route("Sc_ComplementGoods/JIT_SendGoodsBill")]
        [HttpPost]
        public object JIT_SendGoodsBill([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 SourceInterID = sArray[2].ToString();
            string SourceBillNo = sArray[3].ToString();
            try
            {
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");
 
                List<SendGoodsBill> listCa = new List<SendGoodsBill>();
                //获取表格数据集合
                listCa = JsonConvert.DeserializeObject<List<SendGoodsBill>>(msg2.ToString());
 
                for (int i = 0; i < listCa.Count; i++)
                {
                    if (listCa[i].本次拣料数量 > listCa[i].最大拣料数量)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "本次拣料数量超过最大拣料数量!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    if (listCa[i].本次拣料数量 > listCa[i].未拣料数量)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "本次拣料数量超过未拣料数量!";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
 
 
                objJsonResult = AddSendGoodsBill(listCa, SourceInterID, SourceBillNo, user);
 
 
                if (objJsonResult.count==1)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "调拨单生成成功!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = 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 json AddSendGoodsBill(List<SendGoodsBill> listCa, string SourceInterID, string SourceBillNo, string user)
        {
            try
            {
                oCN.BeginTran();
 
                long InterID = 0;
                string HBillNo = "";
                for (int i = 0; i < listCa.Count; i++)
                {
                   
                        if (i == 0)
                        {
                            InterID = DBUtility.ClsPub.CreateBillID_Prod("460201", ref DBUtility.ClsPub.sExeReturnInfo);
                            HBillNo = DBUtility.ClsPub.CreateBillCode_Prod("460201", ref DBUtility.ClsPub.sExeReturnInfo, true);
 
                            //添加主表数据
                            oCN.RunProc("insert into JIT_SendGoodsBillMain(HYear,HPeriod,HBillType,HBillSubType,HInterID,HDate" +
                                ",HBillNo,HBillStatus,HMaker,HMakeDate,HISCheck, HMainSourceInterID, HMainSourceEntryID" +
                                ", HMainSourceBillNo, HMainSourceBillType)" +
                               $"values({DateTime.Now.Year},{DateTime.Now.Month},'460201','460201',{InterID}, GETDATE(), '{HBillNo}'," +
                               $" 1,'{user}', GETDATE(), 0, {SourceInterID}, {listCa[i].HEntryID}, '{SourceBillNo}', '4602')");
                        }
 
                        //添加子表数据
                        oCN.RunProc("insert into JIT_SendGoodsBillSub(HInterID,HBillNo_bak,HEntryID" +
                            ",HMaterID,HQty,HWHID,HSPID,HISCheck,HCheckSubMan,HCheckSubDate,HStockOutOrgID,HStockInOrgID,HSCWHID)" +
                            $"values({InterID},'{HBillNo}',{i + 1}, {listCa[i].HMaterID},{listCa[i].本次拣料数量}," +
                            $" {listCa[i].HWHID},{listCa[i].HSPID},0,'{user}', GETDATE(),{listCa[i].HStockOutOrgID},{listCa[i].HStockInOrgID},{listCa[i].HSCWHID})");
                 
 
                    DataSet dsTable = new DataSet();
                    //查询JIT_ComplementGoodsBillSub_LK
                    dsTable = oCN.RunProcReturn($"select  * from JIT_ComplementGoodsBillSub_LK where HMaterID={listCa[i].HMaterID}  and HInterID={SourceInterID} order by HSourceInterID ", "JIT_ComplementGoodsBillSub_LK");
 
                    for (int j = 0; j < dsTable.Tables[0].Rows.Count; j++)
                    {
                        if (double.Parse(dsTable.Tables[0].Rows[j]["HCallQTY"].ToString()) != double.Parse(dsTable.Tables[0].Rows[j]["HSendQTY"].ToString()))
                        {
                            if (double.Parse(dsTable.Tables[0].Rows[j]["HSendQTY"].ToString()) + listCa[i].本次拣料数量 > double.Parse(dsTable.Tables[0].Rows[j]["HCallQTY"].ToString()))
                            {
                                var HCallQTY = double.Parse(dsTable.Tables[0].Rows[j]["HCallQTY"].ToString()) - double.Parse(dsTable.Tables[0].Rows[j]["HSendQTY"].ToString());
                                //修改调拨数量 超出部分给予下一列数据
                                oCN.RunProc($"update JIT_ComplementGoodsBillSub_LK set HSendQTY=HSendQTY+{HCallQTY} where HMaterID={listCa[i].HMaterID} " +
                                    $" and HSourceInterID={double.Parse(dsTable.Tables[0].Rows[j]["HSourceInterID"].ToString())} and HInterID={SourceInterID} ");
 
                                oCN.RunProc($"update JIT_ComplementGoodsBillSub_LK set HSendQTY=HSendQTY+{(listCa[i].本次拣料数量 - HCallQTY)} where HMaterID={listCa[i].HMaterID} " +
                                   $" and HSourceInterID={dsTable.Tables[0].Rows[j + 1]["HSourceInterID"].ToString()} and HInterID={SourceInterID} ");
                                break;
                            }
                            else
                            {
                                //修改调拨数量
                                oCN.RunProc($"update JIT_ComplementGoodsBillSub_LK set HSendQTY=HSendQTY+{listCa[i].本次拣料数量} where HMaterID={listCa[i].HMaterID} " +
                                    $" and HSourceInterID={dsTable.Tables[0].Rows[j]["HSourceInterID"].ToString()} and HInterID={SourceInterID} ");
                                break;
                            }
                        }
                    }
                }
 
                //同步金蝶直接调拨单
                objJsonResult = AddSendGoodsBillSynChronizAtion(HBillNo);
 
                oCN.Commit();
 
                if (objJsonResult.count == 1)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = null;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = objJsonResult.Message;
                    objJsonResult.data = null;
                    return objJsonResult;
                }
            }
            catch (Exception e)
            {
                oCN.RollBack();
 
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = e.Message;
                return objJsonResult;
                throw (e);
            }
        }
 
        //同步金蝶直接调拨单
        public json AddSendGoodsBillSynChronizAtion(string HBillNo)
        {
            ds = oCN.RunProcReturn("exec JIT_SendGoodsBillSynChronizAtion '" + HBillNo + "'", "JIT_SendGoodsBillSynChronizAtion");
            if (ds.Tables[0].Rows.Count != 0)
            {
 
 
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    JObject model = new JObject();
                    model.Add("FBillTypeID", new JObject() { ["Fnumber"] = dr["FBillTypeID"].ToString() }); //单据类型
                    model.Add("FDate", dr["FDate"].ToString());
                    model.Add("FBILLNO", dr["FBILLNO"].ToString());
                    model.Add("FStockOrgId", new JObject() { ["Fnumber"] = ds.Tables[1].Rows[0]["FStockOrgId"].ToString() });
                    model.Add("FOwnerTypeIdHead", dr["FOwnerTypeIdHead"].ToString());
                    model.Add("FTransferBizType", ds.Tables[1].Rows[0]["FTransferBizType"].ToString());
                    model.Add("FOwnerTypeOutIdHead", dr["FOwnerTypeOutIdHead"].ToString());
                    model.Add("FTransferDirect", dr["FTransferDirect"].ToString());
                    model.Add("FStockOutOrgId", new JObject() { ["Fnumber"] = ds.Tables[1].Rows[0]["FStockOutOrgId"].ToString() });
 
                    JArray Fentity = new JArray();
                    foreach (DataRow item in ds.Tables[1].Rows)
                    {
                        JObject FentityModel = new JObject();
                        FentityModel.Add("FMaterialId", new JObject() { ["Fnumber"] = item["FMaterialId"].ToString() });
                        FentityModel.Add("FUnitID", new JObject() { ["Fnumber"] = item["FUnitID"].ToString() });
                        FentityModel.Add("FBaseUnitId", new JObject() { ["Fnumber"] = item["FBaseUnitId"].ToString() });
                        FentityModel.Add("FOwnerTypeId", item["FOwnerTypeId"].ToString());
                        FentityModel.Add("FOwnerId", new JObject() { ["Fnumber"] = item["FOwnerId"].ToString() });
                        FentityModel.Add("FOwnerTypeOutId", item["FOwnerTypeOutId"].ToString());
                        FentityModel.Add("FOwnerOutId", new JObject() { ["Fnumber"] = item["FOwnerOutId"].ToString() });
                        FentityModel.Add("FKeeperTypeId", item["FKeeperTypeId"].ToString());
                        FentityModel.Add("FKeeperId", new JObject() { ["Fnumber"] = item["FKeeperId"].ToString() });
                        FentityModel.Add("FKeeperTypeOutId", item["FKeeperTypeOutId"].ToString());
                        FentityModel.Add("FKeeperOutId", new JObject() { ["Fnumber"] = item["FKeeperOutId"].ToString() });
                        FentityModel.Add("FDestStockId", new JObject() { ["Fnumber"] = item["FDestStockId"].ToString() });
                        FentityModel.Add("FSrcStockId", new JObject() { ["Fnumber"] = item["FSrcStockId"].ToString() });
                        FentityModel.Add("FQty", item["FQty"].ToString());
                        Fentity.Add(FentityModel);
                    }
                    model.Add("FBillEntry", Fentity);
 
                    JObject jsonRoot = new JObject()
                    {
                        ["Creator"] = "",
                        ["NeedUpDateFields"] = new JArray(),
                        ["NeedReturnFields"] = new JArray(),
                        ["IsDeleteEntry"] = "false",
                        ["SubSystemId"] = "",
                        ["IsVerifyBaseDataField"] = "false",
                        ["Model"] = model
                    };
                    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.Save("STK_TransferDirect", jsonRoot.ToString());
                    if (JObject.Parse(result)["Result"]["ResponseStatus"]["IsSuccess"].ToString().ToUpper() == "TRUE")
                    {
                        objJsonResult.code = "1";
                        objJsonResult.count = 1;
                        objJsonResult.Message = null;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    else
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = result;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                }
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "调拨单已经存在,无法生成!";
                objJsonResult.data = null;
                return objJsonResult;
            }
            objJsonResult.code = "0";
            objJsonResult.count = 0;
            objJsonResult.Message = null;
            objJsonResult.data = null;
            return objJsonResult;
        }
        #endregion
 
        #region 上查
        [Route("Sc_ComplementGoods/ComplementGoodsList_Sc")]
        [HttpGet]
        public object ComplementGoodsList_Sc(string HInterID, string HMaterID)
        {
            try
            {
 
                ds = oCN.RunProcReturn($"exec h_p_JIT_ComplementGoodsBillList_UpQuery '{HInterID}',{HMaterID}", "h_p_JIT_ComplementGoodsBillList_UpQuery");
 
                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_ComplementGoods/JIT_SendGoodsBillList_Xc")]
        [HttpGet]
        public object JIT_SendGoodsBillList_Xc(string HInterID, string HMaterID)
        {
            try
            {
 
                ds = oCN.RunProcReturn($"exec h_p_JIT_ComplementGoodsBillList_DownQuery '{HInterID}',{HMaterID}", "h_p_JIT_ComplementGoodsBillList_DownQuery");
 
                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
    }
}