|
|
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 OtherOutDoc { 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["OtherOutCRDNAME"];
/// <summary>
/// 生成其他出库单
/// </summary>
/// <param name="Bills"></param>
/// <returns></returns>
public string CreateRd09(List<ICSOtherOutCreateDoc> Bills) { string msg = string.Empty; string connS = ""; string sql = string.Empty; SqlConnection conn = new SqlConnection(); DataSet ds = null; int num = 0; SqlCommand cmd = new SqlCommand(); string iFatherIdTwo = "";
if (Bills.Count <= 0) { throw new Exception("传送数据为空!"); } List<string> 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 (ICSOtherOutCreateDoc head in Bills) { iFatherIdTwo = ""; if (WorkPoint != head.WorkPoint) { continue; } 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];
num = head.details.Count(); string cardnewcode = DBHelper.GetCardNumber(carname, cmd); Dictionary<string, int> 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); ICSUserInfo userInfo = new ICSUserInfo(); userInfo = DBHelper.GetPersonInfo(head.User, cmd); //取得out库单的默认显示模版
string DEF_ID09 = DBHelper.GetDefaultTemplate(cardnewcode, cmd); //取得其他出库单单据 表头ID,表体DID
VouchKey key09 = new VouchKey(); #region 其他出库单表头
key09.ID += 1; sql = @"INSERT INTO dbo.RdRecord09
( ID ,bRdFlag ,cVouchType ,cBusType ,cSource ,cWhCode ,dDate ,cCode , cRdCode ,cDepCode ,cHandler ,bTransFlag ,cMaker ,dVeriDate ,bpufirst , biafirst ,VT_ID ,bIsSTQc ,bFromPreYear ,bIsComplement ,iDiscountTaxType , iBG_OverFlag ,cBG_Auditor ,cBG_AuditTime ,ControlResult ,ireturncount , iverifystate ,iswfcontrolled ,cModifyPerson ,dnmaketime ,dnverifytime , iPrintCount) VALUES(@ID,0,'09','其他出库','库存',@cWhCode,CONVERT(NVARCHAR(15),GETDATE(),23),@cCode, @cRdCode,@cDepCode,@cHandler,0,@cMaker,CONVERT(NVARCHAR(15),GETDATE(),23),0, 0,@VT_ID,0,0,0,0, 0,'','',-1,0, 0,0,'',GETDATE(),GETDATE(),0) ";
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("@cRdCode", cRDcode)); cmd.Parameters.Add(new SqlParameter("@cDepCode", head.DepCode)); cmd.Parameters.Add(new SqlParameter("@cHandler", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@cMaker", userInfo.UserName)); cmd.Parameters.Add(new SqlParameter("@VT_ID", DEF_ID09)); cmd.CommandText = sql; try { cmd.ExecuteNonQuery(); } catch (Exception) { log.Error("生成其他出库单失败!SQL:\r\n" + sql); throw new Exception("程序异常,请联系开发人员!"); } #endregion
foreach (ICSOtherOutCreateDocs body in head.details) { #region 3.5.1 表体
sql = @"INSERT INTO dbo.rdrecords09
( AutoID ,ID ,cInvCode ,iQuantity ,iFlag ,bLPUseFree ,iRSRowNO ,iOriTrackID ,cBatch,iNNum,cItemCode, bCosting ,bVMIUsed ,iExpiratDateCalcu ,iordertype ,isotype ,irowno ,iposflag,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10) VALUES(@AutoID,@ID,@cInvCode,@iQuantity,0,0,0,0,@cBatch,@iNNum,@cItemCode, 1,0,0,0,0,@irowno,1,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10)";
cmd.Parameters.Clear(); cmd.Parameters.Add(new SqlParameter("@AutoID", iChildId)); cmd.Parameters.Add(new SqlParameter("@ID", iFatherId)); cmd.Parameters.Add(new SqlParameter("@cInvCode", body.InvCode)); cmd.Parameters.Add(new SqlParameter("@iQuantity", body.Quantity)); cmd.Parameters.Add(new SqlParameter("@iNNum", body.Amount.ToString("0.00"))); cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence)); cmd.Parameters.Add(new SqlParameter("@cBatch", body.BatchCode)); cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode)); 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; try { cmd.ExecuteNonQuery(); } catch (Exception) { log.Error("生成其他出库单表体失败!SQL:\r\n" + sql); throw new Exception("程序异常,请联系开发人员!"); } #endregion
//更新现存量
key09.cBustypeUN = "其他出库"; key09.cVouchTypeUN = "09"; key09.TableName = "IA_ST_UnAccountVouch09";
//DBHelper.UpdateCurrentStock(cmd, body.InvCode, head.WHCode, "", -body.Quantity, key09);
//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, key09);
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, "", key09, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId, iChildId);
iFatherIdTwo = "'" + iFatherId + "',"; iChildId--; }
#region 查询
sql = @"
select DISTINCT a.ID as ID,a.ID as IDs,cBusCode as SourceCode , cCode as OtherOutCode,a.cDepCode,c.cDepName,a.cWhCode,d.cWhName, cSource as SourceType,cMaker as CreateUser,dnmaketime as CreateDateTime, cHandler as Checker,dnverifytime as CheckDateTime from rdrecord09 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,irowno as Sequence,b.cInvCode as InvCode ,iQuantity as Quantity,b.iDebitIDs as SourceDetailID,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 rdrecord09 a left join rdrecords09 b on a.ID=b.ID left join Department c on a.cDepCode=c.cDepCode left join Warehouse d on a.cWhCode=d.cWhCode 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; } /// <summary>
/// 审核其他出库单
/// </summary>
/// <param name="Bills"></param>
/// <returns></returns>
public bool ConfirmRd09(List<ICSOtherOutDoc> Bills) { bool ResultFlag = false; string connS = ""; SqlConnection conn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); if (Bills.Count <= 0) { throw new Exception("传送数据为空!"); } LogInfo(Bills);
List<string> 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 (ICSOtherOutDoc head in Bills) { if (WorkPoint != head.WorkPoint) continue;
if (!DBHelper.IsInventoryConsolidation(cmd, head.WorkPoint)) throw new Exception("U8正在整理现存量,请稍后再试");
string sql = ""; ICSUserInfo userInfo = new ICSUserInfo(); userInfo = DBHelper.GetPersonInfo(head.User, cmd);
#region 检验单号是否存在
sql = "select * from RdRecord09 where ID='" + head.ID + "'"; DataTable dt = DBHelper.SQlReturnData(sql, cmd); if (dt != null && dt.Rows.Count > 0) { string cHandler = dt.Rows[0]["cHandler"].ToString(); if (!string.IsNullOrEmpty(cHandler)) { throw new Exception("单据ID:" + head.ID + "不是开立状态!"); } } else { throw new Exception("单据ID:" + head.ID + "在U8中不存在!"); } #endregion
#region 审核其他出库单
sql = @"UPDATE dbo.RdRecord09 SET cHandler='" + userInfo.UserName + @"' ,
dVeriDate=CONVERT(VARCHAR(50),GETDATE(),112),dnverifytime=GETDATE() WHERE ID='" + head.ID + "'";
DBHelper.CmdExecuteNonQuery(sql, cmd, "审核其他出库单失败!"); #endregion
sql = "select * from dbo.RdRecords09 a inner join RdRecord09 b on a.ID=b.ID where b.ID='" + head.ID + "';"; DataTable dtChecks = DBHelper.SQlReturnData(sql, cmd); if (head.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 (!dtItem.Rows[0]["cBatch"].ToString().Equals(body.cBatch))
//{
// throw new Exception("物料条码的批号与U8现存量物料批号不一致,物料:" + body.cInvCode);
//}
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 = "09"; key.TableName = "IA_ST_UnAccountVouch09"; //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);
string inum = dtChecks.Rows[i]["iNum"].ToString(); if (inum == "") { inum = "0"; } 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()), -Convert.ToDecimal(inum), 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())); //回写fOutQuantityy
// sql = @"Update CurrentStock set fOutQuantity=isnull(fOutQuantity,0)-" + Convert.ToDecimal(dtChecks.Rows[i]["iQuantity"].ToString()) + @"
// where cInvCode='" + dtChecks.Rows[i]["cInvCode"].ToString() + "' and cWhCode='" + dtChecks.Rows[i]["cWhCode"].ToString() + "' ";
//if (body.cBatch != null)
//{
// sql += "and cBatch='" + body.cBatch + "'";
//}
//else
//{
// sql += "and cBatch=''";
//}
//DBHelper.CmdExecuteNonQuery(sql, cmd, "回写fOutQuantity失败!");
if (head.UpdateTodoQuantity == true) { #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(); } } ResultFlag = true; return ResultFlag; } /// <summary>
/// 记录日志
/// </summary>
/// <param name="Bills"></param>
private void LogInfo(List<ICSOtherOutDoc> Bills) { string HeadList = string.Empty; string BodyList = string.Empty; foreach (ICSOtherOutDoc head in Bills) { HeadList += "\r\n 表头主键ID:" + head.ID + ",用户:" + head.User + ",站点:" + head.WorkPoint+""; } log.Info(HeadList); log.Info(BodyList); }
} }
|