1
yusijie
2025-09-09 c6291a646588c665968208a6260feff070314e37
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
using DBUtility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SQLHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Script.Serialization;
using WebAPI.Code;
using WebAPI.Models;
using WebAPI.Utility;
 
namespace WebAPI.Controllers
{
    public class SRM_MaterialBarCodeBillController : ApiController
    {
        private JsonResult objJsonResult = new JsonResult();
        private json objjson = new json();
        public new ClsCN oCn = new ClsCN();
        //获取系统参数
        Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
        // GET: SRM_MaterialBarCodeBill
        #region 条码批量生辰
        [Route("GetSRMHBarCodeShowBillMain")]
        public object GetSRMHBarCodeShowBillMain(string HInterID)
        {
            try
            {
                DataSet ds;
                string s = "";
                string ShowItem = " * ";//显示的字段
                //if (DBUtility.ClsPub.isLong(HInterID) == 0)
                //{
                //    objjson.code = "0";
                //    objjson.count = 0;
                //    objjson.Message = "获取失败";
                //    objjson.data = null;
                //    return objjson;
                //}
                DAL.ClsWeb_BarCodeBill oClsWeb_BarCodeBill = new DAL.ClsWeb_BarCodeBill();
                //string sql = string.Format(@"select * from h_v_Cg_POOrderBillMain_ForWeb2 where HInterID = " + sMsg);
                string sql = string.Format(@"select * from h_v_SRM_BarCodeBill where HItemID in  ( " + HInterID + ")");
                ds = oCn.RunProcReturn(sql, "h_v_SRM_BarCodeBill");
                //ds = oClsWeb_BarCodeBill.ShowBillMain1(DBUtility.ClsPub.isLong(sMsg), ShowItem, ref DBUtility.ClsPub.sExeReturnInfo);
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败";
                    objjson.data = null;
                    return objjson;
                }
                else
                {
 
                    objjson.code = "0";
                    objjson.count = 10000;
                    objjson.Message = "获取成功";
                    objjson.data = ds.Tables[0];
                    return objjson;
                }
            }
            catch (Exception e)
            {
                objjson.code = "0";
                objjson.count = 0;
                objjson.Message = "获取失败" + e.ToString();
                objjson.data = null;
                return objjson;
            }
        }
        #endregion
 
        #region 条码保存
        [Route("SRM_MaterialBarCodeBillController/GetBarcodeSaveBillBoxBycg")]
        [HttpPost]
        /// <summary>
        /// 生成条码 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="msg2"></param>
        /// <returns></returns>
        public object GetBarcodeSaveBillBoxBycg([FromBody] JObject msg)
        {
            string FCusName = oSystemParameter.GetSingleSystemParameter("WMS_CampanyName", ref DBUtility.ClsPub.sExeReturnInfo);
            var _value = msg["msg"].ToString();
            string msg1 = _value.ToString();
            string[] sArray = msg1.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            string msg2 = sArray[0].ToString();//
            string msg3 = sArray[1].ToString();// 
            string UserName = "";
            ListModels oListModels = new ListModels();
            try
            {
                #region 获取用户以及相关参数
                List<Model.Cls_MaterialHandingModel> lsmain = new List<Model.Cls_MaterialHandingModel>();
                msg2 = msg2.Replace("\\", "");
                msg2 = msg2.Replace("\n", "");  //\n
                lsmain = oListModels.getObjectByJson_MaterialHanding(msg2);
                int get_BarCodecount = 0;
                foreach (Model.Cls_MaterialHandingModel oItem in lsmain)
                {
                    get_BarCodecount++;
                    UserName = oItem.HMaker;
                }
                DAL.ClsWeb_BarCodeBill oBill = new DAL.ClsWeb_BarCodeBill();
                msg3 = msg3.Substring(1, msg3.Length - 2);
                msg3 = msg3.Replace("\\", "");
                msg3 = msg3.Replace("\n", "");  //\n
                #endregion
                List<Model.ClsGy_MaterialBarCodeBillModel> ls = new List<Model.ClsGy_MaterialBarCodeBillModel>();
 
                ls = oListModels.getObjectByJson_BarCodeBill(msg3);//列表数据model
 
 
                #region 数据准备
                long linterid = Pub_Class.ClsPub.CreateBillID_SRMProd("8888", ref DBUtility.ClsPub.sExeReturnInfo);
                int LSHlen = 6;             //流水号长度
                int SumLen = 10;            //总长度
                string TM = "";             //条码
                double HSumQty = 0;         //产品数量
                int LSH = 0;                //流水号
                string LSH2 = "";           //流水号转换成字符
                string sDate = "";            //日期
                string sYear = "";          //年
                string sPeriod = "";        //月
                string sDay = "";            //日
                string WeiShu = ""; //尾数
                string HKFDQDate= ls[0].HKFDQDate;//到期日期
                long HKFPeriod= (ls[0].HKFPeriod.ToString()==""?0: ls[0].HKFPeriod);//保质期
                string HKFDate= ls[0].HKFDQDate==null? DateTime.Now.ToString("yyyy/MM/dd"): ls[0].HKFDQDate;//生产日期
                string HKFDate_1 = string.Join("", HKFDate.Split('/').Select(x => x.PadLeft(2, '0')));
                double HMinQty= ls[0].HMinQty;//标准包装数
 
                double HBQty= ls[0].HBQty;//最小包装数
                long HISKFPERIOD= ls[0].HISKFPERIOD;//是否启用保质期
                long HBatchManager= ls[0].HBatchManager;//是否启用批号
 
                string HSourceBillType= ls[0].HSourceBillType;//条码类型
 
                string HBatchNo= ls[0].HBatchNo==null?"": ls[0].HBatchNo;//批号
                //物料
                long HMaterID=ls[0].HMaterID;
                string HMaterNumber= ls[0].HMaterNumber;
                string HMaterShortNumber = ls[0].HMaterShortNumber;
                string HMaterName = ls[0].HMaterName;
                //供应商
                long HSupID= ls[0].HSupID;
                string HSupName= ls[0].HSupName;
                string HMaker= ls[0].HMaker;
                DateTime HMakeDate= ls[0].HMakeDate;
                double HQty= ls[0].HQty; //本次生成条码数量
                double biaoqianzhangshu= ls[0].biaoqianzhangshu;
        //流水号
        DataSet Ds;
                SQLHelper.ClsCN oCn = new SQLHelper.ClsCN();
                oCn.BeginTran();
                Ds = oCn.RunProcReturn("exec GetLSH '" + DateTime.Today + "'", "GetLSH");
                LSH = ClsPub.isInt(Ds.Tables[0].Rows[0][0]);
                #endregion
 
                foreach (Model.ClsGy_MaterialBarCodeBillModel oItemSub in ls)
                {
                    //数量为0 跳过
                    if (oItemSub.HQty == 0 || oItemSub.HMinQty == 0 )
                        continue;
                    if(oItemSub.HBatchManager!=0&&oItemSub.HBatchNo=="")
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "已启用批号,批号不能为空,请核对";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    if (oItemSub.HISKFPERIOD != 0 && oItemSub.HKFPeriod == 0)
                    {
                        objJsonResult.code = "0";
                        objJsonResult.count = 0;
                        objJsonResult.Message = "已启用保质期,保质期不能为0,请核对";
                        objJsonResult.data = null;
                        return objJsonResult;
                    }
                    string sql = "";
                    //校验批号 和 生产日期
                    //string sql = $"select top 1 HKFDate from Gy_BarCodeBill where HSupID = '{oItemSub.HSupID}' and HBatchNo = '{HBatchNo.TrimStart().TrimEnd()}' and HMaterID ={oItemSub.HMaterID} and isnull(HKFDate, '') <> ''";
                    //LogService.Write($"sql:{sql}");
                    //DataTable dt = oCn.RunProcReturn(sql, "tables").Tables[0];
                    //if (dt.Rows.Count > 0)
                    //{
                    //    LogService.Write($"HKFDate:{dt.Rows[0][0]}");
                    //    sql = $"select DATEDIFF(D,{dt.Rows[0][0]},{HKFDate})";
                    //    DataSet ds = oCn.RunProcReturn(sql, "tables2");
                    //    LogService.Write($"DATEDIFF:{Convert.ToInt32(ds.Tables[0].Rows[0][0])}");
                    //    if (ds.Tables[0].Rows.Count > 0 && Convert.ToInt32(ds.Tables[0].Rows[0][0]) != 0)
                    //    {
                    //        objJsonResult.code = "0";
                    //        objJsonResult.count = 0;
                    //        objJsonResult.Message = $"物料编码{oItemSub.HMaterNumber},批号{oItemSub.HBatchNo.TrimStart().TrimEnd()},生产日期为{dt.Rows[0][0]},请核对";
                    //        objJsonResult.data = null;
                    //        return objJsonResult;
                    //    }
                    //}
                    if (ClsPub.isLong(oItemSub.HMaterID) != 0)
                    {
 
                        HMaterID = oItemSub.HMaterID;//物料代码
                        HQty = ClsPub.isDoule(oItemSub.HQty);//本次生成条码数量
                        HBQty = ClsPub.isInt(oItemSub.HBQty);//最小包装数(内箱数)
                        HMinQty = ClsPub.isDoule(oItemSub.HMinQty);//标准包装数
                        int virtualCount = 1;//实际上插入数量
                        // HSumQty = ClsPub.isDoule(oItemSub.HQty);  //产品数量
                       for (int i = 0; i < biaoqianzhangshu; i++)
                        {
                            ///供应商名称+@+物料代码+@+物料简称+@+数量+@+日期+@+流水号  
                            LSH += 1;
                            TM = $"{oItemSub.HSupName}@{oItemSub.HMaterID}@{oItemSub.HMaterName}@{oItemSub.biaoqianzhangshu}@{HKFDate_1}@{LSH}";
                            Model.ClsGy_BarCodeBill_WMS_Model bar = new Model.ClsGy_BarCodeBill_WMS_Model();
 
                           // var HBarCode_verify = TM.Split('@'); //校验条码信息
                           // var HMaterNumber_verify = HBarCode_verify[0]; //校验物料编码
                           // var HMaterName_verify = HBarCode_verify[1]; //校验物料名称
                           // //var HMaterModel_verify = HBarCode_verify[2]; //校验规格型号
                           // var HSupName_verify = HBarCode_verify[7]; //校验供应商
                           //// var HSourceBillNo_verify = HBarCode_verify[8]; //校验采购订单号
 
                           // sql = $"select * from Gy_Material where HNumber = '{HMaterNumber_verify}' and HName = '{HMaterName_verify}'";
                           // DataSet verifyMatInfo = oCn.RunProcReturn(sql, "tables");
 
 
                           // sql = $"select * from Gy_Supplier where HName = '{HSupName_verify}'";
                           // DataSet verifySupInfo = oCn.RunProcReturn(sql, "tables");
                            string HBarCodeType = "唯一条码";
                            //sql = "insert into Gy_BarCodeBill (HInterID,HBarCode,HBarCodeType,HMaterID,HUnitID,HQty,HKFDate,HKFPeriod,HKFDQDate" +
                            //               ",HBatchNo,HBarcodeQtys,HBarcodeNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HEndQty,HSupflag" +
                            //               ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HMTONO,HProduceDate,HExpiryDate,HISKFPERIOD" +
                            //               ") values ("
                            //               + linterid.ToString() + ",'" + TM + "','" + HBarCodeType + "'," + HMaterID + "," + "" + "," + HQty + ",'" + HKFDate + "'," + HKFPeriod + ",'" + HKFDQDate
                            //               + "','" + HBatchNo + "','" + get_BarCodecount + "','" + virtualCount + "'," + HSupID + "," + "" + ",'" + ClsPub.CurUserName + "','" + DateTime.Today + "'," + "1" + ",'" + DateTime.Today + "',1"
                            //               + "," + "1" + "," + "1" + ",'" + "1" + "','" + HSourceBillType + "','" + "1" + "','"
                            //               + HKFDate + "','" + HKFDQDate + "'," + HISKFPERIOD + ")";
                            sql = "INSERT INTO Gy_BarCodeBill (HInterID, HBarCode, HBarCodeType, HMaterID, HUnitID, HQty, HKFDate, HKFPeriod, HKFDQDate, " +
       "HBatchNo, HBarcodeQtys, HBarcodeNo, HSupID, HGroupID, HMaker, HMakeDate, HPrintQty, HEndQty, HSupflag, " +
       "HSourceInterID, HSourceEntryID, HSourceBillNo, HSourceBillType, HMTONO, HProduceDate, HExpiryDate, HISKFPERIOD) " +
      "VALUES (" +
       linterid.ToString() + ", '" + TM + "', '" + HBarCodeType + "', " + HMaterID + ", " + "0" + ", " + HQty + ", '" + HKFDate + "', " + HKFPeriod + ", '" + HKFDQDate + "', " +
       "'" + HBatchNo + "', " + get_BarCodecount + ", '" + virtualCount + "', " + HSupID + ", " + "0" + ", '" + ClsPub.CurUserName + "', '" + DateTime.Today.ToString("yyyy-MM-dd") + "', " + "1" + ", " + "0" + ", 1, " +
       "1, 1, '1', '" + HSourceBillType + "', '1', '" +
       HKFDate + "', '" + HKFDQDate + "', " + HISKFPERIOD + ")";
                            LogService.Write("生成条码: " + sql);
                            oCn.RunProc(sql);
                            oCn.RunProc("exec setLSH '" + DateTime.Today + "'");
                            virtualCount++;
                           }
 
                        }
                }
                oCn.Commit();
 
            }
            catch (Exception e)
            {
                LogService.Write("生成条码发生错误: " + e);
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "" + e.Message + DBUtility.ClsPub.sExeReturnInfo;
                objJsonResult.data = null;
                return objJsonResult;
            }
 
            objJsonResult.code = "0";
            objJsonResult.count = 1;
            objJsonResult.Message = "生成条码成功!";
            WebAPIController.Add_Log("条码下推", UserName, "生成条码");
            objJsonResult.data = null;
            return objJsonResult;
        }
        #endregion
 
        #region 条码列表
        [Route("showHBarCodeBillDetail")]
        [HttpGet]
        public object showHBarCodeBillDetail(string sMsg,string Organization)
        {
            try
            {
                DataSet ds;
                string s = "";
                string ShowItem = " * ";//显示的字段
                if (sMsg == "")
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败";
                    objjson.data = null;
                    return objjson;
                }
                string sql = string.Format(@"select * from h_v_Gy_UserMaterRelationPrint where HItemID in (   " + sMsg)+")";
                ds = oCn.RunProcReturn(sql, "h_v_Gy_UserMaterRelationPrint");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败" + DBUtility.ClsPub.sExeReturnInfo;
                    objjson.data = null;
                    return objjson;
                }
                else
                {
                    objjson.code = "0";
                    objjson.count = 1;
                    objjson.Message = "获取成功!";
                    objjson.data = ds.Tables[0];
                    return objjson;
 
                }
            }
            catch (Exception e)
            {
                objjson.code = "0";
                objjson.count = 0;
                objjson.Message = "获取单据表体失败:" + e.Message;
                objjson.data = null;
                return objjson;
            }
        }
        #endregion
 
 
    }
}