public List <ManageOrganisationDetailsForYearViewModel> GetOrganisationDetailsForYears()
        {
            List <int> reportingYears  = ReportingYearsHelper.GetReportingYears();
            var        detailsForYears = new List <ManageOrganisationDetailsForYearViewModel>();

            foreach (int reportingYear in reportingYears)
            {
                // Get organisation's scope for given reporting year
                var scopeForYear = Organisation.GetScopeForYear(reportingYear);

                if (scopeForYear != null)
                {
                    detailsForYears.Add(
                        new ManageOrganisationDetailsForYearViewModel(
                            Organisation,
                            reportingYear,
                            allDraftReturns.Where(d => d.SnapshotYear == reportingYear)
                            .OrderByDescending(d => d.Modified)
                            .FirstOrDefault()
                            )
                        );
                }
            }

            return(detailsForYears);
        }
 public static void ThrowIfReportingYearIsOutsideOfRange(int reportingYear)
 {
     if (!ReportingYearsHelper.GetReportingYears().Contains(reportingYear))
     {
         throw new PageNotFoundException();
     }
 }
示例#3
0
        public IActionResult Downloads()
        {
            var viewModel = new AdminDownloadsViewModel
            {
                ReportingYears = ReportingYearsHelper.GetReportingYears()
            };

            return(View("Downloads", viewModel));
        }
        private void UpdateDownloadFilesAction()
        {
            CustomLogger.Information($"UpdateDownloadFiles: Loading Organisations");
            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            List <Organisation> activeOrganisations = dataRepository.GetAll <Organisation>()
                                                      .Where(o => o.Status == OrganisationStatuses.Active)
                                                      .Include(o => o.OrganisationNames)
                                                      .Include(o => o.OrganisationAddresses)
                                                      .Include(o => o.OrganisationSicCodes)
                                                      .ToList();

            CustomLogger.Information($"UpdateDownloadFiles: Loading Returns");
            List <Return> allReturns = dataRepository.GetAll <Return>()
                                       .Where(r => r.Organisation.Status == OrganisationStatuses.Active)
                                       .Include(r => r.Organisation)
                                       .ToList();

            CustomLogger.Information($"UpdateDownloadFiles: Creating downloads for each year");
            foreach (int year in ReportingYearsHelper.GetReportingYears())
            {
                CustomLogger.Information($"UpdateDownloadFiles: Creating download for year {year}");

                CustomLogger.Information($"UpdateDownloadFiles: - Filtering Returns");
                List <Return> returns = allReturns
                                        .Where(r => r.AccountingDate.Year == year)
                                        .Where(r => r.Status == ReturnStatuses.Submitted)
                                        .ToList();

                CustomLogger.Information($"UpdateDownloadFiles: - Converting Returns into results");
                List <DownloadResult> downloadData = returns.ToList()
                                                     .Select(r => r.ToDownloadResult())
                                                     .OrderBy(d => d.EmployerName)
                                                     .ToList();

                CustomLogger.Information($"UpdateDownloadFiles: - Saving results to file");
                string filePath = Path.Combine(Global.DownloadsLocation, $"GPGData_{year}-{year + 1}.csv");

                try
                {
                    SaveCsvFile(downloadData, filePath);
                }
                catch (Exception ex)
                {
                    CustomLogger.Error(ex.Message, new { Error = ex });
                }

                CustomLogger.Information($"UpdateDownloadFiles: Done for year {year}");
            }

            CustomLogger.Information($"UpdateDownloadFiles: Done");
        }
示例#5
0
        public List <DateTime> GetDatesOfLatestReports(List <int> years)
        {
            if (years.IsNullOrEmpty())
            {
                years = ReportingYearsHelper.GetReportingYears();
            }

            var latestReports = new List <DateTime>();

            foreach (var year in years)
            {
                if (ReportingYearToDateOfLatestReportMap.TryGetValue(year, out DateTime latestDate))
                {
                    latestReports.Add(latestDate);
                }
            }

            return(latestReports);
        }
示例#6
0
        public List <OrganisationSizes> GetOrganisationSizes(List <int> years)
        {
            if (years.IsNullOrEmpty())
            {
                years = ReportingYearsHelper.GetReportingYears();
            }

            var organisationSizes = new List <OrganisationSizes>();

            foreach (var year in years)
            {
                if (ReportingYearToOrganisationSizesMap.TryGetValue(year, out List <OrganisationSizes> sizes))
                {
                    organisationSizes = organisationSizes.Union(sizes).ToList();
                }
            }

            return(organisationSizes);
        }
示例#7
0
        private int GetRandomReportingYear(bool ignoreYearsExcludedFromLateFlagEnforcement = false)
        {
            var reportingYears = ReportingYearsHelper.GetReportingYears();

            if (ignoreYearsExcludedFromLateFlagEnforcement)
            {
                reportingYears = ReportingYearsHelper.GetReportingYears()
                                 .Except(reportingStartYearsToExcludeFromLateFlagEnforcement)
                                 .ToList();
            }

            var random = new Random();
            int index  = random.Next(reportingYears.Count);

            var reportingYear = reportingYears[index];

            Console.WriteLine("Testing Reporting Year: " + reportingYear);

            return(reportingYear);
        }
示例#8
0
        public List <ManageOrganisationDetailsForYearViewModel> GetOrganisationDetailsForYears()
        {
            List <int> reportingYears  = ReportingYearsHelper.GetReportingYears();
            var        detailsForYears = new List <ManageOrganisationDetailsForYearViewModel>();

            foreach (int reportingYear in reportingYears)
            {
                // Get organisation's scope for given reporting year
                var scopeForYear = Organisation.GetScopeForYear(reportingYear);

                if (scopeForYear != null)
                {
                    detailsForYears.Add(new ManageOrganisationDetailsForYearViewModel(
                                            Organisation,
                                            reportingYear,
                                            yearsWithDraftReturns.Contains(reportingYear)
                                            ));
                }
            }

            return(detailsForYears);
        }
示例#9
0
        private static IEnumerable <dynamic> BuildOrganisationsWithNoSubmittedReturnsRecords(IEnumerable <Organisation> organisationsWithNoSubmittedReturns)
        {
            return(organisationsWithNoSubmittedReturns.Select(
                       org =>
            {
                dynamic record = new ExpandoObject();
                record.OrganisationId = org.OrganisationId;
                record.EmployerReference = org.EmployerReference;
                record.OrganisationName = org.OrganisationName;
                record.CompanyNumber = org.CompanyNumber;
                record.SectorType = org.SectorType;
                record.Address = org.GetLatestAddress()?.GetAddressString();

                UserOrganisation latestUserOrg = org.UserOrganisations
                                                 .OrderByDescending(uo => uo.Modified)
                                                 .FirstOrDefault(
                    uo => uo.HasBeenActivated() &&
                    uo.User.Status == UserStatuses.Active);
                Return latestReturn = org.Returns
                                      .OrderByDescending(r => r.StatusDate)
                                      .FirstOrDefault(r => r.Status == ReturnStatuses.Submitted);

                record.Size = latestReturn?.OrganisationSize.GetAttribute <DisplayAttribute>().Name;
                record.FirstName = latestUserOrg?.User.Firstname;
                record.LastName = latestUserOrg?.User.Lastname;
                record.JobTitle = latestUserOrg?.User.JobTitle;
                record.PhoneNumber = latestUserOrg?.User.ContactPhoneNumber;
                record.EmailAddress = latestUserOrg?.User.EmailAddress;

                foreach (var repYear in ReportingYearsHelper.GetReportingYears().ToSortedSet())
                {
                    ((IDictionary <string, object>)record)["ReportDateTimeFor" + repYear] = org.GetReturn(repYear)?.StatusDate;
                }

                return record;
            })
                   .ToList());
        }
示例#10
0
        private static List <SearchCachedOrganisation> LoadAllOrganisations(IDataRepository repository)
        {
            DateTime start = VirtualDateTime.Now;

            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            var allOrgsWithNames = repository.GetAll <Organisation>()
                                   .Include(o => o.OrganisationNames)
                                   .ToList();

            CustomLogger.Information($"Search Repository: Time taken to load Names: {VirtualDateTime.Now.Subtract(start).TotalSeconds} seconds");

            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            var allOrgsWithReturns = repository.GetAll <Organisation>()
                                     .Include(o => o.Returns)
                                     .ToList();

            CustomLogger.Information($"Search Repository: Time taken to load Returns: {VirtualDateTime.Now.Subtract(start).TotalSeconds} seconds");

            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            var allOrgsWithScopes = repository.GetAll <Organisation>()
                                    .Include(o => o.OrganisationScopes)
                                    .ToList();

            CustomLogger.Information($"Search Repository: Time taken to load Scopes: {VirtualDateTime.Now.Subtract(start).TotalSeconds} seconds");

            List <Organisation> allOrganisations = repository
                                                   .GetAll <Organisation>()
                                                   //.Include(o => o.OrganisationNames) // Moved into separate pre-load query
                                                   //.Include(o => o.Returns) // Moved into separate pre-load query
                                                   //.Include(o => o.OrganisationScopes) // Moved into separate pre-load query
                                                   .Include(o => o.OrganisationSicCodes)
                                                   .ThenInclude(osc => osc.SicCode)
                                                   .ThenInclude(sc => sc.SicSection)
                                                   .ToList();

            CustomLogger.Information($"Search Repository: Time taken to load Organisations: {VirtualDateTime.Now.Subtract(start).TotalSeconds} seconds");

            List <SearchCachedOrganisation> searchCachedOrganisations = allOrganisations
                                                                        .Select(
                o =>
            {
                var sicCodeSynonyms = o.OrganisationSicCodes.Select(osc => osc.SicCode.Synonyms)
                                      .Where(s => s != null)
                                      .Select(s => new SearchReadyValue(s))
                                      .ToList();

                foreach (var osc in o.OrganisationSicCodes)
                {
                    sicCodeSynonyms.Add(new SearchReadyValue(osc.SicCode.Description));
                }

                var submittedReports = o.Returns.Where(r => r.Status == ReturnStatuses.Submitted).ToList();

                return(new SearchCachedOrganisation
                {
                    OrganisationId = o.OrganisationId,
                    EncryptedId = o.GetEncryptedId(),
                    OrganisationName = new SearchReadyValue(o.OrganisationName),
                    CompanyNumber = o.CompanyNumber?.Trim(),
                    EmployerReference = o.EmployerReference?.Trim(),
                    OrganisationNames =
                        o.OrganisationNames.OrderByDescending(n => n.Created)
                        .Select(on => new SearchReadyValue(on.Name))
                        .ToList(),
                    MinEmployees = o.GetLatestReturn()?.MinEmployees ?? 0,
                    Status = o.Status,
                    ReportingYearToOrganisationSizesMap = submittedReports
                                                          .GroupBy(r => r.AccountingDate.Year)
                                                          .ToDictionary(
                        g => g.Key,
                        g => g.ToList().Select(r => r.OrganisationSize).Distinct().ToList()),
                    SicSectionIds =
                        o.OrganisationSicCodes.Select(osc => Convert.ToChar(osc.SicCode.SicSection.SicSectionId)).ToList(),
                    ReportingYears = submittedReports.Select(r => r.AccountingDate.Year).ToList(),
                    ReportingYearToDateOfLatestReportMap = ReportingYearsHelper.GetReportingYears()
                                                           .ToDictionary(
                        y => y,
                        y => o.GetReturn(y) != null ? o.GetReturn(y).StatusDate.Date : new DateTime(1999, 1, 1)),
                    ReportedWithCompanyLinkYears = submittedReports.Where(r => !r.CompanyLinkToGPGInfo.IsNullOrEmpty())
                                                   .Select(r => r.AccountingDate.Year)
                                                   .ToList(),
                    ReportedLateYears = submittedReports.Where(r => r.IsLateSubmission).Select(r => r.AccountingDate.Year).ToList(),
                    SicCodeIds = o.OrganisationSicCodes.Select(osc => osc.SicCode.SicCodeId.ToString()).ToList(),
                    SicCodeSynonyms = sicCodeSynonyms,
                    IncludeInViewingService = GetIncludeInViewingService(o),
                    Sector = o.SectorType
                });
            })
                                                                        .ToList();

            CustomLogger.Information($"Search Repository: Time taken to convert Organisations into SearchCachedOrganisations: {VirtualDateTime.Now.Subtract(start).TotalSeconds} seconds");

            return(searchCachedOrganisations);
        }
示例#11
0
 private static List <int> GetActiveReportingYears(SectorTypes sectorType)
 {
     return(ReportingYearsHelper.GetReportingYears()
            .Where(year => GetDeadlineDateForReportingYear(sectorType, year) > VirtualDateTime.Now)
            .ToList());
 }
        public IActionResult ScopesAndReturns(string password)
        {
            string expectedPassword = Global.GpgAnalysisAppApiPassword;

            if (string.IsNullOrWhiteSpace(expectedPassword))
            {
                throw new ArgumentException("GpgAnalysisAppApiPassword must be set to enable the API");
            }
            if (password != expectedPassword)
            {
                return(Unauthorized());
            }

            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            var allOrgsWithScopes = dataRepository.GetAll <Organisation>()
                                    .Include(o => o.OrganisationScopes)
                                    .ToList();

            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            var allOrgsWithReturns = dataRepository.GetAll <Organisation>()
                                     .Include(o => o.Returns)
                                     .ToList();

            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            var allOrgsWithPublicSectorTypes = dataRepository.GetAll <Organisation>()
                                               .Include(o => o.LatestPublicSectorType)
                                               .ToList();

            List <Organisation> organisations = dataRepository
                                                .GetAll <Organisation>()
                                                //.Include(o => o.OrganisationScopes) // Moved into separate pre-load query
                                                //.Include(o => o.Returns) // Moved into separate pre-load query
                                                //.Include(o => o.LatestPublicSectorType) // Moved into separate pre-load query
                                                .Include(o => o.OrganisationSicCodes)
                                                .ThenInclude(osc => osc.SicCode)
                                                .ThenInclude(sc => sc.SicSection)
                                                .ToList();

            List <int> reportingYears = ReportingYearsHelper.GetReportingYears();

            var records = new List <object>();

            foreach (Organisation organisation in organisations)
            {
                foreach (int reportingYear in reportingYears)
                {
                    OrganisationScope scopeForYear = organisation.GetLatestScopeForSnapshotYear(reportingYear);

                    if (scopeForYear != null)
                    {
                        Return returnForYear = organisation.GetReturn(reportingYear);

                        // Organisations are allowed multiple SIC codes
                        // But, for this API, we just want 1 SIC code per organisation
                        // We use this method to keep this API consistent with the database query
                        OrganisationSicCode firstSicCodeForOrganisation = organisation.OrganisationSicCodes
                                                                          .Where(osc => !osc.IsRetired())
                                                                          .OrderBy(osc => osc.SicCodeId)
                                                                          .ThenBy(osc => osc.Source)
                                                                          .FirstOrDefault();

                        var record = new
                        {
                            OrganisationId            = organisation.OrganisationId,
                            OrganisationName          = organisation.OrganisationName,
                            EmployerReference         = organisation.EmployerReference,
                            CompanyNumber             = organisation.CompanyNumber,
                            OrganisationStatus        = $"{organisation.Status.ToString()} ({(int)organisation.Status})",
                            SectorType                = $"{organisation.SectorType.ToString()} ({(int)organisation.SectorType})",
                            ScopeStatus               = $"{ScopeStatusToString(scopeForYear.ScopeStatus)} ({(int)scopeForYear.ScopeStatus})",
                            SnapshotDate              = reportingYear,
                            SicCodeSectionDescription = firstSicCodeForOrganisation?.SicCode?.SicSection?.Description,
                            ReturnId = returnForYear?.ReturnId,
                            PublicSectorDescription = organisation.LatestPublicSectorType?.PublicSectorType?.Description,
                            OrganisationSize        = returnForYear?.OrganisationSize.GetAttribute <DisplayAttribute>().Name
                        };

                        records.Add(record);
                    }
                }
            }

            return(Json(records));
        }