using NFine.Data.Extensions; using System; using System.Collections.Generic; using System.Data; using System.Text; using NFine.Repository; using System.Data.Common; using NFine.Domain._03_Entity.SRM; using Newtonsoft.Json; using System.Configuration; using Newtonsoft.Json.Linq; using System.Net; using System.IO; using NFine.Domain._03_Entity.WMS; using System.Data.SqlClient; namespace NFine.Application.JHWMS { public class DeciliterApp : RepositoryFactory { /// /// 点击委外退料生成条码 /// public string GetNewLotNo(string LotNO) { DataTable dt = new DataTable(); List parameter = new List(); //string sql = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' "; string sql = @"SELECT TOP 1 LotNO as NewLotNo FROM ICSInventoryLot WHERE EATTRIBUTE1='{0}' ORDER BY CAST(SUBSTRING(LotNO, (LEN(LotNO)-CHARINDEX('-',REVERSE(LotNO))+1)+1,CHARINDEX('-',REVERSE(LotNO))-1) AS INT) DESC"; sql = string.Format(sql, LotNO); dt = Repository().FindTableBySql(sql.ToString()); if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "") { return LotNO + "-1"; } else { string newLotNO = dt.Rows[0]["NewLotNo"].ToString(); int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1; return LotNO + "-" + COUNT.ToString(); } } public string GetNewLotNoByPacking(string LotNO) { DataTable dt = new DataTable(); List parameter = new List(); //string sql = @"select max(LotNo) as NewLotNo from ICSInventoryLot where EATTRIBUTE1='{0}' "; string sql = @"SELECT TOP 1 LotNO as NewLotNo FROM ICSInventoryLot WHERE EATTRIBUTE1='{0}' ORDER BY CAST(SUBSTRING(LotNO, (LEN(LotNO)-CHARINDEX('-',REVERSE(LotNO))+1)+1,CHARINDEX('-',REVERSE(LotNO))-1) AS INT) DESC"; sql = string.Format(sql, LotNO); dt = Repository().FindTableBySql(sql.ToString()); if (dt == null || dt.Rows.Count == 0 || dt.Rows[0]["NewLotNo"].ToString() == "") { // return LotNO + "-1"; return "1"; } else { string newLotNO = dt.Rows[0]["NewLotNo"].ToString(); int COUNT = Convert.ToInt32(newLotNO.Substring(newLotNO.LastIndexOf('-') + 1)) + 1; //return LotNO + "-" + COUNT.ToString(); return COUNT.ToString(); } } public string Split(string Parameter) { string msg = ""; string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "LOTSplit/Create"; string result = HttpPost(APIURL, Parameter); JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText); string MessAge = Obj["Message"].ToString(); string Success = Obj["Success"].ToString(); if (Success.ToUpper() == "FALSE") { msg = MessAge; } return msg; } //接口api解析 public static string HttpPost(string url, string body) { try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*"; request.ContentType = "application/json; charset=utf-8"; byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { throw new Exception(ex.Message); } } /// /// 根据ID获取条码 /// /// public DataTable GetLotNoByID(string ID) { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string sql = @"select '' as ID,'' as LotNo union all SELECT ID,LotNo FROM ICSWareHouseLotInfo WITH (NOLOCK) WHERE ID in ({0}) "; sql = string.Format(sql, ID.TrimEnd(',')); //string role = NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode; //if (role != "admin") //{ // sql += " and b.WorkPoint='" + WorkPoint + "'"; //} DataTable dt = SqlHelper.GetDataTableBySql(sql); return dt; } //合批 public string Combine(string LotNo, string ID) { try { string connString = SqlHelper.DataCenterConnString; string msg = ""; string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string sql = @" select distinct LotNo,Quantity from ICSWareHouseLotInfo WHERE ID IN (" + ID.TrimEnd(',') + ") AND LotNo!='" + LotNo + "'"; DataTable dt = Repository().FindTableBySql(sql.ToString()); if (dt.Rows.Count == 0) { throw new Exception("未查询到相关条码信息,无法分批!"); } #region 验证条码批号是否相同,获取批号不同的条码在最后调用形态转换接口 string smtjson = ""; string smtjsondetail = ""; string smtsql = @" select a.LotNo AS FinalLotNo,e.BatchCode AS FinalBatchCode ,c.LotNo AS SourceLotNo,f.BatchCode AS SourceBatchCode,c.Quantity ,a.InvCode,a.WarehouseCode from ICSWareHouseLotInfo a inner join ICSInventoryLot b on b.LotNo=a.LotNo and b.WorkPoint=a.WorkPoint left join ICSWareHouseLotInfo c on c.ID in (" + ID.TrimEnd(',') + @") and c.LotNo !='" + LotNo + @"' and c.WorkPoint=a.WorkPoint inner join ICSInventoryLot d on d.LotNo=c.LotNo and d.WorkPoint=c.WorkPoint left join ICSExtension e on e.ID=b.ExtensionID left join ICSExtension f on f.ID=d.ExtensionID where a.LotNo='" + LotNo + "' and a.WorkPoint='" + WorkPoint + "'"; DataTable smtdt = Repository().FindTableBySql(smtsql.ToString()); if (smtdt.Rows.Count > 0) { int SequenceCount = 0; int GroupCount = 0; smtjson += "[ {"; smtjson += "\"User\": \"" + MUSER + "\","; smtjson += "\"MTime\": \"" + DateTime.Now + "\","; smtjson += "\"WorkPoint\": \"" + WorkPoint + "\","; smtjson += "\"UpdateStock\": \"true\","; smtjson += "\"cMemo\": \"\","; smtjson += "\"details\": [ "; smtjson += "<0>"; smtjson += " ] "; smtjson += "} ]"; foreach (DataRow smtdr in smtdt.Rows) { SequenceCount++; GroupCount++; if (smtdr["FinalBatchCode"].ToString() != smtdr["SourceBatchCode"].ToString()) { smtjsondetail += "{ "; smtjsondetail += "\"Sequence\": \"" + SequenceCount + "\","; smtjsondetail += "\"GroupNO\": \"" + GroupCount + "\","; smtjsondetail += "\"Type\": \"转换前\","; smtjsondetail += "\"InvCode\": \"" + smtdr["InvCode"].ToString() + "\","; smtjsondetail += "\"WHCode\": \"" + smtdr["WarehouseCode"].ToString() + "\","; smtjsondetail += "\"BatchCode\": \"" + smtdr["SourceBatchCode"].ToString() + "\","; smtjsondetail += "\"ProjectCode\": \"\","; smtjsondetail += "\"Quantity\": " + Convert.ToDecimal(smtdr["Quantity"].ToString()) + ","; smtjsondetail += "\"Amount\": 0,"; smtjsondetail += "\"Brand\": \"\","; smtjsondetail += "\"cFree1\": \"\","; smtjsondetail += "\"cFree2\": \"\","; smtjsondetail += "\"cFree3\": \"\","; smtjsondetail += "\"cFree4\": \"\","; smtjsondetail += "\"cFree5\": \"\","; smtjsondetail += "\"cFree6\": \"\","; smtjsondetail += "\"cFree7\": \"\","; smtjsondetail += "\"cFree8\": \"\","; smtjsondetail += "\"cFree9\": \"\","; smtjsondetail += "\"cFree10\": \"\""; smtjsondetail += " },"; SequenceCount++; smtjsondetail += "{ "; smtjsondetail += "\"Sequence\": \"" + SequenceCount + "\","; smtjsondetail += "\"GroupNO\": \"" + GroupCount + "\","; smtjsondetail += "\"Type\": \"转换后\","; smtjsondetail += "\"InvCode\": \"" + smtdr["InvCode"].ToString() + "\","; smtjsondetail += "\"WHCode\": \"" + smtdr["WarehouseCode"].ToString() + "\","; smtjsondetail += "\"BatchCode\": \"" + smtdr["FinalBatchCode"].ToString() + "\","; smtjsondetail += "\"ProjectCode\": \"\","; smtjsondetail += "\"Quantity\": " + Convert.ToDecimal(smtdr["Quantity"].ToString()) + ","; smtjsondetail += "\"Amount\": 0,"; smtjsondetail += "\"Brand\": \"\","; smtjsondetail += "\"cFree1\": \"\","; smtjsondetail += "\"cFree2\": \"\","; smtjsondetail += "\"cFree3\": \"\","; smtjsondetail += "\"cFree4\": \"\","; smtjsondetail += "\"cFree5\": \"\","; smtjsondetail += "\"cFree6\": \"\","; smtjsondetail += "\"cFree7\": \"\","; smtjsondetail += "\"cFree8\": \"\","; smtjsondetail += "\"cFree9\": \"\","; smtjsondetail += "\"cFree10\": \"\""; smtjsondetail += " },"; } } smtjsondetail = smtjsondetail.TrimEnd(','); smtjson = smtjson.Replace("<0>", smtjsondetail); } #endregion using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString)) { conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; cmd.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["TimeOut"]); try { string Identification = Guid.NewGuid().ToString(); #region 合并前目标条码日志 sql = @" INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode, FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity, Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID, ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName, MTIME,WorkPoint,EATTRIBUTE1) SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode , '','',c.WarehouseCode,c.LocationCode,c.Quantity, '','0','{4}','{5}','0','', '','','',f.F_Account ,f.F_RealName , SYSDATETIME() ,a.WorkPoint ,'' FROM ICSInventoryLot a INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'"; sql = string.Format(sql, LotNo, WorkPoint, MUSER, Identification, "5", "33"); cmd.CommandText = sql; int result = cmd.ExecuteNonQuery(); if (result == 0) { throw new Exception("合批前目标条码日志记录写入失败!"); } #endregion foreach (DataRow dr in dt.Rows) { #region 合并前来源条码日志 sql = @" INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode, FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity, Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID, ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName, MTIME,WorkPoint,EATTRIBUTE1) SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode , '','',c.WarehouseCode,c.LocationCode,c.Quantity, '','0','{4}','{5}','0','', '','','',f.F_Account ,f.F_RealName , SYSDATETIME() ,a.WorkPoint ,'' FROM ICSInventoryLot a INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location WHERE a.LotNo='{0}' AND a.WorkPoint='{1}'"; sql = string.Format(sql, dr["LotNo"], WorkPoint, MUSER, Identification, "5", "33"); cmd.CommandText = sql; result = cmd.ExecuteNonQuery(); if (result == 0) { throw new Exception("合批前来源条码日志记录写入失败!"); } #endregion #region 更新目标条码库存 sql = @" IF NOT EXISTS(SELECT a.LotNo FROM ICSInventoryLot a INNER JOIN ICSInventoryLot b ON b.LotNo='{3}' AND a.InvCode=b.InvCode AND a.WorkPoint=b.WorkPoint WHERE a.LotNo='{0}' AND a.WorkPoint='{1}') BEGIN RAISERROR('所选条码物料不同,无法合批',16,1); RETURN END IF NOT EXISTS(SELECT a.LotNo FROM ICSWareHouseLotInfo a INNER JOIN ICSWareHouseLotInfo b ON b.LotNo='{3}' AND a.WarehouseCode=b.WarehouseCode AND a.WorkPoint=b.WorkPoint INNER JOIN ICSInventoryLot c on a.LotNo=c.LotNo and a.WorkPoint=c.WorkPoint INNER JOIN ICSInventoryLot d on b.LotNo=d.LotNo and b.WorkPoint=d.WorkPoint WHERE a.LotNo='{0}' AND a.WorkPoint='{1}') BEGIN RAISERROR('不同仓库条码,不能合并',16,1); RETURN END UPDATE ICSWareHouseLotInfo SET Quantity=ISNULL(Quantity,0)+{2} WHERE LotNo='{0}' AND WorkPoint='{1}' UPDATE ICSInventoryLot SET Amount=(ISNULL(Amount,0)+(select Amount from ICSInventoryLot where LotNo='{3}' AND WorkPoint='{1}')) WHERE LotNo='{0}' AND WorkPoint='{1}'"; sql = string.Format(sql, LotNo, WorkPoint, Convert.ToDecimal(dr["Quantity"]), dr["LotNo"].ToString()); cmd.CommandText = sql; result = cmd.ExecuteNonQuery(); if (result == 0) { throw new Exception("目标条码库存增加失败!"); } #endregion #region 更新来源条码库存 sql = @" UPDATE ICSWareHouseLotInfo SET Quantity=ISNULL(Quantity,0)-'{2}' WHERE LotNo='{0}' AND WorkPoint='{1}' UPDATE ICSInventoryLot SET Amount=(ISNULL(Amount,0)-(select Amount from ICSInventoryLot where LotNo='{0}' AND WorkPoint='{1}')) WHERE LotNo='{0}' AND WorkPoint='{1}' IF EXISTS(SELECT a.LotNo FROM ICSWareHouseLotInfo a WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' AND Quantity<0) BEGIN RAISERROR('来源条码库存不足,请确认条码库存是否出现变更!',16,1); RETURN END"; sql = string.Format(sql, dr["LotNo"].ToString(), WorkPoint, Convert.ToDecimal(dr["Quantity"])); cmd.CommandText = sql; result = cmd.ExecuteNonQuery(); if (result == 0) { throw new Exception("来源条码库存扣减失败!"); } #endregion #region 来源条码合并后日志写入 sql = @" INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode, FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity, Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID, ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName, MTIME,WorkPoint,EATTRIBUTE1) SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode , '','',c.WarehouseCode,c.LocationCode,c.Quantity, '','0','{4}','{5}','0','', '','','',f.F_Account ,f.F_RealName , SYSDATETIME() ,a.WorkPoint ,'' FROM ICSInventoryLot a INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' "; sql = string.Format(sql, dr["LotNo"].ToString(), WorkPoint, MUSER, Identification, "5", "34"); cmd.CommandText = sql; result = cmd.ExecuteNonQuery(); if (result == 0) { throw new Exception("来源条码合并后日志写入失败!"); } #endregion } #region 合并后目标条码日志写入 sql = @" INSERT INTO ICSWareHouseLotInfoLog(ID,Identification,TransCode,TransSequence,LotNo,InvCode, FromWarehouseCode,FromLocationCode,ToWarehouseCode,ToLocationCode,Quantity, Memo,Lock,TransType,BusinessCode,ERPUpload,ERPID, ERPDetailID,ERPCode,ERPSequence,MUSER,MUSERName, MTIME,WorkPoint,EATTRIBUTE1) SELECT NEWID(),'{3}',b.TransCode,b.TransSequence,a.LotNo ,a.InvCode , '','',c.WarehouseCode,c.LocationCode,c.Quantity, '','0','{4}','{5}','0','', '','','',f.F_Account ,f.F_RealName , SYSDATETIME() ,a.WorkPoint ,'' FROM ICSInventoryLot a INNER JOIN ICSInventoryLotDetail b ON a.LotNo=b.LotNo AND a.WorkPoint=b.WorkPoint INNER JOIN ICSWareHouseLotInfo c ON a.LotNo=c.LotNo AND a.WorkPoint=c.WorkPoint INNER JOIN Sys_SRM_User f ON f.F_Account='{2}' AND a.WorkPoint=f.F_Location WHERE a.LotNo='{0}' AND a.WorkPoint='{1}' "; sql = string.Format(sql, LotNo, WorkPoint, MUSER, Identification, "5", "34"); cmd.CommandText = sql; result = cmd.ExecuteNonQuery(); if (result == 0) { throw new Exception("合并后目标条码日志写入失败!"); } #endregion if (smtjsondetail != "") { #region 开始调用形态转换接口 string APIURL = ConfigurationManager.ConnectionStrings["ERPAPIURL"].ConnectionString + "MorphologicalTransformationDoc/Create"; string apiresult = HttpPost(APIURL, smtjson); JObject Obj = (JObject)JsonConvert.DeserializeObject(apiresult);//或者JObject jo = JObject.Parse(jsonText); string MessAge = Obj["Message"].ToString(); string Success = Obj["Success"].ToString(); string ERPSql = ""; if (Success.ToUpper() == "FALSE") { throw new Exception("ERP接口调用失败:" + MessAge); } #endregion } cmd.Transaction.Commit(); return ""; } catch (Exception ex) { if (cmd.Transaction != null) cmd.Transaction.Rollback(); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } } catch (Exception ex) { return ex.Message; } } } }