using ICSSoft.Common;
using ICSSoft.Entity;
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"];
///
/// 生成形态转换单
///
///
///
public string Create(List 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 cRDcode08 = DBHelper.GetRDCode("转换入库", cmd);
string cRDcode09 = DBHelper.GetRDCode("转换入库", cmd);
ICSUserInfo userInfo = new ICSUserInfo();
userInfo = DBHelper.GetPersonInfo(head.User, cmd);
#region 形态转换单
#region 形态转换单表头
//形态转换单CartonNo:0305
//取得out库单的默认显示模版
string DEF_ID07 = DBHelper.GetDefaultTemplate("0305", cmd);
//取得形态转换入库单单据 表头ID,表体DID
num = head.details.Count();
Dictionary dic = DBHelper.GetAllCode(ERPDB, "as", num.ToString());
int iFatherId = Convert.ToInt32(dic["iFatherId"].ToString());
int iChildId = Convert.ToInt32(dic["iChildId"].ToString());
DateTime date = DateTime.Now;
// 取得表头单据号
string Code07 = DBHelper.GetAllRDCode("0305", "" + date + "", "admin");
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("0301", cmd);
//取得表头ID,表体DID
Dictionary dic09 = DBHelper.GetAllCode(ERPDB, "rd", num.ToString());
int iFatherId09 = Convert.ToInt32(dic09["iFatherId"].ToString());
int iChildId09 = Convert.ToInt32(dic09["iChildId"].ToString());
// 取得表头单据号
string Code09 = DBHelper.GetAllRDCode("0301", "" + date + "", "admin");
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("0301", cmd);
//取得入库单单据 表头ID,表体DID
Dictionary dic08 = DBHelper.GetAllCode(ERPDB, "rd", num.ToString());
int iFatherId08 = Convert.ToInt32(dic08["iFatherId"].ToString());
int iChildId08 = Convert.ToInt32(dic08["iChildId"].ToString());
// 取得表头单据号
string Code08 = DBHelper.GetAllRDCode("0301", "" + date + "", "admin");
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 + "'";
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,
cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iGroupNO,iPPrice)
SELECT @AutoID,@ID,cInvCode,@iQuantity,'0','0','0',@iTrIds,null,
'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("@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);
#endregion
}
}
else
{
Rd08irowno += 1;
#region 其他入库单表体
sql = @"INSERT INTO dbo.rdrecords08
(AutoID,ID,cInvCode,iQuantity,iSOutQuantity,iSOutNum,iFlag,iTrIds,iNQuantity,
cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,iGroupNO)
SELECT @AutoID,@ID,cInvCode,@iQuantity,'0','0','0',@iTrIds,@iAVQuantity,
'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);
#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);
dtNew1.Merge(ds.Tables[0]);
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";
DataRelation dh = new DataRelation(RelationName, dtNew1.Columns["IDs"], dtNew2.Columns["IDs"]);
DataSet dz = new DataSet();
dz.Relations.Add(dh);
msg = DBHelper.DataSetToJson(dz, RelationName);
return msg;
}
}
}