public DataTable QueryNewOnhdQty(ArrayList ParameterList)
 {
     VDS_IVM11_DBO dbo = new VDS_IVM11_DBO(ref USEDB);
     DataTable dtResult;
     try
     {
         dtResult = dbo.QueryNewOnhdQty(ParameterList);
         return dtResult;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        public DataTable QueryMainByFind(ArrayList ParameterList)
        {

            VDS_IVM11_DBO dbo = new VDS_IVM11_DBO(ref USEDB);
            DataTable dtResult;
            try
            {
                dtResult = dbo.QueryAdjustMainByFind(ParameterList);
                return dtResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public DataTable QueryAdjustDataEmpty()
 {
     VDS_IVM11_DBO dbo = new VDS_IVM11_DBO(ref USEDB);
     DataTable dtResult;
     try
     {
         dtResult = dbo.QueryAdjustDataEmpty();
         return dtResult;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        private bool CheckSpace(DataTable dtDetail, out ArrayList ErrItemList, out ArrayList ItemQtyList, DbTransaction RootDBT)
        {
            bool IsRootTranscation = false;

            try
            {

                //判斷是否有傳入Root Transcation 
                IsRootTranscation = (RootDBT == null) ? true : false;

                #region 啟動交易或指定RootTranscation

                if (IsRootTranscation)
                {
                    //獨立呼叫啟動Transcation
                    Conn = USEDB.CreateConnection();
                    Conn.Open();
                    DBT = Conn.BeginTransaction();
                }
                else
                {
                    DBT = RootDBT;
                }

                #endregion

                VDS_IVM11_DBO dbo = new VDS_IVM11_DBO(ref USEDB);
                return dbo.CkeckLocateSize(dtDetail, out ErrItemList, out ItemQtyList,DBT);
            }
            catch (Exception ex)
            {
                #region 交易失敗

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation失敗
                    DBT.Rollback();
                }

                #endregion

                throw ex;
            }
            finally
            {
                #region 判斷是否關閉交易連線

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation,關閉連線
                    if (Conn.State == ConnectionState.Connecting)
                    {
                        Conn.Close();
                    }
                }

                #endregion
            }

        }
        /// <summary>
        /// 新增調整單主程式
        /// </summary>
        /// <param name="ParameterList">1.USER_ID 2.AdjustDate 3.AdjustReason 4.Option FALG(沒有的話就用預設值1) </param>
        /// <param name="dtDetail">Detail資訊</param>
        /// <param name="RootDBT">傳入的交易</param>
        /// <returns>true false 失敗</returns>
        /// <param name="ReturnList">[0]:ID [1]:AdjustNo [2]:ErrItem</param>
        public bool CreateAdjustData(ArrayList ParameterList, DataTable dtDetail, DbTransaction RootDBT, out string MainID, out string AdjustNo, out ArrayList ErrList)
        {
            bool HaveSpace = true;
            MainID = "";
            AdjustNo = "";
            int PID = 0;
            string UserID = ParameterList[0].ToString();
            string AdjustDate = ParameterList[1].ToString();
            string AdjustReason = ParameterList[2].ToString();
            bool IsRootTranscation = false;

            string strDateNow = DateTime.Now.ToString("yyyyMMddHHmmss");

            ArrayList ItemQtyList = new ArrayList();

            try
            {
                VDS_IVM11_DBO dbo = new VDS_IVM11_DBO(ref USEDB);

                MaintainStockInOut BCOStock = new MaintainStockInOut(strConn);

                //判斷是否有傳入Root Transcation 
                IsRootTranscation = (RootDBT == null) ? true : false;

                #region 啟動交易或指定RootTranscation

                if (IsRootTranscation)
                {
                    //獨立呼叫啟動Transcation
                    Conn = USEDB.CreateConnection();
                    Conn.Open();
                    DBT = Conn.BeginTransaction();
                }
                else
                {
                    DBT = RootDBT;
                }

                #endregion

                #region 檢查儲位大小是否可以新增

                HaveSpace = CheckSpace(dtDetail, out ErrList, out ItemQtyList,DBT);

                if (HaveSpace == false)
                {
                    if (IsRootTranscation)
                    {
                        DBT.Rollback();
                    }
                    return HaveSpace;
                }

                #endregion

                #region 建立調整單主檔

                ArrayList AdjustMainList = new ArrayList();
                AdjustMainList.Clear();
                AdjustMainList.Add(UserID);
                AdjustMainList.Add(AdjustDate);
                AdjustMainList.Add(AdjustReason);
                AdjustMainList.Add(strDateNow);

                dbo.CreateAdjustMain(AdjustMainList, DBT, out PID, out AdjustNo);

                MainID = PID.ToString();

                #endregion

                #region Build AdjustDetail

                ArrayList AdjustDetlList = new ArrayList();

                DataTable dtStockIn = dtDetail.Clone();

                DataTable dtStockOut = dtDetail.Clone();

                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    AdjustDetlList.Clear();
                    AdjustDetlList.Add(UserID);
                    AdjustDetlList.Add(PID);
                    AdjustDetlList.Add(dtDetail.Rows[i][0].ToString());
                    AdjustDetlList.Add(dtDetail.Rows[i][1].ToString());
                    AdjustDetlList.Add(dtDetail.Rows[i][2].ToString());
                    AdjustDetlList.Add(dtDetail.Rows[i][3].ToString());
                    AdjustDetlList.Add(int.Parse(ItemQtyList[i].ToString()));
                    AdjustDetlList.Add(int.Parse(dtDetail.Rows[i][4].ToString()));
                    AdjustDetlList.Add(double.Parse(dtDetail.Rows[i][5].ToString()));
                    AdjustDetlList.Add(strDateNow);
                    dbo.CreateAdjustDetl(AdjustDetlList, DBT);


                    //需要將資料表分成兩個

                    if (int.Parse(dtDetail.Rows[i][4].ToString()) > 0)
                    {
                        DataRow drIn = dtStockIn.NewRow();
                        for (int j = 0; j < dtDetail.Rows[i].ItemArray.Length; j++)
                        {
                            drIn[j] = dtDetail.Rows[i][j];
                        }
                        dtStockIn.Rows.Add(drIn);
                    }
                    else if (int.Parse(dtDetail.Rows[i][4].ToString()) < 0)
                    {
                        DataRow drOut = dtStockOut.NewRow();
                        for (int j = 0; j < dtDetail.Rows[i].ItemArray.Length; j++)
                        {
                            if (j == 4)
                            {
                                drOut[j] = int.Parse(dtDetail.Rows[i][j].ToString()) * -1;
                            }
                            else
                            {
                                drOut[j] = dtDetail.Rows[i][j];
                            }
                        }
                        dtStockOut.Rows.Add(drOut);
                    }
                }

                #region 出入庫單參數

                ArrayList StockList = new ArrayList();

                if (ParameterList.Count == 4)
                {
                    StockList.Add(ParameterList[3].ToString());
                }
                else
                {
                    StockList.Add("1");
                }
                StockList.Add("S6");
                StockList.Add("");
                StockList.Add(AdjustNo);
                StockList.Add(UserID);

                #endregion

                if (dtStockIn.Rows.Count > 0)
                {
                    BCOStock.CreateStockIn(StockList, dtStockIn, DBT);
                }
                if (dtStockOut.Rows.Count > 0)
                {
                    BCOStock.CreateStockOut(StockList, dtStockOut, DBT);
                }

                #endregion

                #region 交易成功

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation成立
                    DBT.Commit();
                }

                #endregion

                return HaveSpace;

            }
            catch (Exception ex)
            {
                #region 交易失敗

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation失敗
                    DBT.Rollback();
                }

                #endregion

                throw ex;
            }
            finally
            {
                #region 判斷是否關閉交易連線

                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation,關閉連線
                    if (Conn.State == ConnectionState.Connecting)
                    {
                        Conn.Close();
                    }
                }

                #endregion
            }

        }