using NFine.Data.Extensions; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using NFine.Code; using NFine.Repository; using System.Data.Common; using NFine.Domain._03_Entity.SRM; using ICS.Application.Entity; using Newtonsoft.Json; using System.Configuration; using System.Data.SqlClient; using ICS.Data; using Newtonsoft.Json.Linq; namespace NFine.Application.WMS { public class ICSDisassemblyDocApp : RepositoryFactory { public DataTable GetGridJson(string queryJson, ref Pagination jqgridparam) { DataTable dt = new DataTable(); var queryParam = queryJson.ToJObject(); List parameter = new List(); #region [SQL] string sql = @" select a.DABDOCType, a.DABDOCCode as Code, a.Sequence as Seq, a.Quantity as Qty, a.InvCode, a.WHCode, b.InvName, CASE WHEN c.TransSequence IS NOT NULL THEN '已选' ELSE '未选' END AS isInput, CASE WHEN f.TransCode IS NOT NULL THEN '已生成' ELSE '未生成' END AS isCreate,e.* from ICSDisassemblyDoc a left join ICSInventory b on a.InvCode=b.InvCode and a.WorkPoint=b.WorkPoint left join (select TransCode,TransSequence,WorkPoint FROM ICSWareHouseLotInfoLog WHERE BusinessCode='37' group by TransCode,TransSequence,WorkPoint)c ON a.DABDOCCode=c.TransCode AND a.Sequence=c.TransSequence AND a.WorkPoint=c.WorkPoint left join (select TransCode,WorkPoint FROM ICSInventoryLotDetail group by TransCode,WorkPoint)f ON a.DABDOCCode=f.TransCode AND a.WorkPoint=f.WorkPoint LEFT JOIN ICSWarehouse d ON a.WHCode=d.WarehouseCode AND a.WorkPoint=d.WorkPoint left join ICSExtension e on a.ExtensionID=e.ID and a.WorkPoint=e.WorkPoint where a.DABDOCType='1'"; sql = string.Format(sql, DbHelper.GetErpIp(), DbHelper.GetErpName()); #endregion if (!string.IsNullOrWhiteSpace(queryJson)) { if (!string.IsNullOrWhiteSpace(queryParam["DABDOCCode"].ToString())) { sql += " and a.DABDOCCode like '%" + queryParam["DABDOCCode"].ToString() + "%' "; } } if (NFine.Code.OperatorProvider.Provider.GetCurrent().RoleEnCode != "admin") { sql += " and a.WorkPoint='" + NFine.Code.OperatorProvider.Provider.GetCurrent().Location + "'"; } return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam); } public DataTable GetSubGridJson(string Code) { DataTable dt = new DataTable(); //var queryParam = queryJson.ToJObject(); string sql = string.Empty; List parameter = new List(); string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location.TrimEnd(','); sql = @"select a.Sequence, a.DABDOCType, a.DABDOCCode as Code, a.Quantity as Qty, a.InvCode, a.WHCode, e.BatchCode, b.InvName, f.LotNo from ICSDisassemblyDoc a left join ICSInventory b on a.InvCode=b.InvCode and a.WorkPoint=b.WorkPoint left join (select TransCode,TransSequence,WorkPoint,LotNo FROM ICSInventoryLotDetail group by TransCode,TransSequence,WorkPoint,LotNo)f ON a.DABDOCCode=f.TransCode AND a.Sequence=f.TransSequence AND a.WorkPoint=f.WorkPoint LEFT JOIN ICSWarehouse d ON a.WHCode=d.WarehouseCode AND a.WorkPoint=d.WorkPoint left join ICSExtension e on a.ExtensionID=e.ID and a.WorkPoint=e.WorkPoint where a.DABDOCType='2' and a.DABDOCCode='{0}' order by a.Sequence"; sql = string.Format(sql, Code, WorkPoint); return Repository().FindTableBySql(sql.ToString()); } //查询符合套件的在库条码 public DataTable GetLotNoByKit(string InvCode, string WHCode, string BatchCode, ref Pagination jqgridparam) { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; List parameter = new List(); DataTable table = new DataTable(); string sql = string.Empty; sql = @"select a.LotNo,a.InvCode,a.WarehouseCode,a.LocationCode,c.BatchCode,a.Quantity from dbo.ICSWareHouseLotInfo a left join dbo.ICSInventoryLot b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint left join ICSExtension c on b.ExtensionID=c.ID and b.WorkPoint=c.WorkPoint where a.InvCode='{0}' and a.WarehouseCode='{1}' and isnull(c.BatchCode,'')='{2}'"; sql = string.Format(sql, InvCode, WHCode, BatchCode); //return Repository().FindTableBySql(sql.ToString()); return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam); } //绑定套件 public string CreateLogByKit(string Code, string Parameter) { string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName; string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string msg = ""; string sql = string.Empty; JArray res = (JArray)JsonConvert.DeserializeObject(Parameter); string Identification = Guid.NewGuid().ToString(); foreach (var item in res) { JObject jo = (JObject)item; sql += @"update ICSWareHouseLotInfo set LockQuantity='{6}' where LotNo='{2}' and WorkPoint='{9}'"; 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) Values(NEWID(),'{0}','{1}','1','{2}','{3}','{4}','{5}','','','{6}','',1,'11','37',0,'','','','','{7}','{8}',getdate(),'{9}','')"; sql = string.Format(sql, Identification, Code, jo["LotNo"].ToString(), jo["InvCode"].ToString(), jo["WHCode"].ToString(), jo["LocationCode"].ToString(), jo["Quantity"].ToString(), MUSER, MUSERNAME, WorkPoint); } try { if (SqlHelper.CmdExecuteNonQueryLi(sql) > 0) { } else { msg = "绑定失败"; } } catch (Exception ex) { throw new Exception(ex.Message); } return msg; } //批量生成条码 public string CreateInventoryLot(string Code) { string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode; string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName; string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string msg = ""; string sql = string.Empty; string sqls= string.Empty; string sqlLot = string.Empty; sqls = @"select DABDOCCode,Sequence,InvCode,Quantity,Amount,ExtensionID from ICSDisassemblyDoc where DABDOCType='2' and DABDOCCode='{0}'"; sqls = string.Format(sqls, Code); var parent = Repository().FindTableBySql(sqls.ToString()); sqlLot = string.Format(@"SELECT A.LotNO AS LOTNO FROM ICSInventoryLot A WHERE A.LotNO LIKE '{0}%'", Code); var parentLot = Repository().FindTableBySql(sqlLot.ToString()); foreach (DataRow dts in parent.Rows) { string LotNo = ""; string DABDOCCode = dts["DABDOCCode"].ToString(); string Sequence = dts["Sequence"].ToString(); var drs=parentLot.Select("LOTNO", "LOTNO DESC"); if (drs.Length==0) { LotNo = DABDOCCode + Sequence + "00001"; } else { var OldLotNo = drs[0]["LOTNO"].ToString(); LotNo = DABDOCCode + Sequence + (Convert.ToInt64(OldLotNo.Substring(OldLotNo.Length - 5))+1).ToString().PadLeft(5,'0'); } sql += @"INSERT INTO ICSInventoryLot(ID,LotNo,InvCode,ProductDate,ExpirationDate,Quantity,AmountRate,ExtensionID,Type, PrintTimes,LastPrintUser,LastPrintTime,MUSER,MUSERName,MTIME,WorkPoint) Values(NEWID(),'{0}','{1}',getdate(),'2999-12-31','{2}','{3}','{4}','9','','','','{5}','{6}',getdate(),'{7}')"; sql+= @"INSERT INTO ICSInventoryLotDetail( LotNo,TransCode,TransSequence,MUSER,MUSERName,MTIME,WorkPoint) Values('{0}','{8}','{9}','{5}','{6}',getdate(),'{7}')"; sql = string.Format(sql, LotNo, dts["InvCode"].ToString(), dts["Quantity"].ToString(), dts["Amount"].ToString(), dts["ExtensionID"].ToString(), MUSER, MUSERNAME, WorkPoint, dts["DABDOCCode"].ToString(), dts["Sequence"].ToString()); } try { if (SqlHelper.CmdExecuteNonQueryLi(sql.ToString()) > 0) { } else { msg = "生成失败"; } } catch (Exception ex) { throw new Exception(ex.Message); } return msg; } //批量删除条码 public string DeleteInventoryLot(string Code) { string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location; string msg = string.Empty; string sql = string.Format(@"select * from dbo.ICSInventoryLot a inner join dbo.ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint inner join dbo.ICSWareHouseLotInfo c on a.LotNo=c.LotNo and a.WorkPoint=c.WorkPoint where b.TransCode='{0}' and a.WorkPoint='{1}'", Code, WorkPoint); DataTable dtASN = SqlHelper.GetDataTableBySql(sql); if (dtASN != null && dtASN.Rows.Count > 0) { msg = "该单据下的条码已入库,无法删除!"; } if (string.IsNullOrEmpty(msg)) { string sqls = string.Format("delete a from ICSInventoryLot a inner join ICSInventoryLotDetail b on a.LotNo=b.LotNo and a.WorkPoint=b.WorkPoint where b.TransCode='{0}' and a.WorkPoint='{1}'", Code, WorkPoint); sqls += string.Format("delete from ICSInventoryLotDetail where TransCode='{0}' and WorkPoint='{1}'", Code, WorkPoint); SqlHelper.CmdExecuteNonQueryLi(sqls); } return msg; } } }