public List <IBusinessUnit> GetDuplicateBusinessUnit()
        {
            var duplicateBusinessUnits = new List <IBusinessUnit>();

            if (!string.IsNullOrWhiteSpace(_obj.TIN))
            {
                duplicateBusinessUnits = BusinessUnits.GetAll(b => !Equals(b, _obj) && b.Status == CoreEntities.DatabookEntry.Status.Active &&
                                                              _obj.TIN == b.TIN && _obj.TRRC == b.TRRC).ToList();
            }
            return(duplicateBusinessUnits);
        }
        public static List <IBusinessUnit> GetBusinessUnits(string tin, string trrc)
        {
            var searchByTin  = !string.IsNullOrWhiteSpace(tin);
            var searchByTrrc = !string.IsNullOrWhiteSpace(trrc);

            if (!searchByTin && !searchByTrrc)
            {
                return(new List <IBusinessUnit>());
            }

            // Отфильтровать закрытые НОР.
            var businessUnits = BusinessUnits.GetAll().Where(x => x.Status != Sungero.CoreEntities.DatabookEntry.Status.Closed);

            // Поиск по ИНН, если ИНН передан.
            if (searchByTin)
            {
                var strongTinBusinessUnits = businessUnits.Where(x => x.TIN == tin).ToList();

                // Поиск по КПП, если КПП передан.
                if (searchByTrrc)
                {
                    var strongTrrcBusinessUnits = strongTinBusinessUnits
                                                  .Where(c => !string.IsNullOrWhiteSpace(c.TRRC) && c.TRRC == trrc)
                                                  .ToList();

                    if (strongTrrcBusinessUnits.Count > 0)
                    {
                        return(strongTrrcBusinessUnits);
                    }

                    return(strongTinBusinessUnits.Where(c => string.IsNullOrWhiteSpace(c.TRRC)).ToList());
                }
                return(strongTinBusinessUnits);
            }
            return(new List <IBusinessUnit>());
        }
        public virtual List <Structures.ResponsibilitiesReport.ResponsibilitiesReportTableLine> GetResponsibilitiesReportData(IEmployee employee)
        {
            var result = new List <Structures.ResponsibilitiesReport.ResponsibilitiesReportTableLine>();
            // HACK: Получаем отображаемое имя модуля.
            var moduleGuid     = new CompanyModule().Id;
            var moduleName     = Sungero.Metadata.Services.MetadataSearcher.FindModuleMetadata(moduleGuid).GetDisplayName();
            var modulePriority = Company.Constants.ResponsibilitiesReport.CompanyPriority;

            // Должность.
            result = AppendResponsibilitiesReportResult(result, employee.JobTitle, moduleName, modulePriority, Resources.Jobtitle);

            // Подразделения.
            if (Departments.AccessRights.CanRead())
            {
                var employeeDepartments = Departments.GetAll()
                                          .Where(d => d.RecipientLinks.Any(e => Equals(e.Member, employee)))
                                          .Where(d => d.Status == Sungero.CoreEntities.DatabookEntry.Status.Active);
                result = AppendResponsibilitiesReportResult(result, employeeDepartments, moduleName, modulePriority, Resources.Departments, employee.Department);
            }

            // НОР.
            if (Departments.AccessRights.CanRead() &&
                BusinessUnits.AccessRights.CanRead())
            {
                var businessUnits = Departments.GetAll()
                                    .Where(d => d.RecipientLinks.Any(e => Equals(e.Member, employee)))
                                    .Where(d => d.Status == Sungero.CoreEntities.DatabookEntry.Status.Active)
                                    .Select(b => b.BusinessUnit)
                                    .Where(b => b.Status == Sungero.CoreEntities.DatabookEntry.Status.Active).Distinct();
                result = AppendResponsibilitiesReportResult(result, businessUnits, moduleName, modulePriority, Resources.BusinessUnits, employee.Department.BusinessUnit);
            }

            // Руководитель подразделений.
            if (Departments.AccessRights.CanRead())
            {
                var managerOfDepartments = Departments.GetAll().Where(d => Equals(d.Manager, employee))
                                           .Where(d => d.Status == Sungero.CoreEntities.DatabookEntry.Status.Active);
                result = AppendResponsibilitiesReportResult(result, managerOfDepartments, moduleName, modulePriority, Resources.ManagerOfDepartmens);
            }

            // Руководители НОР.
            if (BusinessUnits.AccessRights.CanRead())
            {
                var businessUnitsCEO = BusinessUnits.GetAll().Where(b => Equals(b.CEO, employee))
                                       .Where(d => d.Status == Sungero.CoreEntities.DatabookEntry.Status.Active);
                result = AppendResponsibilitiesReportResult(result, businessUnitsCEO, moduleName, modulePriority, Resources.BusinessUnitsCEO);
            }

            // Роли.
            if (Roles.AccessRights.CanRead())
            {
                var roles = Roles.GetAll().Where(r => r.RecipientLinks.Any(e => Equals(e.Member, employee)))
                            .Where(d => d.Status == Sungero.CoreEntities.DatabookEntry.Status.Active);
                result = AppendResponsibilitiesReportResult(result, roles, moduleName, modulePriority, Resources.Roles);
            }

            // Помощники руководителей.
            if (ManagersAssistants.AccessRights.CanRead())
            {
                var managersAssistants = ManagersAssistants.GetAll()
                                         .Where(m => Equals(m.Assistant, employee) || Equals(m.Manager, employee))
                                         .Where(d => d.Status == Sungero.CoreEntities.DatabookEntry.Status.Active)
                                         .ToDictionary <IEntity, IEntity, string>(x => x,
                                                                                  x => string.Format("{0}: {1}{2}{3}: {4}",
                                                                                                     Resources.Manager,
                                                                                                     PublicFunctions.Employee.GetShortName(ManagersAssistants.As(x).Manager, false),
                                                                                                     Environment.NewLine,
                                                                                                     Resources.Assistant,
                                                                                                     PublicFunctions.Employee.GetShortName(ManagersAssistants.As(x).Assistant, false)));
                result = AppendResponsibilitiesReportResult(result, managersAssistants, moduleName, modulePriority + result.Count, Resources.ManagersAssistants);
            }

            // Замещения.
            if (Substitutions.AccessRights.CanRead())
            {
                var substitutions = Substitutions.GetAll()
                                    .Where(s => (Equals(s.Substitute, employee) ||
                                                 Equals(s.User, employee)) &&
                                           (!s.EndDate.HasValue || s.EndDate >= Calendar.UserToday))
                                    .Where(s => s.IsSystem != true)
                                    .ToDictionary <IEntity, IEntity, string>(x => x, x => CreateSubstitutionPresentation(Substitutions.As(x)));
                result = AppendResponsibilitiesReportResult(result, substitutions, moduleName, modulePriority + result.Count, Resources.Substitutions);
            }

            return(result);
        }
 public static IQueryable <IBusinessUnit> GetBusinessUnits()
 {
     return(BusinessUnits.GetAll().Where(b => b.Status == Status.Active));
 }
        /// <summary>
        /// Синхронизировать руководителя в роль "Руководители наших организаций".
        /// </summary>
        public virtual void SynchronizeCEOInRole()
        {
            var originalCEO = _obj.State.Properties.CEO.OriginalValue;
            var ceo         = _obj.CEO;

            // Добавить руководителя в роль "Руководители наших организаций".
            var ceoRole = Functions.Module.GetCEORole();

            if (ceoRole == null)
            {
                return;
            }

            if (ceo != null && ceo.IncludedIn(ceoRole) && Equals(originalCEO, ceo) &&
                _obj.State.Properties.Status.OriginalValue == _obj.Status)
            {
                return;
            }

            var ceoRoleRecipients = ceoRole.RecipientLinks;

            if (_obj.Status != CoreEntities.DatabookEntry.Status.Closed && ceo != null && !ceo.IncludedIn(ceoRole))
            {
                ceoRoleRecipients.AddNew().Member = ceo;
            }

            // Удалить руководителя из роли "Руководители наших организаций"
            // при смене или закрытии, если он не руководитель других действующих организаций.
            if (_obj.Status == CoreEntities.DatabookEntry.Status.Closed ||
                (originalCEO != null && !Equals(originalCEO, ceo) &&
                 !BusinessUnits.GetAll(c => c.Status == CoreEntities.DatabookEntry.Status.Active &&
                                       Equals(originalCEO, c.CEO) &&
                                       c.Id != _obj.Id).Any()))
            {
                while (ceoRoleRecipients.Any(r => Equals(r.Member, originalCEO)))
                {
                    ceoRoleRecipients.Remove(ceoRoleRecipients.First(r => Equals(r.Member, originalCEO)));
                }
            }

            // Исключить из роли "Руководители подразделений" нового руководителя НОР, либо
            // включить в роль "Руководители подразделений" при смене или закрытии, если он остался руководителем подразделения.
            var managerRole = Roles.GetAll(r => r.Sid == Constants.Module.DepartmentManagersRole).SingleOrDefault();

            if (managerRole != null)
            {
                var managerRoleRecipients = managerRole.RecipientLinks;
                // Исключить из роли "Руководители подразделений".
                if (_obj.Status != CoreEntities.DatabookEntry.Status.Closed && ceo != null && ceo.IncludedIn(managerRole))
                {
                    while (managerRoleRecipients.Any(r => Equals(r.Member, ceo)))
                    {
                        managerRoleRecipients.Remove(managerRoleRecipients.First(r => Equals(r.Member, ceo)));
                    }
                }

                if (originalCEO == null)
                {
                    return;
                }
                if (_obj.Status == CoreEntities.DatabookEntry.Status.Closed ||
                    (Departments.GetAll().Any(d => Equals(d.Manager, originalCEO)) &&
                     !BusinessUnits.GetAll(c => c.Status == CoreEntities.DatabookEntry.Status.Active &&
                                           Equals(originalCEO, c.CEO) &&
                                           c.Id != _obj.Id).Any()))
                {
                    // Включить в роль руководителя подразделений.
                    managerRoleRecipients.AddNew().Member = originalCEO;
                }
            }
        }