protected void bt_Save_Click(object sender, EventArgs e)
 {
     ORD_GiftApplyAmountBLL applyamount = new ORD_GiftApplyAmountBLL((int)ViewState["ID"]);
     pn_Detail.GetData(applyamount.Model);
     applyamount.Model.BalanceAmount = decimal.Parse(lbl_BalanceAmount.Text);
     applyamount.Model["AdjustAmount"] = "0";
     applyamount.Model.UpdateStaff = (int)Session["UserID"];
     applyamount.Update();
     Session["SuccessFlag"] = true;
     ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", "<script language='javascript'>window.close();</script>", false);
 }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        ORD_GiftApplyAmountBLL applyamount = new ORD_GiftApplyAmountBLL((int)ViewState["ID"]);

        pn_Detail.GetData(applyamount.Model);
        applyamount.Model.BalanceAmount   = decimal.Parse(lbl_BalanceAmount.Text);
        applyamount.Model["AdjustAmount"] = "0";
        applyamount.Model.UpdateStaff     = (int)Session["UserID"];
        applyamount.Update();
        Session["SuccessFlag"] = true;
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message", "<script language='javascript'>window.close();</script>", false);
    }
        private void threadStartImport(BackgroundWorker worker, DoWorkEventArgs e)
        {
            bool bFind = false;

            StringBuilder _xhs_success = new StringBuilder("");//成功导入的记录序号(用于定期批量更新导入标志)
            string XH = "";
            int ClientID = 0, AccountMonth = 0, Brand = 0, Classify = 0,DIClassify=0;
            decimal PreBalance, DeductibleAmount;
            try
            {
                lb_Count.Text = dt_Amount.Rows.Count.ToString();
                for (int i = 0; i < dt_Amount.Rows.Count; i++)
                {
                    worker.ReportProgress((i + 1) * 100 / this.dt_Amount.Rows.Count, i);
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                    XH = dt_Amount.Rows[i]["序号"].ToString();
                    IList<CM_Client> _cmlist = CM_ClientBLL.GetModelList("Code='" + dt_Amount.Rows[i]["经销商代码"].ToString() + "' AND ActiveFlag=1 AND ApproveFlag=1");

                    if (_cmlist.Count == 1)
                    {

                        DIClassify = int.Parse(_cmlist[0]["DIClassify"]);
                        ClientID =DIClassify==3?_cmlist[0].Supplier: _cmlist[0].ID;//经销商为子户头时,取主户头
                    }
                    else
                    {
                        UpdateImportFlag(i, "导入失败,经销商代码不正确");
                        continue;
                    }

                    if (dt_Amount.Rows[i]["品牌"].ToString()!= "")
                    {
                        IList<PDT_Brand> _listbrand = PDT_BrandBLL.GetModelList("Name='" + dt_Amount.Rows[i]["品牌"].ToString()+"'");
                        if (_listbrand.Count == 0)
                        {
                            UpdateImportFlag(i, "导入失败,该品牌不存在");
                            continue;
                        }
                        else
                        {
                            Brand = _listbrand[0].ID;
                        }
                    }
                    if (dt_Amount.Rows[i]["会计月"].ToString() != "")
                    {
                        IList<AC_AccountMonth> _monthlist = AC_AccountMonthBLL.GetModelList("Name='" + dt_Amount.Rows[i]["会计月"].ToString() + "'");
                        if (_monthlist.Count == 0)
                        {
                            UpdateImportFlag(i, "导入失败,会计月填写错误");
                            continue;
                        }
                        else
                        {
                            AccountMonth = _monthlist[0].ID;
                        }
                    }
                    foreach (Dictionary_Data item in DictionaryBLL.Dictionary_Data_GetAlllList("Type IN (SELECT ID FROM MCS_SYS.dbo.Dictionary_Type WHERE TableName='ORD_GiftClassify')"))
                    {
                        if (item.Name == dt_Amount.Rows[i]["有导无导标志"].ToString().Trim())
                        {
                            Classify = int.Parse(item.Code);
                            bFind = true;
                            break;
                        }
                    }
                    if (!bFind)
                    {
                        UpdateImportFlag(i, "导入失败,有导无导标志错误!");
                        continue;
                    }
                    IList<ORD_GiftApplyAmount> _listamount = ORD_GiftApplyAmountBLL.GetModelList("AccountMonth=" + AccountMonth.ToString() + " AND Client=" + ClientID.ToString()
                                                                                                + " AND Brand=" + Brand.ToString() + " AND Classify=" + Classify.ToString());
                    if (_listamount.Count == 0)
                    {
                        UpdateImportFlag(i, "导入失败,没有找到符合该条件的赠品余额记录。");
                        continue;
                    }
                    if (ORD_GiftApplyAmountBLL.GetModelList("AccountMonth=" + (AccountMonth + 1).ToString() + " AND Client=" + ClientID.ToString()
                                                                                                + " AND Brand=" + Brand.ToString() + " AND Classify=" + Classify.ToString()).Count > 0)
                    {
                        UpdateImportFlag(i, "导入失败,不能更新历史记录。");
                        continue;
                    }
                    ORD_GiftApplyAmountBLL _bll = new ORD_GiftApplyAmountBLL(_listamount[0].ID);
                    #region 记录需更新费用

                    decimal.TryParse(dt_Amount.Rows[i]["赠品抵扣额"].ToString().Trim(), out DeductibleAmount);
                    if (decimal.TryParse(dt_Amount.Rows[i]["上月余额"].ToString().Trim(), out PreBalance))
                    {
                        if (DIClassify == 1)
                        {
                            _bll.Model.PreBalance = PreBalance;
                        }
                        else
                        {
                            _bll.Model.PreBalance += PreBalance;
                        }
                    }
                    _bll.Model.DeductibleAmount += DeductibleAmount;
                    _bll.Model.BalanceAmount = _bll.Model.AvailableAmount + _bll.Model.PreBalance - _bll.Model.AppliedAmount - _bll.Model.DeductibleAmount;
                    _bll.Update();
                    dt_Amount.Rows[i]["导入标志"] = "导入成功";
                    _xhs_success.Append(XH + ",");
                    #endregion

                }
                UpdateSuccess(_xhs_success.ToString());
            }
            catch (System.Exception err)
            {
                UpdateSuccess(_xhs_success.ToString());
                MessageBox.Show(err.Source + "~r~n" + err.StackTrace, err.Message);
            }

            return;
        }
        private void threadStartImport(BackgroundWorker worker, DoWorkEventArgs e)
        {
            bool bFind = false;

            StringBuilder _xhs_success = new StringBuilder("");//成功导入的记录序号(用于定期批量更新导入标志)
            string        XH = "";
            int           ClientID = 0, AccountMonth = 0, Brand = 0, Classify = 0, DIClassify = 0;
            decimal       PreBalance, DeductibleAmount;

            try
            {
                lb_Count.Text = dt_Amount.Rows.Count.ToString();
                for (int i = 0; i < dt_Amount.Rows.Count; i++)
                {
                    worker.ReportProgress((i + 1) * 100 / this.dt_Amount.Rows.Count, i);
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }
                    XH = dt_Amount.Rows[i]["序号"].ToString();
                    IList <CM_Client> _cmlist = CM_ClientBLL.GetModelList("Code='" + dt_Amount.Rows[i]["经销商代码"].ToString() + "' AND ActiveFlag=1 AND ApproveFlag=1");

                    if (_cmlist.Count == 1)
                    {
                        DIClassify = int.Parse(_cmlist[0]["DIClassify"]);
                        ClientID   = DIClassify == 3?_cmlist[0].Supplier: _cmlist[0].ID;//经销商为子户头时,取主户头
                    }
                    else
                    {
                        UpdateImportFlag(i, "导入失败,经销商代码不正确");
                        continue;
                    }



                    if (dt_Amount.Rows[i]["品牌"].ToString() != "")
                    {
                        IList <PDT_Brand> _listbrand = PDT_BrandBLL.GetModelList("Name='" + dt_Amount.Rows[i]["品牌"].ToString() + "'");
                        if (_listbrand.Count == 0)
                        {
                            UpdateImportFlag(i, "导入失败,该品牌不存在");
                            continue;
                        }
                        else
                        {
                            Brand = _listbrand[0].ID;
                        }
                    }
                    if (dt_Amount.Rows[i]["会计月"].ToString() != "")
                    {
                        IList <AC_AccountMonth> _monthlist = AC_AccountMonthBLL.GetModelList("Name='" + dt_Amount.Rows[i]["会计月"].ToString() + "'");
                        if (_monthlist.Count == 0)
                        {
                            UpdateImportFlag(i, "导入失败,会计月填写错误");
                            continue;
                        }
                        else
                        {
                            AccountMonth = _monthlist[0].ID;
                        }
                    }
                    foreach (Dictionary_Data item in DictionaryBLL.Dictionary_Data_GetAlllList("Type IN (SELECT ID FROM MCS_SYS.dbo.Dictionary_Type WHERE TableName='ORD_GiftClassify')"))
                    {
                        if (item.Name == dt_Amount.Rows[i]["有导无导标志"].ToString().Trim())
                        {
                            Classify = int.Parse(item.Code);
                            bFind    = true;
                            break;
                        }
                    }
                    if (!bFind)
                    {
                        UpdateImportFlag(i, "导入失败,有导无导标志错误!");
                        continue;
                    }
                    IList <ORD_GiftApplyAmount> _listamount = ORD_GiftApplyAmountBLL.GetModelList("AccountMonth=" + AccountMonth.ToString() + " AND Client=" + ClientID.ToString()
                                                                                                  + " AND Brand=" + Brand.ToString() + " AND Classify=" + Classify.ToString());
                    if (_listamount.Count == 0)
                    {
                        UpdateImportFlag(i, "导入失败,没有找到符合该条件的赠品余额记录。");
                        continue;
                    }
                    if (ORD_GiftApplyAmountBLL.GetModelList("AccountMonth=" + (AccountMonth + 1).ToString() + " AND Client=" + ClientID.ToString()
                                                            + " AND Brand=" + Brand.ToString() + " AND Classify=" + Classify.ToString()).Count > 0)
                    {
                        UpdateImportFlag(i, "导入失败,不能更新历史记录。");
                        continue;
                    }
                    ORD_GiftApplyAmountBLL _bll = new ORD_GiftApplyAmountBLL(_listamount[0].ID);
                    #region 记录需更新费用

                    decimal.TryParse(dt_Amount.Rows[i]["赠品抵扣额"].ToString().Trim(), out DeductibleAmount);
                    if (decimal.TryParse(dt_Amount.Rows[i]["上月余额"].ToString().Trim(), out PreBalance))
                    {
                        if (DIClassify == 1)
                        {
                            _bll.Model.PreBalance = PreBalance;
                        }
                        else
                        {
                            _bll.Model.PreBalance += PreBalance;
                        }
                    }
                    _bll.Model.DeductibleAmount += DeductibleAmount;
                    _bll.Model.BalanceAmount     = _bll.Model.AvailableAmount + _bll.Model.PreBalance - _bll.Model.AppliedAmount - _bll.Model.DeductibleAmount;
                    _bll.Update();
                    dt_Amount.Rows[i]["导入标志"] = "导入成功";
                    _xhs_success.Append(XH + ",");
                    #endregion
                }
                UpdateSuccess(_xhs_success.ToString());
            }
            catch (System.Exception err)
            {
                UpdateSuccess(_xhs_success.ToString());
                MessageBox.Show(err.Source + "~r~n" + err.StackTrace, err.Message);
            }

            return;
        }