public IActionResult DownloadCompareData(int year = 0)
        {
            if (year == 0)
            {
                year = ReportingYearsHelper.GetTheMostRecentCompletedReportingYear();
            }

            var result    = CompareEmployers(year) as ViewResult;
            var viewModel = result.Model as CompareViewModel;
            IEnumerable <CompareReportModel> data = viewModel?.CompareReports;

            //Ensure we some data
            if (data == null || !data.Any())
            {
                return(new HttpNotFoundResult($"There is no employer data for year {year}"));
            }

            DataTable model = OrganisationBusinessLogic.GetCompareDatatable(data);

            //Setup the HTTP response
            var contentDisposition = new ContentDisposition {
                FileName = $"Compared GPG Data {ReportingYearsHelper.FormatYearAsReportingPeriod(year)}.csv", Inline = false
            };

            HttpContext.SetResponseHeader("Content-Disposition", contentDisposition.ToString());

            /* No Longer required as AspNetCore has response buffering on by default
             * Response.BufferOutput = true;
             */
            //Track the download
            WebTracker.TrackPageView(this, contentDisposition.FileName);

            //Return the data
            return(Content(model.ToCSV(), "text/csv"));
        }
Exemplo n.º 2
0
        public static FileContentResult GenerateEhrcAllOrganisationsForYearFile(IDataRepository dataRepository, int year)
        {
            // IMPORTANT: This variable isn't used, but running this query makes the next query much faster
            var allOrgsWithAddresses = dataRepository.GetAll <Organisation>()
                                       .Include(o => o.OrganisationAddresses)
                                       .ToList();

            // 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 allOrgsWithUserOrgs = dataRepository.GetAll <Organisation>()
                                      .Include(o => o.UserOrganisations)
                                      .ToList();

            List <Organisation> organisations = dataRepository
                                                .GetAll <Organisation>()
                                                //.Include(org => org.OrganisationAddresses) // Moved into separate pre-load query
                                                .Include(org => org.OrganisationSicCodes)
                                                //.Include(org => org.OrganisationScopes) // Moved into separate pre-load query
                                                //.Include(org => org.Returns) // Moved into separate pre-load query
                                                //.Include(org => org.UserOrganisations) // Moved into separate pre-load query
                                                .ToList();

            var records = organisations.Select(
                o => new
            {
                OrganisationId           = o.OrganisationId,
                EmployerReference        = o.EmployerReference,
                OrganisationName         = o.OrganisationName,
                CompanyNo                = o.CompanyNumber,
                Sector                   = o.SectorType,
                Status                   = o.Status,
                StatusDate               = o.StatusDate,
                StatusDetails            = o.StatusDetails,
                Address                  = o.GetLatestAddress()?.GetAddressString(),
                SicCodes                 = o.GetSicCodeIdsString(),
                LatestRegistrationDate   = o.UserOrganisations.OrderByDescending(uo => uo.Created).FirstOrDefault()?.Created,
                LatestRegistrationMethod = o.UserOrganisations.OrderByDescending(uo => uo.Created).FirstOrDefault()?.Method.ToString(),
                LatestReturn             = o.GetLatestReturn()?.Modified,
                ScopeStatus              = o.GetLatestScopeForSnapshotYear(year)?.ScopeStatus,
                ScopeDate                = o.GetLatestScopeForSnapshotYear(year)?.ScopeStatusDate,
                Created                  = o.Created,
            })
                          .ToList();

            string            fileDownloadName  = $"GPG-Organisations_{ReportingYearsHelper.FormatYearAsReportingPeriod(year)}.csv";
            FileContentResult fileContentResult = DownloadHelper.CreateCsvDownload(records, fileDownloadName);

            return(fileContentResult);
        }
Exemplo n.º 3
0
        public void CompareController_DownloadCompareData_WithYear_SameSortAsync()
        {
            // Arrange
            var routeData = new RouteData();

            routeData.Values.Add("Action", nameof(CompareController.DownloadCompareData));
            routeData.Values.Add("Controller", "Viewing");

            var controller = UiTestHelper.GetController <CompareController>(0, routeData);


            controller.CompareViewService.SortColumn    = "OrganisationSize";
            controller.CompareViewService.SortAscending = false;

            var firstReportingYear = Global.FirstReportingYear;

            var      mockOrg            = OrganisationHelper.GetOrganisationInScope("MockedOrg", firstReportingYear);
            DateTime accountingDateTime = mockOrg.SectorType.GetAccountingStartDate(firstReportingYear);

            //create the comparison data
            var expectedModel = ViewingServiceHelper.GetCompareTestData(5).ToList();

            //Setup the mocked business logic
            var mockOrgBL = new Mock <IOrganisationBusinessLogic>();

            mockOrgBL
            .Setup(x => x.GetCompareData(It.IsAny <IEnumerable <string> >(), It.IsAny <int>(), It.IsAny <string>(), It.IsAny <bool>()))
            .Returns(expectedModel);
            var expectedData = expectedModel.ToDataTable();

            mockOrgBL
            .Setup(x => x.GetCompareDatatable(It.IsAny <IEnumerable <CompareReportModel> >()))
            .Returns(expectedData);
            controller.OrganisationBusinessLogic = mockOrgBL.Object;

            // Act
            var result = controller.DownloadCompareData(firstReportingYear) as ContentResult;

            // Assert

            //Test the google analytics tracker was executed once on the controller
            var filename = $"Compared GPG Data {ReportingYearsHelper.FormatYearAsReportingPeriod(firstReportingYear)}.csv";

            controller.WebTracker.GetMockFromObject().Verify(mock => mock.TrackPageView(It.IsAny <Controller>(), filename, null), Times.Once());

            Assert.NotNull(result);
            Assert.AreEqual(result.ContentType, "text/csv");
            Assert.AreEqual(controller.Response.Headers["Content-Disposition"], $"attachment; filename=\"{filename}\"");

            Assert.AreEqual(controller.CompareViewService.SortColumn, "OrganisationSize");
            Assert.AreEqual(controller.CompareViewService.SortAscending, false);

            Assert.NotNull(result.Content);
            Assert.AreEqual(result.Content, expectedData.ToCSV());
        }
Exemplo n.º 4
0
 public string GetReportingPeriod()
 {
     return(ReportingYearsHelper.FormatYearAsReportingPeriod(AccountingDate.Year, "/"));
 }
 public object GetFormattedYearText()
 {
     return(ReportingYearsHelper.FormatYearAsReportingPeriod(ReportingYear));
 }