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();
    //            DataSet ds = null;
    //                 string iFatherIdTwo = "";

    //            if (Bills.Count <= 0)
    //            {
    //                throw new Exception("传送数据为空！");
    //            }
    //            string sql = "";
    //            int num = 0;


    //               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 (ICSAssemVouch head in Bills)
    //                       {
    //                           iFatherIdTwo = "";
    //                           if (WorkPoint != head.WorkPoint)
    //                           {
    //                               continue;
    //                           }
    //                           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 crdname1 = dd[2];
    //                           //string crdname2 = dd[3];
    //                           //string carname = dd[0];
    //                           //string surface = dd[1];
    //                           //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", cRDcode09, 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", cRDcode08, 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)
    //                           {
    //                               if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
    //                               {
    //                                   if (body.BatchCode == "")
    //                                   {
    //                                       log.Error("该物料：" + body.InvCode + "已启用批次，请重新填写！");
    //                                       throw new Exception("该物料：" + body.InvCode + "已启用批次，请重新填写！");
    //                                   }
    //                               }
    //                               else
    //                               {
    //                                   if (body.BatchCode != "")
    //                                   {
    //                                       log.Error("该物料：" + body.InvCode + "未启用批次，请重新填写！");
    //                                       throw new Exception("该物料：" + body.InvCode + "未启用批次，请重新填写！");
    //                                   }
    //                               }
    //                               #region 形态转换单转换前表体
    //                               sql = @"INSERT INTO dbo.AssemVouchs
    //                                (ID,cAVCode,bAVType,cWhCode,cInvCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,
    //                                iAVQuantity,cAVBatch,autoID,bCosting,iExpiratDateCalcu,iAVNum,cItemCode,
    //                                irowno,iGroupNO)
    //                                VALUES(@ID,@cAVCode,@bAVType,@cWhCode,@cInvCode,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10,
    //                                @iAVQuantity,@cAVBatch,@autoID,0,0,@iAVNum,@cItemCode,
    //                                @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("@iAVNum", body.Amount.ToString("0.00")));
    //                               cmd.Parameters.Add(new SqlParameter("@cAVBatch", body.BatchCode));
    //                               cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
    //                               cmd.Parameters.Add(new SqlParameter("@autoID", iChildId));
    //                               cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
    //                               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.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,cItemCode,
    //                        cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iGroupNO,iPPrice)
    //                        SELECT @AutoID,@ID,cInvCode,iAVQuantity,'0','0','0',@iTrIds,null,iAVNum ,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cItemCode,
    //                         '0','0','0','0','1','0','0','0',@irowno,null,cAVBatch,iAVPCost,@iGroupNO,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("@iGroupNO", body.GroupNO));
    //                                   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

    //                                   #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);
    //                                   DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.OutWhCode, 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, iFatherId09, iChildId09);
    //                                   #endregion

    //                               }
    //                               else
    //                               {

    //                                   if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
    //                                   {
    //                                       if (body.BatchCode == "")
    //                                       {
    //                                           log.Error("该物料：" + body.InvCode + "已启用批次，请重新填写！");
    //                                           throw new Exception("该物料：" + body.InvCode + "已启用批次，请重新填写！");
    //                                       }
    //                                   }
    //                                   else
    //                                   {
    //                                       if (body.BatchCode != "")
    //                                       {
    //                                           log.Error("该物料：" + body.InvCode + "未启用批次，请重新填写！");
    //                                           throw new Exception("该物料：" + body.InvCode + "未启用批次，请重新填写！");
    //                                       }
    //                                   }
    //                                   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,iNum,cItemCode,
    //                        cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,iGroupNO)
    //                        SELECT @AutoID,@ID,cInvCode,iAVQuantity,'0','0','0',@iTrIds,iAVQuantity,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,iAVNum,cItemCode,
    //                         '0','0','0','0','1','0','0','0',@irowno,null,cAVBatch,iAVPCost,iAVPPrice,@iGroupNO 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("@iTrIds", iChildId));
    //                                   cmd.Parameters.Add(new SqlParameter("@iGroupNO", body.GroupNO));
    //                                   cmd.Parameters.Add(new SqlParameter("@irowno", Rd08irowno));
    //                                   cmd.CommandText = sql;
    //                                   DBHelper.CmdExecuteNonQuery(sql, cmd, "其他入库单表体保存失败！");
    //                                   #endregion

    //                                   #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);
    //                                   DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.InWhCode, 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,
    //                                       "", key08, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId08, iChildId08);
    //                                   #endregion

    //                               }
    //                               iFatherIdTwo = "'" + iFatherId + "',";
    //                               iChildId--;
    //                               iChildId09--;
    //                               iChildId08--;
    //                           }
    //                           #endregion

    //                           #region 查询
    //                           sql = @"SELECT DISTINCT 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,isnull(b.cAVBatch,'') AS BatchCode,isnull(b.cItemCode,'') AS ProjectCode
    //                                ,'' 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
    //                                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,BatchCode,ProjectCode,version,brand,
    //cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
    //                                FROM #TempERP

    //                                DROP TABLE #TempERP";
    //                           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)
    //                   {
    //                       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", "Costre");
    //            return msg;
    //        }

    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();
            DataSet ds = null;
            string iFatherIdTwo = "";

            if (Bills.Count <= 0)
            {
                throw new Exception("传送数据为空！");
            }
            string sql = "";
            int num = 0;


            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 (ICSAssemVouch head in Bills)
                    {
                        iFatherIdTwo = "";
                        if (WorkPoint != head.WorkPoint)
                        {
                            continue;
                        }
                        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 crdname1 = dd[2];
                        //string crdname2 = dd[3];
                        //string carname = dd[0];
                        //string surface = dd[1];
                        //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", cRDcode09, 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", cRDcode08, 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)
                        {
                            if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
                            {
                                if (body.BatchCode == "")
                                {
                                    log.Error("该物料：" + body.InvCode + "已启用批次，请重新填写！");
                                    throw new Exception("该物料：" + body.InvCode + "已启用批次，请重新填写！");
                                }
                            }
                            else
                            {
                                if (body.BatchCode != "")
                                {
                                    log.Error("该物料：" + body.InvCode + "未启用批次，请重新填写！");
                                    throw new Exception("该物料：" + body.InvCode + "未启用批次，请重新填写！");
                                }
                            }
                            #region 形态转换单转换前表体
                            sql = @"INSERT INTO dbo.AssemVouchs
                                (ID,cAVCode,bAVType,cWhCode,cInvCode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10,
                                iAVQuantity,cAVBatch,autoID,bCosting,iExpiratDateCalcu,iAVNum,cItemCode,
                                irowno,iGroupNO)
                                VALUES(@ID,@cAVCode,@bAVType,@cWhCode,@cInvCode,@cFree1,@cFree2,@cFree3,@cFree4,@cFree5,@cFree6,@cFree7,@cFree8,@cFree9,@cFree10,
                                @iAVQuantity,@cAVBatch,@autoID,0,0,@iAVNum,@cItemCode,
                                @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("@iAVNum", body.Amount.ToString("0.00")));
                            cmd.Parameters.Add(new SqlParameter("@cAVBatch", body.BatchCode));
                            cmd.Parameters.Add(new SqlParameter("@cItemCode", body.ProjectCode));
                            cmd.Parameters.Add(new SqlParameter("@autoID", iChildId));
                            cmd.Parameters.Add(new SqlParameter("@irowno", body.Sequence));
                            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.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,cItemCode,
                        cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iGroupNO,iPPrice)
                        SELECT @AutoID,@ID,cInvCode,iAVQuantity,'0','0','0',@iTrIds,null,iAVNum ,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,cItemCode,
                         '0','0','0','0','1','0','0','0',@irowno,null,cAVBatch,iAVPCost,@iGroupNO,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("@iGroupNO", body.GroupNO));
                                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

                                #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);
                                DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.OutWhCode, 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, iFatherId09, iChildId09);
                                #endregion

                            }
                            else
                            {

                                if (DBHelper.bInvBatch(body.InvCode, cmd) == true)
                                {
                                    if (body.BatchCode == "")
                                    {
                                        log.Error("该物料：" + body.InvCode + "已启用批次，请重新填写！");
                                        throw new Exception("该物料：" + body.InvCode + "已启用批次，请重新填写！");
                                    }
                                }
                                else
                                {
                                    if (body.BatchCode != "")
                                    {
                                        log.Error("该物料：" + body.InvCode + "未启用批次，请重新填写！");
                                        throw new Exception("该物料：" + body.InvCode + "未启用批次，请重新填写！");
                                    }
                                }
                                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,iNum,cItemCode,
                        cMassUnit,bLPUseFree,iRSRowNO,iOriTrackID,bCosting,bVMIUsed,iExpiratDateCalcu,iordertype,irowno,cbaccounter,cBatch,iPUnitCost,iPPrice,iGroupNO)
                        SELECT @AutoID,@ID,cInvCode,iAVQuantity,'0','0','0',@iTrIds,iAVQuantity,cFree1, cFree2, cFree3, cFree4, cFree5, cFree6, cFree7, cFree8, cFree9, cFree10,iAVNum,cItemCode,
                         '0','0','0','0','1','0','0','0',@irowno,null,cAVBatch,iAVPCost,iAVPPrice,@iGroupNO 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("@iTrIds", iChildId));
                                cmd.Parameters.Add(new SqlParameter("@iGroupNO", body.GroupNO));
                                cmd.Parameters.Add(new SqlParameter("@irowno", Rd08irowno));
                                cmd.CommandText = sql;
                                DBHelper.CmdExecuteNonQuery(sql, cmd, "其他入库单表体保存失败！");
                                #endregion

                                #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);
                                DBHelper.UpdateCurrentStockCCGC(cmd, body.InvCode, head.InWhCode, 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,
                                    "", key08, "", "", "", "", 0, 0, 0, 0, 0, 0, 0, 0, iFatherId08, iChildId08);
                                #endregion

                            }
                            iFatherIdTwo = "'" + iFatherId + "',";
                            iChildId--;
                            iChildId09--;
                            iChildId08--;
                        }
                        #endregion

                        #region 查询
                        sql = @"SELECT DISTINCT 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,isnull(b.cAVBatch,'') AS BatchCode,isnull(b.cItemCode,'') AS ProjectCode
                                ,'' 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
                                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,BatchCode,ProjectCode,version,brand,
cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10
                                FROM #TempERP

                                DROP TABLE #TempERP";
                        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)
                {
                    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", "Costre");
            return msg;
        }

    }
}
