示例#1
0
    }//UC_Delete

    private void UC_Save()
    {

        #region
        ErrorMsgLabel.Text = "";
        string MainID = string.Empty;
        string AdjustNO = string.Empty;
        ArrayList ErrList = new ArrayList();

        try
        {
            string SessionIDName = "IVM11_DTL_" + PageTimeStamp.Value;

            #region 檢查必要條件

            //檢查頁面上的Parameter是否都有資料

            if (SLP_User1.Text == string.Empty || SLP_SLPDate1.Text == string.Empty)
            {
                ErrorMsgLabel.Text = "請確認欄位都已填寫完成";
                return;
            }

            //檢查頁面上的DATATABLE資料都有齊全

            if (GridView1.Rows.Count == 0)
            {
                ErrorMsgLabel.Text = "尚未輸入調整單明細資料,新增失敗";
                return;
            }

            DataTable Dt = (DataTable)Session[SessionIDName];

            if (Dt == null ||Dt.Rows.Count == 0)
            {
                ErrorMsgLabel.Text = "尚未輸入調整單明細資料,新增失敗";
                return;
            }

            //變更DATATABLE 資料內容
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                Label lblItem = (Label)GridView1.Rows[i].FindControl("lblItem");
                Label lblPeriod = (Label)GridView1.Rows[i].FindControl("lblPeriod");
                Label lblLocateSection = (Label)GridView1.Rows[i].FindControl("lblLocateSection");
                Label lblOnhdQty = (Label)GridView1.Rows[i].FindControl("lblOnhdQty");
                TextBox txtAdjustQty = (TextBox)GridView1.Rows[i].FindControl("SLP_Number").FindControl("TextBoxCode");

                for (int x = 0; x < Dt.Rows.Count; x++)
                {
                    string Item = Dt.Rows[x]["item"].ToString();
                    string Period = Dt.Rows[x]["period"].ToString();
                    string LocateSec = Dt.Rows[x]["locate_section"].ToString();

                    if (lblItem.Text == Item && lblPeriod.Text == Period && lblLocateSection.Text == LocateSec)
                    {
                        if (int.Parse(txtAdjustQty.Text) + int.Parse(lblOnhdQty.Text) >= 0)
                        {
                            Dt.Rows[x]["adjust_qty"] = txtAdjustQty.Text;
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(this.UpdatePanel1, typeof(UpdatePanel), "隨便寫", "document.getElementById('" + txtAdjustQty.ClientID + "').focus();", true);
                            return;
                        }
                    }
                }
            }

            #endregion

            IVMModel.MaintainAdjustData BCO = new IVMModel.MaintainAdjustData(ConntionDB);

            //新增主檔所需PARAMETERLIST

            DropDownList ddl = (DropDownList)SLP_CodeFile1.FindControl("D1");

            ParameterList.Clear();
            ParameterList.Add(Session["UID"].ToString());
            ParameterList.Add(DateTime.Now.ToString("yyyyMMddHHmmss"));
            ParameterList.Add(ddl.SelectedValue);

            Dt.Columns.Remove("ROWNUM");
            Dt.Columns.Remove("id");
            Dt.Columns.Remove("BarCode");
            Dt.Columns.Remove("item_name");
            Dt.Columns.Remove("locate_name");
            Dt.Columns.Remove("onhd_qty");

            bool InsertOK = false;

            InsertOK = BCO.CreateAdjustData(ParameterList, Dt, null, out MainID, out AdjustNO, out ErrList);

            CraeteMainID = MainID;

            if (InsertOK)
            {
                //先把QueryID清空
                ArrayList IDList = new ArrayList();
                IDList.Add(MainID);
                Session["IVM11QueryID"] = IDList;

                Session[SessionIDName] = null;

                Response.Redirect(string.Format("IVM112.aspx?Code=IVM11&ID={0}&PageMode=1", MainID));
            }
            else
            {
                string strError = "";
                for(int i = 0; i < ErrList.Count;i++)
                {
                    if (i == ErrList.Count - 1)
                    {
                        strError += ErrList[i].ToString();
                    }
                    else
                    {
                        strError += ErrList[i].ToString() + ",";
                    }
                }
                //ScriptManager.RegisterStartupScript(this.UpdatePanel1, typeof(UpdatePanel), "IVM112", "alert('" + strError + "於指定儲位之庫存量不足');", true);
                ErrorMsgLabel.Text = strError + "於指定儲位之庫存量不足";

                return;
            }
        }
        catch (Exception ex)
        {
            ErrorMsgLabel.Text = ex.Message;
        }
        #endregion

    }//UC_Save
        /// <summary>
        /// 建立退貨單主檔
        /// </summary>
        /// <param name="ParameterList">0:UserID 1:ReturnDate(yyyy/MM/dd HH:mm:ss) 2:@IsAdj 3:退貨LocateSection 4:Vendor 5:ReturnNo 6:AdjustNo 7:UnTax_Total_Amt  8:Tax_Total_Amt 9:UnTax_ReMain_Amt 10:Tax_ReMain_Am 11:Tax_Amt</param>
        /// <param name="dtDetail">欄位 0:item 1:Period 2:tax_type 3:onhd_qty 4:return_qty 5:adjust_qty 6:cost 7:price</param>
        /// <param name="RootDBT">使用的交易 null代表無傳入交易系統可以自行產生</param>
        /// <param name="ReturnNo">退貨單號 10碼</param>
        /// <param name="MainID"> 主表單ID </param>
        /// <param name="AdjustNo">調整單號</param>
        /// <returns>是否成功</returns>
        
        public bool CreateReturnData(ArrayList ParameterList, DataTable dtDetail, DbTransaction RootDBT,
                                     out string ReturnNo, out string MainID, out string AdjustNo)
        {
            ReturnNo = "";
            MainID = "";
            AdjustNo = "";

            string AdjMainID = "";
            int PID = 0;
            bool IsRootTranscation = false;
            string UserID = ParameterList[0].ToString();
            string ReturnDate = ParameterList[1].ToString();
            string IsAdj = ParameterList[2].ToString();
            string LocateSection = ParameterList[3].ToString();
            string LocateNo = LocateSection.Substring(0,1);
            string Vendor = ParameterList[4].ToString();
            string ReasonNo = ParameterList[5].ToString();
            string AdjReason = ParameterList[6].ToString();

            string strReasonNO = "";

            ArrayList ErrorList = new ArrayList();

            try
            {
                //建立退貨主檔資料
                DBO.CreateReturnData db = new CreateReturnData(ref USEDB);
                DBO.UpdateReturnData dbu = new UpdateReturnData(ref USEDB);
                IVMDBO.MaintainAdjustData dbAdj = new IVMDBO.MaintainAdjustData(strConn);
                IVMDBO.MaintainStockInOut dbStock = new IVMDBO.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


                if (IsAdj == "1") //建立調整單並建立出庫單
                {
                    ArrayList AdjustList = new ArrayList();
                    AdjustList.Add(UserID);
                    AdjustList.Add(ReturnDate);
                    AdjustList.Add(AdjReason);
                    AdjustList.Add(ReasonNo);

                    //轉換DataTable資料格式
                    DataTable DtAdjust = new DataTable();

                    //轉換DataTable資料格式
                    DtAdjust.Columns.Add("Locate_No", typeof(String));
                    DtAdjust.Columns.Add("Locate_Section", typeof(String));
                    DtAdjust.Columns.Add("Item", typeof(String));
                    DtAdjust.Columns.Add("Period", typeof(String));
                    DtAdjust.Columns.Add("Adjust_Qty", typeof(Int32));
                    DtAdjust.Columns.Add("Cost", typeof(Double));

                    for (int i = 0; i < dtDetail.Rows.Count; i++)
                    {
                        if (int.Parse(dtDetail.Rows[i]["Adjust_Qty"].ToString()) != 0)
                        {
                            DataRow dr = DtAdjust.NewRow();
                            dr["Locate_No"] = LocateNo;
                            dr["Locate_Section"] = LocateSection;
                            dr["Item"] = dtDetail.Rows[i][0].ToString();
                            dr["Period"] = dtDetail.Rows[i][1].ToString();
                            dr["Adjust_Qty"] = int.Parse(dtDetail.Rows[i][5].ToString());
                            dr["Cost"] = double.Parse(dtDetail.Rows[i][6].ToString());
                            DtAdjust.Rows.Add(dr);
                        }
                    }

                    //建立調整單
                    dbAdj.CreateAdjustData(AdjustList, DtAdjust, DBT, out AdjMainID, out AdjustNo, out ErrorList);

                }

                ArrayList ReturnMainList = new ArrayList();
                ReturnMainList.Add(UserID);
                ReturnMainList.Add(ReturnDate);
                ReturnMainList.Add(Vendor);
                ReturnMainList.Add(decimal.Parse(ParameterList[7].ToString()));
                ReturnMainList.Add(decimal.Parse(ParameterList[8].ToString()));
                ReturnMainList.Add(decimal.Parse(ParameterList[9].ToString()));
                ReturnMainList.Add(decimal.Parse(ParameterList[10].ToString()));
                ReturnMainList.Add(decimal.Parse(ParameterList[11].ToString()));

                db.CreateReturnMain(ReturnMainList, DBT, out PID,out ReturnNo);

                strReasonNO = ReturnNo;

                MainID = PID.ToString();

                //建立Detail資料

                ArrayList ReturnDetailList = new ArrayList();

                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    ReturnDetailList.Clear();
                    ReturnDetailList.Add(UserID);
                    ReturnDetailList.Add(ReturnDate);
                    ReturnDetailList.Add(PID);
                    ReturnDetailList.Add(dtDetail.Rows[i][0].ToString()); //item
                    ReturnDetailList.Add(dtDetail.Rows[i][1].ToString()); //Period
                    ReturnDetailList.Add(dtDetail.Rows[i][2].ToString()); //tax_type
                    ReturnDetailList.Add(dtDetail.Rows[i][3].ToString()); //onhd_qty
                    ReturnDetailList.Add(dtDetail.Rows[i][4].ToString()); //return_qty
                    ReturnDetailList.Add(dtDetail.Rows[i][5].ToString()); //adjust_qty
                    ReturnDetailList.Add(dtDetail.Rows[i][6].ToString()); //cost
                    ReturnDetailList.Add(dtDetail.Rows[i][7].ToString()); //price
                    db.CreateReturnDetl(ReturnDetailList, DBT, out PID);
                }

                //計算稅額
                ReturnMainList.Clear();
                ReturnMainList.Add(ReturnNo);
                ReturnMainList.Add(UserID);
                ReturnMainList.Add(ReturnDate);

                dbu.UpdateReturnMainTax(ReturnMainList, DBT);

                //建立出庫單

                ArrayList StockList = new ArrayList();
                StockList.Add("1");
                StockList.Add("S4");
                StockList.Add(AdjustNo);
                StockList.Add(strReasonNO);
                StockList.Add(UserID);

                DataTable DtStock = new DataTable();
                DtStock.Columns.Add("Locate_No", typeof(String));
                DtStock.Columns.Add("Locate_Section", typeof(String));
                DtStock.Columns.Add("Item", typeof(String));
                DtStock.Columns.Add("Period", typeof(String));
                DtStock.Columns.Add("InOut_Qty", typeof(Int32));
                DtStock.Columns.Add("Cost", typeof(Double));

                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    DataRow dr = DtStock.NewRow();
                    dr["Locate_No"] = LocateNo;
                    dr["Locate_Section"] = LocateSection;
                    dr["Item"] = dtDetail.Rows[i][0].ToString();
                    dr["Period"] = dtDetail.Rows[i][1].ToString();
                    dr["InOut_Qty"] = int.Parse(dtDetail.Rows[i][4].ToString());
                    dr["Cost"] = double.Parse(dtDetail.Rows[i][6].ToString());
                    DtStock.Rows.Add(dr);
                }

                dbStock.CreateStockOut(StockList, DtStock, DBT);

                db.UpdateMainAdjustNo(MainID, AdjustNo, DBT);

                #region 交易成功

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

            }
            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
            }
        }