/// <summary>
        /// 新增資料
        /// </summary>
        protected void btn_add_Click(object sender, EventArgs e)
        {
            if (txt_ID.Text.Trim() != "")
            {
                RedGlovePermission.Model.Basic_Companies model = new RedGlovePermission.Model.Basic_Companies();

                model.Company = txt_ID.Text.Trim();
                model.Track = txt_Track.Text.Trim();
                model.Description = txt_Name.Text.Trim();
                UserSession user = SessionBox.GetUserSession();
                model.Creator = user.LoginName.ToString();
                model.Create_Date = DateTime.Now.ToString("yyyyMMdd");

                if (!bll.Exists(txt_ID.Text.Trim()))
                {
                    if (bll.CreateCompany(model))
                    {
                        ScriptManager.RegisterClientScriptBlock(CustomPanel1, this.GetType(), "MsgBox", "alert('" + ResourceManager.GetString("Pub_Msg_add_true") + "')", true);
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(CustomPanel1, this.GetType(), "MsgBox", "alert('" + ResourceManager.GetString("Pub_Msg_add_false") + "')", true);
                    }
                    txt_ID.Text = "";
                    txt_Track.Text = "";
                    txt_Name.Text = "";
                    BindOrder();
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(CustomPanel1, this.GetType(), "MsgBox", "alert('" + ResourceManager.GetString("Pub_Msg_IsCustomer") + "')", true);
                }
            }
        }
        /// <summary>
        /// 更新資料
        /// </summary>
        protected void CompaniesLists_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            RedGlovePermission.Model.Basic_Companies model = new RedGlovePermission.Model.Basic_Companies();
            model.Company = CompaniesLists.DataKeys[e.RowIndex].Values[0].ToString();
            model.Track = ((TextBox)CompaniesLists.Rows[e.RowIndex].FindControl("txt_Track")).Text.Trim();
            model.Description = ((TextBox)CompaniesLists.Rows[e.RowIndex].FindControl("txt_Name")).Text.Trim();
            UserSession user = SessionBox.GetUserSession();
            model.Modifier = user.LoginName.ToString();
            model.Modi_Date = DateTime.Now.ToString("yyyyMMdd");

            if (!bll.UpdateCompany(model))
            {
                ScriptManager.RegisterClientScriptBlock(CustomPanel1, this.GetType(), "MsgBox", "alert('" + ResourceManager.GetString("Pub_Msg_update_false") + "')", true);
            }
            //返回瀏覽狀態
            CompaniesLists.EditIndex = -1;
            BindOrder2();
        }
        protected void gvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "InsertNewM")
            {
                //TextBox tbOrderID = (TextBox)gvMaster.Controls[0].Controls[0].FindControl("tbOrderID");
                TextBox tbMA001 = (TextBox)gvMaster.Controls[0].Controls[0].FindControl("tbMA001");
                TextBox tbCompany = (TextBox)gvMaster.Controls[0].Controls[0].FindControl("tbCompany");
                TextBox tbReturn_Date = (TextBox)gvMaster.Controls[0].Controls[0].FindControl("tbReturn_Date");
                TextBox tbCaseNo = (TextBox)gvMaster.Controls[0].Controls[0].FindControl("tbCaseNo");
                DropDownList ddlDepartment = (DropDownList)gvMaster.Controls[0].Controls[0].FindControl("ddl_Department");
                DropDownList ddlTaxRate = (DropDownList)gvMaster.Controls[0].Controls[0].FindControl("ddl_TaxRate");
                TextBox tbRemark = (TextBox)gvMaster.Controls[0].Controls[0].FindControl("tbRemark");

                //if (tbOrderID != null)
                //    dsMaster.InsertParameters["OrderID"].DefaultValue = tbOrderID.Text;
                RedGlovePermission.Model.Basic_Companies model = new RedGlovePermission.Model.Basic_Companies();
                model = bll.GetCCompanyModel(CompanyList.SelectedItem.ToString());
                dsMaster.InsertParameters["ReturnID"].DefaultValue = bllReturn.GetNewReturnID(model.Track + DateTime.Now.ToString("yyyyMMdd"));
                if (tbMA001 != null)
                    dsMaster.InsertParameters["MA001"].DefaultValue = tbMA001.Text;
                dsMaster.InsertParameters["COMPANY"].DefaultValue = CompanyList.SelectedItem.ToString();
                if (tbReturn_Date != null)
                    dsMaster.InsertParameters["Return_DATE"].DefaultValue = tbReturn_Date.Text;
                if (tbCaseNo != null)
                    dsMaster.InsertParameters["CaseNo"].DefaultValue = tbCaseNo.Text;
                dsMaster.InsertParameters["Department"].DefaultValue = ddlDepartment.SelectedValue;
                dsMaster.InsertParameters["TaxRate"].DefaultValue = ddlTaxRate.SelectedValue;
                if (tbRemark != null)
                    dsMaster.InsertParameters["Remark"].DefaultValue = tbRemark.Text;
                UserSession user = SessionBox.GetUserSession();
                dsMaster.InsertParameters["CREATOR"].DefaultValue = user.LoginName.ToString();
                dsMaster.InsertParameters["CREATE_DATE"].DefaultValue = DateTime.Now.ToString("yyyyMMdd");
                int aff_row = dsMaster.Insert();
                if (aff_row == 0)
                {
                    ScriptManager.RegisterStartupScript(Page, GetType(), Guid.NewGuid().ToString(), "alert('" + ResourceManager.GetString("Pub_Msg_add_false") + "');", true);
                    _hfSearch.Value = "";
                }
                else 
                //{
                //    ScriptManager.RegisterStartupScript(Page, GetType(), Guid.NewGuid().ToString(), "alert('" + ResourceManager.GetString("Pub_Msg_add_true") + dsMaster.InsertParameters["OrderID"].DefaultValue.ToString() + "');", true);
                //    _hfSearch.Value = "";
                //}
                    _hfSearch.Value = "ReturnID='" + dsMaster.InsertParameters["ReturnID"].DefaultValue.ToString() + "'";
                BindData();
            }
            if (e.CommandName == "InsertCancelM")
            {
                _hfSearch.Value = "";
                BindData();
            }
            //if (e.CommandName == "Prt")
            //{}
        }
        protected void gvChild_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow gvr = (GridViewRow)((GridView)sender).NamingContainer;
            HiddenField _hfChildSearch = (HiddenField)gvr.FindControl("_hfChildSearch");
            SqlDataSource dsChild = (SqlDataSource)gvr.FindControl("dsChild");

            if (e.CommandName == "InsertNew")
            {
                //TextBox tbSeq = (TextBox)((GridView)sender).Controls[0].Controls[0].FindControl("tbSeq");
                TextBox tbMB001 = (TextBox)((GridView)sender).Controls[0].Controls[0].FindControl("tbMB001");
                TextBox tbQty = (TextBox)((GridView)sender).Controls[0].Controls[0].FindControl("tbQty");
                TextBox tbUnitPrice = (TextBox)((GridView)sender).Controls[0].Controls[0].FindControl("tbUnitPrice");
                TextBox tbDiscount = (TextBox)((GridView)sender).Controls[0].Controls[0].FindControl("tbDiscount");
                //if (tbSeq != null)
                //    dsChild.InsertParameters["Seq"].DefaultValue = tbSeq.Text;
                RedGlovePermission.Model.Basic_Companies model = new RedGlovePermission.Model.Basic_Companies();
                model = bll.GetCCompanyModel(CompanyList.SelectedItem.ToString());
                dsChild.InsertParameters["Seq"].DefaultValue = bllReturn.GetNewReturnSeq(gvr.Cells[5].Text);
                if (tbMB001 != null)
                    dsChild.InsertParameters["MB001"].DefaultValue = tbMB001.Text;
                if (tbQty != null)
                    dsChild.InsertParameters["Qty"].DefaultValue = tbQty.Text;
                if (tbUnitPrice != null)
                    dsChild.InsertParameters["unitPrice"].DefaultValue = tbUnitPrice.Text;
                if (tbDiscount != null)
                    dsChild.InsertParameters["Discount"].DefaultValue = tbDiscount.Text;
                else
                    dsChild.InsertParameters["Discount"].DefaultValue = "1";
                dsChild.InsertParameters["ReturnId"].DefaultValue = gvr.Cells[5].Text;
                UserSession user = SessionBox.GetUserSession();
                dsChild.InsertParameters["CREATOR"].DefaultValue = user.LoginName.ToString();
                dsChild.InsertParameters["CREATE_DATE"].DefaultValue = DateTime.Now.ToString("yyyyMMdd");
                dsChild.Insert();
                //處理單頭合計及稅額
                if (bllReturn.ReturnTotal(gvr.Cells[5].Text) == 0)
                    ScriptManager.RegisterStartupScript(Page, GetType(), Guid.NewGuid().ToString(), "alert('" + ResourceManager.GetString("Pub_Msg_total_false") + "');", true);
                else
                {
                    RedGlovePermission.Model.Sales_Return Returnmodel = new RedGlovePermission.Model.Sales_Return();
                    Returnmodel = bllReturn.GetReturnModel(gvr.Cells[5].Text);
                    gvr.Cells[13].Text = Returnmodel.Amount.ToString();
                    gvr.Cells[14].Text = Returnmodel.Tax.ToString();
                }
                ScriptManager.RegisterStartupScript(Page, GetType(), Guid.NewGuid().ToString(), "alert('" + ResourceManager.GetString("Pub_Msg_update_true") + "');", true);
                _hfChildSearch.Value = "";
                BindChildData((GridView)sender);
            }
            if (e.CommandName == "InsertCancel")
            {
                _hfChildSearch.Value = "";
                BindChildData((GridView)sender);
            }
        }
        /// <summary>
        /// 得到一家公司資料
        /// </summary>
        /// <param name="Company">公司名</param>
        /// <returns></returns>
        public RedGlovePermission.Model.Basic_Companies GetCompanyModel(string Company)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 Company,Description,Track from Basic_Company ");
            strSql.Append(" where Company=@Company ");
            SqlParameter[] parameters = {
					new SqlParameter("@Company", SqlDbType.Char,10)};
            parameters[0].Value = Company;

            RedGlovePermission.Model.Basic_Companies model = new RedGlovePermission.Model.Basic_Companies();
            DataSet ds = SqlServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                //if (ds.Tables[0].Rows[0]["MA001"].ToString() != "")
                //{
                model.Company = ds.Tables[0].Rows[0]["Company"].ToString();
                //}
                model.Track = ds.Tables[0].Rows[0]["Track"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }