Example #1
0
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        #region
        try
        {
            ErrorMsgLabel.Text = "";
            ResultMsgLabel.Text = "";
            hiddenErrMsg.Value = "";

            DateTime ProcessingDateTime = DateTime.Now;

            string strItem = SLP_SKU1.Text;
            string strPeriod = SLP_ItemPeriod1.Text;
            string strDisType = SLP_PARAM1.Text;
            string strDisDate = SLP_SLPDate1.Text;
            string strStAcceptDate = SLP_SLPDate2.Text;
            string strUserId = Session["UID"].ToString();
            string strErrMsgTmp = "";
            #region


            if (!GetItemPeriodDisStateByItemPeriod(strItem, strPeriod, strStAcceptDate, out strErrMsgTmp))
            {
                ErrorMsgLabel.Text = strErrMsgTmp;
                return;
            }

            //如果Rank類別=109 且未設定pattern等級設定則不繼續執行
            if (strDisType == "109" && (dtDisTypeTmp109 == null || dtDisTypeTmp109.Rows.Count <= 0))
            {
                ErrorMsgLabel.Text = "無設定等級資料,請確認配本方式或重新設定";
                return;
            }

            SetDisTmpRec(strDisDate, strStAcceptDate, strItem, strPeriod, strDisType, strUserId, ProcessingDateTime);
            SetDisMain(strDisDate, strStAcceptDate, strUserId, ProcessingDateTime);
            SetDisItem(strItem, strPeriod, strDisType, strUserId, ProcessingDateTime);
            SetDisChan(strItem, strPeriod, strStAcceptDate, strDisType, strUserId, ProcessingDateTime);

            ALOModel.MaintainDisRecord BCO = new ALOModel.MaintainDisRecord(ConnectionDB);

            //當為新增模式時,或編輯模式下改變配本方式
            if (CurrentPageMode == PageCurrentMode.Insert || (CurrentPageMode == PageCurrentMode.Edit && s_DIS_TYPE_Original != strDisType))
            {
                if (strDisType == "101" || strDisType == "103" || strDisType == "104")
                {
                    dtDisItemRefer = BCO.QueryDisItemReferByDisType(dtParam, dtDisItem, strDisType, strItem, strPeriod, vItemReferCount);

                    currentGvMode = CurrentGridviewMode.View;
                    gv_ItemRefer.DataSource = dtDisItemRefer;
                    gv_ItemRefer.DataBind();
                    hiddenDtDisItemReferCounts.Value = dtDisItemRefer.Rows.Count.ToString();
                    CalculateReferRateSum();

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", string.Format("SwitchControlDisplay('{0}','{1}',1);", Panel_ItemRefer.ClientID, hiddenItemReferDisplayMode.ClientID), true);
                }
            }

            SetDisItemRefer(strUserId, ProcessingDateTime);
            #endregion

            if (CurrentPageMode == PageCurrentMode.Insert)
            {
                #region

                ALOModel.ALOCommon BCOCommon = new ALOModel.ALOCommon();
                System.Data.Common.DbTransaction DBT = BCOCommon.OpenAndGetConnection(ConnectionDB).BeginTransaction();
                try
                {
                    string strDisNo = BCO.CreateDisRecordFor131(dtDisTmpRec, dtDisMain, dtDisItem, dtDisChan, dtDisItemRefer, dtDisTypeTmp109, dtDisTypeTmp, strItem, strPeriod, strUserId, DBT);

                    if (strDisNo != "")
                    {
                        ALOModel.MaintainDisTmpRec BCO2 = new ALOModel.MaintainDisTmpRec(ConnectionDB);
                        BCO2.CreateDisTmpRec(dtDisTmpRec, strDisNo, DBT);
                    }


                    DBT.Commit();//交易成功

                }
                catch (Exception ex)
                {
                    DBT.Rollback();//交易失敗
                    throw ex;
                }

                QueryData();

                ResultMsgLabel.Text = "新增成功";
                #endregion
            }
            else if (CurrentPageMode == PageCurrentMode.Edit)
            {
                #region

                SetPrimaryKeyForDataTable();

                ALOModel.ALOCommon BCOCommon = new ALOModel.ALOCommon();
                System.Data.Common.DbTransaction DBT = BCOCommon.OpenAndGetConnection(ConnectionDB).BeginTransaction();
                try
                {

                    bool bResult = BCO.UpdateDisRecordFor131(dtDisTmpRec, dtDisTmpRecOld,
                      dtDisMain, dtDisMainOld, dtDisItem, dtDisItemOld,
                      dtDisChan, dtDisChanOld, dtDisItemRefer, dtDisItemReferOld,
                      dtDisTypeTmp109, dtDisType109TmpOld,
                      dtDisTypeTmp, dtDisTypeTmpOld, null, s_DISNO, strItem, strPeriod, strUserId, strStAcceptDate, DBT);

                    if (bResult)
                    {
                        ALOModel.MaintainDisTmpRec BCO2 = new ALOModel.MaintainDisTmpRec(ConnectionDB);
                        BCO2.UpdateDisTmpRec(dtDisTmpRec, dtDisTmpRecOld, s_DISNO, DBT);
                    }


                    DBT.Commit();//交易成功

                }
                catch (Exception ex)
                {
                    DBT.Rollback();//交易失敗
                    throw ex;
                }

                QueryData();

                ResultMsgLabel.Text = "更新成功";
                #endregion
            }

            ScriptManager.RegisterStartupScript(this, this.GetType(), "SwitchControlDisplay", string.Format("SwitchControlDisplay('{0}','{1}',0);", Panel_AddNewItem.ClientID, hiddenDisplayMode.ClientID), true);

            SwitchSaveCancelButton(false);

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