|
|
using ICSSoft.Common; using ICSSoft.Entity; 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 CreateAssemVouch { 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 CRNALL = System.Configuration.ConfigurationManager.AppSettings["AssemCRDNAME"]; /// <summary>
/// 生成形态转换单
/// </summary>
/// <param name="Bills"></param>
/// <returns></returns>
public string Create(List<ICSAssemVouch> Bills) { string msg = ""; string connS = ""; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); DataTable dtNew1 = null; DataTable dtNew2 = null; if (Bills.Count <= 0) { throw new Exception("传送数据为空!"); } string sql = ""; int num = 0; foreach (ICSAssemVouch head in Bills) { try { string iFatherIdTwo = ""; connS = string.Format(connString, head.WorkPoint); conn = new System.Data.SqlClient.SqlConnection(connS); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; string[] ss = head.WorkPoint.Split('_'); ERPDB = ss[1];
string[] dd = CRNALL.Split('~'); string crdname1 = dd[0]; string crdname2 = dd[1]; string carname = dd[2]; string surface = dd[3]; string carname1 = dd[4]; string carname2 = dd[5]; string surface1 = dd[6];
string cardnewcode = DBHelper.GetCardNumber(carname, cmd); string cardnewcode1 = DBHelper.GetCardNumber(carname1, cmd); string cardnewcode2 = DBHelper.GetCardNumber(carname2, cmd);
string cRDcode08 = DBHelper.GetRDCode(crdname1, cmd); string cRDcode09 = DBHelper.GetRDCode(crdname2, cmd); ICSUserInfo userInfo = new ICSUserInfo(); userInfo = DBHelper.GetPersonInfo(head.User, cmd); #region 形态转换单
#region 形态转换单表头
//形态转换单CartonNo:0305
//取得out库单的默认显示模版
string DEF_ID07 = DBHelper.GetDefaultTemplate(cardnewcode, cmd); //取得形态转换入库单单据 表头ID,表体DID
num = head.details.Count(); Dictionary<string, int> dic = DBHelper.GetAllCode(ERPDB, surface, num.ToString(), head.WorkPoint); int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString()); int iChildId = Convert.ToInt32(dic["iChildId"].ToString()); DateTime date = DateTime.Now; // 取得表头单据号
string Code07 = DBHelper.GetAllRDCode(cardnewcode, "" + date + "", "admin", head.WorkPoint);
sql = @"INSERT INTO AssemVouch
(cAVCode,cVouchType,dAVDate,cDepCode,cIRdCode,cORdCode,cMaker,iNetLock, ID,bTransFlag,VT_ID,cVerifyPerson,dVerifyDate, dnmaketime,dnverifytime,iPrintCount,ctransflag) VALUES (@cAVCode,'15',CONVERT(NVARCHAR(15),GETDATE(),23),@cDepCode,@cIRdCode,@cORdCode,@cMaker,0, @ID,'0',@VT_ID,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23), GETDATE(),GETDATE(),0,1) ";
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@cAVCode", Code07)); cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode)); cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@cIRdCode", cRDcode08)); cmd.Parameters.Add(new SqlParameter("@cORdCode", cRDcode09)); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId)); cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID07)); cmd.CommandText = sql; DBHelper.CmdExecuteNonQuery(sql, cmd, "形态转换单表头保存失败!"); #endregion
#region 其他出库单表头
//取得out库单的默认显示模版
string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode2, cmd); //取得表头ID,表体DID
Dictionary<string, int> dic09 = DBHelper.GetAllCode(ERPDB, surface1, num.ToString(), head.WorkPoint); int iFatherId09 = Convert.ToInt32(dic09["iFatherId"].ToString()); int iChildId09 = Convert.ToInt32(dic09["iChildId"].ToString()); // 取得表头单据号
string Code09 = DBHelper.GetAllRDCode(cardnewcode2, "" + date + "", "admin", head.WorkPoint);
sql = @"INSERT INTO dbo.RdRecord09
(ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode, cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst, bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount, iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,ctransflag,cDepCode)
SELECT @ID,'0','09','转换出库','形态转换',@cBusCode,@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRDcode, @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0', '85','0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0',null,'1',cDepCode from dbo.AssemVouch WHERE cAVCode='" + Code07 + "' ";
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId09)); cmd.Parameters.Add(new SqlParameter("@cBusCode", Code07)); cmd.Parameters.Add(new SqlParameter("@cWhCode", head.OutWhCode)); cmd.Parameters.Add(new SqlParameter("@cCode", Code09.ToString().PadLeft(10, '0'))); cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@cMemo", "")); cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09)); cmd.Parameters.Add(new SqlParameter("@cRDcode", cRDcode09)); cmd.CommandText = sql; DBHelper.CmdExecuteNonQuery(sql, cmd, "其他出库单表头保存失败!");
#endregion
#region 其他入库单表头
//取得out库单的默认显示模版
string DEF_ID08 = DBHelper.GetDefaultTemplate(cardnewcode1, cmd); //取得入库单单据 表头ID,表体DID
Dictionary<string, int> dic08 = DBHelper.GetAllCode(ERPDB, surface1, num.ToString(), head.WorkPoint); int iFatherId08 = Convert.ToInt32(dic08["iFatherId"].ToString()); int iChildId08 = Convert.ToInt32(dic08["iChildId"].ToString()); // 取得表头单据号
string Code08 = DBHelper.GetAllRDCode(cardnewcode1, "" + date + "", "admin", head.WorkPoint); sql = @"INSERT INTO dbo.RdRecord08
(ID,bRdFlag,cVouchType,cBusType,cSource,cBusCode,cWhCode,dDate,cCode,cRdCode, cHandler,cMemo,bTransFlag,cMaker,dVeriDate,bpufirst,biafirst,VT_ID,bIsSTQc,bOMFirst, bFromPreYear,bIsComplement,iDiscountTaxType,ireturncount, iverifystate,iswfcontrolled,dnmaketime,dnverifytime,bredvouch,cSourceLs,ctransflag,cDepCode)
SELECT @ID,'1','08','转换入库','形态转换',@cBusCode,@cIWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode,@cRDcode, @cHandler,@cMemo,'0',@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),'0','0', @VT_ID,'0','0','0','0','0','0','0','0',GETDATE(),GETDATE(),'0',null,'1',cDepCode from dbo.AssemVouch WHERE cAVCode='" + Code07 + "' ";
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId08)); cmd.Parameters.Add(new SqlParameter("@cBusCode", Code07)); cmd.Parameters.Add(new SqlParameter("@cIWhCode", head.InWhCode)); cmd.Parameters.Add(new SqlParameter("@cCode", Code08)); cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@cMemo", "")); cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID08)); cmd.Parameters.Add(new SqlParameter("@cRDcode", cRDcode08)); cmd.CommandText = sql; DBHelper.CmdExecuteNonQuery(sql, cmd, "其他入库单表头保存失败!");
#endregion
int Rd08irowno = 0; int Rd09irowno = 0; foreach (ICSAssemVouchs body in head.details) { #region 形态转换单转换前表体
sql = @"INSERT INTO dbo.AssemVouchs
(ID,cAVCode,bAVType,cWhCode,cInvCode, iAVQuantity,cAVBatch,autoID,bCosting,iExpiratDateCalcu, irowno,iGroupNO) VALUES(@ID,@cAVCode,@bAVType,@cWhCode,@cInvCode, @iAVQuantity,@cAVBatch,@autoID,0,0, @irowno,@iGroupNO)";
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId)); cmd.Parameters.Add(new SqlParameter("@cAVCode", Code07)); cmd.Parameters.Add(new SqlParameter("@bAVType", body.Type)); cmd.Parameters.Add(new SqlParameter("@cWhCode", body.WHCode)); cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode)); cmd.Parameters.Add(new SqlParameter("@iAVQuantity", body.Quantity)); cmd.Parameters.Add(new SqlParameter("@cAVBatch", body.BatchCode)); cmd.Parameters.Add(new SqlParameter("@autoID", iChildId)); cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence)); cmd.Parameters.Add(new SqlParameter("@iGroupNO", body.GroupNO)); cmd.CommandText = sql; DBHelper.CmdExecuteNonQuery(sql, cmd, "形态转换单表体保存失败!"); #endregion
if (body.Type == "转换前") { Rd09irowno += 1; #region 其他出库单表体
//判断物料批号与现存量表批号是否一致、数量不能超过现存量物料数量
//sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + body.BatchCode + "'and cWhCode='" + head.OutWhCode + "'";
sql = @"SELECT cBatch,iQuantity from CurrentStock WHERE cInvCode='" + body.InvCode + "'AND cBatch='" + body.BatchCode + @"'AND cFree1='" + body.cFree1 + @"' AND cFree2='" + body.cFree2 + @"' AND cFree3='" + body.cFree3 + @"' AND cFree4='" + body.cFree4 + @"' AND cFree5='" + body.cFree5 + @"' AND cFree6='" + body.cFree6 + @"' AND cFree7='" + body.cFree7 + @"' AND cFree8='" + body.cFree8 + @"' AND cFree9='" + body.cFree9 + @"' AND cFree10='" + body.cFree10 + @"' and cWhCode='" + head.OutWhCode + "'";
DataTable dtItem = DBHelper.SQlReturnData(sql, cmd); if (dtItem != null && dtItem.Rows.Count > 0) { if (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.BatchCode)) { throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.InvCode); } if (Convert.ToDecimal(dtItem.Rows[0]["iQuantity"].ToString()) < body.Quantity) { throw new Exception("物料条码的数量大于U8现存量物料数量,物料:" + body.InvCode); }
} else { throw new Exception("物料:" + body.InvCode + "在现存量表中不存在!"); }
sql = @"INSERT INTO dbo.rdrecords09
(AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,iNum, cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iGroupNO,iPPrice) SELECT @AutoID,@ID,cInvCode,@iQuantity,'0','0','0',@iTrIds,null,@iNum,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, '0','0','0','0','1','0','0','0',@irowno,null,@cTVBatch,iAVPCost,'1',iAVPPrice from AssemVouchs where autoID=" + iChildId + @" ;";
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId09)); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId09)); cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity)); cmd.Parameters.Add(new SqlParameter("@iNum", body.Amount)); cmd.Parameters.Add(new SqlParameter("@cTVBatch", body.BatchCode)); cmd.Parameters.Add(new SqlParameter("@iTrIds", iChildId)); cmd.Parameters.Add(new SqlParameter("@irowno", Rd09irowno)); cmd.CommandText = sql; DBHelper.CmdExecuteNonQuery(sql, cmd, "其他出库单表体保存失败!");
sql = @"INSERT INTO dbo.rdrecords09sub
( AutoID , ID ,iBG_Ctrl , iBGSTSum ) values('" + iChildId09 + "','" + iFatherId09 + "',0,0)";
cmd.CommandText = sql; cmd.ExecuteNonQuery();
#endregion
if (head.UpdateStock) { #region 更新现存量
VouchKey key09 = new VouchKey(); key09.cBustypeUN = "转换出库"; key09.cVouchTypeUN = "09"; key09.TableName = "IA_ST_UnAccountVouch09"; //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.OutWhCode, body.BatchCode, -body.Quantity, key09);
DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.OutWhCode, body.BatchCode, -body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key09); #endregion
} } else { Rd08irowno += 1; #region 其他入库单表体
sql = @"INSERT INTO dbo.rdrecords08
(AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,iGroupNO) SELECT @AutoID,@ID,cInvCode,@iQuantity,'0','0','0',@iTrIds,@iAVQuantity,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10, '0','0','0','0','1','0','0','0',@irowno,null,@cTVBatch,iAVPCost,iAVPPrice,'1' from AssemVouchs where autoID=" + iChildId + @" ;";
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId08)); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId08)); cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity)); cmd.Parameters.Add(new SqlParameter("@cTVBatch", body.BatchCode)); cmd.Parameters.Add(new SqlParameter("@iTrIds", iChildId)); cmd.Parameters.Add(new SqlParameter("@iAVQuantity", body.Quantity)); cmd.Parameters.Add(new SqlParameter("@irowno", Rd08irowno)); cmd.CommandText = sql; DBHelper.CmdExecuteNonQuery(sql, cmd, "其他入库单表体保存失败!"); #endregion
if (head.UpdateStock) { #region 更新现存量
VouchKey key08 = new VouchKey(); key08.cBustypeUN = "转换入库"; key08.cVouchTypeUN = "08"; key08.TableName = "IA_ST_UnAccountVouch08"; //DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.InWhCode, body.BatchCode, body.Quantity, key08);
DBHelper.UpdateCurrentStockNEW(cmd, body.InvCode, head.InWhCode, body.BatchCode, body.Quantity, body.cFree1, body.cFree2, body.cFree3, body.cFree4, body.cFree5, body.cFree6, body.cFree7, body.cFree8, body.cFree9, body.cFree10, key08); #endregion
} } iFatherIdTwo += "'" + iFatherId + "',"; iChildId--; iChildId09--; iChildId08--; } #endregion
#region 查询
sql = @"SELECT a.ID AS Costre,a.ID,b.cAVCode AS MTCode,a.cDepCode AS DepCode,'' AS DepName,a.cMaker AS CreateUser,a.dnmaketime AS CreateDateTime,a.cVerifyPerson AS Checker,a.dnverifytime AS CheckDateTime,
b.autoID AS DetailID,b.irowno AS Sequence,b.iGroupNO AS GroupNO,b.bAVType AS Type,b.cInvCode AS InvCode,b.cWhCode AS WHCode,b.iAVQuantity AS Quantity,0 AS Amount,b.cAVBatch AS BatchCode,'' AS ProjectCode INTO #TempERP FROM AssemVouch a INNER JOIN AssemVouchs b ON a.ID=b.ID WHERE a.ID IN({0}) SELECT DISTINCT Costre,ID,MTCode,DepCode,DepName,CreateUser,CreateDateTime,Checker,CheckDateTime FROM #TempERP SELECT Costre,DetailID,Sequence,GroupNO,Type,InvCode,WHCode,BatchCode,ProjectCode,Quantity,Amount FROM #TempERP
DROP TABLE #TempERP";
sql = string.Format(sql, iFatherIdTwo.TrimEnd(',')); DataSet ds = DBHelper.SQlReturnDataSet(sql, cmd); if (dtNew1 == null) dtNew1 = ds.Tables[0]; else dtNew1.Merge(ds.Tables[0]); if (dtNew2 == null) dtNew2 = ds.Tables[1]; else dtNew2.Merge(ds.Tables[1]); #endregion
cmd.Transaction.Commit(); } 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(); } } string RelationName = "details"; DataSet dz = new DataSet(); dtNew1.TableName = "Table0"; dtNew2.TableName = "Table1"; dz.Tables.Add(dtNew1.Copy()); dz.Tables.Add(dtNew2.Copy()); DataRelation dh = new DataRelation(RelationName, dz.Tables[0].Columns["IDs"], dz.Tables[1].Columns["IDs"]); dz.Relations.Add(dh); msg = DBHelper.DataSetToJson(dz, RelationName); return msg; }
} }
|