chenhaozhe
3 天以前 e4f1a927520a98fbc69dd63ef982dd890ba4fbdc
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
using WebAPI.Models;
using System.Globalization;
using System.Threading;
using System.Diagnostics;
namespace WebAPI.Controllers.SCGL.日计划管理
{
    public class Xs_SendGoodsPlanBillController : ApiController
    {
        private json objJsonResult = new json();    
        public DataSet ds = new DataSet();
        public WebServer webserver = new WebServer();
        SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
        Xs_SendGoodsPlanBillMain omdelMian = new Xs_SendGoodsPlanBillMain();
        string BillType = "1423";
        //List<Sc_WorkBillSortBillSub> omodelsub = new List<Sc_WorkBillSortBillSub>();
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
 
        #region 发货计划交期(三升导入) 文件上传
        [Route("Xs_SendGoodsPlanBill/ExcelImport")]
        [HttpPost]
        public object ExcelImport()
        {
            try
            {
                //获取文件名称
                var file = HttpContext.Current.Request.Files[0];
                //获取文件物理路径
                string ExcelPath = HttpContext.Current.Server.MapPath("~/" + file.FileName);
                //保存文件
                file.SaveAs(ExcelPath);
 
                NpoiHelper np = new NpoiHelper();
                DataSet ExcelDs = np.ReadExcel(ExcelPath, 1, 1, "0");
 
                //删除文件
                File.Delete(ExcelPath);
 
                //创建临时表
                DataTable tb2 = new DataTable("dt2");
 
                //添加列名
                for (int i = 0; i < ExcelDs.Tables[0].Columns.Count; i++)
                {
                    tb2.Columns.Add(ExcelDs.Tables[0].Rows[0][i].ToString());
                }
                //模板缺少列 但需要从数据库中查询出来显示在页面的字段
                tb2.Columns.Add("hmainid", typeof(Int32));
                tb2.Columns.Add("HMaterID", typeof(Int32));
                tb2.Columns.Add("SeOrderHInterID", typeof(Int32));
                tb2.Columns.Add("SeOrderHEntryID", typeof(Int32));
                //获取系统参数
                string Ret = "";
                oSystemParameter.ShowBill(ref Ret);
 
                LogService.Write("excel值:" + ExcelDs.Tables[0].Rows[1][1].ToString());//2024-11-26
                //添加数据
                for (int i = 1; i < ExcelDs.Tables[0].Rows.Count; i++)
                {
                    DataRow row = tb2.NewRow();
                    for (int j = 0; j < ExcelDs.Tables[0].Columns.Count; j++)
                    {
                        row[j] = ExcelDs.Tables[0].Rows[i][j].ToString();
                    }
                    //如果表格第i行的第一列为空,则判断为这一行的数据为空,跳出循环并且不把数据写入 tb2
                    if (ExcelDs.Tables[0].Rows[i][0].ToString() == "" && ExcelDs.Tables[0].Rows[i][1].ToString() == "")
                    {
                        continue;
                    }
 
                    else
                    {
                        tb2.Rows.Add(row);
                    }
                }
 
                var error = "";
                var ErrorResult = "";
 
                //查询销售表判断有没有销售订单信息
                for (int i = 0; i < tb2.Rows.Count; i++)
                {
                    if (tb2.Rows[i]["发货日期"].ToString() == "")
                        error += "发货日期不能为空;";
                    if (tb2.Rows[i]["客户订单号"].ToString() == "")
                        error += "客户订单号不能为空;";
                    if (tb2.Rows[i]["物料编码"].ToString() == "")
                        error += "物料编码不能为空;";
 
                    if (error.Length > 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = ErrorResult += $"Excel模板存在错误,行数{i + 1}:{error}\r\n"; ;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    //查找是否存在对应销售订单
                    string sql1 = string.Format("select * from h_v_Xs_SendGoodsPlanSANSTEXTList where F_SANS_TEXT='{0}' and HNumber='{1}'",
                        tb2.Rows[i]["客户订单号"].ToString(), tb2.Rows[i]["物料编码"].ToString());
                    ds = oCN.RunProcReturn(sql1, "h_v_Xs_SendGoodsPlanSANSTEXTList");
 
                    if (ds.Tables[0].Rows.Count == 0)
                    {
                        error += $"没有符合条件得销售订单信息,客户订单号:{ tb2.Rows[i]["客户订单号"].ToString()},产品代码:{tb2.Rows[i]["物料编码"].ToString()}";
                    }
 
                    if (error.Length > 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = ErrorResult += $"Excel模板存在错误,行数{i + 1}:{error}\r\n"; ;
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    else
                    {
                        tb2.Rows[i]["HMaterID"] = ds.Tables[0].Rows[0]["HMaterID"].ToString();
                        tb2.Rows[i]["SeOrderHInterID"] = ds.Tables[0].Rows[0]["FID"].ToString();//销售订单主id
                        tb2.Rows[i]["SeOrderHEntryID"] = ds.Tables[0].Rows[0]["FENTRYID"].ToString();//销售订单子id
                    }
                    //查找是否存在对应发货计划工单
                    var dt = oCN.RunProcReturn($"select * from Xs_SendGoodsPlanBillMain where HSeOrderInterID in({ds.Tables[0].Rows[0]["FID"].ToString()}) and  HSeOrderEntryID in({ds.Tables[0].Rows[0]["FENTRYID"].ToString()})", "Xs_SendGoodsPlanBillMain");
                    if (dt.Tables[0].Rows.Count > 0)
                    {
                        tb2.Rows[i]["hmainid"] = dt.Tables[0].Rows[0]["HInterID"].ToString();
                    }
 
                }
 
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = ErrorResult;
                objJsonResult.data = tb2;
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        #endregion
 
        #region 发货计划三升交期 导入(保存)
        [Route("Xs_SendGoodsPlanBill/ExcelSave")]
        [HttpPost]
        public object ExcelSave([FromBody] JObject sMainSub)
        {
            var _value = sMainSub["sMainSub"].ToString();
            string msg1 = _value.ToString();
            oCN.BeginTran();
            //保存主表
            objJsonResult = AddBillMain_SS(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;
        }
 
        public json AddBillMain_SS(string msg1)
        {
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string msg3 = sArray[1].ToString();
            string user = sArray[2].ToString();
            
            try
            {
                //if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Edit", 1, false, user))
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "无保存权限!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
 
                //表头字段
                omdelMian = Newtonsoft.Json.JsonConvert.DeserializeObject<Xs_SendGoodsPlanBillMain>(msg2);
 
                //JSON序列化转换字典集合
                List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
                List<object> jb = JsonConvert.DeserializeObject<List<object>>(msg3);
                foreach (JObject item in jb)
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    foreach (var itm in item.Properties())
                    {
                        dic.Add(itm.Name, itm.Value.ToString());
                    }
                    list.Add(dic);
                }
 
 
 
 
                for (int i = 0; i < list.Count; i++)
                {
                    long HInterID = 0;
                    var HBillNo = "";
                    if (list[i]["hmainid"].ToString() == "")
                    {
                        HBillNo = DBUtility.ClsPub.CreateBillCode_Prod("1423", ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号
                        HInterID = DBUtility.ClsPub.CreateBillID_Prod("1423", ref DBUtility.ClsPub.sExeReturnInfo);//获得一个新的id
                        var HMainSourceInterID = list[i]["SeOrderHInterID"].ToString();
                        var HMainSourceEntryID = list[i]["SeOrderHEntryID"].ToString();
                        var HMaterID = list[i]["HMaterID"].ToString();
                        var HSendDate = list[i]["发货日期"].ToString();
                        //获取对应订单信息
                        ds = oCN.RunProcReturn($"select * from h_v_Xs_SendGoodsPlanSANSTEXTList where FID='{HMainSourceInterID}' and FENTRYID='{HMainSourceEntryID}'", "h_v_Xs_SendGoodsPlanSANSTEXTList");
                        if (ds.Tables[0].Rows.Count == 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"没找到对应符合条件得订单信息,行数{i + 1}\r\n"; ;
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
                        
                        var HUnitID = ds.Tables[0].Rows[0]["HUnitID"].ToString();
                        var HQty = ds.Tables[0].Rows[0]["HQty"].ToString();
                        var HOrgID = ds.Tables[0].Rows[0]["FSALEORGID"].ToString();
                        var HMainSourceBillNo = ds.Tables[0].Rows[0]["FBILLNO"].ToString();
                        //主表
                        oCN.RunProc("insert into Xs_SendGoodsPlanBillMain(HInterID,HBillNo,HYear,HPeriod,HBillType," +
                            "HBillSubType,HDate,HBillStatus,HMaker,HMakeDate,HOrgID,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HSeOrderInterID," +
                            "HSeOrderEntryID,HMaterID,HUnitID,HQty,HSendDate"+
                            ")values" +
                            $"({HInterID},'{HBillNo}',{DateTime.Now.Year},{DateTime.Now.Month},'{BillType}'," +
                            $"'{BillType}',GETDATE(),1,'{user}',GETDATE(),'{HOrgID}','{HMainSourceInterID}'," +
                            $"{HMainSourceEntryID},'{HMainSourceBillNo}',{HMainSourceInterID}, {HMainSourceEntryID},{HMaterID}, {HUnitID}," +
                            $" {(HQty == "" ? 0.ToString() : HQty)}, '{HSendDate}')");
                    }//如果已存在发货计划工单更新对应计划结束日期
                    else
                    {
                        var hmainid = list[i]["hmainid"].ToString();
                        var HSendDate = list[i]["发货日期"].ToString();
                        //更新交期
                        if (HSendDate != "")
                        {
                            oCN.RunProc($"update Xs_SendGoodsPlanBillMain set HSendDate = '{HSendDate}' where HInterID ={hmainid}");
                        }
                    }
 
                }
 
 
 
                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
 
        #region 发货计划 删除
        [Route("Xs_SendGoodsPlanBill/Delete")]
        [HttpGet]
        public object Delete(string HInterID,string User)
        {
            try
            {
                //删除权限
                //if (!DBUtility.ClsPub.Security_Log_second("Sc_WorkBillSortBill_Drop", 1, false, User))
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "无权限删除!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
                //DataType 1=发货计划工单 多选删除   2=发货计划平台多选删除
                
                var NumData = HInterID.Split(',');
 
                for (int i = 0; i < NumData.Length; i++)
                {
                    var NumData_T = NumData[i].Split('_');
 
                    ds = oCN.RunProcReturn("select * from h_v_Xs_SendGoodsPlanBillList where 1=1 and hmainid=" + NumData_T[0], "h_v_Xs_SendGoodsPlanBillList");
 
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (ds.Tables[0].Rows[0]["单据状态"].ToString() != "1")
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = "当前单据不能删除!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        oCN.BeginTran();//开启事务
 
                        if (ds.Tables[0].Rows.Count == 1)
                        {
                            oCN.RunProc($"delete from Xs_SendGoodsPlanBillMain where HInterID={ NumData_T[0]}");
                        }
                        oCN.Commit();//结束事务
                    }
                }
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "删除成功!";
                objJsonResult.data = null;
                return objJsonResult;
                
                //else if (DataType == 2)
                //{
                //    var NumData = HInterID.Split(',');
 
                //    //获取系统参数
                //    string sReturn = "";
                //    oSystemParameter.ShowBill(ref sReturn);
 
                //    for (int i = 0; i < NumData.Length; i++)
                //    {
                        
                //        ds = oCN.RunProcReturn("select * from h_v_JIT_Sc_WorkBillSortBillList_Left where 1=1 and hmainid=" + NumData[i], "h_v_JIT_Sc_WorkBillSortBillList");
 
                //        if (ds.Tables[0].Rows.Count > 0)
                //        {
                //            if (ds.Tables[0].Rows[0]["单据状态"].ToString() != "1")
                //            {
                //                objJsonResult.code = "0";
                //                objJsonResult.count = 0;
                //                objJsonResult.Message = "当前单据不能删除!";
                //                objJsonResult.data = null;
                //                return objJsonResult;
                //            }
 
                //            oCN.BeginTran();//开启事务
 
                //            oCN.RunProc($"delete from Sc_WorkBillSortBillMain where HInterID={ NumData[i]}");
 
                //            oCN.Commit();//结束事务
                //        }
                        
                //    }
                //    objJsonResult.code = "1";
                //    objJsonResult.count = 1;
                //    objJsonResult.Message = "删除成功!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
 
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                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;
            }
        }
        #endregion
 
        #region 发货计划平台 保存
        [Route("Xs_SendGoodsPlanBill/btnSave")]
        [HttpPost]
        public object btnSave([FromBody] JObject sMainSub)
        {
            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;
        }
 
        public json AddBillMain(string msg1)
        {
            string[] sArray = msg1.Split(new string[] { "&和" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();
            string user = sArray[1].ToString();
 
            try
            {
                Stopwatch sw = new Stopwatch();//计时器
                sw.Start();//开始计时
 
                //if (!DBUtility.ClsPub.Security_Log("Sc_WorkBillSortBill_Edit", 1, false, user))
                //{
                //    objJsonResult.code = "0";
                //    objJsonResult.count = 0;
                //    objJsonResult.Message = "无保存权限!";
                //    objJsonResult.data = null;
                //    return objJsonResult;
                //}
                //JSON序列化转换字典集合
                List<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
                List<object> jb = JsonConvert.DeserializeObject<List<object>>(msg2);
                foreach (JObject item in jb)
                {
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    foreach (var itm in item.Properties())
                    {
                        dic.Add(itm.Name, itm.Value.ToString());
                    }
                    list.Add(dic);
                }
 
                string sReturn = "";
                oSystemParameter.ShowBill(ref sReturn);
                for (int i = 0; i < list.Count; i++)
                {
                    long HInterID = 0;
                    var HBillNo = "";
                    if (list[i]["单据号"].ToString() == "" && list[i]["hmainid"].ToString() == "")
                    {
 
 
                        ds = oCN.RunProcReturn($"select * from Xs_SendGoodsPlanBillMain where HSeOrderInterID in({list[i]["HSeOrderInterID"].ToString()}) and  HSeOrderEntryID in({list[i]["HSeOrderEntryID"].ToString()})", "Xs_SendGoodsPlanBillMain");
                        
 
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            objJsonResult.code = "0";
                            objJsonResult.count = 0;
                            objJsonResult.Message = $"第{i + 1}行销售订单有重复,请修改!";
                            objJsonResult.data = null;
                            return objJsonResult;
                        }
 
                        //新增单据号 id
                        HBillNo = DBUtility.ClsPub.CreateBillCode_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo, true);//获得一个新的单据号
                        HInterID = DBUtility.ClsPub.CreateBillID_Prod(BillType, ref DBUtility.ClsPub.sExeReturnInfo);//获得一个新的id
                        var HMainSourceInterID = list[i]["HSeOrderEntryID"].ToString();
                        var HMainSourceEntryID = list[i]["HSeOrderInterID"].ToString();
                        var HMaterID = list[i]["HMaterID"].ToString();
                        var HSendDate = list[i]["发货日期"].ToString();
                        var HOrgID = list[i]["HOrgID"].ToString();
                        var HUnitID = list[i]["HUnitID"].ToString();
                        var HQty = list[i]["发货数量"].ToString();
                        var HMainSourceBillNo = ds.Tables[0].Rows[0]["销售订单号"].ToString();
                        //主表
                        oCN.RunProc("insert into Xs_SendGoodsPlanBillMain(HInterID,HBillNo,HYear,HPeriod,HBillType," +
                            "HBillSubType,HDate,HBillStatus,HMaker,HMakeDate,HOrgID,HMainSourceInterID,HMainSourceEntryID,HMainSourceBillNo,HSeOrderInterID," +
                            "HSeOrderEntryID,HMaterID,HUnitID, HQty,HSendDate" +
                            ")values" +
                            $"({HInterID},'{HBillNo}',{DateTime.Now.Year},{DateTime.Now.Month},'{BillType}'," +
                            $"'{BillType}',GETDATE(),1,'{user}',GETDATE(),'{HOrgID}','{HMainSourceInterID}'," +
                            $"{HMainSourceEntryID},{HMainSourceBillNo},{HMainSourceInterID}, {HMainSourceEntryID},{HMaterID}, {HUnitID}," +
                            $" {(HQty == "" ? 0.ToString() : HQty)}, '{HSendDate}')");
 
                        //LogService.Write("sql:" + sql);
                        //主表
                        //oCN.RunProc(sql);
                    }
                    else
                    {
                        var hmainid = list[i]["hmainid"].ToString();
                        var HSendDate = list[i]["发货日期"].ToString();
                        var HQty = list[i]["发货数量"].ToString();
 
                        oCN.RunProc($"update Xs_SendGoodsPlanBillMain set HSendDate = '{HSendDate}',HQty={HQty} where HInterID ={hmainid}");
 
                    }
                }
 
                
                sw.Stop();//结束计时
                
 
                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
 
    }
}