/// <summary>
        /// 功能說明:進項發票收據資料查詢
        /// 
        /// 傳入參數:
        /// 〔@依照頁面輸入的查詢條件〕
        /// 傳回訊息:
        /// 〔@回傳gridview 參照web ui spec〕
        /// 
        /// TABLE:
        /// A=VDS_VAM _INVOICE_MAIN(進項發票主檔)
        /// B=VDS_VAM_INVOICE_DETL(進項發票明細檔)
        /// C= VDS_VAM_DIFF_INVOICE(物料差主檔)
        /// 
        /// 關聯:
        /// A.INV_NO=B.INV_NO=C.INV_NO
        /// 
        /// 說明:
        /// ENABLE 欄為=1才可以被撈出,依照查詢條件回傳資料清單
        /// </summary>
        public DataTable QueryByFind(ArrayList ParameterList)
        {
            try
            {

                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);
                return dbo.doQueryInvoiceMain(ParameterList);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //取得未日結日期
        public void CheckDailyClose(string V_CHECK_DATE_S, string V_CHECK_DATE_E, out string V_MSG, out string V_RESULT)
        {
            try
            {

                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);
                dbo.doCheckDailyClose(V_CHECK_DATE_S, V_CHECK_DATE_E, out V_MSG, out V_RESULT);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //20100316 新增自動沖帳功能
        public void AutoBalanceInvoice(ArrayList ParameterList,DbTransaction RootDBT, out string V_MSG, out string V_RESULT)
        {
            bool IsRootTranscation = false;

            try
            {
                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);

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


                #region 啟動交易或指定RootTranscation

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

                #endregion
               

                //做發票主檔新增
                dbo.doAutoBalanceInvoice(ParameterList, DBT,out V_MSG,out V_RESULT);
                
                if (IsRootTranscation)
                {
                    //獨立呼叫Transcation成立
                    DBT.Commit();
                }
            }
            catch (Exception ex)
            {
                #region 交易失敗

                DBT.Rollback();
                #endregion

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

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

                #endregion

            }
        }
        /// <summary>
        /// 傳入參數:
        /// 〔@發票號碼〕
        /// 傳回訊息:
        /// 〔@刪除是否成功〕
        /// //spec中的語法為參考,pg撰寫程式時需要調整程式語法。
        /// TABLE:
        /// A=VDS_VAM_INVOICE_MAIN(進項發票主檔)
        /// B=VDS_VAM_INVOICE_DETL(進項發票明細檔)
        /// C= VDS_VAM_DIFF_INVOICE(物料差主檔)
        /// 
        /// 
        /// 檢查:
        /// 1.已經被折讓的發票資料A. NON_DISC_AMT <> A.INV_UAMT 則不能刪除,顯示提醒無法刪除。
        /// 2.已經被折讓的物料差資料C. NON_DISC_AMT <> C. DIFF_INV_UAMT 則不能刪除,顯示提醒無法刪除。
        /// 3.回傳無法刪除訊息。
        /// 
        /// 說明:
        /// 呼叫VDS_ACM_VAM06.SP_DeletePurchaseInvoice利用sp進行
        /// </summary>
        public string DeletePurchaseInvoice(ArrayList ParameterList1, DbTransaction RootDBT)
        {
            bool IsRootTranscation = false;

            try
            {
                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);

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


                #region 啟動交易或指定RootTranscation

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

                #endregion
                int N_ID = 0;
                int N_ID1 = 0;
                string V_MSG, V_RESULT;

                //先刪除原發票資料
                dbo.doDeletePurchaseInvoice(ParameterList1, DBT, out V_MSG, out V_RESULT);

                if (V_RESULT == "1")//刪除成功
                {
                    V_MSG = "刪除成功";
                    if (IsRootTranscation)
                    {
                        //獨立呼叫Transcation成立
                        DBT.Commit();
                    }
                }
                else
                {
                    DBT.Rollback();                    
                }
                return V_MSG;
            }
            catch (Exception ex)
            {
                #region 交易失敗

                DBT.Rollback();
                #endregion

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

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

                #endregion

            }
        }
        public string UpdatePurchaseInvoice(ArrayList ParameterList1, ArrayList ParameterList2, DataTable dtA, DbTransaction RootDBT)
        {
            bool IsRootTranscation = false;

            try
            {
                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);

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


                #region 啟動交易或指定RootTranscation

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

                #endregion
                int N_ID = 0;
                int N_ID1 = 0;
                string V_MSG, V_RESULT;

                //先刪除原發票資料
                dbo.doDeletePurchaseInvoice(ParameterList1, DBT, out V_MSG, out V_RESULT);

                if (V_RESULT == "1")//刪除成功
                {
                    //做發票主檔新增
                    dbo.doInsertPurchaseInvoiceMain(ParameterList2, out N_ID, DBT);


                    //做發票明細檔新增
                    ArrayList Parameter_Det = new ArrayList();

                    if (dtA != null)
                    {
                        for (int i = 0; i < dtA.Rows.Count; i++)
                        {
                            Parameter_Det.Clear();
                            //N_PID
                            Parameter_Det.Add(N_ID);
                            //V_INV_NO
                            Parameter_Det.Add(ParameterList2[0]);
                            //V_TAX_TYPE
                            Parameter_Det.Add(ParameterList2[3]);
                            //V_SOURCE_TYPE
                            Parameter_Det.Add(GetValueSetParameter(dtA.Rows[i]["SOURCE_TYPE"].ToString(), "string", false));
                            //V_SOURCE_NO
                            Parameter_Det.Add(GetValueSetParameter(dtA.Rows[i]["SOURCE_NO"].ToString(), "string", false));
                            //N_REMAIN_AMT目前沒用到該欄位所以填NULL
                            Parameter_Det.Add(GetValueSetParameter("", "float", false));
                            //N_AMT
                            Parameter_Det.Add(GetValueSetParameter(dtA.Rows[i]["AMT"].ToString(), "float", false));
                            //D_UPDATEDATE
                            Parameter_Det.Add(ParameterList2[8]);
                            //V_UPDATEUID
                            Parameter_Det.Add(ParameterList2[9]);
                            //V_CREATEUID
                            Parameter_Det.Add(ParameterList2[10]);
                            //D_CREATEDATE
                            Parameter_Det.Add(ParameterList2[11]);

                            dbo.doInsertPurchaseInvoiceDetl(Parameter_Det, out N_ID1, DBT);
                        }
                    }


                    //會產生物料差單號
                    dbo.doGenDiffInvoice(ParameterList2, DBT);

                    if (IsRootTranscation)
                    {
                        //獨立呼叫Transcation成立
                        DBT.Commit();
                    }
                }
                else
                {
                    DBT.Rollback();
                }
                return V_MSG;
            }
            catch (Exception ex)
            {
                #region 交易失敗

                DBT.Rollback();
                #endregion

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

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

                #endregion

            }
        }
        //查詢稅率
        public void QueryTaxRate(out decimal N_TAX_RATE)
        {
            try
            {

                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);
                dbo.doGetTaxRate(out N_TAX_RATE);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //查詢發票明細檔資料
        public DataTable QueryInvoiceDetl(string V_INV_NO)
        {
            try
            {

                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);
                return dbo.doQueryInvoiceDetl(V_INV_NO);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 傳入值:
        /// 〔@單據起日〕
        /// 〔@單據迄日〕
        /// 〔@廠商統編〕
        /// 〔@課稅別〕
        /// 
        /// 傳回值:
        /// 〔@單據別〕
        /// 〔@單據號碼〕
        /// 〔@原始單據金額〕
        /// 〔@已維護金額〕
        /// 〔@未維護金額〕 (驗收單為@UNREMAIN_ ACCEPT_AMT,@退貨單為UNREMAIN_ RETURN_AMT)
        /// //spec中的語法為參考,pg撰寫程式時需要調整程式語法。
        /// MAIN TABLE:
        /// A=VDS_VAM_INVOICE_MAIN(進項發票主檔)
        /// B=VDS_VAM_INVOICE_DETL(進項發票明細檔)
        /// 
        /// REFERENCE TABLE:
        /// 驗收單:
        /// 1.VDS_VGI_ACCEPT_MAIN (驗收主檔)
        /// 2. VDS_VAM_CHECK_OUT (供應商帳務日結檔)
        /// 
        /// 退貨單:
        /// 1.VDS_VGR_RETURN_MAIN (廠退主檔)
        /// 2.VDS_VGR_RETURN_DETL (廠退明細檔)
        /// 3.VDS_ITM_PERIOD_MAIN (期別主檔)
        /// 
        /// 說明:
        /// 呼叫VDS_ACM_VAM06.SP_QueryPurchaseInvoice利用sp進行查詢。
        /// ENABLE 欄為=1才可以被撈出。資料清單顯示在VAM062_進項發票收據維護(編輯模式)
        /// </summary>
        public DataTable QueryPurchaseInvoice(ArrayList ParameterList)
        {
            try
            {

                DBO.VAM061_DBO dbo = new VAM061_DBO(ref USEDB);
                return dbo.doQueryInvoiceSource(ParameterList);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }