/// <summary>
        /// Проверить индекс дела на уникальность в рамках нашей организации.
        /// </summary>
        /// <param name="periodStart">Начало периода.</param>
        /// <param name="periodEnd">Конец периода.</param>
        /// <returns>True, если индекс уникален, иначе false.</returns>
        public virtual bool CheckIndexForUniquenessInPeriod(DateTime?periodStart, DateTime?periodEnd)
        {
            var originalStartDate     = periodStart;
            var hasOriginalEndDate    = periodEnd != null;
            var originalEndDate       = hasOriginalEndDate ? periodEnd : Calendar.SqlMaxValue;
            var originalIndex         = _obj.Index;
            var originalBusinessUnit  = _obj.BusinessUnit;
            var originalDepartment    = _obj.Department;
            var hasOriginalDepartment = originalDepartment != null;

            if (originalBusinessUnit == null && hasOriginalDepartment)
            {
                originalBusinessUnit = Company.PublicFunctions.BusinessUnit.Remote.GetBusinessUnit(_obj.Department);
            }
            var originalBusinessUnitDepartmentIds = new List <int>();

            if (originalBusinessUnit != null)
            {
                originalBusinessUnitDepartmentIds = Company.PublicFunctions.BusinessUnit.Remote.GetAllDepartmentIds(originalBusinessUnit);
            }

            return(!CaseFiles.GetAll().Any(f => !Equals(f, _obj) &&
                                           f.Status.Value == CoreEntities.DatabookEntry.Status.Active &&
                                           f.Index == originalIndex &&
                                           (originalBusinessUnit == null && !hasOriginalDepartment && f.BusinessUnit == null && f.Department == null ||
                                            originalBusinessUnit == null && hasOriginalDepartment && f.BusinessUnit == null && Equals(originalDepartment, f.Department) ||
                                            originalBusinessUnit != null && f.BusinessUnit != null && Equals(originalBusinessUnit, f.BusinessUnit) ||
                                            originalBusinessUnit != null && f.BusinessUnit == null && f.Department != null && originalBusinessUnitDepartmentIds.Contains(f.Department.Id)) &&
                                           originalEndDate >= f.StartDate &&
                                           (f.EndDate != null && originalStartDate <= f.EndDate ||
                                            f.EndDate == null)));
        }
        /// <summary>
        /// Получить дела для копирования.
        /// </summary>
        /// <param name="sourcePeriodStartDate">Начало исходного периода.</param>
        /// <param name="sourcePeriodEndDate">Конец исходного периода.</param>
        /// <param name="businessUnitId">ИД нашей организации.</param>
        /// <param name="departmentId">ИД подразделения.</param>
        /// <returns>Дела для копирования.</returns>
        public static List <ICaseFile> GetCaseFilesToCopy(DateTime sourcePeriodStartDate, DateTime sourcePeriodEndDate,
                                                          int businessUnitId, int departmentId)
        {
            var caseFiles = CaseFiles.GetAll()
                            .Where(x => x.Status == Docflow.CaseFile.Status.Active &&
                                   x.LongTerm != true &&
                                   x.StartDate.Value >= sourcePeriodStartDate &&
                                   x.EndDate.HasValue &&
                                   x.EndDate.Value <= sourcePeriodEndDate);

            if (businessUnitId > 0)
            {
                caseFiles = caseFiles.Where(x => x.BusinessUnit != null &&
                                            x.BusinessUnit.Id == businessUnitId);
            }

            if (departmentId > 0)
            {
                var department = Sungero.Company.Departments.Get(departmentId);
                var subordinateDepartmentIds = Company.PublicFunctions.Department.Remote.GetSubordinateDepartmentIds(department);
                caseFiles = caseFiles.Where(x => x.Department != null &&
                                            (Equals(x.Department, department) ||
                                             subordinateDepartmentIds.Contains(x.Department.Id)));
            }

            return(caseFiles.ToList());
        }