Example #1
0
    //儲存鈕
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            #region 在儲存前把畫面上的退貨數量再UPDATE回dtForBrowse一次

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string strItem = ((Label)this.GridView1.Rows[i].FindControl("lblItem")).Text;
                string strPeriod = ((Label)this.GridView1.Rows[i].FindControl("lblPeriod")).Text;
                string strBackWardQty = ((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).Text.Trim();

                DataRow dr_d = dtForBrowse.Select("ITEM = '" + strItem + "' and PERIOD = '" + strPeriod + "'")[0];

                if (strBackWardQty != "")
                {
                    if (strBackWardQty != "0")
                    {
                        if (strBackWardQty != dr_d["BACKWARD_QTY"].ToString())
                        {
                            dr_d["BACKWARD_QTY"] = strBackWardQty;
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(Page, this.GetType(), "VGR022.aspx", "alert('退貨數量請輸入0以外的正整數! ');", true);
                        SetClinetFocus(((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).ClientID);
                        return;
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "VGR022.aspx", "alert('退貨數量請輸入0以外的正整數! ');", true);
                    SetClinetFocus(((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).ClientID);
                    return;
                }

                //foreach (DataRow dr in dtForBrowse.Rows)
                //{
                //    if (dr.RowState != DataRowState.Deleted)
                //    {
                //        if (dr["ITEM"].ToString() == strItem && dr["PERIOD"].ToString() == strPeriod)
                //        {
                //            if (((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).Text.Trim() != "")
                //            {
                //                if (((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).Text.Trim() != "0")
                //                {
                //                    if (((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).Text.Trim() != dr["BACKWARD_QTY"].ToString())
                //                    {
                //                        dr["BACKWARD_QTY"] = ((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).Text;
                //                    }
                //                }
                //                else
                //                {
                //                    ScriptManager.RegisterStartupScript(Page, this.GetType(), "VGR022.aspx", "alert('退貨數量請輸入0以外的正整數! ');", true);
                //                    SetClinetFocus(((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).ClientID);
                //                    return;
                //                }
                //            }
                //            else
                //            {
                //                ScriptManager.RegisterStartupScript(Page, this.GetType(), "VGR022.aspx", "alert('退貨數量請輸入0以外的正整數! ');", true);
                //                SetClinetFocus(((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).ClientID);
                //                return;
                //                //dr["BACKWARD_QTY"] = 0;
                //                //((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).Text = "0";
                //            }
                //        }
                //    }
                //}
            }

            #endregion

            if (CheckHaveDetailData())
            {
                ErrorMsgLabel.Text = "";
                VGRModel.MaintainChanReturnData BCO = new PIC.VDS2G.LGT.VGR.MaintainChanReturnData(ConntionDB);

                switch (Request["PageMode"])//編輯
                {
                    case "2":
                        ParameterList.Clear();
                        ParameterList.Add(txtBatchNo.Text);
                        ParameterList.Add(txtChanSourceNo.Text);
                        ParameterList.Add(Session["UID"].ToString());
                        ParameterList.Add(DateTime.Parse(PageTimeStamp.Value));
                        ParameterList.Add(GetUpdateUID());
                        ParameterList.Add(DateTime.ParseExact(GetUpdateDate(), "yyyyMMddHHmmssfffff", null));
                        BCO.DoUpdate(ParameterList, DBT);

                        UpdateViewState(Session["UID"].ToString(), DateTime.Parse(PageTimeStamp.Value).ToString("yyyyMMddHHmmssfffff"));

                        DataTable Dt_D = dtForBrowse.GetChanges(DataRowState.Deleted);
                        DataTable Dt_M = dtForBrowse.GetChanges(DataRowState.Modified);
                        DataTable Dt_A = dtForBrowse.GetChanges(DataRowState.Added);

                        string strID = string.Empty;

                        if (Dt_D != null)
                        {
                            if (Request.QueryString["ID"] != null)
                            {
                                strID = Request.QueryString["ID"].ToString();
                            }
                            else if (Session["PID"] != null && Session["PID"].ToString() != "")
                            {
                                strID = Session["PID"].ToString();
                            }

                            for (int x = 0; x < Dt_D.Rows.Count; x++)
                            {
                                ParameterList.Clear();

                                ParameterList.Add(int.Parse(strID));
                                ParameterList.Add(Dt_D.Rows[x]["ITEM", DataRowVersion.Original].ToString());
                                ParameterList.Add(Dt_D.Rows[x]["PERIOD", DataRowVersion.Original].ToString());
                                BCO.doDeleteDtl(ParameterList, DBT);
                            }
                            Dt_D.Clear();
                        }

                        if (Dt_M != null)
                        {
                            for (int y = 0; y < Dt_M.Rows.Count; y++)
                            {
                                ParameterList.Clear();

                                ParameterList.Add(int.Parse(Dt_M.Rows[y]["PID", DataRowVersion.Original].ToString()));
                                ParameterList.Add(Dt_M.Rows[y]["ITEM", DataRowVersion.Original].ToString());
                                ParameterList.Add(Dt_M.Rows[y]["PERIOD", DataRowVersion.Original].ToString());
                                ParameterList.Add(int.Parse(Dt_M.Rows[y]["BACKWARD_QTY", DataRowVersion.Current].ToString()));
                                ParameterList.Add(Session["UID"].ToString());
                                ParameterList.Add(DateTime.Parse(PageTimeStamp.Value));
                                ParameterList.Add(Dt_M.Rows[y]["UPDATEUID"].ToString());
                                ParameterList.Add(DateTime.Parse(Dt_M.Rows[y]["UPDATEDATE", DataRowVersion.Original].ToString()));

                                BCO.UpdChanReturnData(ParameterList, DBT);
                            }
                            Dt_M.Clear();
                        }

                        if (Dt_A != null)
                        {
                            DateTime dtTemp;
                            for (int z = 0; z < Dt_A.Rows.Count; z++)
                            {
                                ParameterList.Clear();

                                ParameterList.Add(int.Parse(Request.QueryString["ID"].ToString()));
                                ParameterList.Add(Dt_A.Rows[z]["ITEM", DataRowVersion.Current].ToString());
                                ParameterList.Add(Dt_A.Rows[z]["PERIOD", DataRowVersion.Current].ToString());
                                ParameterList.Add(Decimal.Parse(Dt_A.Rows[z]["CHAN_PRICE", DataRowVersion.Current].ToString()));
                                if (DateTime.TryParse(Dt_A.Rows[z]["CHAN_END_DATE"].ToString(), out dtTemp))
                                {
                                    ParameterList.Add(DateTime.Parse(Dt_A.Rows[z]["CHAN_END_DATE", DataRowVersion.Current].ToString()));
                                }
                                else
                                {
                                    ParameterList.Add(DBNull.Value);
                                }
                                ParameterList.Add(int.Parse(Dt_A.Rows[z]["BACKWARD_QTY", DataRowVersion.Current].ToString()));
                                ParameterList.Add(Session["UID"].ToString());
                                ParameterList.Add(Session["UID"].ToString());
                                ParameterList.Add(DateTime.Parse(PageTimeStamp.Value));
                                ParameterList.Add(DateTime.Parse(PageTimeStamp.Value));

                                BCO.DoInsertDetl(ParameterList, DBT);
                            }
                            Dt_A.Clear();
                        }

                        #region 舊程式碼

                        //for (int i = 0; i < GridView1.Rows.Count; i++)
                        //{
                        //    ParameterList.Clear();
                        //    bool bl = true;
                        //    string strPID = ((Label)this.GridView1.Rows[i].FindControl("Label1")).Text;
                        //    string strITEM = ((Label)this.GridView1.Rows[i].FindControl("lblItem")).Text;
                        //    string strPERIOD = ((Label)this.GridView1.Rows[i].FindControl("lblPeriod")).Text;
                        //    string strCREATEUID = ((Label)this.GridView1.Rows[i].FindControl("Label4")).Text;
                        //    string strUPDATEUID = ((Label)this.GridView1.Rows[i].FindControl("Label2")).Text;
                        //    string strUPDATEDATE = ((Label)this.GridView1.Rows[i].FindControl("Label3")).Text;
                        //    string strCREATEDATE = ((Label)this.GridView1.Rows[i].FindControl("Label5")).Text;
                        //    string strBACKWARD_QTY = ((TextBox)this.GridView1.Rows[i].FindControl("txtBackWardQty")).Text;

                        //    string strCHAN_PRICE = ((Label)this.GridView1.Rows[i].FindControl("lblChanPrice")).Text;
                        //    string strChanEndDate = ((Label)this.GridView1.Rows[i].FindControl("lblChanEndDate")).Text;

                        //    foreach (DataRow dr in OldDt.Rows)
                        //    {
                        //        bl = true;
                        //        if (dr["ITEM"].ToString() == strITEM && dr["PERIOD"].ToString() == strPERIOD && dr["BACKWARD_QTY"].ToString() != strBACKWARD_QTY)
                        //        {
                        //            ParameterList.Add(int.Parse(strPID));
                        //            ParameterList.Add(strITEM);
                        //            ParameterList.Add(strPERIOD);
                        //            ParameterList.Add(int.Parse(strBACKWARD_QTY));
                        //            ParameterList.Add(Session["UID"].ToString());
                        //            ParameterList.Add(DateTime.Parse(PageTimeStamp.Value));
                        //            ParameterList.Add(dr["UPDATEUID"].ToString());
                        //            ParameterList.Add(DateTime.Parse(dr["UPDATEDATE"].ToString()));

                        //            BCO.UpdChanReturnData(ParameterList, DBT);
                        //            bl = false;
                        //            break;
                        //        }
                        //        else if (dr["ITEM"].ToString() == strITEM && dr["PERIOD"].ToString() == strPERIOD)
                        //        {
                        //            bl = false;
                        //            break;
                        //        }
                        //    }
                        //    if (bl)
                        //    {
                        //        string strBatchNo = "";
                        //        int iPID = 0;

                        //        ParameterList.Clear();
                        //        ParameterList.Add(int.Parse(Request.QueryString["ID"].ToString()));
                        //        ParameterList.Add(strITEM);
                        //        ParameterList.Add(strPERIOD);
                        //        ParameterList.Add(Decimal.Parse(strCHAN_PRICE));
                        //        ParameterList.Add(DateTime.Parse(strChanEndDate));
                        //        ParameterList.Add(int.Parse(strBACKWARD_QTY));
                        //        ParameterList.Add(Session["UID"].ToString());
                        //        ParameterList.Add(Session["UID"].ToString());
                        //        ParameterList.Add(DateTime.Parse(PageTimeStamp.Value));
                        //        ParameterList.Add(DateTime.Parse(PageTimeStamp.Value));

                        //        BCO.DoInsertDetl(ParameterList, DBT);

                        //    }
                        //}
                        #endregion

                        if (this.Request["ID"] != null)
                            Response.Redirect(string.Format("VGR022.aspx?ID=" + this.Request["ID"].ToString() + "&PageMode=1&Batch_No=" + this.Request["Batch_No"].ToString() + "&Chan_No=" + this.Request["Chan_No"].ToString() + "&Sign_Date=" + this.Request["Sign_Date"].ToString() + "&P_DATE=" + this.Request["P_DATE"].ToString() + "&Store=" + this.Request["Store"].ToString() + "&Chan_Source_No=" + txtChanSourceNo.Text + "&STATUS_EXPLAIN=" + this.Request["STATUS_EXPLAIN"].ToString() + "&UPDATEUID=" + Session["UID"].ToString() + "&UPDATEDATE=" + DateTime.Parse(PageTimeStamp.Value).ToString("yyyyMMddHHmmssfffff")));
                        else
                            Response.Redirect(string.Format("VGR022.aspx?ID=" + Session["PID"].ToString() + "&PageMode=1&Batch_No=" + txtBatchNo.Text + "&Chan_No=" + SLP_StoreChain1.Text + "&Sign_Date=" + SLP_SLPDate1.Text + "&P_DATE=" + SLP_SLPDate1.Text + "&Store=" + SLP_Store1.Text + "&Chan_Source_No=" + txtChanSourceNo.Text + "&STATUS_EXPLAIN=" + "25&UPDATEUID=" + Session["UID"].ToString() + "&UPDATEDATE=" + DateTime.Parse(PageTimeStamp.Value).ToString("yyyyMMddHHmmssfffff")));
                        break;

                    case "3"://新增

                        if (QueryChanReturnToday(SLP_SLPDate2.Text, SLP_Store1.Text) == false)
                        {
                            doInsert();
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(Page, this.GetType(), "VGR022.aspx", "if(confirm('今日已有此門市之通路退貨單,確定新增?')){__doPostBack('doSave','');}", true);
                        }


                        break;

                    default:
                        break;
                }
            }
            else
            {
                throw new Exception("請先輸入明細資料");
            }
        }
        catch (Exception ex)
        {
            ErrorMsgLabel.Text = ex.Message;
        }
    }