using ICSSoft.Common; using ICSSoft.Entity; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ICSSoft.DataProject { /// /// 生产退料单(红字材料出库) /// public class MOIssueDocNegatives { private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"]; private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"]; private static string Type = System.Configuration.ConfigurationManager.AppSettings["Type"]; private static string CRNALL = System.Configuration.ConfigurationManager.AppSettings["MoICRDNAME"]; public string Get(List infos) { List szJson = new List(); DataTable dt = null; DataTable dtNew = null; string connS = ""; string json = ""; if (infos.Count <= 0) { throw new Exception("传送数据为空!"); } string res = string.Empty; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); string sql = string.Empty; List result = infos.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 (ICSMOIssueDocNegative info in infos) { if (WorkPoint != info.WorkPoint) continue; ICSUserInfo userInfo = new ICSUserInfo(); userInfo = DBHelper.GetPersonInfo(info.User, cmd); if (info.MTime < new DateTime(2000, 01, 01)) throw new Exception("请输入正确的操作时间:" + info.MTime); sql = @" select a.ID,iMPoIds,iordercode,cCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName,cSource,cMaker,dnmaketime,cHandler,dnverifytime,cSourceCodeLs,b.AutoID ,irowno,b.cInvCode,iQuantity,f.MoDId,iNum,g.MoCode from rdrecord11 a left join rdrecords11 b on a.ID=b.ID left join Department c on a.cDepCode=c.cDepCode left join Warehouse d on a.cWhCode=d.cWhCode left join mom_moallocate e on b.iMPoIds=e.AllocateId INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId INNER JOIN dbo.mom_order g ON f.MoId=g.MoId where b.iQuantity<0"; if (!string.IsNullOrWhiteSpace(info.IssueNEGCode)) { sql += " and a.cCode='{0}'"; } if (!string.IsNullOrWhiteSpace(info.MTime.ToString())) { sql += " and ISNULL(a.dnmodifytime,ISNULL(a.dnverifytime, ISNULL(a.dnmodifytime, a.dnmaketime)))>='{1}'"; } if (!string.IsNullOrWhiteSpace(info.User)) { sql += "and a.CMAKER='{2}'"; } sql = string.Format(sql, info.IssueNEGCode, info.MTime, userInfo.UserName); dt = DBHelper.SQlReturnData(sql, cmd); if (dt.Rows.Count <= 0 || dt == null) throw new Exception("材料出库单:" + info.IssueNEGCode + ",无信息!"); if (dtNew == null) dtNew = dt; else dtNew.Merge(dt); } cmd.Transaction.Commit(); } catch (Exception ex) { if (cmd.Transaction != null) cmd.Transaction.Rollback(); log.Error(ex.Message); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } json = JsonConvert.SerializeObject(dtNew); return json; } public string CreateRd11(List Bills) { string msg = ""; DataSet ds = null; string connS = ""; string sql = string.Empty; string iFatherIdTwo = string.Empty; VouchKey key = new VouchKey(); int num = 0; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); if (Bills.Count <= 0) { throw new Exception("传送数据为空!"); } LogInfo(Bills); 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 (ICSMOIssueDocNegative head in Bills) { iFatherIdTwo = ""; num = head.details.Count(); if (WorkPoint != head.WorkPoint) continue; cmd.CommandTimeout = 300; if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint)) throw new Exception("U8正在整理现存量,请稍后再试"); string[] ss = head.WorkPoint.Split('_'); ERPDB = ss[1]; string[] dd = CRNALL.Split('~'); string crdname = dd[0]; string carname = dd[1]; string surface = dd[2]; string cardnewcode = DBHelper.GetCardNumber(carname, cmd); Dictionary dic = DBHelper.GetAllCode("" + ERPDB + "", surface, "" + num + "", head.WorkPoint, cmd); int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString()); int iChildId = Convert.ToInt32(dic["iChildId"].ToString()); DateTime date = DateTime.Now; string cRDcode = DBHelper.GetRDCode(crdname, cmd); string iBaseCodeLen = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", cRDcode, head.WorkPoint, cmd); string DEF_ID = DBHelper.GetDefaultTemplate(cardnewcode, cmd); ICSUserInfo userInfo = new ICSUserInfo(); userInfo = DBHelper.GetPersonInfo(head.User, cmd); // #region 判断工单是否已关闭 // sql = @"SELECT isnull(CloseUser,'') as CloseUser from mom_orderdetail a // inner join mom_order b on a.MoId=b.MoId where b.MoId='" + head.DetailID + "' "; // DataTable dtClose = DBHelper.SQlReturnData(sql, cmd); // if (dtClose != null && dtClose.Rows.Count > 0) // { // if (!string.IsNullOrEmpty(dtClose.Rows[0][0].ToString())) // { // throw new Exception("该工单行已关闭,无法领料!"); // } // } // #endregion #region 材料出库单表头 if (head.SourceType != "领料申请单") { sql = @"INSERT INTO dbo.rdrecord11 ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate , cCode ,cRdCode ,cDepCode,cHandler ,bTransFlag ,cMaker , dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cPsPcode ,cMPoCode ,iproorderid ,bFromPreYear , bIsLsQuery ,bIsComplement ,iDiscountTaxType ,ireturncount ,iverifystate , iswfcontrolled ,dnmaketime ,dnverifytime ,bredvouch ,iPrintCount, iMQuantity) SELECT distinct @ID,0,'11','领料','生产订单',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23), @cCode,@cRdCode,a.MDeptCode,@cHandler,0,@cMaker, CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0, null,b.MoCode,a.MoId,0, 0,0,0,0,0, 0,GETDATE(),GETDATE(),1,1, null FROM dbo.mom_orderdetail a left JOIN dbo.mom_order b ON a.MoId=b.MoId left join mom_morder c on a.modid=c.modid WHERE b.MoCode='" + head.SourceCode + "'"; cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId)); cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode)); cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen)); cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID)); cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode)); //cmd.Parameters.Add(new SqlParameter("@bredvouch",0)); cmd.CommandText = sql; } else { sql = @"INSERT INTO dbo.rdrecord11 ( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cBusCode ,cWhCode , dDate ,cCode ,cRdCode ,cDepCode ,cPersonCode ,cHandler ,cMemo , bTransFlag ,cMaker ,cDefine1 ,cDefine2 ,cDefine3 ,cDefine4 , cDefine5 ,cDefine6 ,cDefine7 ,cDefine8 ,cDefine9 ,cDefine10 , dVeriDate ,bpufirst ,biafirst ,VT_ID ,bIsSTQc ,cDefine11 , cDefine12 ,cDefine13 ,cDefine14 ,cDefine15 ,cDefine16 , bOMFirst ,bFromPreYear ,bIsComplement ,iDiscountTaxType , ireturncount ,iverifystate ,iswfcontrolled ,cModifyPerson , dnmaketime ,dnverifytime ,bredvouch ,bmotran ,bHYVouch ,iPrintCount ) SELECT distinct @ID,0,'11','领料','领料申请单',a.cCode,@cWhCode, CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRdCode,a.cDepCode,a.cPersonCode,@cHandler,a.cMemo, 0,@cMaker,a.cDefine1,cdefine2,a.cDefine3,a.cDefine4, a.cDefine5,a.cDefine6,a.cDefine7,a.cDefine8,a.cDefine9,a.cDefine10, CONVERT(NVARCHAR(15),GETDATE(),23),0,0,@VT_ID,0,a.cDefine11, a.cDefine12,a.cDefine13,a.cDefine14,a.cDefine15,a.cDefine16, 0,0,0,0, 0,0,0,'', GETDATE(),GETDATE(),0,0,0,0 FROM dbo.MaterialAppVouch a WHERE a.cCode='" + head.SourceCode + "'"; cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId)); cmd.Parameters.Add(new SqlParameter("@cWhCode", head.WHCode)); cmd.Parameters.Add(new SqlParameter("@cCode", iBaseCodeLen)); cmd.Parameters.Add(new SqlParameter("@cHandler", head.User)); cmd.Parameters.Add(new SqlParameter("@cMaker", head.User)); cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID)); cmd.Parameters.Add(new SqlParameter("@cRdCode", cRDcode)); //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!"); cmd.CommandText = sql; } try { int count = cmd.ExecuteNonQuery(); if (count <= 0) { log.Error("生成材料出库单表头失败,受影响行数<=0;"); throw new Exception("生成材料出库单表头失败,受影响行数<=0;"); } } catch (Exception ex) { log.Error("生成材料出库单表头失败" + sql, ex); throw new Exception("生成材料出库单表头失败" + sql, ex); } #endregion #region 材料出库单表体 int irowno = 0; foreach (ICSMOIssueDocNegatives body in head.details) { irowno++; if (head.SourceType != "领料申请单") { sql = @"INSERT INTO dbo.rdrecords11 ( AutoID ,ID ,cInvCode ,iQuantity ,cBatch ,iFlag ,iNum, cItemCode ,cName ,iNQuantity ,iMPoIds ,bLPUseFree , iOriTrackID ,bCosting ,bVMIUsed ,cmocode ,invcode ,imoseq , iopseq ,iExpiratDateCalcu ,iorderdid ,iordertype ,isotype , ipesodid ,ipesotype ,cpesocode ,ipesoseq ,irowno , bcanreplace ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10) SELECT @AutoID,@ID,a.InvCode,@iQuantity,@cBatch,0,@iNum, @cItemCode,null,a.Qty,a.AllocateId,0, 0,1,0,c.MoCode,b.InvCode,b.SortSeq, '0000',0,0,0,0, a.AllocateId,'7',c.MoCode,b.SortSeq,@irowno, 0,null,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10 FROM dbo.mom_moallocate a INNER JOIN dbo.mom_orderdetail b ON a.MoDId=b.MoDId INNER JOIN dbo.mom_order c ON b.MoId=c.MoId WHERE a.AllocateId='" + body.SourceDetailID + "'"; cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId)); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId)); cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity)); cmd.Parameters.Add(new SqlParameter("@iNum", -Convert.ToDecimal(body.Amount.ToString("0.00")))); cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode)); cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode)); cmd.Parameters.Add(new SqlParameter("@irowno", irowno)); cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1)); cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2)); cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3)); cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4)); cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5)); cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6)); cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7)); cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8)); cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9)); cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10)); cmd.CommandText = sql; } else { sql = @"SELECT isnull(fOutQuantity,0) as fOutQuantity,iQuantity from MaterialAppVouchs a where a.AutoID='" + body.SourceDetailID + "' "; DataTable dtClose = DBHelper.SQlReturnData(sql, cmd); if (dtClose != null && dtClose.Rows.Count > 0) { if (Convert.ToDecimal(dtClose.Rows[0][0].ToString()) + body.Quantity > Convert.ToDecimal(dtClose.Rows[0][1].ToString())) { throw new Exception("退料数量不能大于领料单的已出数量!"); } } sql = @"INSERT INTO dbo.rdrecords11 ( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,cDefine22 ,iMPoIds,cmocode,iNum, cDefine23 ,cDefine24 ,cDefine25 ,cDefine26 ,cDefine27 , cItem_class ,cItemCode ,cName ,cItemCName ,iNQuantity , cDefine28 ,cDefine29 ,cDefine30 ,cDefine31 ,cDefine32 , cDefine33 ,cDefine34 ,cDefine35 ,cDefine36 ,cDefine37 , bLPUseFree ,iOriTrackID ,bCosting ,bVMIUsed ,iMaIDs , iExpiratDateCalcu ,iordertype ,isotype ,ipesotype ,irowno, cBatch,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10) SELECT @AutoID,@ID,a.cInvCode,@iQuantity,0,a.cDefine22,a.iMPoIds,a.cmocode,@iNum, a.cDefine23,a.cDefine24,a.cDefine25,a.cDefine26,a.cDefine27, a.cItem_class,a.cItemCode,a.cName,a.cItemCName,@iQuantity, a.cDefine28,a.cDefine29,a.cDefine30,a.cDefine31,a.cDefine32, a.cDefine33,a.cDefine34,a.cDefine35,a.cDefine36,a.cDefine37, 0,0,1,0,a.AutoID, 0,0,0,0,@irowno, @cBatch,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10 FROM dbo.MaterialAppVouchs a WHERE a.AutoID='" + body.SourceDetailID + "'"; cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId)); cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId)); cmd.Parameters.Add(new SqlParameter("@iQuantity", -body.Quantity)); cmd.Parameters.Add(new SqlParameter("@iNum", -Convert.ToDecimal(body.Amount.ToString("0.00")))); cmd.Parameters.Add(new SqlParameter("@irowno", irowno)); cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode)); cmd.Parameters.Add(new SqlParameter("@cFree1", body.cFree1)); cmd.Parameters.Add(new SqlParameter("@cFree2", body.cFree2)); cmd.Parameters.Add(new SqlParameter("@cFree3", body.cFree3)); cmd.Parameters.Add(new SqlParameter("@cFree4", body.cFree4)); cmd.Parameters.Add(new SqlParameter("@cFree5", body.cFree5)); cmd.Parameters.Add(new SqlParameter("@cFree6", body.cFree6)); cmd.Parameters.Add(new SqlParameter("@cFree7", body.cFree7)); cmd.Parameters.Add(new SqlParameter("@cFree8", body.cFree8)); cmd.Parameters.Add(new SqlParameter("@cFree9", body.cFree9)); cmd.Parameters.Add(new SqlParameter("@cFree10", body.cFree10)); //DBHelper.CmdExecuteNonQuery(sql, cmd, "生成材料出库单表头失败!"); cmd.CommandText = sql; } try { int count = cmd.ExecuteNonQuery(); if (count <= 0) { log.Error("生成材料出库单表体失败,受影响行数<=0;"); throw new Exception("生成材料出库单表体失败,受影响行数<=0;"); } } catch (Exception ex) { log.Error("生成材料出库单表体失败" + sql, ex); throw new Exception("生成材料出库单表体失败 " + sql, ex); } if (head.SourceType != "领料申请单") { #region 回写工单子件已领数量 sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+(" + body.Quantity + ") where AllocateId='" + body.SourceDetailID + "' "; DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!"); #endregion } else { #region 回写工单子件已领数量 sql = "update mom_moallocate set IssQty=isnull(IssQty,0)+" + body.Quantity + ",RequisitionIssQty=isnull(RequisitionIssQty,0)+" + body.Quantity + " where AllocateId=(select iMPoIds from MaterialAppVouchs where AutoID='" + body.SourceDetailID + @"') update MaterialAppVouchs set fOutQuantity=isnull(fOutQuantity,0)+" + body.Quantity + @" where AutoID='" + body.SourceDetailID + @"' "; DBHelper.CmdExecuteNonQuery(sql, cmd, "回写工单子件已领数量失败!"); #endregion } if (head.SourceType != "领料申请单") { sql = @"select isnull(IssQty,0)fOutQuantity,isnull(qty,0)iQuantity,ISNULL(AuxQty, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(AuxQty, 0)-ISNULL(NiNum, 0) SiNum from mom_moallocate a LEFT JOIN (select sum(iNum) NiNum,ipesodid from rdrecords11 GROUP BY ipesodid) b on a.AllocateId=b.ipesodid where a.AllocateId='" + body.SourceDetailID + "'"; DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd); if (dtDisCheck != null && dtDisCheck.Rows.Count > 0) { foreach (DataRow item1 in dtDisCheck.Rows) { if (Convert.ToDecimal(item1["fOutQuantity"].ToString()) == Convert.ToDecimal(item1["iQuantity"].ToString())) { if (Convert.ToDecimal(item1["iNum"].ToString()) != 0) { if (Convert.ToDecimal(item1["SiNum"].ToString()) != 0) { sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item1["SiNum"].ToString() + ") where autoid='" + iChildId + "' "; body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item1["SiNum"].ToString())); DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!"); } } } } } } else { sql = @"select isnull(fOutQuantity,0)fOutQuantity,isnull(iQuantity,0)iQuantity,ISNULL(iNum, 0)iNum,ISNULL(NiNum, 0)NiNum,ISNULL(iNum, 0)-ISNULL(NiNum, 0) SiNum from MaterialAppVouchs a LEFT JOIN (select sum(iNum) NiNum,iMaIDs from rdrecords11 GROUP BY iMaIDs) b on a.AutoID=b.iMaIDs where a.AutoID='" + body.SourceDetailID + "'"; DataTable dtDisCheck = DBHelper.SQlReturnData(sql, cmd); if (dtDisCheck != null && dtDisCheck.Rows.Count > 0) { foreach (DataRow item in dtDisCheck.Rows) { if (Convert.ToDecimal(item["fOutQuantity"].ToString()) == Convert.ToDecimal(item["iQuantity"].ToString())) { if (Convert.ToDecimal(item["iNum"].ToString()) != 0) { if (Convert.ToDecimal(item["SiNum"].ToString()) != 0) { sql = @"Update rdrecords11 set iNum=isnull(iNum,0)+(" + item["SiNum"].ToString() + ") where autoid='" + iChildId + "' "; body.Amount = (Convert.ToDecimal(body.Amount.ToString("0.00"))) + (Convert.ToDecimal(item["SiNum"].ToString())); DBHelper.CmdExecuteNonQuery(sql, cmd, "辅计量余数处理失败!"); } } } } } } //更新现存量 key.cBustypeUN = "领料"; key.cVouchTypeUN = "11"; key.TableName = "IA_ST_UnAccountVouch11"; //DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key); //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", body.Quantity, key); DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.WHCode, body.BatchCode, body.Quantity, body.Amount, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId); iFatherIdTwo = "'" + iFatherId + "',"; iChildId--; } #endregion #region 查询 sql = @" select DISTINCT a.ID as ID,a.ID as IDs,cMPoCode as SourceCode , cCode as IssueNEGCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName, cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime, cHandler as Checker,dnverifytime as CheckDateTime from rdrecord11 a left join Department c on a.cDepCode=c.cDepCode left join Warehouse d on a.cWhCode=d.cWhCode WHERE a.ID in ({0}) select DISTINCT a.ID as IDs,b.AutoID as DetailID,b.irowno as Sequence,b.cInvCode as InvCode ,b.iQuantity as Quantity,(case when isnull(l.AutoID,'')='' then e.AllocateId else l.AutoID end) as SourceDetailID,b.iNum as Amount ,isnull(b.cItemCode,'') ProjectCode,isnull(b.cbatch,'') cBatch,'' version ,'' brand, isnull(b.cFree1,'') as cFree1, isnull(b.cFree2,'') as cFree2, isnull(b.cFree3,'') as cFree3, isnull(b.cFree4,'') as cFree4, isnull(b.cFree5,'') as cFree5, isnull(b.cFree6,'') as cFree6, isnull(b.cFree7,'') as cFree7, isnull(b.cFree8,'') as cFree8, isnull(b.cFree9,'') as cFree9, isnull(b.cFree10,'') as cFree10 from rdrecord11 a left join rdrecords11 b on a.ID=b.ID left join MaterialAppVouchs l on b.iMaIDs=l.autoid left join Department c on a.cDepCode=c.cDepCode left join Warehouse d on a.cWhCode=d.cWhCode left join mom_moallocate e on b.iMPoIds=e.AllocateId INNER JOIN dbo.mom_orderdetail f ON e.MoDId=f.MoDId INNER JOIN dbo.mom_order g ON f.MoId=g.MoId WHERE a.ID in ({0})"; sql = string.Format(sql, iFatherIdTwo.TrimEnd(',')); if (ds != null) ds.Merge(DBHelper.SQlReturnDataSet(sql, cmd)); else ds = DBHelper.SQlReturnDataSet(sql, cmd); #endregion } cmd.Transaction.Commit(); } catch (Exception ex) { if (cmd.Transaction != null) cmd.Transaction.Rollback(); log.Error(ex.Message); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } msg = JSON.DataSetToJson(ds, "details", "IDs"); return msg; } public string Approve(List infos) { List szJson = new List(); string connS = ""; string json = ""; if (infos.Count <= 0) { throw new Exception("传送数据为空!"); } string res = string.Empty; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); string sql = string.Empty; List result = infos.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 (ICSMOIssueDocNegative info in infos) { if (WorkPoint != info.WorkPoint) continue; ICSUserInfo userInfo = new ICSUserInfo(); userInfo = DBHelper.GetPersonInfo(info.User, cmd); if (info.MTime < new DateTime(2000, 01, 01)) throw new Exception("请输入正确的操作时间:" + info.MTime); sql = @"UPDATE dbo.rdrecord11 SET cHandler ='" + userInfo.UserName + @"' , dnmodifytime =CONVERT(VARCHAR(50),GETDATE(),112),dModifyDate =GETDATE() WHERE ID='{0}'"; sql = string.Format(sql, info.ID); DBHelper.CmdExecuteNonQuery(sql, cmd, "审核生产退料单失败!"); sql = @"select * from dbo.rdrecords11 a left join dbo.rdrecord11 on a.ID=b.ID WHERE b.ID='{0}'"; sql = string.Format(sql, info.ID); DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd); if (info.UpdateStock) { #region 更新现存量 for (int i = 0; i < dtChecks.Rows.Count; i++) { //判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量 //sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "'AND cBatch=''and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "'"; sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "'AND cBatch='" + dtChecks.Rows[i]["cBatch"].ToString() + @"'AND cFree1='" + dtChecks.Rows[i]["cFree1"].ToString() + @"' AND cFree2='" + dtChecks.Rows[i]["cFree2"].ToString() + @"' AND cFree3='" + dtChecks.Rows[i]["cFree3"].ToString() + @"' AND cFree4='" + dtChecks.Rows[i]["cFree4"].ToString() + @"' AND cFree5='" + dtChecks.Rows[i]["cFree5"].ToString() + @"' AND cFree6='" + dtChecks.Rows[i]["cFree6"].ToString() + @"' AND cFree7='" + dtChecks.Rows[i]["cFree7"].ToString() + @"' AND cFree8='" + dtChecks.Rows[i]["cFree8"].ToString() + @"' AND cFree9='" + dtChecks.Rows[i]["cFree9"].ToString() + @"' AND cFree10='" + dtChecks.Rows[i]["cFree10"].ToString() + @"' and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "'"; DataTable dtItem = DBHelper.SQlReturnData(sql, cmd); if (dtItem != null && dtItem.Rows.Count > 0) { if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString())) { throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + dtChecks.Rows[i]["cInvCode"].ToString()); } } else { throw new Exception("物料:" + dtChecks.Rows[i]["cInvCode"].ToString() + "在现存量表中不存在!"); } VouchKey key = new VouchKey(); key.cBustypeUN = "领料"; key.cVouchTypeUN = "11"; key.TableName = "IA_ST_UnAccountVouch11"; //DBHelper.UpdateCurrentStock(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(),"", -Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), key); //DBHelper.UpdateCurrentStockNEW(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(), dtChecks.Rows[i]["cBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), key); DBHelper.UpdateCurrentStockCCGC(cmd, dtChecks.Rows[i]["cInvCode"].ToString(), dtChecks.Rows[i]["cWhCode"].ToString(), dtChecks.Rows[i]["cBatch"].ToString(), -Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()), dtChecks.Rows[i]["iNum"].ToString() == "" ? 0 : -Convert.ToDecimal(dtChecks.Rows[i]["iNum"].ToString()), dtChecks.Rows[i]["cFree1"].ToString(), dtChecks.Rows[i]["cFree2"].ToString(), dtChecks.Rows[i]["cFree3"].ToString(), dtChecks.Rows[i]["cFree4"].ToString(), dtChecks.Rows[i]["cFree5"].ToString(), dtChecks.Rows[i]["cFree6"].ToString(), dtChecks.Rows[i]["cFree7"].ToString(), dtChecks.Rows[i]["cFree8"].ToString(), dtChecks.Rows[i]["cFree9"].ToString(), dtChecks.Rows[i]["cFree10"].ToString(), "", key, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, Convert.ToInt32(dtChecks.Rows[i]["ID"].ToString()), Convert.ToInt32(dtChecks.Rows[i]["AutoID"].ToString())); #region 判断现存量是否足够 sql = @"IF EXISTS(SELECT AutoID FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0) BEGIN DECLARE @MSG NVARCHAR(100) SELECT @MSG='ERP待出库数量不足!AutoID:'+CAST(AutoID AS NVARCHAR(100)) FROM dbo.CurrentStock WHERE iQuantity<0 OR iNum<0 OR fOutQuantity<0 RAISERROR(@MSG,16,1) END"; cmd.CommandText = sql; cmd.ExecuteNonQuery(); #endregion } #endregion } } cmd.Transaction.Commit(); } catch (Exception ex) { if (cmd.Transaction != null) cmd.Transaction.Rollback(); log.Error(ex.Message); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } return json; } /// /// 删除请购单 /// /// /// public string Delete(List infos) { List szJson = new List(); if (infos.Count <= 0) { throw new Exception("传送数据为空!"); } string res = string.Empty; string connS = ""; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); string sql = string.Empty; List result = infos.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 (ICSMOIssueDocNegative info in infos) { if (WorkPoint != info.WorkPoint) continue; if (info.MTime < new DateTime(2000, 01, 01)) throw new Exception("请输入正确的操作时间:" + info.MTime); sql = @"delete rdrecord11 where rdrecord11.ID='" + info.ID + "'"; sql += @"delete rdrecords11 where rdrecords11.ID='" + info.ID + "'"; //UPDATE dbo.rdrecord11 a SET cHandler='" + info.User + @"' , // dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() // WHERE a.ID='" + info.ID + "'"; DBHelper.CmdExecuteNonQuery(sql, cmd, "删除材料出库单失败!"); } cmd.Transaction.Commit(); } catch (Exception ex) { if (cmd.Transaction != null) cmd.Transaction.Rollback(); log.Error(ex.Message); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } return res; } private void LogInfo(List Bills) { string HeadList = string.Empty; string BodyList = string.Empty; foreach (ICSMOIssueDocNegative head in Bills) { HeadList += "\r\n 表头主键ID:" + head.ID + ",仓库:" + head.WHCode + ",用户:" + head.User + ",站点:" + head.WorkPoint; foreach (ICSMOIssueDocNegatives body in head.details) { BodyList += "\r\n 表体主键ID: " + body.SourceDetailID + ",数量:" + body.Quantity; } } log.Info(HeadList); log.Info(BodyList); } /// /// 表体合并 /// /// private void MergeObject(List Bills, SqlCommand cmd) { foreach (ICSMOIssueDocNegative head in Bills) { DataTable dt = DBHelper.ToDataTable(head.details.ToList()); DataTable dtNew = dt.Clone(); dtNew = DBHelper.MergeDataTable(dt, "AllocateId", "cBatch", "iQuantity", "InvCode", cmd); List list = DBHelper.ConvertTo(dtNew).ToList(); head.details = list; } } } }