Пример #1
0
        private void SendReminderEmailsForSectorType(
            User user,
            List <Organisation> inScopeOrganisationsThatStillNeedToReport,
            SectorTypes sectorType)
        {
            var organisationsOfSectorType = inScopeOrganisationsThatStillNeedToReport
                                            .Where(o => o.SectorType == sectorType)
                                            .ToList();

            if (organisationsOfSectorType.Count > 0)
            {
                if (IsAfterEarliestReminder(sectorType) &&
                    ReminderEmailWasNotSentAfterLatestReminderDate(user, sectorType))
                {
                    try
                    {
                        SendReminderEmail(user, sectorType, organisationsOfSectorType);
                    }
                    catch (Exception ex)
                    {
                        _CustomLogger.Error(
                            "Failed whilst sending or saving reminder email",
                            new
                        {
                            user.UserId,
                            SectorType      = sectorType,
                            OrganisationIds =
                                inScopeOrganisationsThatStillNeedToReport.Select(o => o.OrganisationId),
                            Exception = ex.Message
                        });
                    }
                }
            }
        }
Пример #2
0
 private DateTime GetLatestReminderEmailDate(SectorTypes sectorType)
 {
     return(GetReminderDates(sectorType)
            .Where(reminderDate => reminderDate < VirtualDateTime.Now)
            .OrderBy(reminderDate => reminderDate)
            .FirstOrDefault());
 }
Пример #3
0
        private List <DateTime> GetReminderDates(SectorTypes sectorType)
        {
            var deadlineDate = GetDeadlineDate(sectorType);

            return(_SharedBusinessLogic.SharedOptions.ReminderEmailDays
                   .Select(reminderDay => deadlineDate.AddDays(-reminderDay)).ToList());
        }
        private void SendReminderEmailsForSectorType(SectorTypes sector, string runId, DateTime startTime)
        {
            if (IsAfterEarliestReminder(sector))
            {
                DateTime latestReminderEmailDate = GetLatestReminderEmailDate(sector);

                IEnumerable <User> usersUncheckedSinceLatestReminderDate = dataRepository.GetAll <User>()
                                                                           .Where(user => !user.ReminderEmails
                                                                                  .Where(re => re.SectorType == sector)
                                                                                  .Where(re => re.DateChecked > latestReminderEmailDate)
                                                                                  .Any());

                foreach (User user in usersUncheckedSinceLatestReminderDate)
                {
                    if (VirtualDateTime.Now > startTime.AddMinutes(59))
                    {
                        var endTime = VirtualDateTime.Now;
                        CustomLogger.Information($"Function finished: {nameof(SendReminderEmails)}. Hit timeout break.",
                                                 new
                        {
                            runId,
                            environment = Config.EnvironmentName,
                            endTime,
                            TimeTakenInSeconds = (endTime - startTime).TotalSeconds
                        });
                        break;
                    }

                    CheckUserAndSendReminderEmailsForSectorType(user, sector);
                }
            }
        }
        private static List <DateTime> GetReminderDates(SectorTypes sectorType)
        {
            List <int> reminderDays = GetReminderEmailDays();
            DateTime   deadlineDate = GetDeadlineDate(sectorType);

            return(reminderDays.Select(reminderDay => deadlineDate.AddDays(-reminderDay)).ToList());
        }
 private void SendReminderEmailsForSectorType(
     User user,
     List <Organisation> inScopeOrganisationsForUserAndSectorTypeThatStillNeedToReport,
     SectorTypes sectorType)
 {
     try
     {
         bool anyOrganisationsToEmailAbout = inScopeOrganisationsForUserAndSectorTypeThatStillNeedToReport.Count > 0;
         if (anyOrganisationsToEmailAbout)
         {
             SendReminderEmail(user, sectorType, inScopeOrganisationsForUserAndSectorTypeThatStillNeedToReport);
         }
         SaveReminderEmailRecord(user, sectorType, anyOrganisationsToEmailAbout);
     }
     catch (Exception ex)
     {
         CustomLogger.Error(
             "Failed whilst sending or saving reminder email",
             new
         {
             user.UserId,
             SectorType      = sectorType,
             OrganisationIds = inScopeOrganisationsForUserAndSectorTypeThatStillNeedToReport.Select(o => o.OrganisationId),
             Exception       = ex.Message
         });
     }
 }
Пример #7
0
        public void PreservesDeclaredScopes(SectorTypes testSectorType,
                                            ScopeStatuses testDeclaredScopeStatus,
                                            ScopeStatuses expectedPresumedScopeStatus)
        {
            // setup
            var testCreatedDate =
                mockSharedBusinessLogic.GetAccountingStartDate(testSectorType,
                                                               ConfigHelpers.SharedOptions.FirstReportingYear);
            var testOrg = CreateOrgWithDeclaredAndPresumedScopes(
                testSectorType,
                testDeclaredScopeStatus,
                testCreatedDate,
                testCreatedDate);

            // act
            var actualChanged = scopeBusinessLogic.FillMissingScopes(testOrg);

            // assert
            Assert.IsTrue(actualChanged, "Expected change to be true for missing scopes");

            var actualScopesArray = testOrg.OrganisationScopes.ToArray();

            Assert.AreEqual(testDeclaredScopeStatus, actualScopesArray[0].ScopeStatus,
                            "Expected first year scope status to match");

            // check that each year is presumed out of scope after first year
            for (var i = 1; i < actualScopesArray.Length; i++)
            {
                var scope = actualScopesArray[i];
                Assert.AreEqual(expectedPresumedScopeStatus, scope.ScopeStatus,
                                "Expected presumed scope statuses to match");
            }
        }
Пример #8
0
        /// <summary>
        ///     Returns the accounting start date for the specified sector and year
        /// </summary>
        /// <param name="sectorType">The sector type of the organisation</param>
        /// <param name="year">The starting year of the accounting period. If 0 then uses current accounting period</param>
        public static DateTime GetAccountingStartDate(this SectorTypes sectorType, int year = 0)
        {
            var tempDay   = 0;
            var tempMonth = 0;

            DateTime now = VirtualDateTime.Now;

            switch (sectorType)
            {
            case SectorTypes.Private:
                tempDay   = Global.PrivateAccountingDate.Day;
                tempMonth = Global.PrivateAccountingDate.Month;
                break;

            case SectorTypes.Public:
                tempDay   = Global.PublicAccountingDate.Day;
                tempMonth = Global.PublicAccountingDate.Month;
                break;

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(sectorType),
                          sectorType,
                          "Cannot calculate accounting date for this sector type");
            }

            if (year == 0)
            {
                year = now.Year;
            }

            var tempDate = new DateTime(year, tempMonth, tempDay);

            return(now > tempDate ? tempDate : tempDate.AddYears(-1));
        }
        public void PresumesOutOfScopeForSnapshotYearsBeforeOrgCreatedDate(SectorTypes testSectorType)
        {
            // setup
            Organisation testOrg = CreateOrgWithNoScopes(1, testSectorType, VirtualDateTime.Now);

            // act
            bool actualChanged = scopeBusinessLogic.FillMissingScopes(testOrg);

            // assert
            Assert.IsTrue(actualChanged, "Expected change for missing scopes");

            // test the count of scopes set is correct
            DateTime currentSnapshotDate = testOrg.SectorType.GetAccountingStartDate();
            int      expectedScopeCount  = (currentSnapshotDate.Year - Global.FirstReportingYear) + 1;

            Assert.AreEqual(expectedScopeCount, testOrg.OrganisationScopes.Count);

            // check each scope before current snapshot year are set to presumed out of scope
            OrganisationScope[] actualScopesArray = testOrg.OrganisationScopes.ToArray();
            for (var i = 0; i < actualScopesArray.Length - 1; i++)
            {
                OrganisationScope scope = actualScopesArray[i];
                Assert.AreEqual(ScopeStatuses.PresumedOutOfScope, scope.ScopeStatus);
            }

            // assert current year is presumed in scope
            Assert.AreEqual(ScopeStatuses.PresumedInScope, actualScopesArray[actualScopesArray.Length - 1].ScopeStatus);
        }
Пример #10
0
 public static OrganisationScope GetOrganisationScope(int snapshotYear, SectorTypes organisationSectorType)
 {
     return(new OrganisationScope {
         SnapshotDate = SectorTypeHelper.GetSnapshotDateForSector(snapshotYear, organisationSectorType),
         Status = ScopeRowStatuses.Active
     });
 }
Пример #11
0
 private static DateTime GetLatestReminderEmailDateForReportingYear(SectorTypes sectorType, int year)
 {
     return(GetReminderDatesForReportingYear(sectorType, year)
            .Where(reminderDate => reminderDate < VirtualDateTime.Now)
            .OrderByDescending(reminderDate => reminderDate)
            .FirstOrDefault());
 }
Пример #12
0
        private static List <DateTime> GetReminderDatesForReportingYear(SectorTypes sectorType, int year)
        {
            List <int> reminderDays = GetReminderEmailDays();
            DateTime   deadlineDate = GetDeadlineDateForReportingYear(sectorType, year);

            return(reminderDays.Select(reminderDay => deadlineDate.AddDays(-reminderDay)).ToList());
        }
        public void ReturnsFalseForHistoricYears(SectorTypes testSector)
        {
            // Arrange
            DateTime testSnapshotDate            = testSector.GetAccountingStartDate();
            int      testHistoricYear            = testSnapshotDate.Year;
            var      expectCalledGetSnapshotDate = false;

            // Mocks
            var mockService = new Mock <WebUI.Classes.Services.SubmissionService>(
                mockDataRepo.Object,
                mockScopeBL.Object,
                mockDraftFileBL.Object);

            mockService.CallBase = true;

            // Override GetPreviousReportingStartDate and return expectedYear
            mockService.Setup(ss => ss.GetSnapshotDate(It.IsIn(testSector), It.IsAny <int>()))
            .Returns(
                () => {
                expectCalledGetSnapshotDate = true;
                return(testSnapshotDate);
            });

            // Assert
            WebUI.Classes.Services.SubmissionService testService = mockService.Object;
            bool actual = testService.IsHistoricSnapshotYear(testSector, testHistoricYear);

            Assert.IsTrue(expectCalledGetSnapshotDate, "Expected to call GetSnapshotDate");
            Assert.IsFalse(actual, "Expected IsHistoricSnapshotYear to return false");
        }
Пример #14
0
        private Organisation CreateOrgWithDeclaredAndPresumedScopes(
            SectorTypes testSector,
            ScopeStatuses testDeclaredScopeStatus,
            DateTime testCreated,
            DateTime testSnapshotDate)
        {
            var testOrg = CreateOrgWithNoScopes(1, testSector, testCreated);

            testOrg.OrganisationScopes.Add(
                new OrganisationScope
            {
                OrganisationScopeId = 1,
                Status       = ScopeRowStatuses.Active,
                SnapshotDate = testSnapshotDate,
                ScopeStatus  = testDeclaredScopeStatus
            });

            testOrg.OrganisationScopes.Add(
                new OrganisationScope
            {
                OrganisationScopeId = 2,
                Status       = ScopeRowStatuses.Active,
                SnapshotDate = testSnapshotDate.AddYears(1),
                ScopeStatus  = testDeclaredScopeStatus == ScopeStatuses.InScope
                        ? ScopeStatuses.PresumedInScope
                        : ScopeStatuses.PresumedOutOfScope
            });

            return(testOrg);
        }
Пример #15
0
        //Current account Year method
        public DateTime GetAccountYearStartDate(SectorTypes sectorType, int year = 0)
        {
            var tempDay   = 0;
            var tempMonth = 0;

            var now = DateTime.Now;

            switch (sectorType)
            {
            case SectorTypes.Private:
                tempDay   = Settings.Default.PrivateAccountingDate.Day;
                tempMonth = Settings.Default.PrivateAccountingDate.Month;
                break;

            case SectorTypes.Public:
                tempDay   = Settings.Default.PublicAccountingDate.Day;
                tempMonth = Settings.Default.PublicAccountingDate.Month;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(sectorType), sectorType, "Cannot calculate accounting date for this sector type");
            }

            var tempDate = new DateTime(year == 0 ? DateTime.Now.Year : year, tempMonth, tempDay);

            return(now > tempDate ? tempDate : tempDate.AddYears(-1));
        }
Пример #16
0
 private Organisation CreateOrgWithMissingScopesForAllYears(int testOrgId, SectorTypes testSector)
 {
     return(new Organisation
     {
         OrganisationId = testOrgId, SectorType = testSector, Status = OrganisationStatuses.Active
     });
 }
        public void ReturnsFalseWhenNotCurrentYear(SectorTypes testSector, int testYear)
        {
            // Arrange
            DateTime testSnapshotDate            = mockSharedBusinessLogic.GetAccountingStartDate(testSector);
            var      expectCalledGetSnapshotDate = false;

            // Mocks
            var mockService = new Mock <SubmissionPresenter>(
                mockDataRepo.Object,
                mockScopeBL.Object,
                mockFileRepo.Object,
                mockDraftFileBL.Object,
                null);

            mockService.CallBase = true;

            // Override GetPreviousReportingStartDate and return expectedYear
            mockService.Setup(ss => ss.GetSnapshotDate(It.IsIn(testSector), It.IsAny <int>()))
            .Returns(
                () => {
                expectCalledGetSnapshotDate = true;
                return(testSnapshotDate);
            });

            // Assert
            SubmissionPresenter testService = mockService.Object;
            bool actual = testService.IsCurrentSnapshotYear(testSector, testYear);

            Assert.IsTrue(expectCalledGetSnapshotDate, "Expected to call GetSnapshotDate");
            Assert.IsFalse(actual, "Expected IsCurrentSnapshotYear to return true");
        }
        public void PresumesInScopeForSnapshotYearsDuringAndAfterOrgCreatedDate(SectorTypes testSectorType)
        {
            // setup
            DateTime     testCreatedDate = testSectorType.GetAccountingStartDate().AddYears(-1);
            Organisation testOrg         = CreateOrgWithNoScopes(1, testSectorType, testCreatedDate);

            // act
            bool actualChanged = scopeBusinessLogic.FillMissingScopes(testOrg);

            // assert
            Assert.IsTrue(actualChanged, "Expected change for missing scopes");

            // test the count of scopes set is correct
            DateTime currentSnapshotDate = testOrg.SectorType.GetAccountingStartDate();
            int      expectedScopeCount  = (currentSnapshotDate.Year - Global.FirstReportingYear) + 1;

            Assert.AreEqual(expectedScopeCount, testOrg.OrganisationScopes.Count);

            // check each scope after created date is set to presumed in of scope
            OrganisationScope[] actualScopesArray = testOrg.OrganisationScopes.ToArray();
            for (int i = actualScopesArray.Length - 2; i < actualScopesArray.Length; i++)
            {
                OrganisationScope scope = actualScopesArray[i];
                Assert.AreEqual(ScopeStatuses.PresumedInScope, scope.ScopeStatus);
            }
        }
Пример #19
0
        public void PresumesInScopeForSnapshotYearsDuringAndAfterOrgCreatedDate(SectorTypes testSectorType)
        {
            // setup
            var testCreatedDate = mockSharedBusinessLogic.GetAccountingStartDate(testSectorType).AddYears(-1);
            var testOrg         = CreateOrgWithNoScopes(1, testSectorType, testCreatedDate);

            // act
            var actualChanged = scopeBusinessLogic.FillMissingScopes(testOrg);

            // assert
            Assert.IsTrue(actualChanged, "Expected change for missing scopes");

            // test the count of scopes set is correct
            var currentSnapshotDate = mockSharedBusinessLogic.GetAccountingStartDate(testOrg.SectorType);
            var expectedScopeCount  = currentSnapshotDate.Year - ConfigHelpers.SharedOptions.FirstReportingYear + 1;

            Assert.AreEqual(expectedScopeCount, testOrg.OrganisationScopes.Count);

            // check each scope after created date is set to presumed in of scope
            var actualScopesArray = testOrg.OrganisationScopes.ToArray();

            for (var i = actualScopesArray.Length - 2; i < actualScopesArray.Length; i++)
            {
                var scope = actualScopesArray[i];
                Assert.AreEqual(ScopeStatuses.PresumedInScope, scope.ScopeStatus);
            }

            Assert.NotNull(testOrg.LatestScope, "Expected latest scope to be set");
        }
Пример #20
0
        public void PresumesOutOfScopeForSnapshotYearsBeforeOrgCreatedDate(SectorTypes testSectorType)
        {
            // setup
            var testOrg = CreateOrgWithNoScopes(1, testSectorType, VirtualDateTime.Now);

            // act
            var actualChanged = scopeBusinessLogic.FillMissingScopes(testOrg);

            // assert
            Assert.IsTrue(actualChanged, "Expected change for missing scopes");

            // test the count of scopes set is correct
            var currentSnapshotDate = mockSharedBusinessLogic.GetAccountingStartDate(testOrg.SectorType);
            var expectedScopeCount  = currentSnapshotDate.Year - ConfigHelpers.SharedOptions.FirstReportingYear + 1;

            Assert.AreEqual(expectedScopeCount, testOrg.OrganisationScopes.Count);

            // check each scope before current snapshot year are set to presumed out of scope
            var actualScopesArray = testOrg.OrganisationScopes.ToArray();

            for (var i = 0; i < actualScopesArray.Length - 1; i++)
            {
                var scope = actualScopesArray[i];
                Assert.AreEqual(ScopeStatuses.PresumedOutOfScope, scope.ScopeStatus);
            }

            // assert current year is presumed in scope
            Assert.AreEqual(ScopeStatuses.PresumedInScope, actualScopesArray[actualScopesArray.Length - 1].ScopeStatus);
            Assert.NotNull(testOrg.LatestScope, "Expected latest scope to be set");
        }
Пример #21
0
        private Organisation CreateOrgWithScopeForAllYears(int testOrgId,
                                                           SectorTypes testSector,
                                                           ScopeStatuses testScopeStatus,
                                                           DateTime snapshotDate)
        {
            var firstYear = SectorTypeHelper.SnapshotDateHelper.FirstReportingYear;
            var lastYear  = SectorTypeHelper.SnapshotDateHelper.CurrentSnapshotYear;

            var testOrg = CreateOrgWithNoScopes(testOrgId, testSector, VirtualDateTime.Now);

            // for all snapshot years check if scope exists
            for (var year = firstYear; year < lastYear; year++)
            {
                testOrg.OrganisationScopes.Add(
                    new OrganisationScope
                {
                    OrganisationScopeId = 1,
                    Status       = ScopeRowStatuses.Active,
                    SnapshotDate = new DateTime(year, snapshotDate.Month, snapshotDate.Day),
                    ScopeStatus  = testScopeStatus
                });
            }

            return(testOrg);
        }
Пример #22
0
        private void ChangeSector(AdminChangeSectorViewModel viewModel, long organisationId)
        {
            var organisation = dataRepository.Get <Organisation>(organisationId);

            SectorTypes previousSector = organisation.SectorType;
            SectorTypes newSector      = viewModel.NewSector.Value == NewSectorTypes.Private ? SectorTypes.Private : SectorTypes.Public;

            // Update the sector
            organisation.SectorType = newSector;

            // Remove SIC codes when company changes between sectors
            organisation.OrganisationSicCodes.Clear();

            // Change snapshot date for all organisation scopes to match new sector
            organisation.OrganisationScopes.ForEach(
                scope => scope.SnapshotDate = organisation.SectorType.GetAccountingStartDate(scope.SnapshotDate.Year)
                );

            // Change accounting date for all returns to match new sector
            organisation.Returns.ForEach(
                returnItem => returnItem.AccountingDate = organisation.SectorType.GetAccountingStartDate(returnItem.AccountingDate.Year)
                );

            dataRepository.SaveChanges();

            // Audit log
            auditLogger.AuditChangeToOrganisation(
                AuditedAction.AdminChangedOrganisationSector,
                organisation,
                new AdminChangeSectorAuditLogDetails {
                OldSector = previousSector, NewSector = newSector, Reason = viewModel.Reason
            },
                User);
        }
        public async Task ReportCountIsControlledBySubmissionOptions(SectorTypes testSector, int testEditableReportCount)
        {
            // Arrange
            var testConfig = new SubmissionOptions {
                EditableReportCount = testEditableReportCount
            };
            var testOrg = new Organisation {
                OrganisationId = 1, SectorType = testSector
            };
            var testUserOrg = new UserOrganisation {
                Organisation = testOrg
            };
            DateTime testSnapshotDate = mockSharedBusinessLogic.GetAccountingStartDate(testOrg.SectorType);

            var mockService = new Mock <SubmissionPresenter>(
                mockDataRepo.Object,
                mockScopeBL.Object,
                null,
                mockDraftFileBL.Object,
                MoqHelpers.CreateIOptionsSnapshotMock(testConfig));

            // Call the real functions unless overridden
            mockService.CallBase = true;

            // Act
            SubmissionPresenter    testService   = mockService.Object;
            List <ReportInfoModel> actualResults = await testService.GetAllEditableReportsAsync(testUserOrg, testSnapshotDate);

            // Assert
            Assert.AreEqual(
                testEditableReportCount,
                actualResults.Count,
                $"Expected editable report count to be {testEditableReportCount}");
        }
        public void Is_False_When_ModifiedDate_Is_Late_And_OutOfScope(SectorTypes sector, ScopeStatuses scopeStatus)
        {
            var totalYearOffsets = 4;

            for (var yearOffset = 0; yearOffset < totalYearOffsets; yearOffset++)
            {
                // Arrange
                int      testYear         = VirtualDateTime.Now.Year - yearOffset;
                DateTime snapshotDate     = sector.GetAccountingStartDate(testYear);
                DateTime nextSnapshotDate = snapshotDate.AddYears(1);
                DateTime modifiedDate     = nextSnapshotDate.AddDays(2);

                Organisation testOrganisation = sector == SectorTypes.Private
                    ? OrganisationHelper.GetPrivateOrganisation()
                    : OrganisationHelper.GetPublicOrganisation();

                OrganisationScope testScope = ScopeHelper.CreateScope(scopeStatus, snapshotDate);

                Return testReturn = ReturnHelper.CreateLateReturn(testOrganisation, snapshotDate, modifiedDate, testScope);

                // Act
                bool actual = testReturn.IsLateSubmission;

                // Assert
                Assert.AreEqual(false, actual);
            }
        }
        public bool IsCurrentSnapshotYear(SectorTypes sector, int snapshotYear)
        {
            // Get the current reporting year
            var currentReportingStartYear = GetCurrentSnapshotDate(sector).Year;

            // Return year compare result
            return(snapshotYear == currentReportingStartYear);
        }
        public bool IsHistoricSnapshotYear(SectorTypes sector, int snapshotYear)
        {
            // Get the previous reporting year
            int currentReportingStartYear = GetCurrentSnapshotDate(sector).Year;

            // Return year compare result
            return(snapshotYear < currentReportingStartYear);
        }
Пример #27
0
 private Organisation CreateOrgWithNoScopes(int testOrgId, SectorTypes testSector, DateTime testCreated)
 {
     return(new Organisation
     {
         OrganisationId = testOrgId, SectorType = testSector, Status = OrganisationStatuses.Active,
         Created = testCreated
     });
 }
Пример #28
0
 private Expression <Func <User, bool> > UserHasNotBeenEmailedYet(SectorTypes sector, DateTime reminderEmailDate)
 {
     return(user => !user.ReminderEmails
            .Any(
                reminderEmail => reminderEmail.SectorType == sector &&
                reminderEmail.ReminderDate.HasValue &&
                reminderEmail.ReminderDate.Value.Date == reminderEmailDate.Date &&
                reminderEmail.Status == ReminderEmailStatus.Completed));
 }
Пример #29
0
        private static DateTime GetEarliestReminderDateForReportingYear(SectorTypes sectorType, int year)
        {
            List <int> reminderEmailDays   = GetReminderEmailDays();
            int        earliestReminderDay = reminderEmailDays[reminderEmailDays.Count - 1];

            DateTime deadlineDate = GetDeadlineDateForReportingYear(sectorType, year);

            return(deadlineDate.AddDays(-earliestReminderDay));
        }
Пример #30
0
 private ReminderEmail GetReminderEmailRecord(User user, SectorTypes sectorType, DateTime reminderDate)
 {
     return(dataRepository
            .GetAll <ReminderEmail>()
            .FirstOrDefault(
                re => re.UserId == user.UserId &&
                re.ReminderDate.HasValue &&
                re.ReminderDate.Value.Date == reminderDate.Date &&
                re.SectorType == sectorType));
 }