private void SendOnInputLimitExecutorSighting(BillDemand billDemand)
 {
     var limits = GetLimits(billDemand, false);
     var executorIds = new List<Guid>();
     limits.Where(lim => lim.ExecutorId.HasValue).ForEach(lim => executorIds.Add(lim.ExecutorId.Value));
     var employees = EmployeeService.GetEmployeesBySecurityTrusteeIdsForNotification(executorIds, billDemand.BudgetId, true);
     SendMailsToEmployee(employees, billDemand, WorkflowState.BillDemandLimitExecutorSighting);
 }
        private void CheckOnInputInAccountingWithExport(BillDemand billDemand)
        {
            if (BillDemandBuinessService.IsBillDemandSkipInAccountingState(billDemand.Id))
                return;
            if (!billDemand.FilialId.HasValue || !SecurityEntityService.CheckThatSomebodyHasRoleInFilial(BudgetRole.Accountant, billDemand.FilialId.Value, billDemand.BudgetId))
            {
                var parameters = GetDefaultParameters(billDemand);
                if (billDemand.FilialId.HasValue && billDemand.Filial != null)
                    parameters.Add("$FILIALNAME$", billDemand.Filial.Name);
                else
                    parameters.Add("$FILIALNAME$", "Не определено");

                EmailService.SendEmail("BD_NOT_FOUND_ACCOUNTANT", parameters);
            }
        }
 partial void InsertBillDemand(BillDemand instance);
 private void SendOnInputLimitManagerSighting(BillDemand billDemand)
 {
     var limits = GetLimits(billDemand, false);
     var managerIds = new List<Guid>();
     var sightedLimits = SightedByManagerLimitIds(billDemand);
     limits.Where(lim => lim.ManagerId.HasValue && !sightedLimits.Contains(lim.Id)).ForEach(lim => managerIds.Add(lim.ExecutorId.Value));
     var employees = EmployeeService.GetEmployeesBySecurityTrusteeIdsForNotification(managerIds, billDemand.BudgetId, true);
     SendMailsToEmployee(employees, billDemand, WorkflowState.BillLimitManagerSighting);
 }
 private void CheckOnInputLimitExecutorSighting(BillDemand billDemand)
 {
     List<Limit> limits = GetLimits(billDemand);
     foreach (var limit in limits)
     {
         if (!limit.ExecutorId.HasValue || !limit.Executor.Enabled || limit.Executor.Employees.Count(p => !p.IsDeleted && p.BudgetId == billDemand.BudgetId) <= 0)
         {
             var parameters = GetDefaultParameters(billDemand);
             parameters = AddLimitParameters(parameters, limit);
             EmailService.SendEmail("BD_NOT_FOUND_LIMIT_EXECUTOR", parameters);
         }
     }
 }
 private List<Limit> GetLimits(BillDemand billDemand, bool loademployees = true)
 {
     using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
     {
         using (var context = this.CreateContext())
         {
             var dlo = new DataLoadOptions();
             dlo.LoadWith<BillDemandDistribution>(p => p.Demand);
             dlo.LoadWith<Demand>(p => p.Limit);
             if (loademployees)
             {
                 dlo.LoadWith<Limit>(p => p.Manager);
                 dlo.LoadWith<Limit>(p => p.Executor);
                 dlo.LoadWith<SecurityTrustee>(p => p.Employees);
             }
             context.LoadOptions = dlo;
             context.DeferredLoadingEnabled = false;
             return context.BillDemandDistributions.Where(p => p.BillDemandId == billDemand.Id).Select(
                 p => p.Demand.Limit).ToList();
         }
     }
 }
 private void CheckOnInputUpkzControllerSighting(BillDemand billDemand)
 {
     if (!SecurityEntityService.CheckThatSomebodyHasRole(BudgetRole.Controller))
     {
         EmailService.SendEmail("BD_NOT_FOUND_UPKZ_CONTROLLER", GetDefaultParameters(billDemand));
     }
 }
 private void SendEmailToEmployee(BillDemand bd, Employee employee, WorkflowState state)
 {
     var parameters = GetDefaultParameters(bd);
     parameters.Add("$BILLDEMANDLINK$", string.Format("{0}/BillDemand/?tid={1}", PublicPagesUrl, WorkflowTicketService.CreateTicket(employee.IdentityId, bd.Id, state.WorkflowStateName)));
     EmailService.SendEmail("BILL_DEMAND_NOTIFICATION", parameters, employee.Email);
 }
		private void attach_BillDemands(BillDemand entity)
		{
			this.SendPropertyChanging();
			entity.Currency = this;
		}
		private void detach_BillDemands2(BillDemand entity)
		{
			this.SendPropertyChanging();
			entity.SecurityTrustee2 = null;
		}
		private void attach_BillDemands1(BillDemand entity)
		{
			this.SendPropertyChanging();
			entity.SecurityTrustee1 = this;
		}
		private void detach_BillDemands(BillDemand entity)
		{
			this.SendPropertyChanging();
			entity.StructDivision = null;
		}
 partial void DeleteBillDemand(BillDemand instance);
 partial void UpdateBillDemand(BillDemand instance);
        private void SendOnInputHeadInitiatorSighting(BillDemand billDemand)
        {
            if (!billDemand.AuthorId.HasValue)
                throw new InvalidOperationException("billDemand.AuthorId is null");

            var trusteeIds = SecurityEntityService.GetHeadIds(billDemand.AuthorId.Value, billDemand.BudgetId);

            var employees = EmployeeService.GetEmployeesBySecurityTrusteeIdsForNotification(trusteeIds,
                                                                                            billDemand.BudgetId, true); 

            SendMailsToEmployee(employees, billDemand, WorkflowState.BillDemandHeadInitiatorSighting);
        }
        private void SendMailsToEmployee(IEnumerable<Employee> employees, BillDemand billDemand, WorkflowState state)
        {
            bool hasErrors = false;
            var errors = new StringBuilder();
            var distinctemployees = employees;//.Distinct();
            foreach (var employee in distinctemployees)
            {
                try
                {
                    SendEmailToEmployee(billDemand, employee, state);
                }
                catch (Exception ex)
                {
                    errors.AppendLine(ex.Message);
                    hasErrors = true;
                }
            }

            if (hasErrors)
                throw new InvalidOperationException(errors.ToString());
        }
		private void detach_BillDemands1(BillDemand entity)
		{
			this.SendPropertyChanging();
			entity.Currency1 = null;
		}
        private void CheckOnInputHeadInitiatorSighting(BillDemand billDemand)
        {
            if (!billDemand.AuthorId.HasValue)
                throw new InvalidOperationException("billDemand.AuthorId is null");

            var trusteeIds = SecurityEntityService.GetHeadIds(billDemand.AuthorId.Value, billDemand.BudgetId);

            if (trusteeIds.Count() < 1)
            {
                var parameters = GetDefaultParameters(billDemand);
                parameters.Add("$INITIATOR$", billDemand.Author.Name);
                EmailService.SendEmail("BD_NOT_FOUND_INITIATOR_HEAD", parameters);
            }
        }
		private void detach_BillDemands(BillDemand entity)
		{
			this.SendPropertyChanging();
			entity.Budget = null;
		}
 private void CheckOnInputUpkzHeadSighting(BillDemand billDemand)
 {
     if (!SecurityEntityService.CheckThatSomebodyHasRole(BudgetRole.UPKZHead))
     {
         EmailService.SendEmail("BD_NOT_FOUND_UPKZ_HEAD", GetDefaultParameters(billDemand));
     }
 }
 private List<LimitSighter> GetLimitManagerSightersOperative (Budget2DataContext context, BillDemand billDemand)
 {
     return  billDemand.BillDemandDistributions.Where(
           p =>
           p.DemandId.HasValue && p.Demand.ExecutorStructId.HasValue).Select(
               p =>
               new LimitSighter { LimitId = p.Demand.ExecutorStructId.Value, SighterId = null }).
           Distinct().ToList();
 }
 private IEnumerable<Guid> SightedByManagerLimitIds(BillDemand billDemand)
 {
     using (var scope = ReadUncommittedSupressedScope)
     {
         using (var context = this.CreateContext())
         {
             return
                 context.WorkflowSightings.Where(
                     p =>
                     p.EntityId == billDemand.Id &&
                     p.SightingType == SightingType.BillDemandLimitManagerSighting.Id).Select(p => p.ItemId).
                     ToList();
         }
     }
 }
 private bool CheckUPKZHeadMustSight(BillDemand billDemand)
 {
     return billDemand.Sum >= Common.Settings.Instance.BillDemandWfLimitSum;
 }
 private Dictionary<string, string> GetDefaultParameters(BillDemand billDemand)
 {
     return new Dictionary<string, string>()
                {
                    {
                        "$BILLDEMANDNUMBER$",
                        billDemand.IdNumber.ToString(CultureInfo.CurrentCulture)
                        },
                    {
                        "$BILLDEMANDDATE$",
                        billDemand.AllocationDate.HasValue
                            ? billDemand.AllocationDate.Value.ToString(CultureInfo.CurrentCulture)
                            : "-"
                        }
                };
 }
 private BudgetDocumentKind GetDocumentKind(BillDemand billDemand)
 {
     if (!billDemand.DocKindId.HasValue)
         return BudgetDocumentKind.InBudget;
     return BudgetDocumentKind.All.First(p => p.Id == billDemand.DocKindId);
 }
 private bool IsBillDemandSupportExport(BillDemand billDemand)
 {
     return billDemand.Filial.IsBillDemandExportable && billDemand.Contract != null && billDemand.Contract.CustomerCounteragent != null
            && billDemand.Contract.CustomerCounteragent.Number.ToString(CultureInfo.InvariantCulture) == BossAvailiableExecutorCode;
 }
 private bool IsBillDemandSkipInAccountingState(BillDemand billDemand)
 {
     return billDemand.Filial != null && billDemand.Filial.IsSkipInAccountingState;
 }
 private void SendOnInputUpkzHeadSighting(BillDemand billDemand)
 {
     var employees = SecurityEntityService.GetAllEmployeesInRole(BudgetRole.UPKZHead, billDemand.BudgetId, true);
     SendMailsToEmployee(employees, billDemand, WorkflowState.BillDemandUPKZHeadSighting);
 }
		private void detach_BillDemands(BillDemand entity)
		{
			this.SendPropertyChanging();
			entity.Counteragent = null;
		}