protected void bt_GoBack_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            Car_DispatchRide m = new Car_DispatchRide();
            pl_detail.GetData(m);

            if (m.KilometresEnd == 0)
            {
                MessageBox.Show(this, "请输入还车时公里数!");
                return;
            }

            if (m.KilometresEnd < m.KilometresStart)
            {
                MessageBox.Show(this, "还车时公里数不能小于发车时公里数!");
                return;
            }

            Car_DispatchRideBLL _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
            _bll.GoBack(m.KilometresEnd, (int)Session["UserID"], m.RoadToll, m.FuelFee, m.ParkingFee, m.OtherFee);

            MessageBox.ShowAndRedirect(this, "确认还车成功!", "Car_DispatchRideList.aspx?State=3");
        }
    }
    protected void bt_GoBack_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            Car_DispatchRide m = new Car_DispatchRide();
            pl_detail.GetData(m);

            if (m.KilometresEnd == 0)
            {
                MessageBox.Show(this, "请输入还车时公里数!");
                return;
            }

            if (m.KilometresEnd < m.KilometresStart)
            {
                MessageBox.Show(this, "还车时公里数不能小于发车时公里数!");
                return;
            }

            Car_DispatchRideBLL _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
            _bll.GoBack(m.KilometresEnd, (int)Session["UserID"], m.RoadToll, m.FuelFee, m.ParkingFee, m.OtherFee);

            MessageBox.ShowAndRedirect(this, "确认还车成功!", "Car_DispatchRideList.aspx?State=3");
        }
    }
    protected void bt_GoOut_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            Car_DispatchRide m = new Car_DispatchRide();
            pl_detail.GetData(m);

            if (m.CarID == 0)
            {
                MessageBox.Show(this, "请选择车辆!");
                return;
            }

            if (m.DriverStaff == 0)
            {
                MessageBox.Show(this, "请选择驾驶员!");
                return;
            }

            Car_DispatchRideBLL _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
            _bll.GoOut(m.CarID, m.DriverStaff, m.KilometresStart, (int)Session["UserID"]);

            MessageBox.ShowAndRedirect(this, "确认出车成功!", "Car_DispatchRideList.aspx?State=2");
        }
    }
    protected void bt_ApproveFlag_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            Car_DispatchRideBLL _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
            _bll.Model.ApproveFlag = 1;
            _bll.Update();

            MessageBox.ShowAndRedirect(this, "审核成功!", "Car_DispatchRideList.aspx?State=1");
        }
    }
    protected void bt_ApproveFlag_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            Car_DispatchRideBLL _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
            _bll.Model.ApproveFlag = 1;
            _bll.Update();

            MessageBox.ShowAndRedirect(this, "审核成功!", "Car_DispatchRideList.aspx?State=1");
        }
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        Car_DispatchRideBLL _bll;

        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
        }
        else
        {
            //新增
            _bll = new Car_DispatchRideBLL();
        }

        pl_detail.GetData(_bll.Model);

        #region 判断必填项

        #endregion
        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll.Model.UpdateStaff = (int)Session["UserID"];
            if (_bll.Update() == 0)
            {
                MessageBox.ShowAndRedirect(this, "修改成功!", "Car_DispatchRideList.aspx");
            }
        }
        else
        {
            //新增
            _bll.Model.InsertStaff = (int)Session["UserID"];
            _bll.Model.State       = 1;
            _bll.Model.ApproveFlag = 2;
            _bll.Model.ApplyStaff  = (int)Session["UserID"];

            ViewState["ID"] = _bll.Add();
            if ((int)ViewState["ID"] > 0)
            {
                if (_bll.Model.DispatchingCode == "")
                {
                    _bll.Model.DispatchingCode = ViewState["ID"].ToString();
                    _bll.Update();
                }
                MessageBox.ShowAndRedirect(this, "新增成功!", "Car_DispatchRideList.aspx?State=1");
            }
        }
    }
    private void BindData()
    {
        Car_DispatchRide m = new Car_DispatchRideBLL((int)ViewState["ID"]).Model;

        if (m != null)
        {
            pl_detail.BindData(m);
        }


        switch (m.State)
        {
        case 1:         //备车中
            if (m.ApproveFlag == 1)
            {
                pl_detail.SetPanelEnable("Panel_OA_Car_DispatchRide_Detail_01", false);
                bt_Save.Visible        = false;
                bt_ApproveFlag.Visible = false;
            }
            else
            {
                pl_detail.SetPanelVisible("Panel_OA_Car_DispatchRide_Detail_02", false);
                bt_GoOut.Visible = false;
            }

            bt_GoBack.Visible = false;
            pl_detail.SetPanelVisible("Panel_OA_Car_DispatchRide_Detail_03", false);
            break;

        case 2:         //已发车
            pl_detail.SetPanelEnable("Panel_OA_Car_DispatchRide_Detail_01", false);
            bt_Save.Visible        = false;
            bt_ApproveFlag.Visible = false;
            bt_GoOut.Visible       = false;
            break;

        case 3:         //已还车
        case 4:         //已取消
            pl_detail.SetControlsEnable(false);
            bt_Save.Visible        = false;
            bt_ApproveFlag.Visible = false;
            bt_GoBack.Visible      = false;
            bt_GoOut.Visible       = false;
            bt_Cancel.Visible      = false;
            break;
        }
    }
    private void BindData()
    {
        int id = (int)ViewState["ID"];

        FNA_EvectionRoute m = new FNA_EvectionRouteBLL(id).Model;

        if (m != null)
        {
            pn_detail.BindData(m);
            ddl_Transport_SelectedIndexChanged(null, null);

            if (m["RelateJournal"] == "")
            {
                bt_ToJournal.Visible = false;
            }
            else
            {
                ViewState["RelateJournal"] = m["RelateJournal"];
            }

            if (m.Transport == "4" && m["RelateCarDispatch"] != "")
            {
                int dispatch = 0;
                if (int.TryParse(m["RelateCarDispatch"], out dispatch) && dispatch > 0)
                {
                    Car_DispatchRide d = new Car_DispatchRideBLL(dispatch).Model;
                    if (d != null)
                    {
                        pn_detail.BindData(d);
                        ddl_CarID_SelectedIndexChanged(null, null);
                    }
                }
            }

            if (m.WriteOffID > 0 || m.InsertStaff != (int)Session["UserID"] || m.ApproveFlag == 1)
            {
                pn_detail.Enabled = false;
                bt_Delete.Visible = false;
                bt_Save.Visible   = false;
            }
        }
    }
    protected void gv_List_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (MCSTabControl1.SelectedIndex == 1 && e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hy_DispatchRide = (HyperLink)e.Row.FindControl("hy_DispatchRide");
            if (hy_DispatchRide != null)
            {
                int carid = (int)gv_List.DataKeys[e.Row.RowIndex][0];
                IList <Car_DispatchRide> lists = Car_DispatchRideBLL.GetModelList("Car_DispatchRide.State = 2 AND Car_DispatchRide.CarID=" + carid.ToString());

                if (lists.Count > 0)
                {
                    Car_DispatchRide m = lists[lists.Count - 1];

                    hy_DispatchRide.Text        = m.Destination;
                    hy_DispatchRide.NavigateUrl = "Car_DispatchRideDetail.aspx?ID=" + m.ID.ToString();
                }
            }
        }
    }
    private void BindData()
    {
        Car_DispatchRide m = new Car_DispatchRideBLL((int)ViewState["ID"]).Model;
        if (m != null) pl_detail.BindData(m);

        switch (m.State)
        {
            case 1:     //备车中
                if (m.ApproveFlag == 1)
                {
                    pl_detail.SetPanelEnable("Panel_OA_Car_DispatchRide_Detail_01", false);
                    bt_Save.Visible = false;
                    bt_ApproveFlag.Visible = false;
                }
                else
                {
                    pl_detail.SetPanelVisible("Panel_OA_Car_DispatchRide_Detail_02", false);
                    bt_GoOut.Visible = false;
                }

                bt_GoBack.Visible = false;
                pl_detail.SetPanelVisible("Panel_OA_Car_DispatchRide_Detail_03", false);
                break;
            case 2:     //已发车
                pl_detail.SetPanelEnable("Panel_OA_Car_DispatchRide_Detail_01", false);
                bt_Save.Visible = false;
                bt_ApproveFlag.Visible = false;
                bt_GoOut.Visible = false;
                break;
            case 3:     //已还车
            case 4:     //已取消
                pl_detail.SetControlsEnable(false);
                bt_Save.Visible = false;
                bt_ApproveFlag.Visible = false;
                bt_GoBack.Visible = false;
                bt_GoOut.Visible = false;
                bt_Cancel.Visible = false;
                break;
        }
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        Car_DispatchRideBLL _bll;
        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
        }
        else
        {
            //新增
            _bll = new Car_DispatchRideBLL();
        }

        pl_detail.GetData(_bll.Model);

        #region 判断必填项

        #endregion
        if ((int)ViewState["ID"] != 0)
        {
            //修改
            _bll.Model.UpdateStaff = (int)Session["UserID"];
            if (_bll.Update() == 0)
            {
                MessageBox.ShowAndRedirect(this, "修改成功!", "Car_DispatchRideList.aspx");
            }
        }
        else
        {
            //新增
            _bll.Model.InsertStaff = (int)Session["UserID"];
            _bll.Model.State = 1;
            _bll.Model.ApproveFlag = 2;
            _bll.Model.ApplyStaff = (int)Session["UserID"];

            ViewState["ID"] = _bll.Add();
            if ((int)ViewState["ID"] > 0)
            {
                if (_bll.Model.DispatchingCode == "")
                {
                    _bll.Model.DispatchingCode = ViewState["ID"].ToString();
                    _bll.Update();
                }
                MessageBox.ShowAndRedirect(this, "新增成功!", "Car_DispatchRideList.aspx?State=1");
            }
        }
    }
    private void ReComputeCost(int flag)
    {
        decimal Cost1 = 0, Cost2 = 0, Cost3 = 0, Cost4 = 0, Cost5 = 0;
        decimal Cost11 = 0, Cost12 = 0, Cost13 = 0;     //车辆费用
        decimal Cost21 = 0, Cost22 = 0;                 //培训差旅费
        DateTime minbegindate = DateTime.Parse("2999-1-1"), maxenddate = DateTime.Parse("1900-1-1");

        #region 求选中行的差旅费、住宿费、补贴合计
        List<int> selectedevectionids = new List<int>();

        foreach (GridViewRow row in gv_List.Rows)
        {
            CheckBox cbx = (CheckBox)row.FindControl("cbx");
            if (cbx != null && cbx.Checked)
            {
                int id = (int)gv_List.DataKeys[row.RowIndex][0];
                FNA_EvectionRoute evection = new FNA_EvectionRouteBLL(id).Model;

                #region 根据关联的工作日志的类型,判断是否培训费
                //获取关联的日志类型
                int journalid = 0, workingclassify = 0;
                if (int.TryParse(evection["RelateJournal"], out journalid) && journalid > 0)
                {
                    JN_Journal Joural = new JN_JournalBLL(journalid).Model;
                    if (Joural != null) workingclassify = Joural.WorkingClassify;
                }

                switch (workingclassify)
                {
                    case 4:     //总部组织的培训,计入总部培训-差旅费
                        Cost21 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                        break;
                    case 5:     //省区组织的培训,计入营业部培训-差旅费
                        Cost22 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                        break;
                    default:
                        Cost1 += evection.Cost1;      //交通费
                        Cost2 += evection.Cost2;      //住宿费
                        Cost3 += evection.Cost3;      //补贴
                        Cost4 += evection.Cost4;      //市内交通费
                        Cost5 += evection.Cost5;      //的士费
                        break;
                }
                #endregion

                if (minbegindate > evection.BeginDate) minbegindate = evection.BeginDate;
                if (maxenddate < evection.EndDate) maxenddate = evection.EndDate;

                int cardispatchid = 0;
                if (int.TryParse(gv_List.DataKeys[row.RowIndex]["Car_DispatchRide_ID"].ToString(), out cardispatchid)
                    && cardispatchid > 0)
                {
                    Car_DispatchRide dispatch = new Car_DispatchRideBLL(cardispatchid).Model;
                    if (dispatch != null)
                    {
                        Cost11 += dispatch.RoadToll;    //过路过桥费
                        Cost12 += dispatch.FuelFee;     //油费
                        Cost13 += dispatch.ParkingFee;  //停车费
                        Cost13 += dispatch.OtherFee;    //其他费
                    }
                }
                selectedevectionids.Add(id);
            }
        }
        if (Cost1 + Cost2 + Cost3 + Cost4 + Cost5 + Cost11 + Cost12 + Cost13 + Cost21 + Cost22 == 0)
        {
            MessageBox.Show(this, "对不起,请勾选要报销的差旅行程记录!");
            return;
        }
        #endregion

        #region 重设科目金额
        FNA_FeeWriteOffBLL bll = new FNA_FeeWriteOffBLL((int)ViewState["WriteOffID"]);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost1ACTitle"), Cost1, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost2ACTitle"), Cost2, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost3ACTitle"), Cost3, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost4ACTitle"), Cost4, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost5ACTitle"), Cost5, flag, minbegindate, maxenddate);

        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost11ACTitle"), Cost11, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost12ACTitle"), Cost12, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost13ACTitle"), Cost13, flag, minbegindate, maxenddate);

        UpdateDetail(bll, ConfigHelper.GetConfigInt("TrainingCost1ACTitle"), Cost21, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("TrainingCost2ACTitle"), Cost22, flag, minbegindate, maxenddate);
        #endregion

        #region 置差旅行程的关联报销单ID
        foreach (int evectionid in selectedevectionids)
        {
            FNA_EvectionRouteBLL evectionbll = new FNA_EvectionRouteBLL(evectionid);
            if (flag == 1)
                evectionbll.Model.WriteOffID = (int)ViewState["WriteOffID"];
            else
                evectionbll.Model.WriteOffID = 0;
            evectionbll.Update();
        }
        #endregion
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        FNA_EvectionRouteBLL bll;
        Car_DispatchRideBLL car_ridebll = null;

        if ((int)ViewState["ID"] == 0)
            bll = new FNA_EvectionRouteBLL();
        else
            bll = new FNA_EvectionRouteBLL((int)ViewState["ID"]);

        pn_detail.GetData(bll.Model);

        #region 保存车辆使用信息
        if (bll.Model.Transport == "4")
        {
            //自驾车时,关联派车单
            if (string.IsNullOrEmpty(bll.Model["RelateCarDispatch"]))
            {
                car_ridebll = new Car_DispatchRideBLL();
                pn_detail.GetData(car_ridebll.Model);
                if (car_ridebll.Model.KilometresEnd < car_ridebll.Model.KilometresStart)
                {
                    MessageBox.Show(this, "对不起,还车公里数不能小于发车公里数!");
                    return;
                }

                car_ridebll.Model.Destination = bll.Model.EvectionLine;
                car_ridebll.Model.Matters = "差旅行程";
                car_ridebll.Model.State = 3; //已交车
                car_ridebll.Model.OrganizeCity = new Org_StaffBLL((int)Session["UserID"]).Model.OrganizeCity;
                car_ridebll.Model.ActGoBackTime = DateTime.Now;
                car_ridebll.Model.ApproveFlag = 2;
                car_ridebll.Model.ApplyStaff = bll.Model.RelateStaff;
                car_ridebll.Model.InsertStaff = (int)Session["UserID"];

                bll.Model["RelateCarDispatch"] = car_ridebll.Add().ToString();
            }
            else
            {
                car_ridebll = new Car_DispatchRideBLL(int.Parse(bll.Model["RelateCarDispatch"]));
                pn_detail.GetData(car_ridebll.Model);
                if (car_ridebll.Model.KilometresEnd < car_ridebll.Model.KilometresStart)
                {
                    MessageBox.Show(this, "对不起,还车公里数不能小于发车公里数!");
                    return;
                }

                car_ridebll.Model.Destination = bll.Model.EvectionLine;
                car_ridebll.Model.UpdateStaff = (int)Session["UserID"];
                car_ridebll.Update();
            }

            //更新车辆最后的公里数
            Car_CarListBLL CarBll = new Car_CarListBLL(car_ridebll.Model.CarID);
            if (CarBll.Model != null)
            {
                if (Car_DispatchRideBLL.GetModelList("CarID=" + car_ridebll.Model.CarID.ToString() +
                    " AND ID > " + car_ridebll.Model.ID.ToString()).Count == 0)
                {
                    //如果当前用车信息是该车辆最后一次用车记录,则更新车辆最后公里数
                    CarBll.Model.Kilometres = car_ridebll.Model.KilometresEnd;
                    CarBll.Update();
                }
            }
        }
        #endregion

        if ((int)ViewState["ID"] == 0)
        {
            bll.Model.ApproveFlag = 2;
            bll.Model.InsertStaff = (int)Session["UserID"];
            ViewState["ID"] = bll.Add();
        }
        else
        {
            bll.Model.UpdateStaff = (int)Session["UserID"];
            bll.Update();
        }

        if (car_ridebll != null && car_ridebll.Model["RelateEvectionRoute"] == "")
        {
            car_ridebll.Model["RelateEvectionRoute"] = ViewState["ID"].ToString();
            car_ridebll.Update();
        }

        if (sender != null)
            MessageBox.ShowAndClose(this, "保存成功!");
    }
    private void BindData()
    {
        int id = (int)ViewState["ID"];

        FNA_EvectionRoute m = new FNA_EvectionRouteBLL(id).Model;
        if (m != null)
        {
            pn_detail.BindData(m);
            ddl_Transport_SelectedIndexChanged(null, null);

            if (m["RelateJournal"] == "")
                bt_ToJournal.Visible = false;
            else
                ViewState["RelateJournal"] = m["RelateJournal"];

            if (m.Transport == "4" && m["RelateCarDispatch"] != "")
            {
                int dispatch = 0;
                if (int.TryParse(m["RelateCarDispatch"], out dispatch) && dispatch > 0)
                {
                    Car_DispatchRide d = new Car_DispatchRideBLL(dispatch).Model;
                    if (d != null)
                    {
                        pn_detail.BindData(d);
                        ddl_CarID_SelectedIndexChanged(null, null);
                    }
                }
            }

            if (m.WriteOffID > 0 || m.InsertStaff != (int)Session["UserID"] || m.ApproveFlag == 1)
            {
                pn_detail.Enabled = false;
                bt_Delete.Visible = false;
                bt_Save.Visible = false;
            }
        }
    }
    protected void bt_GoOut_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["ID"] != 0)
        {
            Car_DispatchRide m = new Car_DispatchRide();
            pl_detail.GetData(m);

            if (m.CarID == 0)
            {
                MessageBox.Show(this, "请选择车辆!");
                return;
            }

            if (m.DriverStaff == 0)
            {
                MessageBox.Show(this, "请选择驾驶员!");
                return;
            }

            Car_DispatchRideBLL _bll = new Car_DispatchRideBLL((int)ViewState["ID"]);
            _bll.GoOut(m.CarID, m.DriverStaff, m.KilometresStart, (int)Session["UserID"]);

            MessageBox.ShowAndRedirect(this, "确认出车成功!", "Car_DispatchRideList.aspx?State=2");
        }
    }
    protected void bt_WriteOff_Click(object sender, EventArgs e)
    {
        #region 求选中行的差旅费、住宿费、补贴合计
        decimal Cost1 = 0, Cost2 = 0, Cost3 = 0, Cost4 = 0, Cost5 = 0;
        decimal Cost11 = 0, Cost12 = 0, Cost13 = 0;     //车辆费用
        decimal Cost21 = 0, Cost22 = 0;

        DateTime   minbegindate = DateTime.Parse("2999-1-1"), maxenddate = DateTime.Parse("1900-1-1");
        List <int> selectedevectionids = new List <int>();

        foreach (GridViewRow row in gv_List.Rows)
        {
            CheckBox cbx = (CheckBox)row.FindControl("cbx");
            if (cbx != null && cbx.Checked)
            {
                int id = (int)gv_List.DataKeys[row.RowIndex][0];
                FNA_EvectionRoute evection = new FNA_EvectionRouteBLL(id).Model;

                #region 根据关联的工作日志的类型,判断是否培训费
                //获取关联的日志类型
                int journalid = 0, workingclassify = 0;
                if (int.TryParse(evection["RelateJournal"], out journalid) && journalid > 0)
                {
                    JN_Journal Joural = new JN_JournalBLL(journalid).Model;
                    if (Joural != null)
                    {
                        workingclassify = Joural.WorkingClassify;
                    }
                }

                switch (workingclassify)
                {
                case 4:         //总部组织的培训,计入总部培训-差旅费
                    Cost21 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                    break;

                case 5:         //省区组织的培训,计入营业部培训-差旅费
                    Cost22 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                    break;

                default:
                    Cost1 += evection.Cost1;          //交通费
                    Cost2 += evection.Cost2;          //住宿费
                    Cost3 += evection.Cost3;          //补贴
                    Cost4 += evection.Cost4;          //市内交通费
                    Cost5 += evection.Cost5;          //的士费
                    break;
                }
                #endregion

                if (minbegindate > evection.BeginDate)
                {
                    minbegindate = evection.BeginDate;
                }
                if (maxenddate < evection.EndDate)
                {
                    maxenddate = evection.EndDate;
                }

                int cardispatchid = 0;
                if (int.TryParse(gv_List.DataKeys[row.RowIndex]["Car_DispatchRide_ID"].ToString(), out cardispatchid) &&
                    cardispatchid > 0)
                {
                    Car_DispatchRide dispatch = new Car_DispatchRideBLL(cardispatchid).Model;
                    if (dispatch != null)
                    {
                        Cost11 += dispatch.RoadToll;    //过路过桥费
                        Cost12 += dispatch.FuelFee;     //油费
                        Cost13 += dispatch.ParkingFee;  //停车费
                        Cost13 += dispatch.OtherFee;    //其他费
                    }
                }
                selectedevectionids.Add(id);
            }
        }
        if (Cost1 + Cost2 + Cost3 + Cost4 + Cost5 + Cost11 + Cost12 + Cost13 + Cost21 + Cost22 == 0)
        {
            MessageBox.Show(this, "对不起,请勾选要报销的差旅行程记录!");
            return;
        }
        #endregion

        #region 新增费用核销单头
        Org_Staff staff = new Org_StaffBLL((int)Session["UserID"]).Model;

        FNA_FeeWriteOffBLL bll = new FNA_FeeWriteOffBLL();
        bll.Model.SheetCode             = FNA_FeeWriteOffBLL.GenerateSheetCode(staff.OrganizeCity); //自动产生报销单号
        bll.Model.AccountMonth          = AC_AccountMonthBLL.GetCurrentMonth();
        bll.Model.FeeType               = ConfigHelper.GetConfigInt("EvectionFeeType");             //差旅费对应的费用类型
        bll.Model.OrganizeCity          = staff.OrganizeCity;
        bll.Model.ApproveFlag           = 1;
        bll.Model.State                 = 1;
        bll.Model.InsertStaff           = (int)Session["UserID"];
        bll.Model["HasFeeApply"]        = "N";        //无申请单
        bll.Model["IsEvectionWriteOff"] = "Y";        //是差旅报销
        #endregion

        #region 新增费用核销单明细
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost1ACTitle"), Cost1, minbegindate, maxenddate);   //交通费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost2ACTitle"), Cost2, minbegindate, maxenddate);   //住宿费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost3ACTitle"), Cost3, minbegindate, maxenddate);   //补贴
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost4ACTitle"), Cost4, minbegindate, maxenddate);   //市内交通费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost5ACTitle"), Cost5, minbegindate, maxenddate);   //的士费

        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost11ACTitle"), Cost11, minbegindate, maxenddate); //车辆费用-路桥费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost12ACTitle"), Cost12, minbegindate, maxenddate); //车辆费用-油费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost13ACTitle"), Cost13, minbegindate, maxenddate); //车辆费用-其他杂费

        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("TrainingCost1ACTitle"), Cost21, minbegindate, maxenddate);  //总部培训费-差旅费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("TrainingCost2ACTitle"), Cost22, minbegindate, maxenddate);  //营业部培训费-差旅费
        #endregion

        int writeoff = bll.Add();

        #region 置差旅行程的关联报销单ID
        foreach (int evectionid in selectedevectionids)
        {
            FNA_EvectionRouteBLL evectionbll = new FNA_EvectionRouteBLL(evectionid);
            evectionbll.Model.WriteOffID = writeoff;
            evectionbll.Update();
        }
        #endregion

        Response.Redirect("FeeWriteoffDetail.aspx?ID=" + writeoff.ToString());
    }
    protected void bt_Save_Click(object sender, EventArgs e)
    {
        FNA_EvectionRouteBLL bll;
        Car_DispatchRideBLL  car_ridebll = null;

        if ((int)ViewState["ID"] == 0)
        {
            bll = new FNA_EvectionRouteBLL();
        }
        else
        {
            bll = new FNA_EvectionRouteBLL((int)ViewState["ID"]);
        }

        pn_detail.GetData(bll.Model);

        #region 保存车辆使用信息
        if (bll.Model.Transport == "4")
        {
            //自驾车时,关联派车单
            if (string.IsNullOrEmpty(bll.Model["RelateCarDispatch"]))
            {
                car_ridebll = new Car_DispatchRideBLL();
                pn_detail.GetData(car_ridebll.Model);
                if (car_ridebll.Model.KilometresEnd < car_ridebll.Model.KilometresStart)
                {
                    MessageBox.Show(this, "对不起,还车公里数不能小于发车公里数!");
                    return;
                }

                car_ridebll.Model.Destination   = bll.Model.EvectionLine;
                car_ridebll.Model.Matters       = "差旅行程";
                car_ridebll.Model.State         = 3; //已交车
                car_ridebll.Model.OrganizeCity  = new Org_StaffBLL((int)Session["UserID"]).Model.OrganizeCity;
                car_ridebll.Model.ActGoBackTime = DateTime.Now;
                car_ridebll.Model.ApproveFlag   = 2;
                car_ridebll.Model.ApplyStaff    = bll.Model.RelateStaff;
                car_ridebll.Model.InsertStaff   = (int)Session["UserID"];

                bll.Model["RelateCarDispatch"] = car_ridebll.Add().ToString();
            }
            else
            {
                car_ridebll = new Car_DispatchRideBLL(int.Parse(bll.Model["RelateCarDispatch"]));
                pn_detail.GetData(car_ridebll.Model);
                if (car_ridebll.Model.KilometresEnd < car_ridebll.Model.KilometresStart)
                {
                    MessageBox.Show(this, "对不起,还车公里数不能小于发车公里数!");
                    return;
                }

                car_ridebll.Model.Destination = bll.Model.EvectionLine;
                car_ridebll.Model.UpdateStaff = (int)Session["UserID"];
                car_ridebll.Update();
            }

            //更新车辆最后的公里数
            Car_CarListBLL CarBll = new Car_CarListBLL(car_ridebll.Model.CarID);
            if (CarBll.Model != null)
            {
                if (Car_DispatchRideBLL.GetModelList("CarID=" + car_ridebll.Model.CarID.ToString() +
                                                     " AND ID > " + car_ridebll.Model.ID.ToString()).Count == 0)
                {
                    //如果当前用车信息是该车辆最后一次用车记录,则更新车辆最后公里数
                    CarBll.Model.Kilometres = car_ridebll.Model.KilometresEnd;
                    CarBll.Update();
                }
            }
        }
        #endregion

        if ((int)ViewState["ID"] == 0)
        {
            bll.Model.ApproveFlag = 2;
            bll.Model.InsertStaff = (int)Session["UserID"];
            ViewState["ID"]       = bll.Add();
        }
        else
        {
            bll.Model.UpdateStaff = (int)Session["UserID"];
            bll.Update();
        }

        if (car_ridebll != null && car_ridebll.Model["RelateEvectionRoute"] == "")
        {
            car_ridebll.Model["RelateEvectionRoute"] = ViewState["ID"].ToString();
            car_ridebll.Update();
        }

        if (sender != null)
        {
            MessageBox.ShowAndClose(this, "保存成功!");
        }
    }
    private void ReComputeCost(int flag)
    {
        decimal  Cost1 = 0, Cost2 = 0, Cost3 = 0, Cost4 = 0, Cost5 = 0;
        decimal  Cost11 = 0, Cost12 = 0, Cost13 = 0;    //车辆费用
        decimal  Cost21 = 0, Cost22 = 0;                //培训差旅费
        DateTime minbegindate = DateTime.Parse("2999-1-1"), maxenddate = DateTime.Parse("1900-1-1");

        #region 求选中行的差旅费、住宿费、补贴合计
        List <int> selectedevectionids = new List <int>();

        foreach (GridViewRow row in gv_List.Rows)
        {
            CheckBox cbx = (CheckBox)row.FindControl("cbx");
            if (cbx != null && cbx.Checked)
            {
                int id = (int)gv_List.DataKeys[row.RowIndex][0];
                FNA_EvectionRoute evection = new FNA_EvectionRouteBLL(id).Model;

                #region 根据关联的工作日志的类型,判断是否培训费
                //获取关联的日志类型
                int journalid = 0, workingclassify = 0;
                if (int.TryParse(evection["RelateJournal"], out journalid) && journalid > 0)
                {
                    JN_Journal Joural = new JN_JournalBLL(journalid).Model;
                    if (Joural != null)
                    {
                        workingclassify = Joural.WorkingClassify;
                    }
                }

                switch (workingclassify)
                {
                case 4:         //总部组织的培训,计入总部培训-差旅费
                    Cost21 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                    break;

                case 5:         //省区组织的培训,计入营业部培训-差旅费
                    Cost22 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                    break;

                default:
                    Cost1 += evection.Cost1;          //交通费
                    Cost2 += evection.Cost2;          //住宿费
                    Cost3 += evection.Cost3;          //补贴
                    Cost4 += evection.Cost4;          //市内交通费
                    Cost5 += evection.Cost5;          //的士费
                    break;
                }
                #endregion

                if (minbegindate > evection.BeginDate)
                {
                    minbegindate = evection.BeginDate;
                }
                if (maxenddate < evection.EndDate)
                {
                    maxenddate = evection.EndDate;
                }

                int cardispatchid = 0;
                if (int.TryParse(gv_List.DataKeys[row.RowIndex]["Car_DispatchRide_ID"].ToString(), out cardispatchid) &&
                    cardispatchid > 0)
                {
                    Car_DispatchRide dispatch = new Car_DispatchRideBLL(cardispatchid).Model;
                    if (dispatch != null)
                    {
                        Cost11 += dispatch.RoadToll;    //过路过桥费
                        Cost12 += dispatch.FuelFee;     //油费
                        Cost13 += dispatch.ParkingFee;  //停车费
                        Cost13 += dispatch.OtherFee;    //其他费
                    }
                }
                selectedevectionids.Add(id);
            }
        }
        if (Cost1 + Cost2 + Cost3 + Cost4 + Cost5 + Cost11 + Cost12 + Cost13 + Cost21 + Cost22 == 0)
        {
            MessageBox.Show(this, "对不起,请勾选要报销的差旅行程记录!");
            return;
        }
        #endregion

        #region 重设科目金额
        FNA_FeeWriteOffBLL bll = new FNA_FeeWriteOffBLL((int)ViewState["WriteOffID"]);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost1ACTitle"), Cost1, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost2ACTitle"), Cost2, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost3ACTitle"), Cost3, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost4ACTitle"), Cost4, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost5ACTitle"), Cost5, flag, minbegindate, maxenddate);

        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost11ACTitle"), Cost11, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost12ACTitle"), Cost12, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("EvectionCost13ACTitle"), Cost13, flag, minbegindate, maxenddate);

        UpdateDetail(bll, ConfigHelper.GetConfigInt("TrainingCost1ACTitle"), Cost21, flag, minbegindate, maxenddate);
        UpdateDetail(bll, ConfigHelper.GetConfigInt("TrainingCost2ACTitle"), Cost22, flag, minbegindate, maxenddate);
        #endregion

        #region 置差旅行程的关联报销单ID
        foreach (int evectionid in selectedevectionids)
        {
            FNA_EvectionRouteBLL evectionbll = new FNA_EvectionRouteBLL(evectionid);
            if (flag == 1)
            {
                evectionbll.Model.WriteOffID = (int)ViewState["WriteOffID"];
            }
            else
            {
                evectionbll.Model.WriteOffID = 0;
            }
            evectionbll.Update();
        }
        #endregion
    }
    protected void bt_WriteOff_Click(object sender, EventArgs e)
    {
        #region 求选中行的差旅费、住宿费、补贴合计
        decimal Cost1 = 0, Cost2 = 0, Cost3 = 0, Cost4 = 0, Cost5 = 0;
        decimal Cost11 = 0, Cost12 = 0, Cost13 = 0;     //车辆费用
        decimal Cost21 = 0, Cost22 = 0;

        DateTime minbegindate = DateTime.Parse("2999-1-1"), maxenddate = DateTime.Parse("1900-1-1");
        List<int> selectedevectionids = new List<int>();

        foreach (GridViewRow row in gv_List.Rows)
        {
            CheckBox cbx = (CheckBox)row.FindControl("cbx");
            if (cbx != null && cbx.Checked)
            {
                int id = (int)gv_List.DataKeys[row.RowIndex][0];
                FNA_EvectionRoute evection = new FNA_EvectionRouteBLL(id).Model;

                #region 根据关联的工作日志的类型,判断是否培训费
                //获取关联的日志类型
                int journalid = 0, workingclassify = 0;
                if (int.TryParse(evection["RelateJournal"], out journalid) && journalid > 0)
                {
                    JN_Journal Joural = new JN_JournalBLL(journalid).Model;
                    if (Joural != null) workingclassify = Joural.WorkingClassify;
                }

                switch (workingclassify)
                {
                    case 4:     //总部组织的培训,计入总部培训-差旅费
                        Cost21 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                        break;
                    case 5:     //省区组织的培训,计入营业部培训-差旅费
                        Cost22 += evection.Cost1 + evection.Cost2 + evection.Cost3 + evection.Cost4 + evection.Cost5;
                        break;
                    default:
                        Cost1 += evection.Cost1;      //交通费
                        Cost2 += evection.Cost2;      //住宿费
                        Cost3 += evection.Cost3;      //补贴
                        Cost4 += evection.Cost4;      //市内交通费
                        Cost5 += evection.Cost5;      //的士费
                        break;
                }
                #endregion

                if (minbegindate > evection.BeginDate) minbegindate = evection.BeginDate;
                if (maxenddate < evection.EndDate) maxenddate = evection.EndDate;

                int cardispatchid = 0;
                if (int.TryParse(gv_List.DataKeys[row.RowIndex]["Car_DispatchRide_ID"].ToString(), out cardispatchid)
                    && cardispatchid > 0)
                {
                    Car_DispatchRide dispatch = new Car_DispatchRideBLL(cardispatchid).Model;
                    if (dispatch != null)
                    {
                        Cost11 += dispatch.RoadToll;    //过路过桥费
                        Cost12 += dispatch.FuelFee;     //油费
                        Cost13 += dispatch.ParkingFee;  //停车费
                        Cost13 += dispatch.OtherFee;    //其他费
                    }
                }
                selectedevectionids.Add(id);
            }
        }
        if (Cost1 + Cost2 + Cost3 + Cost4 + Cost5 + Cost11 + Cost12 + Cost13 + Cost21 + Cost22 == 0)
        {
            MessageBox.Show(this, "对不起,请勾选要报销的差旅行程记录!");
            return;
        }
        #endregion

        #region 新增费用核销单头
        Org_Staff staff = new Org_StaffBLL((int)Session["UserID"]).Model;

        FNA_FeeWriteOffBLL bll = new FNA_FeeWriteOffBLL();
        bll.Model.SheetCode = FNA_FeeWriteOffBLL.GenerateSheetCode(staff.OrganizeCity);   //自动产生报销单号
        bll.Model.AccountMonth = AC_AccountMonthBLL.GetCurrentMonth();
        bll.Model.FeeType = ConfigHelper.GetConfigInt("EvectionFeeType");                             //差旅费对应的费用类型
        bll.Model.OrganizeCity = staff.OrganizeCity;
        bll.Model.ApproveFlag = 1;
        bll.Model.State = 1;
        bll.Model.InsertStaff = (int)Session["UserID"];
        bll.Model["HasFeeApply"] = "N";                //无申请单
        bll.Model["IsEvectionWriteOff"] = "Y";        //是差旅报销
        #endregion

        #region 新增费用核销单明细
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost1ACTitle"), Cost1, minbegindate, maxenddate);//交通费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost2ACTitle"), Cost2, minbegindate, maxenddate);//住宿费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost3ACTitle"), Cost3, minbegindate, maxenddate);//补贴
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost4ACTitle"), Cost4, minbegindate, maxenddate);//市内交通费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost5ACTitle"), Cost5, minbegindate, maxenddate);//的士费

        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost11ACTitle"), Cost11, minbegindate, maxenddate);//车辆费用-路桥费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost12ACTitle"), Cost12, minbegindate, maxenddate);//车辆费用-油费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("EvectionCost13ACTitle"), Cost13, minbegindate, maxenddate);//车辆费用-其他杂费

        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("TrainingCost1ACTitle"), Cost21, minbegindate, maxenddate);//总部培训费-差旅费
        AddWriteOffDetail(bll, ConfigHelper.GetConfigInt("TrainingCost2ACTitle"), Cost22, minbegindate, maxenddate);//营业部培训费-差旅费
        #endregion

        int writeoff = bll.Add();

        #region 置差旅行程的关联报销单ID
        foreach (int evectionid in selectedevectionids)
        {
            FNA_EvectionRouteBLL evectionbll = new FNA_EvectionRouteBLL(evectionid);
            evectionbll.Model.WriteOffID = writeoff;
            evectionbll.Update();
        }
        #endregion

        Response.Redirect("FeeWriteoffDetail.aspx?ID=" + writeoff.ToString());
    }