protected void AddEmptyDetail()
    {
        SaveGrid();

        ListTable<FNA_FeeWriteOffDetail> _details = ViewState["Details"] as ListTable<FNA_FeeWriteOffDetail>;
        for (int i = 0; i < 5; i++)
        {
            ViewState["MaxID"] = (int)ViewState["MaxID"] + 1;

            FNA_FeeWriteOffDetail item;

            item = new FNA_FeeWriteOffDetail();
            item.AccountTitle = 1;
            item.ApplyCost = 0;
            item.WriteOffCost = 0;
            item.ID = (int)ViewState["MaxID"];

            #region 获取当前会计月的开始及截止日期
            int month = AC_AccountMonthBLL.GetMonthByDate(DateTime.Today.AddDays(-7));
            AC_AccountMonth m = new AC_AccountMonthBLL(month).Model;
            item.BeginMonth = m.ID;
            item.EndMonth = m.ID;
            item.BeginDate = m.BeginDate;
            item.EndDate = m.EndDate;
            #endregion

            item.Remark = "";
            _details.Add(item);             //新增科目
        }
        BindGrid();
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        FNA_FeeWriteOffBLL _bll = new FNA_FeeWriteOffBLL((int)ViewState["ID"]);
        FNA_FeeWriteOffDetail m = new FNA_FeeWriteOffDetail();

        if ((int)ViewState["DetailID"] > 0)
        {
            m = _bll.GetDetailModel((int)ViewState["DetailID"]);
            m.ApplyCost = decimal.Parse(tbx_Taxes.Text.Trim());
            m.WriteOffCost = m.ApplyCost;
            m.Remark = tbx_Remark.Text.Trim();
            _bll.UpdateDetail(m);

        }
        else
        {
            m.ID = 0;
            m.ApplyDetailID = 0;
            m.Client = _bll.Model.InsteadPayClient;
            m.BeginMonth = _bll.Model.AccountMonth;
            m.EndMonth = _bll.Model.AccountMonth;
            m.AccountTitle = 129;
            m.ApplyCost = decimal.Parse(tbx_Taxes.Text.Trim());
            m.WriteOffCost = m.ApplyCost;
            m.Remark = tbx_Remark.Text.Trim();
            _bll.AddDetail(m);
        }
        Session["SuccessFlag"] = true;
        MessageBox.ShowAndClose(this, "税金调整成功。");
    }
    protected void bt_AddToWriteOffList_Click(object sender, EventArgs e)
    {
        ListTable<FNA_FeeWriteOffDetail> _details = Session["FeeWriteOffDetails"] as ListTable<FNA_FeeWriteOffDetail>;
        int maxid = 0;
        if (_details != null)
            if (_details.GetListItem().Count > 0) maxid = _details.GetListItem().Max(p => p.ID);
        maxid++;

        foreach (GridViewRow row in gv_FeeAplyList.Rows)
        {
            CheckBox cb_Selected = (CheckBox)row.FindControl("cb_Selected");
            if (cb_Selected.Checked)
            {
                int applyid = (int)gv_FeeAplyList.DataKeys[row.RowIndex][0];
                int applydetialid = (int)gv_FeeAplyList.DataKeys[row.RowIndex][1];

                FNA_FeeApplyBLL applyBLL = new FNA_FeeApplyBLL(applyid);
                FNA_FeeApply apply = applyBLL.Model;
                FNA_FeeApplyDetail applydetail = applyBLL.GetDetailModel(applydetialid);

                #region 陈列、返利费用判断协议是否关联合同
                IList<CM_Contract> contractList;
                contractList = CM_ContractBLL.GetModelList(@"ContractCode!='' AND ContractCode=MCS_SYS.dbo.UF_Spilt('" + applydetail.Remark + "',':',2)");
                if (applydetail.RelateContractDetail != 0)
                {
                    int ID = 0;
                    CM_ContractDetail detail = new CM_ContractBLL().GetDetailModel(applydetail.RelateContractDetail);
                    if (detail != null)
                        ID = detail.ContractID;
                    contractList = CM_ContractBLL.GetModelList("ID=" + ID.ToString());
                }

                if (contractList.Count > 0 && contractList[0].Classify < 3 && ATMT_AttachmentBLL.GetModelList("RelateType=35 AND RelateID=" + contractList[0].ID.ToString()).Count == 0)
                {
                    MessageBox.Show(this, "陈列、返利费用操作费用核销申请时,门店协议必须上传附件,请上传后再核销!");
                    return;
                }
                #endregion

                FNA_FeeWriteOffDetail m = new FNA_FeeWriteOffDetail();
                m.ID = maxid++;
                m.ApplyDetailID = applydetialid;
                m.Client = applydetail.Client;
                m.AccountTitle = applydetail.AccountTitle;
                m.ProductBrand = apply.ProductBrand;
                m.ApplyCost = applydetail.AvailCost;
                m.BeginMonth = applydetail.BeginMonth;
                m.EndMonth = applydetail.EndMonth;
                m.BeginDate = applydetail.BeginDate;
                m.EndDate = applydetail.EndDate;
                m.WriteOffCost = applydetail.AvailCost;
                m.Remark = applydetail.Remark;
                if (applydetail["BankVoucherNo"] != "") m.Remark += ",凭证:" + applydetail["BankVoucherNo"];

                if (applydetail.Remark.IndexOf("是否CA") > 0)
                    m.Remark = applydetail.Remark.Substring(applydetail.Remark.IndexOf("是否CA") - 1);
                if (_details == null)
                    _details = new ListTable<FNA_FeeWriteOffDetail>(new List<FNA_FeeWriteOffDetail>(), "ID");
                _details.Add(m);
            }
        }

        BindGrid();
        gv_FeeAplyList.PageIndex = 0;
        BindFeeApplyNoWriteOff();
    }
    private void UpdateDetail(FNA_FeeWriteOffBLL bll, int AccountTitle, Decimal Cost, int flag, DateTime minbegindate, DateTime maxenddate)
    {
        if (Cost == 0) return;
        FNA_FeeWriteOffDetail d = bll.Items.FirstOrDefault(p => p.AccountTitle == AccountTitle);
        if (d != null)
        {
            if (flag == 1)
            {
                d.WriteOffCost += Cost;
                if (d.BeginDate > minbegindate) d.BeginDate = minbegindate;
                if (d.EndDate < maxenddate) d.EndDate = maxenddate;
            }
            else
            {
                d.WriteOffCost -= Cost;
            }

            if (d.WriteOffCost == 0)
                bll.DeleteDetail(d.ID);
            else
                bll.UpdateDetail(d);
        }
        else
        {
            if (flag == 1)
            {
                d = new FNA_FeeWriteOffDetail();
                d.AccountTitle = AccountTitle;
                d.ApplyCost = 0;
                d.WriteOffCost = Cost;
                d.BeginDate = minbegindate;
                d.EndDate = maxenddate;
                d.BeginMonth = bll.Model.AccountMonth;
                d.EndMonth = bll.Model.AccountMonth;
                bll.Items.Add(d);
                bll.AddDetail(d);
            }
        }
    }
    protected void bt_AddMobileFee_Click(object sender, EventArgs e)
    {
        int staffid = 0;
        int.TryParse(select_MobileStaff.SelectValue, out staffid);
        if (staffid == 0) return;

        Org_Staff staff = new Org_StaffBLL(staffid).Model;
        if (staff == null) return;

        if (string.IsNullOrEmpty(staff["ManageInfo11"])) staff["ManageInfo11"] = "0";
        decimal applycost = 0;
        decimal.TryParse(staff["ManageInfo11"], out applycost);
        if (applycost <= 0) return;

        decimal writeoffcost = decimal.Parse(tbx_MobileCost.Text);

        if (writeoffcost > applycost)
        {
            MessageBox.Show(this, "对不起,实际报销金额不能超过申请限额" + lb_MobileApplyCost.Text + "!");
            tbx_MobileCost.Text = lb_MobileApplyCost.Text;
            return;
        }

        SaveGrid();
        ViewState["MaxID"] = (int)ViewState["MaxID"] + 1;

        #region 组织手机费核销明细
        FNA_FeeWriteOffDetail item = new FNA_FeeWriteOffDetail();
        item.ID = (int)ViewState["MaxID"];
        item.AccountTitle = ConfigHelper.GetConfigInt("MobileCostACTitle");
        item.ApplyCost = applycost;
        item.WriteOffCost = writeoffcost;

        item["MobileFeeRelateStaff"] = staff.ID.ToString();

        #region 获取当前会计月的开始及截止日期
        int month = int.Parse(ddl_MobileCostMonth.SelectedValue);
        AC_AccountMonth m = new AC_AccountMonthBLL(month).Model;
        item.BeginMonth = m.ID;
        item.EndMonth = m.ID;
        item.BeginDate = m.BeginDate;
        item.EndDate = m.EndDate;
        #endregion

        item.Remark = "员工:" + staff.RealName + ";";
        item.Remark += "手机号码:" + staff["Mobile"] + ";月份:" + ddl_MobileCostMonth.SelectedItem.Text +
            ";限额:" + lb_MobileApplyCost.Text + ";";

        if (tbx_MobileRemark.Text != "")
            item.Remark += "说明:" + tbx_MobileRemark.Text;
        #endregion
        if (FNA_FeeWriteOffBLL.CheckMobileFeeHasWriteOff(item.AccountTitle, item.BeginMonth, staffid))
        {
            MessageBox.Show(this, "该员工指定月的手机费,已在其他报销单中报销,请勿重复报销!");
        }
        else
        {
            ListTable<FNA_FeeWriteOffDetail> _details = ViewState["Details"] as ListTable<FNA_FeeWriteOffDetail>;
            if (_details.GetListItem().FirstOrDefault(p =>
                p.AccountTitle == item.AccountTitle &&
                p.BeginMonth == item.BeginMonth &&
                p["MobileFeeRelateStaff"] == item["MobileFeeRelateStaff"]) == null)
            {
                _details.Add(item);             //新增科目
            }
            else
            {
                MessageBox.Show(this, "该员工指定月的手机费,已在本报销单中,请勿重复报销!");
            }
        }
        BindGrid();
        AddEmptyDetail();
    }
    private bool SaveGrid()
    {
        ListTable<FNA_FeeWriteOffDetail> _details = ViewState["Details"] as ListTable<FNA_FeeWriteOffDetail>;
        foreach (GridViewRow gr in gv_List.Rows)
        {
            int id = (int)gv_List.DataKeys[gr.RowIndex]["ID"];
            FNA_FeeWriteOffDetail item = _details.GetListItem().FirstOrDefault(p => p.ID == id);
            if (item == null)
            {
                item = new FNA_FeeWriteOffDetail();
                item.ID = id;
            }

            item.AccountTitle = int.Parse(((DropDownList)gr.FindControl("ddl_AccountTitle")).SelectedValue);

            //忽略手工直填的电话费及手机费,必须通过专用面板加入报销信息
            if (item.AccountTitle == ConfigHelper.GetConfigInt("TeleCostACTitle") ||
                item.AccountTitle == ConfigHelper.GetConfigInt("MobileCostACTitle"))
            {
                if (((TextBox)gr.FindControl("tbx_WriteOffCost")).Enabled) _details.Remove(item);
                continue;
            }

            if (((TextBox)gr.FindControl("tbx_WriteOffCost")).Text.Trim() != "")
                item.WriteOffCost = decimal.Parse(((TextBox)gr.FindControl("tbx_WriteOffCost")).Text.Trim());

            if (item.AccountTitle > 1 && item.WriteOffCost != 0)
            {
                #region 获取选择的会计月
                if (gr.FindControl("ddl_BeginMonth") != null &&
                    item.BeginMonth.ToString() != ((DropDownList)gr.FindControl("ddl_BeginMonth")).SelectedValue)
                {
                    item.BeginMonth = int.Parse(((DropDownList)gr.FindControl("ddl_BeginMonth")).SelectedValue);
                    item.EndMonth = item.BeginMonth;

                    AC_AccountMonth month = new AC_AccountMonthBLL(item.BeginMonth).Model;
                    item.BeginDate = month.BeginDate;
                    item.EndDate = month.EndDate;
                }
                #endregion

                item.Remark = ((TextBox)gr.FindControl("tbx_Remark")).Text;

                if (item.Remark == "")
                {
                    MessageBox.Show(this, "备注必填");
                    return false;
                }
                _details.Update(item);
            }
            else
            {
                _details.Remove(item);
            }
        }

        return true;
    }
    protected void bt_AddTeleFee_Click(object sender, EventArgs e)
    {
        if (ddl_Tele.SelectedValue == "0") return;

        int teleid = int.Parse(ddl_Tele.SelectedValue);
        CM_PropertyInTelephone tele = new CM_PropertyInTelephoneBLL(teleid).Model;

        decimal writeoffcost = decimal.Parse(tbx_TeleCost.Text);
        decimal applycost = tele.TeleCost + tele.NetCost;

        if (writeoffcost > applycost)
        {
            MessageBox.Show(this, "对不起,实际报销金额不能超过申请限额" + lb_TeleApplyCost.Text + "!");
            tbx_TeleCost.Text = lb_TeleApplyCost.Text;
            return;
        }

        SaveGrid();
        ViewState["MaxID"] = (int)ViewState["MaxID"] + 1;

        #region 组织电话费核销明细
        FNA_FeeWriteOffDetail item = new FNA_FeeWriteOffDetail();
        item.ID = (int)ViewState["MaxID"];
        item.AccountTitle = ConfigHelper.GetConfigInt("TeleCostACTitle");
        item.ApplyCost = applycost;
        item.WriteOffCost = writeoffcost;

        item["TeleFeeRelateTelephone"] = tele.ID.ToString();
        item.Client = tele.Client;

        #region 获取当前会计月的开始及截止日期
        int month = int.Parse(ddl_TeleCostMonth.SelectedValue);
        AC_AccountMonth m = new AC_AccountMonthBLL(month).Model;
        item.BeginMonth = m.ID;
        item.EndMonth = m.ID;
        item.BeginDate = m.BeginDate;
        item.EndDate = m.EndDate;
        #endregion

        if (tele.Client > 0)
        {
            CM_Client client = new CM_ClientBLL(tele.Client).Model;
            if (client != null) item.Remark = "物业:" + client.FullName + ";";
        }
        item.Remark += "电话号码:" + ddl_Tele.SelectedItem.Text + ";月份:" + ddl_TeleCostMonth.SelectedItem.Text +
            ";限额:" + lb_TeleApplyInfo.Text + ";";

        if (tbx_TeleRemark.Text != "")
            item.Remark += "说明:" + tbx_TeleRemark.Text;
        #endregion

        if (FNA_FeeWriteOffBLL.CheckTeleFeeHasWriteOff(item.AccountTitle, item.BeginMonth, tele.ID))
        {
            MessageBox.Show(this, "该电话指定月的费用,已在其他报销单中报销,请勿重复报销!");
        }
        else
        {
            ListTable<FNA_FeeWriteOffDetail> _details = ViewState["Details"] as ListTable<FNA_FeeWriteOffDetail>;
            if (_details.GetListItem().FirstOrDefault(p =>
                p.AccountTitle == item.AccountTitle &&
                p.BeginMonth == item.BeginMonth &&
                p["TeleFeeRelateTelephone"] == item["TeleFeeRelateTelephone"]) == null)
            {
                _details.Add(item);             //新增科目
            }
            else
            {
                MessageBox.Show(this, "该电话指定月的费用,已在本报销单中,请勿重复报销!");
            }
        }
        BindGrid();
        AddEmptyDetail();
    }
    private void AddWriteOffDetail(FNA_FeeWriteOffBLL bll, int AccountTitle, Decimal Cost, DateTime minbegindate, DateTime maxenddate)
    {
        if (Cost == 0) return;

        FNA_FeeWriteOffDetail d = new FNA_FeeWriteOffDetail();
        d.AccountTitle = AccountTitle;
        d.ApplyCost = 0;
        d.WriteOffCost = Cost;
        d.BeginDate = minbegindate;
        d.EndDate = maxenddate;
        d.BeginMonth = bll.Model.AccountMonth;
        d.EndMonth = bll.Model.AccountMonth;
        bll.Items.Add(d);
    }