protected void BtnSave_Click(object sender, EventArgs e)
 {
     foreach (GridViewRow row in gv_List.Rows)
     {
         PM_PromotorNumberLimitBLL _bll = new PM_PromotorNumberLimitBLL(int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString()));
         _bll.Model.UpdateStaff = int.Parse(Session["UserID"].ToString());
         _bll.Model.ID = int.Parse(gv_List.DataKeys[row.RowIndex]["ID"].ToString());
         _bll.Model.NumberLimit = int.Parse(((TextBox)row.FindControl("TBLimit")).Text);
         _bll.Model.BudgetNumber = int.Parse(((TextBox)row.FindControl("TBBudget")).Text);
         _bll.Model.Remark = ((TextBox)row.FindControl("TBRemark")).Text;
         _bll.Update();
     }
 }
    protected void bt_OK_Click(object sender, EventArgs e)
    {
        PM_PromotorBLL _promotor = null;

        if ((int)ViewState["PromotorID"] == 0)
        {
            _promotor = new PM_PromotorBLL();
        }
        else
        {
            _promotor = new PM_PromotorBLL((int)ViewState["PromotorID"]);
        }

        int classify = 0, oldClassify = 0;

        int.TryParse(_promotor.Model["Classfiy"], out oldClassify);
        int oldOrganizeCity = _promotor.Model.OrganizeCity;

        UC_DetailView1.GetData(_promotor.Model);

        #region 判断数据有效性
        if (!(_promotor.Model.OrganizeCity > 1))
        {
            MessageBox.Show(this, "请选择导购员所在的管理片区!");
            return;
        }
        int.TryParse(_promotor.Model["Classfiy"], out classify);
        if (classify == 0)
        {
            MessageBox.Show(this, "请正确选择导购员类别!");
            return;
        }
        if (_promotor.Model["InfoSource"] == "" || _promotor.Model["InfoSource"] == "0")
        {
            MessageBox.Show(this, "请正确选择导购来源!");
            return;
        }
        if (_promotor.Model["OldClassify"] == "" || _promotor.Model["OldClassify"] == "0")
        {
            MessageBox.Show(this, "请正确选择原属品牌!");
            return;
        }
        if (_promotor.Model["BankType"] == "" || _promotor.Model["BankType"] == "0")
        {
            MessageBox.Show(this, "请正确选择银行信息");
            return;
        }
        int bankid = int.Parse(ddl_BankType.SelectedValue);
        if (_promotor.Model["BankName"] == "" || bankid <= 2 && _promotor.Model["BankName"] != ddl_BankType.SelectedItem.Text)
        {
            MessageBox.Show(this, "银行备注信息不正确");
            return;
        }
        //Org_Staff _staffM = new Org_StaffBLL((int)Session["UserID"]).Model;
        //if (_staffM.OrganizeCity == _promotor.Model.OrganizeCity)
        //{
        //    MessageBox.Show(this, "对不起,你不能把导购员放在与你同级的管理片区");
        //    return;
        //}

        #endregion


        #region 判断在职及离职状态
        if (_promotor.Model.Dimission == 1)
        {
            _promotor.Model.EndWorkDate = new DateTime(1900, 1, 1);
        }
        else if (_promotor.Model.EndWorkDate == new DateTime(1900, 1, 1))
        {
            _promotor.Model.EndWorkDate = DateTime.Today;
        }
        #endregion
        #region 如果有工资生成判断离职时间
        if ((int)ViewState["PromotorID"] > 0)
        {
            string[]        allowday  = Addr_OrganizeCityParamBLL.GetValueByType(1, 3).Split(new char[] { ',', ',', ';', ';' });
            AC_AccountMonth lastmonth = GetMaxSalaryDate((int)ViewState["PromotorID"], AC_AccountMonthBLL.GetCurrentMonth() - 1);
            if (lastmonth != null && allowday.Contains(DateTime.Now.Day.ToString()) && _promotor.Model.BeginWorkDate > lastmonth.EndDate)
            {
                MessageBox.Show(this, "该导购在" + lastmonth.Name + "生成过工资,入职日期不能大于" + lastmonth.EndDate.ToString("yyyy-MM-dd"));
                return;
            }
            if (_promotor.Model.Dimission == 2 && _promotor.Model.EndWorkDate < _promotor.Model.BeginWorkDate && _promotor.Model.EndWorkDate.AddDays(40) > DateTime.Now)
            {
                MessageBox.Show(this, "导购离职日期不能小于入职日期!");
                return;
            }
            AC_AccountMonth month = GetMaxSalaryDate((int)ViewState["PromotorID"], 0);
            if (month != null && _promotor.Model.Dimission == 2 && _promotor.Model.EndWorkDate < month.BeginDate)
            {
                MessageBox.Show(this, "该导购在" + month.Name + "生成过工资,离职日期不能小于" + month.BeginDate.ToString("yyyy-MM-dd"));
                return;
            }
        }
        #endregion
        if (_promotor.Model["IDCode"] != string.Empty && _promotor._GetModelList("ID!=" + ViewState["PromotorID"].ToString() + " AND MCS_SYS.dbo.UF_Spilt(PM_Promotor.ExtPropertys,'|',1)='" + _promotor.Model["IDCode"] + "'").Count > 0)
        {
            MessageBox.Show(this, "对不起,该身份证号的导购员已在系统中,请核实后再新增!");
            return;
        }

        if ((int)ViewState["PromotorID"] == 0)
        {
            DateTime birthday;
            if (DateTime.TryParse(_promotor.Model["Birthday"], out birthday))
            {
                if (DateTime.Now < birthday.AddYears(16) || DateTime.Now > birthday.AddYears(50))
                {
                    int year = DateTime.Now.Year - birthday.Year;
                    if (birthday.AddYears(year) > DateTime.Now)
                    {
                        year++;
                    }
                    MessageBox.Show(this, "对不起,该导购年龄不符合规则(16~49岁),该人员年龄:" + year);
                    return;
                }
            }

            if (PM_PromotorNumberLimitBLL.CheckAllowAdd(_promotor.Model.OrganizeCity, classify) <= 0)
            {
                MessageBox.Show(this, "对不起当前城市导购员人数满额,要想继续新增请与人事经理联系");
                return;
            }

            _promotor.Model.InputStaff  = (int)Session["UserID"];
            _promotor.Model.ApproveFlag = 2;
            _promotor.Model.Dimission   = 1;
            _promotor.Model.EndWorkDate = new DateTime(1900, 1, 1);
            ViewState["PromotorID"]     = _promotor.Add();
        }
        else
        {
            if (!PM_PromotorNumberLimitBLL.IsSameLimit(oldOrganizeCity, _promotor.Model.OrganizeCity, oldClassify, classify) &&
                PM_PromotorNumberLimitBLL.CheckAllowAdd(_promotor.Model.OrganizeCity, classify) <= 0)
            {
                MessageBox.Show(this, "对不起当前城市导购员人数满额,要想继续新增请与人事经理联系");
                return;
            }

            _promotor.Model.UpdateStaff = (int)Session["UserID"];
            _promotor.Update();
        }
        if (sender != null)
        {
            Response.Redirect("PM_PromotorDetail.aspx?PromotorID=" + ViewState["PromotorID"].ToString());
        }
    }
    protected void bt_AddApply_Click(object sender, EventArgs e)
    {
        if ((int)ViewState["PromotorID"] == 0)
        {
            MessageBox.Show(this, "对不起,请您先保存后在发起申请");
            return;
        }
        bt_OK_Click(null, null);

        PM_PromotorBLL bll = new PM_PromotorBLL((int)ViewState["PromotorID"]);
        DateTime       birthday;

        if (DateTime.TryParse(bll.Model["Birthday"], out birthday))
        {
            if (DateTime.Now < birthday.AddYears(16) || DateTime.Now > birthday.AddYears(50))
            {
                int year = DateTime.Now.Year - birthday.Year;
                if (birthday.AddYears(year) > DateTime.Now)
                {
                    year++;
                }
                MessageBox.Show(this, "对不起,该导购年龄不符合规则(16~49岁),该人员年龄:" + year);
                return;
            }
        }
        if (PM_PromotorNumberLimitBLL.CheckAllowAdd(bll.Model.OrganizeCity, int.Parse(bll.Model["Classfiy"])) < 0)
        {
            MessageBox.Show(this, "对不起当前城市导购员人数满额,要想继续新增请与人事经理联系");
            return;
        }
        if (bll.Model.BeginWorkDate.AddDays(40) < DateTime.Now)
        {
            MessageBox.Show(this, "对不起,入职时间必须在发起日期前40天之内!");
            return;
        }
        if (bll.Model["IDCode"] == "")
        {
            MessageBox.Show(this, "请录入身份证号!");
            return;
        }
        if (bll.Model["InfoSource"] == "" || bll.Model["InfoSource"] == "0")
        {
            MessageBox.Show(this, "请正确选择导购来源!");
            return;
        }
        if (bll.Model["OldClassify"] == "" || bll.Model["OldClassify"] == "0")
        {
            MessageBox.Show(this, "请正确选择原属品牌!");
            return;
        }
        if (bll.Model["BankType"] == "" || bll.Model["BankType"] == "0")
        {
            MessageBox.Show(this, "请正确选择银行信息");
            return;
        }
        string[]        allowday  = Addr_OrganizeCityParamBLL.GetValueByType(1, 3).Split(new char[] { ',', ',', ';', ';' });
        AC_AccountMonth lastmonth = GetMaxSalaryDate((int)ViewState["PromotorID"], AC_AccountMonthBLL.GetCurrentMonth() - 1);

        if (lastmonth != null && allowday.Contains(DateTime.Now.Day.ToString()) && bll.Model.BeginWorkDate > lastmonth.EndDate)
        {
            MessageBox.Show(this, "该导购在" + lastmonth.Name + "生成过工资,入职日期不能大于" + lastmonth.EndDate.ToString("yyyy-MM-dd"));
            return;
        }

        #region 判断是否KA店导购
        bool IsKAChannel = false;         //是否KA店导购

        if (bll.Model["Classify"] != "2") //非流导
        {
            IList <PM_PromotorInRetailer> retailers = PM_PromotorInRetailerBLL.GetModelList(" Promotor=" + ViewState["PromotorID"].ToString());
            if (retailers.Count == 0)
            {
                MessageBox.Show(this, "对不起,请关联该导购所在的工作门店!");
                return;
            }

            //判断导购是否在KA店工作
            foreach (PM_PromotorInRetailer item in retailers)
            {
                CM_Client client = new CM_ClientBLL(item.Client).Model;
                if (client["RTChannel"] == "1" || client["RTChannel"] == "2")
                {
                    IsKAChannel = true;
                }
            }
        }
        #endregion

        #region 判断是否超薪酬超准
        bool bSalaryFlag = false;   //false : 薪酬标准内 true:超标准
        IList <PM_PromotorSalary> salarylists = PM_PromotorSalaryBLL.GetModelList("Promotor=" + bll.Model.ID.ToString() + " AND State IN(1,3) Order BY State");

        if (salarylists.Count == 0 || salarylists.Count > 0 && (bll.Model["Classfiy"] == "6" && salarylists[0].BasePayMode != 3 || bll.Model["Classfiy"] != "6" && salarylists[0].BasePayMode == 3))
        {
            MessageBox.Show(this, "对不起,尚未为该导购设定薪酬信息,请设定完薪酬信息后,再提交入职申请!");
            return;
        }
        else
        {
            PM_PromotorSalary salary = salarylists[0];
            if (salary.BasePaySubsidy > 0)
            {
                bSalaryFlag = true;                              //有底薪补贴
            }
            if (salary.MinimumWageMode == 2)
            {
                bSalaryFlag = true;                              //特殊保底
            }
            if (salary.InsuranceMode == 1 && salary.InsuranceSubsidy > 100)
            {
                bSalaryFlag = true;                                                                 //社保补贴大于100元的
            }
        }
        #endregion

        int budget = PM_PromotorNumberLimitBLL.CheckOverBudget(bll.Model.OrganizeCity, int.Parse(bll.Model["Classfiy"]));

        NameValueCollection dataobjects = new NameValueCollection();
        dataobjects.Add("ID", ViewState["PromotorID"].ToString());
        dataobjects.Add("OrganizeCity", bll.Model.OrganizeCity.ToString());
        dataobjects.Add("SalaryFlag", !bSalaryFlag ? "1" : "2");      //薪酬标志 1:薪酬标准内 2:薪酬标准外
        dataobjects.Add("StaffName", bll.Model.Name.ToString());
        dataobjects.Add("IsKAChannel", IsKAChannel ? "1" : "2");      //是否KA卖场的导购
        dataobjects.Add("IsOverBudget", budget < 0 ? "1" : "2");      //是否超人数预算 1:超 2:未超


        int TaskID = EWF_TaskBLL.NewTask("Add_Promotor", (int)Session["UserID"], "新增导购员流程,姓名:" + bll.Model.Name + "【" + ddl_Classify.SelectedItem.Text.ToString() + "】", "~/SubModule/PM/PM_PromotorDetail.aspx?PromotorID=" + ViewState["PromotorID"].ToString(), dataobjects);
        if (TaskID > 0)
        {
            bll.Submit(TaskID, (int)Session["UserID"]);
        }

        Response.Redirect("~/SubModule/EWF/Apply.aspx?TaskID=" + TaskID.ToString());
    }
    private void BindData()
    {
        PM_Promotor m = new PM_PromotorBLL((int)ViewState["PromotorID"]).Model;

        UC_DetailView1.BindData(m);
        gv_list.ConditionString = " Promotor= " + (int)ViewState["PromotorID"];
        gv_list.BindGrid();

        //验证身份证号码
        txt_BankName.Enabled = m["BankType"] == "3";

        if (m.ApproveFlag == 1)
        {
            bt_Approve.Visible = false;
            if (m.Dimission == 1)
            {
                TextBox txt_EndWorkDate = (TextBox)UC_DetailView1.FindControl("PM_Promotor_EndWorkDate");
                if (txt_EndWorkDate != null)
                {
                    txt_EndWorkDate.Enabled = false;
                }

                bt_AddApply.Visible = false;
                #region 导购兼职/专职转换
                int classfiy = 0;
                int.TryParse(m["Classfiy"], out classfiy);
                if (classfiy == 0)
                {
                    bt_ChangeClassify.Visible = false;
                }
                else if (classfiy == 1 || classfiy == 2)
                {
                    bt_ChangeClassify.Text    = "转为兼职";
                    bt_ChangeClassify.Visible = true;
                }
                else
                {
                    bt_ChangeClassify.Text    = "转为专职/流动";
                    bt_ChangeClassify.Visible = true;
                }
                bt_ChangeClassify.OnClientClick = "PopPMClassify_Approve(" + ViewState["PromotorID"].ToString() + ")";
                #endregion
            }
            Header.Attributes["WebPageSubCode"] = "Modify";
            //TextBox txt_BeginWorkDate = (TextBox)UC_DetailView1.FindControl("PM_Promotor_BeginWorkDate");

            //临时开放修改功能
            //if (txt_BeginWorkDate != null && m.Dimission==1) txt_BeginWorkDate.Enabled = false;

            //有些离职日期会填写错误,更新为权限修改 12.11.21
            //if (m.Dimission == 2)
            //{
            //    TextBox txt_EndWorkDate = (TextBox)UC_DetailView1.FindControl("PM_Promotor_EndWorkDate");
            //    if (txt_EndWorkDate != null) txt_EndWorkDate.Enabled = false;
            //}
            string[] allowdays = Addr_OrganizeCityParamBLL.GetValueByType(1, 9).Replace(" ", "").Split(new char[] { ',', ',', ';', ';' });
            if (allowdays.Contains(DateTime.Now.Day.ToString()))
            {
                bt_ChangeClassify.Enabled = false;
                bt_ChangeClassify.ToolTip = "导购工资生成期间不能互转";
            }
        }

        if (m["State"] == "2")
        {
            //审批中
            bt_AddApply.Visible       = false;
            bt_OK.Visible             = false;
            bt_TaskDetail.Visible     = true;
            bt_ChangeClassify.Enabled = false;
            bt_ChangeClassify.ToolTip = "该导购有未完成的流程,请待流程完成后,再做调整!";
        }
        bt_OK.Text      = "保 存";
        bt_OK.ForeColor = System.Drawing.Color.Red;


        if (m["Classfiy"] != "")
        {
            int budget = PM_PromotorNumberLimitBLL.CheckOverBudget(m.OrganizeCity, int.Parse(m["Classfiy"]));
            if (budget == 0)
            {
                lb_OverBudgetInfo.Text = "导购员数量已等于预定的预算人数,请注意!";
            }
            else if (budget < 0)
            {
                lb_OverBudgetInfo.Text = "导购员数量已超过预定的预算人数 " + (0 - budget).ToString() + "人,请注意!";
            }
        }


        if (m["IDCode"].Length == 18 && !Tools.DoVerifyIDCode(m["IDCode"]))
        {
            lb_OverBudgetInfo.Text += "         注意:该导购员身份证号码错误!";
        }

        ddl_Classify_SelectedIndexChanged(null, null);
    }