//private void SendOnAgreementOPHeadSighting(Demand demand)
        //{
        //    if (!demand.AgreementCfo.HasValue)
        //        return;
        //    var identityIds = EmployeeService.GetAllTrusteeInStructDivisionIds(demand.AgreementCfo.Value);
        //    var employees = SecurityEntityService.GetAllEmployeesInRole(identityIds, BudgetRole.DivisionHead, demand.BudgetVersion.BudgetId, true);
        //    SendMailsToEmployee(employees, demand, WorkflowState.DemandAgreementOPHeadSighting);
        //}

        private void SendOnOPExpertSighting(Demand demand)
        {
            if (!demand.ExecutorStructId.HasValue)
                return;
            var identityIds = EmployeeService.GetAllTrusteeInStructDivisionIds(demand.ExecutorStructId.Value);
            var employees = SecurityEntityService.GetAllEmployeesInRole(identityIds, BudgetRole.Expert, demand.BudgetVersion.BudgetId, true);
            SendMailsToEmployee(employees, demand, WorkflowState.DemandOPExpertSighting);
        }
 private void SendOnAgreementOPExpertSighting(Demand demand)
 {
     if (!demand.AgreementCfo.HasValue)
         return;
     var identityIds = EmployeeService.GetAllTrusteeInStructDivisionIds(demand.AgreementCfo.Value);
     var employees = EmployeeService.GetEmployeesBySecurityTrusteeIdsForNotification(identityIds, demand.BudgetVersion.BudgetId, true);
     SendMailsToEmployee(employees, demand, WorkflowState.DemandAgreementOPExpertSighting);
 }
		private void detach_Demands(Demand entity)
		{
			this.SendPropertyChanging();
			entity.DemandStatusInternal = null;
		}
 partial void DeleteDemand(Demand instance);
 partial void UpdateDemand(Demand instance);
 private void SendOnUPKZHeadSighting(Demand demand)
 {
     if (!demand.InitiatorStructId.HasValue)
         return;
     var employees = SecurityEntityService.GetAllEmployeesInRole(BudgetRole.UPKZHead, demand.BudgetVersion.BudgetId, true);
     SendMailsToEmployee(employees, demand, WorkflowState.DemandUPKZHeadSighting);
 }
		private void attach_Demands(Demand entity)
		{
			this.SendPropertyChanging();
			entity.Limit = this;
		}
		private void detach_Demands1(Demand entity)
		{
			this.SendPropertyChanging();
			entity.SecurityTrustee1 = null;
		}
		private void detach_Demands1(Demand entity)
		{
			this.SendPropertyChanging();
			entity.ExecutorStructDivision = null;
		}
 private void SendOnInitiatorHeadSighting(Demand demand)
 {
     if (!demand.AuthorId.HasValue)
         return;
     var identityIds = SecurityEntityService.GetHeadIds(demand.AuthorId.Value, demand.BudgetVersion.BudgetId);
     var employees = EmployeeService.GetEmployeesBySecurityTrusteeIdsForNotification(identityIds,
                                                                                     demand.BudgetVersion.
                                                                                         BudgetId, true);
     SendMailsToEmployee(employees, demand, WorkflowState.DemandInitiatorHeadSighting);
 }
 private Dictionary<string, string> GetDefaultParameters(Demand demand)
 {
     return new Dictionary<string, string>()
                {
                    {
                        "$DEMANDNUMBER$",
                        demand.Number.ToString(CultureInfo.CurrentCulture)
                        }
                };
 }
 private void SendEmailToEmployee(Demand demand, Employee employee, WorkflowState state)
 {
     var parameters = GetDefaultParameters(demand);
     parameters.Add("$DEMANDLINK$", string.Format("{0}/Demand/?tid={1}", PublicPagesUrl, WorkflowTicketService.CreateTicket(employee.IdentityId, demand.Id, state.WorkflowStateName)));
     EmailService.SendEmail("DEMAND_NOTIFICATION", parameters, employee.Email);
 }
        private void SendMailsToEmployee(IEnumerable<Employee> employees, Demand demand, WorkflowState state)
        {
            var hasErrors = false;
            var errors = new StringBuilder();
            var distinctemployees = employees;//.Distinct();
            foreach (var employee in distinctemployees)
            {
                try
                {
                    SendEmailToEmployee(demand, employee, state);
                }
                catch (Exception ex)
                {
                    errors.AppendLine(ex.Message);
                    hasErrors = true;
                }
            }

            if (hasErrors)
                throw new InvalidOperationException(errors.ToString());
        }
		private void detach_Demands(Demand entity)
		{
			this.SendPropertyChanging();
			entity.CostArticle = null;
		}
		private void detach_Demands1(Demand entity)
		{
			this.SendPropertyChanging();
			entity.AgreementCFO = null;
		}
		private void attach_Demands(Demand entity)
		{
			this.SendPropertyChanging();
			entity.AuthorStructDivision = this;
		}
		private void detach_Demands2(Demand entity)
		{
			this.SendPropertyChanging();
			entity.AuthorCFO = null;
		}
		private void detach_Demands2(Demand entity)
		{
			this.SendPropertyChanging();
			entity.StructDivision2 = null;
		}
		private void detach_Demands(Demand entity)
		{
			this.SendPropertyChanging();
			entity.Counteragent = null;
		}
		private void detach_Demands(Demand entity)
		{
			this.SendPropertyChanging();
			entity.BudgetVersion = null;
		}
 partial void InsertDemand(Demand instance);
		private void detach_Demands(Demand entity)
		{
			this.SendPropertyChanging();
			entity.Limit = null;
		}
 private void SendOnRollbackRequested(Demand demand)
 {
     if (!demand.InitiatorStructId.HasValue)
         return;
     var employees = SecurityEntityService.GetAllEmployeesInRole(BudgetRole.Curator, demand.BudgetVersion.BudgetId, true);
     SendMailsToEmployee(employees, demand, WorkflowState.DemandRollbackRequested);
 }