using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using ICSSoft.Common; using ICSSoft.Entity; namespace ICSSoft.DataProject { public class ISComplete { private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"]; public string ConfirmRd08(List Bills) { int isif = 0; DataSet ds = null; string mag = ""; string sql = ""; DataTable dtCheck = null; string connS = ""; string newsql=""; VouchKey key = new VouchKey(); SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); List result = Bills.Select(t => t.WorkPoint).Distinct().ToList(); foreach (string WorkPoint in result) { try { connS = string.Format(connString, WorkPoint); conn = new System.Data.SqlClient.SqlConnection(connS); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; foreach (Complete head in Bills) { if (WorkPoint != head.WorkPoint) { continue; } if (head.Type == "生产订单") { sql = @" select count(c.AllocateID) num,d.number from DBO.mom_order a left join DBO.mom_orderdetail b on a.moid = b.moid left join DBO.mom_moallocate c on b.modid = c.modid left join(select modid,COUNT(AllocateID) number from DBO.mom_moallocate where WIPType=1 GROUP BY modid ) d on b.modid = d.modid where b.MoDId='{0}' GROUP BY d.number "; sql = string.Format(sql, head.DetailID); dtCheck = DBHelper.SQlReturnData(sql, cmd); if (dtCheck != null && dtCheck.Rows.Count > 0) { if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString()) { isif = 1; } else { isif = 0; } } newsql = "where b.MoDId='{0}'"; newsql = string.Format(newsql, head.DetailID); #region 齐套检验 sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' "; DataTable qt = DBHelper.SQlReturnData(sql, cmd); if (qt != null && qt.Rows.Count > 0) { if (qt.Rows[0]["cValue"].ToString() == "1") { sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from ( select a.MoCode,b.SortSeq, case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty from DBO.mom_order a left join DBO.mom_orderdetail b on a.moid = b.moid left join DBO.mom_moallocate c on b.modid = c.modid left join (select d.AllocateId,e.InvCode,d.MoallocateSubId from DBO.mom_moallocatesub d left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode where b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty ) tt group by MoCode,SortSeq"; sql = string.Format(sql, head.DetailID); } else if (qt.Rows[0]["cValue"].ToString() == "2") { sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'"; qt = DBHelper.SQlReturnData(sql, cmd); if (qt != null && qt.Rows.Count > 0) { if (qt.Rows[0]["cValue"].ToString() == "true") { sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from ( select a.MoCode,b.SortSeq, case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty from DBO.mom_order a left join DBO.mom_orderdetail b on a.moid = b.moid left join DBO.mom_moallocate c on b.modid = c.modid left join (select d.AllocateId,e.InvCode,d.MoallocateSubId from DBO.mom_moallocatesub d left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode where b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 and f.bInvKeyPart=1 group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty ) tt group by MoCode,SortSeq"; sql = string.Format(sql, head.DetailID); } else { sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from ( select a.MoCode,b.SortSeq, case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty from DBO.mom_order a left join DBO.mom_orderdetail b on a.moid = b.moid left join DBO.mom_moallocate c on b.modid = c.modid left join (select d.AllocateId,e.InvCode,d.MoallocateSubId from DBO.mom_moallocatesub d left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode where b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty ) tt group by MoCode,SortSeq"; sql = string.Format(sql, head.DetailID); } } } else { sql = @"select MoCode,SortSeq,MIN(lotqty) min_lotqty from ( select a.MoCode,b.SortSeq, case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end as AllocateId,sum(c.Qty) as Qty,SUM(c.IssQty) IssQty,SUM(c.IssQty)/(SUM(c.Qty)/b.Qty) lotqty from DBO.mom_order a left join DBO.mom_orderdetail b on a.moid = b.moid left join DBO.mom_moallocate c on b.modid = c.modid left join (select d.AllocateId,e.InvCode,d.MoallocateSubId from DBO.mom_moallocatesub d left join DBO.bas_part e ON d.partid = e.PartId ) aa on c.MoallocateSubId=aa.MoallocateSubId left join DBO.Inventory_Sub f on c.InvCode=f.cInvSubCode where b.MoDId='{0}' and c.WIPType<>1 and c.Qty<>0 and c.ByproductFlag<>1 group by a.MoCode,b.SortSeq,case when aa.AllocateId IS null then c.AllocateId else aa.AllocateId end ,b.Qty ) tt group by MoCode,SortSeq"; sql = string.Format(sql, head.DetailID); } } #endregion } else { sql = @" select count(c.MOMaterialsID) num,d.number from DBO.OM_MOMain a left join DBO.OM_MODetails b on a.moid = b.moid left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID left join(select MODetailsID,COUNT(MOMaterialsID) number from DBO.OM_MOMaterials where iWIPtype=1 GROUP BY MODetailsID ) d on b.MODetailsID = d.MODetailsID where b.MODetailsID='{0}' GROUP BY d.number "; sql = string.Format(sql, head.DetailID); dtCheck = DBHelper.SQlReturnData(sql, cmd); if (dtCheck != null && dtCheck.Rows.Count > 0) { if (dtCheck.Rows[0]["num"].ToString() != dtCheck.Rows[0]["number"].ToString()) { isif = 1; } else { isif = 0; } } newsql = " where b.MODetailsID='{0}' "; newsql = string.Format(newsql, head.DetailID); #region 齐套检验 sql = @" select cValue from AccInformation where cName=N'iMOProInCtrlBySet' "; DataTable qt = DBHelper.SQlReturnData(sql, cmd); if (qt != null && qt.Rows.Count > 0) { if (qt.Rows[0]["cValue"].ToString() == "1") { sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from ( select a.cCode,b.iVouchRowNo, c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty from DBO.OM_MOMain a left join DBO.OM_MODetails b on a.moid = b.moid left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode where b.MODetailsID='{0}' and c.iQuantity<>0 and c.iWIPtype<>1 group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity ) tt group by cCode,iVouchRowNo"; sql = string.Format(sql, head.DetailID); } else if (qt.Rows[0]["cValue"].ToString() == "2") { sql = @"select cValue from AccInformation where cName=N'bControlKeyMaterial'"; qt = DBHelper.SQlReturnData(sql, cmd); if (qt != null && qt.Rows.Count > 0) { if (qt.Rows[0]["cValue"].ToString() == "true") { sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from ( select a.cCode,b.iVouchRowNo, c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty from DBO.OM_MOMain a left join DBO.OM_MODetails b on a.moid = b.moid left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode where b.MODetailsID='{0}' and c.iQuantity<>0 and c.iWIPtype<>1 and f.bInvKeyPart=1 group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity ) tt group by cCode,iVouchRowNo"; sql = string.Format(sql, head.DetailID); qt = DBHelper.SQlReturnData(sql, cmd); } else { sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from ( select a.cCode,b.iVouchRowNo, c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty from DBO.OM_MOMain a left join DBO.OM_MODetails b on a.moid = b.moid left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode where b.MODetailsID='{0}' and c.iQuantity<>0 and c.iWIPtype<>1 group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity ) tt group by cCode,iVouchRowNo"; sql = string.Format(sql, head.DetailID); qt = DBHelper.SQlReturnData(sql, cmd); } } } else { sql = @"select cCode,iVouchRowNo,MIN(lotqty) min_lotqty from ( select a.cCode,b.iVouchRowNo, c.MOMaterialsID as AllocateId,sum(c.iQuantity) as Qty,SUM(c.iSendQTY) IssQty,SUM(c.iSendQTY)/(SUM(c.iQuantity)/b.iQuantity) lotqty from DBO.OM_MOMain a left join DBO.OM_MODetails b on a.moid = b.moid left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID left join DBO.Inventory_Sub f on c.cInvCode=f.cInvSubCode where b.MODetailsID='{0}' and c.iQuantity<>0 group by a.cCode,b.iVouchRowNo,c.MOMaterialsID,b.iQuantity ) tt group by cCode,iVouchRowNo"; sql = string.Format(sql, head.DetailID); } } #endregion } if (head.Type == "生产订单") { if (isif == 0) { sql = @"select DISTINCT a.MoCode,b.SortSeq,C.SortSeq SubSortSeq,(case isnull(d.min_lotqty,0) when 0 then b.Qty else d.min_lotqty end) min_lotqty, b.Qty,b.QualifiedInQty,b.InvCode,c.InvCode subInvCode,c.Qty AxuQTY,c.IssQty into #info from mom_order a LEFT JOIN mom_orderdetail b on a.moid = b.moid LEFT JOIN mom_moallocate c on b.modid = c.modid left join (" + sql + @") d on a.MoCode=d.MoCode and b.SortSeq=d.SortSeq 1=1 select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info drop table #info "; } else { sql = @"select DISTINCT a.MoCode,b.SortSeq,C.SortSeq SubSortSeq,isnull(d.min_lotqty,0) min_lotqty,b.Qty,b.QualifiedInQty,b.InvCode,c.InvCode subInvCode,c.Qty AxuQTY,c.IssQty into #info from mom_order a LEFT JOIN mom_orderdetail b on a.moid = b.moid LEFT JOIN mom_moallocate c on b.modid = c.modid left join (" + sql + @") d on a.MoCode=d.MoCode and b.SortSeq=d.SortSeq 1=1 select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info drop table #info "; } sql = sql.Replace("1=1", newsql); } else { if (isif == 0) { sql = @"select DISTINCT a.cCode MoCode,b.iVouchRowNo SortSeq,C.irowno SubSortSeq,(case isnull(d.min_lotqty,0) when 0 then b.iQuantity else d.min_lotqty end) min_lotqty, b.iQuantity Qty,b.iReceivedQTY QualifiedInQty,b.cInvCode InvCode,c.cInvCode subInvCode,c.iQuantity AxuQTY,c.iSendQTY IssQty into #info from OM_MOMain a left join DBO.OM_MODetails b on a.moid = b.moid left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID left join (" + sql + @") d on a.cCode=d.cCode and b.iVouchRowNo=d.iVouchRowNo 1=1 select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info drop table #info "; } else { sql = @"select DISTINCT a.cCode MoCode,b.iVouchRowNo SortSeq,C.irowno SubSortSeq,isnull(d.min_lotqty,0) min_lotqty,b.iQuantity Qty,b.iReceivedQTY QualifiedInQty,b.cInvCode InvCode,c.cInvCode subInvCode,c.iQuantity AxuQTY,c.iSendQTY IssQty into #info from OM_MOMain a left join DBO.OM_MODetails b on a.moid = b.moid left join DBO.OM_MOMaterials c on b.MODetailsID = c.MODetailsID left join (" + sql + @") d on a.cCode=d.cCode and b.iVouchRowNo=d.iVouchRowNo 1=1 select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,min_lotqty,Qty,QualifiedInQty,InvCode from #info select DISTINCT MoCode,SortSeq,MoCode+SortSeq GL,SubSortSeq,subInvCode,AxuQTY,IssQty from #info drop table #info "; } sql = sql.Replace("1=1", newsql); } if (ds != null) ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd)); else ds = DBHelper.SQlReturnDataSet(sql, cmd); } } catch (Exception ex) { cmd.Transaction.Rollback(); log.Error(ex.Message); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } mag = JSON.DataSetToJson(ds, "details", "GL"); return mag; } } }