Exemplo n.º 1
0
        public void GetSubmissionChangeSummary_Detects_when_the_website_url_has_changed()
        {
            var testOldReturn = new Return {
                CompanyLinkToGPGInfo = "", Organisation = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            // Mocks
            var testService = new SubmissionService(
                mockDataRepo.Object,
                mockScopeBL.Object,
                _mockDraftFileBL.Object);

            // Copy the original
            var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

            // Make the change
            testNewReturn.CompanyLinkToGPGInfo = "http://unittesting";

            // Assert
            SubmissionChangeSummary testChangeSummary = testService.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
            string failMessage = $"Assert: Failed to detect {nameof(testNewReturn.CompanyLinkToGPGInfo)} had changed";

            Expect(testChangeSummary.HasChanged, failMessage);
            Expect(testChangeSummary.FiguresChanged == false, failMessage);
            Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
            Expect(testChangeSummary.PersonResonsibleChanged == false, failMessage);
            Expect(testChangeSummary.WebsiteUrlChanged, failMessage);
        }
Exemplo n.º 2
0
        public void GetSubmissionChangeSummary_Detects_when_the_website_url_has_changed()
        {
            var testOldReturn = new Return {
                CompanyLinkToGPGInfo = "", Organisation = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            var sharedBusinessLogic = UiTestHelper.DIContainer.Resolve <ISharedBusinessLogic>();

            // Mocks
            var testSubmissionService = new SubmissionService(sharedBusinessLogic, Mock.Of <ISubmissionBusinessLogic>(), Mock.Of <IScopeBusinessLogic>(), Mock.Of <IDraftFileBusinessLogic>());
            var testPresenter         = new SubmissionPresenter(testSubmissionService, ConfigHelpers.SubmissionOptions, null);

            // Copy the original
            var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

            // Make the change
            testNewReturn.CompanyLinkToGPGInfo = "http://unittesting";

            // Assert
            SubmissionChangeSummary testChangeSummary = testPresenter.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
            string failMessage = $"Assert: Failed to detect {nameof(testNewReturn.CompanyLinkToGPGInfo)} had changed";

            Expect(testChangeSummary.HasChanged, failMessage);
            Expect(testChangeSummary.FiguresChanged == false, failMessage);
            Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
            Expect(testChangeSummary.PersonResonsibleChanged == false, failMessage);
            Expect(testChangeSummary.WebsiteUrlChanged, failMessage);
        }
Exemplo n.º 3
0
        public void SubmissionChangeSummary_ShouldProvideLateReason_PersonResponsibleChanged_Returns_True_When_Changed()
        {
            // Arrange
            var submissionChangeSummary = new SubmissionChangeSummary {
                PersonResonsibleChanged = true, IsPrevReportingStartYear = true
            };

            // Act
            bool actual = submissionChangeSummary.ShouldProvideLateReason;

            // Assert
            Assert.True(
                actual,
                "When the Person Responsible has changed for a report belonging to the previous year, the user 'ShouldProvideLateReason'");
        }
Exemplo n.º 4
0
        public void SubmissionChangeSummary_ShouldProvideLateReason_Figures_Returns_False_When_Changed_On_Current_Year()
        {
            // Arrange
            var submissionChangeSummary = new SubmissionChangeSummary {
                FiguresChanged = true, IsPrevReportingStartYear = false
            };

            // Act
            bool actual = submissionChangeSummary.ShouldProvideLateReason;

            // Assert
            Assert.False(
                actual,
                "When the figures have changed for a report belonging to the current year, the user does not need to provide a late reason");
        }
Exemplo n.º 5
0
        public void SubmissionChangeSummary_ShouldProvideLateReason_WebsiteUrlChanged_Returns_False_When_Changed()
        {
            // Arrange
            var submissionChangeSummary = new SubmissionChangeSummary {
                WebsiteUrlChanged = true, IsPrevReportingStartYear = true
            };

            // Act
            bool actual = submissionChangeSummary.ShouldProvideLateReason;

            // Assert
            Assert.False(
                actual,
                "When the Website Url has changed for a report belonging to the previous year, the user does NOT need to provide a late reason");
        }
Exemplo n.º 6
0
        public void GetSubmissionChangeSummary_Detects_no_changes()
        {
            var testOldReturn = new Return
            {
                DiffMeanBonusPercent        = 100,
                DiffMeanHourlyPayPercent    = 99,
                DiffMedianBonusPercent      = 98,
                DiffMedianHourlyPercent     = 97,
                FemaleLowerPayBand          = 96,
                FemaleMedianBonusPayPercent = 95,
                FemaleMiddlePayBand         = 94,
                FemaleUpperPayBand          = 93,
                FemaleUpperQuartilePayBand  = 92,
                MaleLowerPayBand            = 91,
                MaleMedianBonusPayPercent   = 90,
                MaleUpperQuartilePayBand    = 89,
                MaleMiddlePayBand           = 88,
                MaleUpperPayBand            = 87,
                FirstName            = "Test",
                LastName             = "User",
                JobTitle             = "QA",
                CompanyLinkToGPGInfo = "http://unittesting",
                MinEmployees         = 250,
                MaxEmployees         = 499,
                Organisation         = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            // Mocks
            var testService = new SubmissionService(
                mockDataRepo.Object,
                mockScopeBL.Object,
                _mockDraftFileBL.Object);

            // Copy the original
            var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

            // Assert
            SubmissionChangeSummary testChangeSummary = testService.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
            string failMessage = $"Assert: Failed to detect {nameof(testNewReturn.CompanyLinkToGPGInfo)} had changed";

            Expect(testChangeSummary.HasChanged == false, failMessage);
            Expect(testChangeSummary.FiguresChanged == false, failMessage);
            Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
            Expect(testChangeSummary.PersonResonsibleChanged == false, failMessage);
            Expect(testChangeSummary.WebsiteUrlChanged == false, failMessage);
        }
Exemplo n.º 7
0
        public void GetSubmissionChangeSummary_Detects_no_changes()
        {
            var testOldReturn = new Return {
                DiffMeanBonusPercent        = 100,
                DiffMeanHourlyPayPercent    = 99,
                DiffMedianBonusPercent      = 98,
                DiffMedianHourlyPercent     = 97,
                FemaleLowerPayBand          = 96,
                FemaleMedianBonusPayPercent = 95,
                FemaleMiddlePayBand         = 94,
                FemaleUpperPayBand          = 93,
                FemaleUpperQuartilePayBand  = 92,
                MaleLowerPayBand            = 91,
                MaleMedianBonusPayPercent   = 90,
                MaleUpperQuartilePayBand    = 89,
                MaleMiddlePayBand           = 88,
                MaleUpperPayBand            = 87,
                FirstName            = "Test",
                LastName             = "User",
                JobTitle             = "QA",
                CompanyLinkToGPGInfo = "http://unittesting",
                MinEmployees         = 250,
                MaxEmployees         = 499,
                Organisation         = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            var sharedBusinessLogic = UiTestHelper.DIContainer.Resolve <ISharedBusinessLogic>();

            // Mocks
            var testSubmissionService = new SubmissionService(sharedBusinessLogic, Mock.Of <ISubmissionBusinessLogic>(), Mock.Of <IScopeBusinessLogic>(), Mock.Of <IDraftFileBusinessLogic>());
            var testPresenter         = new SubmissionPresenter(testSubmissionService, ConfigHelpers.SubmissionOptions, null);

            // Copy the original
            var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

            // Assert
            SubmissionChangeSummary testChangeSummary = testPresenter.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
            string failMessage = $"Assert: Failed to detect {nameof(testNewReturn.CompanyLinkToGPGInfo)} had changed";

            Expect(testChangeSummary.HasChanged == false, failMessage);
            Expect(testChangeSummary.FiguresChanged == false, failMessage);
            Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
            Expect(testChangeSummary.PersonResonsibleChanged == false, failMessage);
            Expect(testChangeSummary.WebsiteUrlChanged == false, failMessage);
        }
Exemplo n.º 8
0
        public void GetSubmissionChangeSummary_Detects_when_the_person_responsible_has_changed()
        {
            var testOldReturn = new Return
            {
                FirstName    = "Test",
                LastName     = "User",
                JobTitle     = "QA",
                Organisation = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            string[] personProps = { nameof(testOldReturn.FirstName), nameof(testOldReturn.LastName), nameof(testOldReturn.JobTitle) };

            var changeValue = "Mr T";

            // Mocks
            var testService = new SubmissionService(
                mockDataRepo.Object,
                mockScopeBL.Object,
                _mockDraftFileBL.Object);

            // Assert all figures
            foreach (string personPropName in personProps)
            {
                // Restore the original
                var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

                // Make the change
                testNewReturn.SetProperty(personPropName, changeValue);

                // Assert
                SubmissionChangeSummary testChangeSummary = testService.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
                string failMessage = $"Assert: Failed to detect {personPropName} had changed";
                Expect(testChangeSummary.HasChanged, failMessage);
                Expect(testChangeSummary.FiguresChanged == false, failMessage);
                Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
                Expect(testChangeSummary.PersonResonsibleChanged, failMessage);
                Expect(testChangeSummary.WebsiteUrlChanged == false, failMessage);
            }
        }
Exemplo n.º 9
0
        public void GetSubmissionChangeSummary_Detects_when_the_person_responsible_has_changed()
        {
            var testOldReturn = new Return {
                FirstName = "Test", LastName = "User", JobTitle = "QA", Organisation = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            string[] personProps = { nameof(testOldReturn.FirstName), nameof(testOldReturn.LastName), nameof(testOldReturn.JobTitle) };

            var changeValue = "Mr T";

            var sharedBusinessLogic = UiTestHelper.DIContainer.Resolve <ISharedBusinessLogic>();

            // Mocks
            var testSubmissionService = new SubmissionService(sharedBusinessLogic, Mock.Of <ISubmissionBusinessLogic>(), Mock.Of <IScopeBusinessLogic>(), Mock.Of <IDraftFileBusinessLogic>());
            var testPresenter         = new SubmissionPresenter(testSubmissionService, ConfigHelpers.SubmissionOptions, null);

            // Assert all figures
            foreach (string personPropName in personProps)
            {
                // Restore the original
                var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

                // Make the change
                testNewReturn.SetProperty(personPropName, changeValue);

                // Assert
                SubmissionChangeSummary testChangeSummary = testPresenter.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
                string failMessage = $"Assert: Failed to detect {personPropName} had changed";
                Expect(testChangeSummary.HasChanged, failMessage);
                Expect(testChangeSummary.FiguresChanged == false, failMessage);
                Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
                Expect(testChangeSummary.PersonResonsibleChanged, failMessage);
                Expect(testChangeSummary.WebsiteUrlChanged == false, failMessage);
            }
        }
Exemplo n.º 10
0
        public void GetSubmissionChangeSummary_Detects_when_the_figures_have_changed()
        {
            var testOldReturn = new Return
            {
                DiffMeanBonusPercent        = 100,
                DiffMeanHourlyPayPercent    = 99,
                DiffMedianBonusPercent      = 98,
                DiffMedianHourlyPercent     = 97,
                FemaleLowerPayBand          = 96,
                FemaleMedianBonusPayPercent = 95,
                FemaleMiddlePayBand         = 94,
                FemaleUpperPayBand          = 93,
                FemaleUpperQuartilePayBand  = 92,
                MaleLowerPayBand            = 91,
                MaleMedianBonusPayPercent   = 90,
                MaleUpperQuartilePayBand    = 89,
                MaleMiddlePayBand           = 88,
                MaleUpperPayBand            = 87,
                Organisation = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            string[] figureProps =
            {
                nameof(testOldReturn.DiffMeanBonusPercent),
                nameof(testOldReturn.DiffMeanHourlyPayPercent),
                nameof(testOldReturn.DiffMedianBonusPercent),
                nameof(testOldReturn.DiffMedianHourlyPercent),
                nameof(testOldReturn.FemaleLowerPayBand),
                nameof(testOldReturn.FemaleMedianBonusPayPercent),
                nameof(testOldReturn.FemaleMiddlePayBand),
                nameof(testOldReturn.FemaleUpperPayBand),
                nameof(testOldReturn.FemaleUpperQuartilePayBand),
                nameof(testOldReturn.MaleLowerPayBand),
                nameof(testOldReturn.MaleMedianBonusPayPercent),
                nameof(testOldReturn.MaleUpperQuartilePayBand),
                nameof(testOldReturn.MaleMiddlePayBand),
                nameof(testOldReturn.MaleUpperPayBand)
            };

            decimal changeValue = 0;

            // Mocks
            var testService = new SubmissionService(
                mockDataRepo.Object,
                mockScopeBL.Object,
                _mockDraftFileBL.Object);

            // Assert all figures
            foreach (string figurePropName in figureProps)
            {
                // Restore the original
                var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

                // Make the change
                testNewReturn.SetProperty(figurePropName, changeValue);

                // Assert
                SubmissionChangeSummary testChangeSummary = testService.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
                string failMessage = $"Assert: Failed to detect {figurePropName} had changed";
                Expect(testChangeSummary.HasChanged, failMessage);
                Expect(testChangeSummary.FiguresChanged, failMessage);
                Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
                Expect(testChangeSummary.PersonResonsibleChanged == false, failMessage);
                Expect(testChangeSummary.WebsiteUrlChanged == false, failMessage);
            }
        }
Exemplo n.º 11
0
        public async Task <IActionResult> CheckData()
        {
            #region Check user, then retrieve model from Session

            IActionResult checkResult = CheckUserRegisteredOk(out User currentUser);
            if (checkResult != null)
            {
                return(checkResult);
            }

            var stashedReturnViewModel = this.UnstashModel <ReturnViewModel>();

            #endregion

            stashedReturnViewModel = await LoadReturnViewModelFromDBorFromDraftFileAsync(stashedReturnViewModel, currentUser.UserId);

            if (stashedReturnViewModel.ReportInfo.Draft != null && !stashedReturnViewModel.ReportInfo.Draft.IsUserAllowedAccess)
            {
                this.CleanModelErrors <ReturnViewModel>();
                return(View("CustomError", new ErrorViewModel(3040)));
            }


            if (!stashedReturnViewModel.HasDraftWithContent())
            {
                await TryToReloadDraftContent(stashedReturnViewModel);
            }

            if (stashedReturnViewModel.ReportInfo.Draft.HasDraftBeenModifiedDuringThisSession ||
                stashedReturnViewModel.HasDraftWithContent())
            {
                Return databaseReturn = submissionService.GetSubmissionById(stashedReturnViewModel.ReturnId);

                if (databaseReturn != null)
                {
                    Return stashedReturn = submissionService.CreateDraftSubmissionFromViewModel(stashedReturnViewModel);
                    SubmissionChangeSummary changeSummary = submissionService.GetSubmissionChangeSummary(stashedReturn, databaseReturn);
                    stashedReturnViewModel.IsDifferentFromDatabase = changeSummary.HasChanged;
                    stashedReturnViewModel.ShouldProvideLateReason = changeSummary.ShouldProvideLateReason;
                }
                else
                {
                    // We have some draft info and no DB record, therefore is definitely different
                    stashedReturnViewModel.IsDifferentFromDatabase = true;
                    // Recalculate to know if they're submitting late. This is because it is possible that a draft was created BEFORE the cut-off date ("should provide late reason" would have been marked as 'false') but are completing the submission process AFTER which is when we need them to provide a late reason and the flag is expected to be 'true'.
                    stashedReturnViewModel.ShouldProvideLateReason = submissionService.IsHistoricSnapshotYear(
                        stashedReturnViewModel.SectorType,
                        ReportingOrganisationStartYear.Value);
                }
            }

            if (!submissionService.IsValidSnapshotYear(ReportingOrganisationStartYear.Value))
            {
                return(new HttpBadRequestResult($"Invalid snapshot year {ReportingOrganisationStartYear.Value}"));
            }

            // Don't ask for late reason if the reporting year has been excluded from late flag enforcement (eg. 2019/20 due to COVID-19)
            if (Global.ReportingStartYearsToExcludeFromLateFlagEnforcement.Contains(stashedReturnViewModel.AccountingDate.Year))
            {
                stashedReturnViewModel.ShouldProvideLateReason = false;
            }

            this.StashModel(stashedReturnViewModel);
            return(View("CheckData", stashedReturnViewModel));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> CheckData(ReturnViewModel postedReturnViewModel)
        {
            #region Check user, then retrieve model from Session

            IActionResult checkResult = CheckUserRegisteredOk(out User currentUser);
            if (checkResult != null)
            {
                return(checkResult);
            }

            var stashedReturnViewModel = this.UnstashModel <ReturnViewModel>();

            #endregion

            if (stashedReturnViewModel == null)
            {
                return(SessionExpiredView());
            }

            postedReturnViewModel.ReportInfo.Draft = stashedReturnViewModel.ReportInfo.Draft;

            if (postedReturnViewModel.SectorType == SectorTypes.Public)
            {
                ModelState.Exclude(
                    nameof(postedReturnViewModel.FirstName),
                    nameof(postedReturnViewModel.LastName),
                    nameof(postedReturnViewModel.JobTitle));
            }

            Return postedReturn = submissionService.CreateDraftSubmissionFromViewModel(postedReturnViewModel);

            SubmissionChangeSummary changeSummary = null;
            Return databaseReturn = submissionService.GetSubmissionById(postedReturnViewModel.ReturnId);
            if (databaseReturn != null)
            {
                changeSummary = submissionService.GetSubmissionChangeSummary(postedReturn, databaseReturn);

                if (!changeSummary.HasChanged)
                {
                    return(new HttpBadRequestResult("Submission has no changes"));
                }

                if (!changeSummary.FiguresChanged)
                {
                    // If the figure have not changed
                    //   e.g. if the only change was to the URL / person reporting
                    // Then don't apply a NEW late flag
                    // But DO continue to apply an EXISTING late flag
                    // So, in summary, "If the figure have not changed, copy the old late flag"
                    postedReturn.IsLateSubmission = databaseReturn.IsLateSubmission;
                }

                postedReturn.Modifications = changeSummary.Modifications;

                databaseReturn.SetStatus(ReturnStatuses.Retired, OriginalUser == null ? currentUser.UserId : OriginalUser.UserId);
            }

            if (databaseReturn == null || !changeSummary.ShouldProvideLateReason)
            {
                ModelState.Remove(nameof(postedReturnViewModel.LateReason));
                ModelState.Remove(nameof(postedReturnViewModel.EHRCResponse));
            }

            // Don't mark submission as late if the reporting year has been excluded from late flag enforcement (eg. 2019/20 due to COVID-19)
            if (Global.ReportingStartYearsToExcludeFromLateFlagEnforcement.Contains(stashedReturnViewModel.AccountingDate.Year))
            {
                ModelState.Remove(nameof(postedReturnViewModel.LateReason));
                ModelState.Remove(nameof(postedReturnViewModel.EHRCResponse));
                postedReturn.IsLateSubmission = false;
            }

            ModelState.Remove("ReportInfo.Draft");

            if (!ModelState.IsValid)
            {
                this.CleanModelErrors <ReturnViewModel>();
                return(View("CheckData", postedReturnViewModel));
            }

            if (databaseReturn == null || databaseReturn.Status == ReturnStatuses.Retired)
            {
                DataRepository.Insert(postedReturn);
            }

            postedReturn.SetStatus(ReturnStatuses.Submitted, OriginalUser?.UserId ?? currentUser.UserId);

            Organisation organisationFromDatabase = DataRepository.GetAll <Organisation>()
                                                    .FirstOrDefault(o => o.OrganisationId == postedReturnViewModel.OrganisationId);

            organisationFromDatabase.Returns.Add(postedReturn);

            DataRepository.SaveChanges();

            //This is required for the submission complete page
            postedReturnViewModel.EncryptedOrganisationId = postedReturn.Organisation.GetEncryptedId();
            this.StashModel(postedReturnViewModel);

            if (Global.EnableSubmitAlerts &&
                postedReturn.Organisation.Returns.Count(r => r.AccountingDate == postedReturn.AccountingDate) == 1)
            {
                emailSendingService.SendGeoFirstTimeDataSubmissionEmail(
                    postedReturn.AccountingDate.Year.ToString(),
                    postedReturn.Organisation.OrganisationName,
                    postedReturn.StatusDate.ToShortDateString(),
                    Url.Action(
                        "Report",
                        "Viewing",
                        new { employerIdentifier = postedReturnViewModel.EncryptedOrganisationId, year = postedReturn.AccountingDate.Year },
                        "https"));
            }

            EmailSendingServiceHelpers.SendSuccessfulSubmissionEmailToRegisteredUsers(
                postedReturn,
                GetReportLink(postedReturn),
                GetSubmittedOrUpdated(postedReturn),
                emailSendingService);

            await submissionService.DiscardDraftFileAsync(postedReturnViewModel);

            return(RedirectToAction("SubmissionComplete"));
        }
Exemplo n.º 13
0
        public void GetSubmissionChangeSummary_Detects_when_the_figures_have_changed()
        {
            var testOldReturn = new Return {
                DiffMeanBonusPercent        = 100,
                DiffMeanHourlyPayPercent    = 99,
                DiffMedianBonusPercent      = 98,
                DiffMedianHourlyPercent     = 97,
                FemaleLowerPayBand          = 96,
                FemaleMedianBonusPayPercent = 95,
                FemaleMiddlePayBand         = 94,
                FemaleUpperPayBand          = 93,
                FemaleUpperQuartilePayBand  = 92,
                MaleLowerPayBand            = 91,
                MaleMedianBonusPayPercent   = 90,
                MaleUpperQuartilePayBand    = 89,
                MaleMiddlePayBand           = 88,
                MaleUpperPayBand            = 87,
                Organisation = new Organisation {
                    SectorType = SectorTypes.Private
                }
            };

            string[] figureProps =
            {
                nameof(testOldReturn.DiffMeanBonusPercent),
                nameof(testOldReturn.DiffMeanHourlyPayPercent),
                nameof(testOldReturn.DiffMedianBonusPercent),
                nameof(testOldReturn.DiffMedianHourlyPercent),
                nameof(testOldReturn.FemaleLowerPayBand),
                nameof(testOldReturn.FemaleMedianBonusPayPercent),
                nameof(testOldReturn.FemaleMiddlePayBand),
                nameof(testOldReturn.FemaleUpperPayBand),
                nameof(testOldReturn.FemaleUpperQuartilePayBand),
                nameof(testOldReturn.MaleLowerPayBand),
                nameof(testOldReturn.MaleMedianBonusPayPercent),
                nameof(testOldReturn.MaleUpperQuartilePayBand),
                nameof(testOldReturn.MaleMiddlePayBand),
                nameof(testOldReturn.MaleUpperPayBand)
            };

            decimal changeValue = 0;

            var sharedBusinessLogic = UiTestHelper.DIContainer.Resolve <ISharedBusinessLogic>();

            // Mocks
            var testSubmissionService = new SubmissionService(sharedBusinessLogic, Mock.Of <ISubmissionBusinessLogic>(), Mock.Of <IScopeBusinessLogic>(), Mock.Of <IDraftFileBusinessLogic>());
            var testPresenter         = new SubmissionPresenter(testSubmissionService, ConfigHelpers.SubmissionOptions, null);

            // Assert all figures
            foreach (string figurePropName in figureProps)
            {
                // Restore the original
                var testNewReturn = (Return)testOldReturn.CopyProperties(new Return());

                // Make the change
                testNewReturn.SetProperty(figurePropName, changeValue);

                // Assert
                SubmissionChangeSummary testChangeSummary = testPresenter.GetSubmissionChangeSummary(testNewReturn, testOldReturn);
                string failMessage = $"Assert: Failed to detect {figurePropName} had changed";
                Expect(testChangeSummary.HasChanged, failMessage);
                Expect(testChangeSummary.FiguresChanged, failMessage);
                Expect(testChangeSummary.OrganisationSizeChanged == false, failMessage);
                Expect(testChangeSummary.PersonResonsibleChanged == false, failMessage);
                Expect(testChangeSummary.WebsiteUrlChanged == false, failMessage);
            }
        }
        public async Task <IActionResult> CheckData(ReturnViewModel postedReturnViewModel)
        {
            #region Check user, then retrieve model from Session

            var checkResult = await CheckUserRegisteredOkAsync();

            if (checkResult != null)
            {
                return(checkResult);
            }

            var stashedReturnViewModel = UnstashModel <ReturnViewModel>();

            #endregion

            if (stashedReturnViewModel == null)
            {
                return(SessionExpiredView());
            }

            postedReturnViewModel.ReportInfo.Draft = stashedReturnViewModel.ReportInfo.Draft;

            if (postedReturnViewModel.SectorType == SectorTypes.Public)
            {
                ModelState.Exclude(
                    nameof(postedReturnViewModel.FirstName),
                    nameof(postedReturnViewModel.LastName),
                    nameof(postedReturnViewModel.JobTitle));
            }

            var postedReturn = _SubmissionPresenter.CreateDraftSubmissionFromViewModel(postedReturnViewModel);

            SubmissionChangeSummary changeSummary = null;
            var databaseReturn = await _SubmissionPresenter.GetSubmissionByIdAsync(postedReturnViewModel.ReturnId);

            if (databaseReturn != null)
            {
                changeSummary = _SubmissionPresenter.GetSubmissionChangeSummary(postedReturn, databaseReturn);

                if (!changeSummary.HasChanged)
                {
                    return(new HttpBadRequestResult("Submission has no changes"));
                }

                if (!changeSummary.FiguresChanged)
                {
                    // If the figure have not changed
                    //   e.g. if the only change was to the URL / person reporting
                    // Then don't apply a NEW late flag
                    // But DO continue to apply an EXISTING late flag
                    // So, in summary, "If the figure have not changed, copy the old late flag"
                    postedReturn.IsLateSubmission = databaseReturn.IsLateSubmission;
                }

                postedReturn.Modifications = changeSummary.Modifications;

                databaseReturn.SetStatus(ReturnStatuses.Retired,
                                         OriginalUser == null ? VirtualUser.UserId : OriginalUser.UserId);
            }

            if (databaseReturn == null || !changeSummary.ShouldProvideLateReason)
            {
                ModelState.Remove(nameof(postedReturnViewModel.LateReason));
                ModelState.Remove(nameof(postedReturnViewModel.EHRCResponse));
            }

            ModelState.Remove("ReportInfo.Draft");

            if (!ModelState.IsValid)
            {
                this.CleanModelErrors <ReturnViewModel>();
                return(View("CheckData", postedReturnViewModel));
            }

            if (databaseReturn == null || databaseReturn.Status == ReturnStatuses.Retired)
            {
                SharedBusinessLogic.DataRepository.Insert(postedReturn);
            }

            postedReturn.SetStatus(ReturnStatuses.Submitted, OriginalUser?.UserId ?? VirtualUser.UserId);

            var organisationFromDatabase =
                await SharedBusinessLogic.DataRepository.FirstOrDefaultAsync <Organisation>(o =>
                                                                                            o.OrganisationId == postedReturnViewModel.OrganisationId);

            organisationFromDatabase.Returns.Add(postedReturn);

            if (_SubmissionPresenter.ShouldUpdateLatestReturn(organisationFromDatabase,
                                                              ReportingOrganisationStartYear.Value))
            {
                organisationFromDatabase.LatestReturn = postedReturn;
            }

            await SharedBusinessLogic.DataRepository.SaveChangesAsync();

            if (!VirtualUser.EmailAddress.StartsWithI(SharedBusinessLogic.SharedOptions.TestPrefix))
            {
                await _SubmissionPresenter.SubmissionService.SubmissionBusinessLogic.SubmissionLog.WriteAsync(
                    new SubmissionLogModel
                {
                    StatusDate     = VirtualDateTime.Now,
                    Status         = postedReturn.Status,
                    Details        = "",
                    Sector         = postedReturn.Organisation.SectorType,
                    ReturnId       = postedReturn.ReturnId,
                    AccountingDate = postedReturn.AccountingDate.ToShortDateString(),
                    OrganisationId = postedReturn.OrganisationId,
                    EmployerName   = postedReturn.Organisation.OrganisationName,
                    Address        = postedReturn.Organisation.LatestAddress?.GetAddressString("," + Environment.NewLine),
                    CompanyNumber  = postedReturn.Organisation.CompanyNumber,
                    SicCodes       = postedReturn.Organisation.GetSicCodeIdsString(postedReturn.StatusDate,
                                                                                   "," + Environment.NewLine),
                    DiffMeanHourlyPayPercent    = postedReturn.DiffMeanHourlyPayPercent,
                    DiffMedianHourlyPercent     = postedReturn.DiffMedianHourlyPercent,
                    DiffMeanBonusPercent        = postedReturn.DiffMeanBonusPercent,
                    DiffMedianBonusPercent      = postedReturn.DiffMedianBonusPercent,
                    MaleMedianBonusPayPercent   = postedReturn.MaleMedianBonusPayPercent,
                    FemaleMedianBonusPayPercent = postedReturn.FemaleMedianBonusPayPercent,
                    MaleLowerPayBand            = postedReturn.MaleLowerPayBand,
                    FemaleLowerPayBand          = postedReturn.FemaleLowerPayBand,
                    MaleMiddlePayBand           = postedReturn.MaleMiddlePayBand,
                    FemaleMiddlePayBand         = postedReturn.FemaleMiddlePayBand,
                    MaleUpperPayBand            = postedReturn.MaleUpperPayBand,
                    FemaleUpperPayBand          = postedReturn.FemaleUpperPayBand,
                    MaleUpperQuartilePayBand    = postedReturn.MaleUpperQuartilePayBand,
                    FemaleUpperQuartilePayBand  = postedReturn.FemaleUpperQuartilePayBand,
                    CompanyLinkToGPGInfo        = postedReturn.CompanyLinkToGPGInfo,
                    ResponsiblePerson           = postedReturn.ResponsiblePerson,
                    UserFirstname       = VirtualUser.Firstname,
                    UserLastname        = VirtualUser.Lastname,
                    UserJobtitle        = VirtualUser.JobTitle,
                    UserEmail           = VirtualUser.EmailAddress,
                    ContactFirstName    = VirtualUser.ContactFirstName,
                    ContactLastName     = VirtualUser.ContactLastName,
                    ContactJobTitle     = VirtualUser.ContactJobTitle,
                    ContactOrganisation = VirtualUser.ContactOrganisation,
                    ContactPhoneNumber  = VirtualUser.ContactPhoneNumber,
                    Created             = postedReturn.Created,
                    Modified            = postedReturn.Modified,
                    Browser             = HttpContext.GetBrowser() ?? "No browser in the request",
                    SessionId           = Session.SessionID
                });
            }

            //This is required for the submission complete page
            postedReturnViewModel.EncryptedOrganisationId = SharedBusinessLogic.Obfuscator.Obfuscate(postedReturn.Organisation.OrganisationId);
            StashModel(postedReturnViewModel);

            if (SharedBusinessLogic.SharedOptions.EnableSubmitAlerts &&
                postedReturn.Organisation.Returns.Count(r => r.AccountingDate == postedReturn.AccountingDate) == 1 &&
                !VirtualUser.EmailAddress.StartsWithI(SharedBusinessLogic.SharedOptions.TestPrefix))
            {
                await _SubmissionService.SharedBusinessLogic.SendEmailService.SendGeoMessageAsync(
                    "GPG Data Submission Notification",
                    $"GPG data was submitted for first time in {postedReturn.AccountingDate.Year} by '{postedReturn.Organisation.OrganisationName}' on {postedReturn.StatusDate.ToShortDateString()}\n\n See {Url.Action("Report", "Viewing",new {employerIdentifier = postedReturnViewModel.EncryptedOrganisationId, year = postedReturn.AccountingDate.Year})}",
                    VirtualUser.EmailAddress.StartsWithI(SharedBusinessLogic.SharedOptions.TestPrefix));
            }

            _SubmissionService.SharedBusinessLogic.NotificationService.SendSuccessfulSubmissionEmailToRegisteredUsers(
                postedReturn,
                GetReportLink(postedReturn),
                GetSubmittedOrUpdated(postedReturn));

            await _SubmissionPresenter.DiscardDraftFileAsync(postedReturnViewModel);

            return(RedirectToAction("SubmissionComplete"));
        }