public async Task <IActionResult> WithdrawalDateCheck(Guid applicationId, int sequenceNo, BackViewModel backViewModel, int currentVersionIndex)
        {
            var application = await _applyApiClient.GetApplication(applicationId);

            var organisation = await _apiClient.GetOrganisation(application.OrganisationId);

            var activeApplySequence = application.ApplyData.Sequences.Where(seq => seq.IsActive && !seq.NotRequired).OrderBy(seq => seq.SequenceNo).FirstOrDefault();

            var sequence = await _qnaApiClient.GetSequence(application.ApplicationId, activeApplySequence.SequenceId);

            var sections = await _qnaApiClient.GetSections(application.ApplicationId, sequence.Id);

            var sequenceVm = new WithdrawalDateCheckViewModel(application, organisation, sequence, sections,
                                                              activeApplySequence.Sections,
                                                              backViewModel.BackAction,
                                                              backViewModel.BackController,
                                                              backViewModel.BackOrganisationId,
                                                              currentVersionIndex);

            return(View(nameof(WithdrawalDateCheck), sequenceVm));
        }
        public async Task <IActionResult> WithdrawalDateChange(Guid applicationId, int sequenceNo, BackViewModel backViewModel, string effectiveToDay, string effectiveToMonth, string effectiveToYear, int currentVersionIndex)
        {
            var application = await _applyApiClient.GetApplication(applicationId);

            var organisation = await _apiClient.GetOrganisation(application.OrganisationId);

            var activeApplySequence = application.ApplyData.Sequences.Where(seq => seq.IsActive && !seq.NotRequired).OrderBy(seq => seq.SequenceNo).FirstOrDefault();

            var sequence = await _qnaApiClient.GetSequence(application.ApplicationId, activeApplySequence.SequenceId);

            var sections = await _qnaApiClient.GetSections(application.ApplicationId, sequence.Id);

            var sequenceVm = new WithdrawalDateCheckViewModel(application, organisation, sequence, sections,
                                                              activeApplySequence.Sections,
                                                              backViewModel.BackAction,
                                                              backViewModel.BackController,
                                                              backViewModel.BackOrganisationId,
                                                              currentVersionIndex);

            var errorMessages = new Dictionary <string, string>();

            string effectiveToDateText = $"{effectiveToDay}/{effectiveToMonth}/{effectiveToYear}";

            if (!DateTime.TryParse(effectiveToDateText, out DateTime effectiveToDate))
            {
                errorMessages["RequestedWithdrawalDate"] = "Enter a valid date";
            }
            if (string.IsNullOrWhiteSpace(effectiveToDay) && string.IsNullOrWhiteSpace(effectiveToMonth) && string.IsNullOrWhiteSpace(effectiveToYear))
            {
                errorMessages["RequestedWithdrawalDate"] = "Enter a date";
            }

            if (errorMessages.Any())
            {
                foreach (var error in errorMessages)
                {
                    ModelState.AddModelError(error.Key, error.Value);
                }

                return(View(nameof(WithdrawalDateChange), sequenceVm));
            }

            if (sequenceVm.SequenceNo == ApplyConst.ORGANISATION_WITHDRAWAL_SEQUENCE_NO)
            {
                await WithdrawalOrganisation(application, organisation.EndPointAssessorOrganisationId, effectiveToDate);
            }
            else if (null == sequenceVm.Versions || !sequenceVm.Versions.Any())
            {
                // No versions supplied in the withdrawal application means they are withdrawing from the standard completely
                await UpdateOrganisationStandardWithdrawalDate(organisation.EndPointAssessorOrganisationId, sequenceVm.StandardReference, null, effectiveToDate);
            }
            else
            {
                await UpdateOrganisationStandardWithdrawalDate(organisation.EndPointAssessorOrganisationId, sequenceVm.StandardReference, sequenceVm.Versions[sequenceVm.CurrentVersionIndex.Value], effectiveToDate);
            }

            sequenceVm.IncrementCurrentVersionIndex();
            if (sequenceVm.CurrentVersionIndex.HasValue)
            {
                return(RedirectToAction(nameof(WithdrawalDateCheck), new { currentVersionIndex = sequenceVm.CurrentVersionIndex.Value }));
            }

            return(RedirectToAction(nameof(Assessment)));
        }
        public async Task <IActionResult> WithdrawalDateCheckSave(Guid applicationId, int sequenceNo, BackViewModel backViewModel, string dateApproved, int currentVersionIndex)
        {
            var application = await _applyApiClient.GetApplication(applicationId);

            var organisation = await _apiClient.GetOrganisation(application.OrganisationId);

            var activeApplySequence = application.ApplyData.Sequences.Where(seq => seq.IsActive && !seq.NotRequired).OrderBy(seq => seq.SequenceNo).FirstOrDefault();

            var sequence = await _qnaApiClient.GetSequence(application.ApplicationId, activeApplySequence.SequenceId);

            var sections = await _qnaApiClient.GetSections(application.ApplicationId, sequence.Id);

            var sequenceVm = new WithdrawalDateCheckViewModel(application, organisation, sequence, sections,
                                                              activeApplySequence.Sections,
                                                              backViewModel.BackAction,
                                                              backViewModel.BackController,
                                                              backViewModel.BackOrganisationId,
                                                              currentVersionIndex);

            var errorMessages = new Dictionary <string, string>();

            if (string.IsNullOrWhiteSpace(dateApproved) || (dateApproved.Trim().ToUpper() != "NO" && dateApproved.Trim().ToUpper() != "YES"))
            {
                errorMessages["RequestedWithdrawalDate"] = "Select Yes or No";
            }

            if (errorMessages.Any())
            {
                foreach (var error in errorMessages)
                {
                    ModelState.AddModelError(error.Key, error.Value);
                }

                return(View(nameof(WithdrawalDateCheck), sequenceVm));
            }

            if (dateApproved?.Trim().ToUpper() == "NO")
            {
                return(View(nameof(WithdrawalDateChange), sequenceVm));
            }

            if (sequenceVm.RequestedWithdrawalDate.HasValue)
            {
                if (sequenceVm.SequenceNo == ApplyConst.ORGANISATION_WITHDRAWAL_SEQUENCE_NO)
                {
                    await WithdrawalOrganisation(application, organisation.EndPointAssessorOrganisationId, sequenceVm.RequestedWithdrawalDate.Value);
                }
                else if (null == sequenceVm.Versions || !sequenceVm.Versions.Any())
                {
                    // No versions supplied in the withdrawal application means they are withdrawing from the standard completely
                    await UpdateOrganisationStandardWithdrawalDate(organisation.EndPointAssessorOrganisationId, sequenceVm.StandardReference, null, sequenceVm.RequestedWithdrawalDate.Value);
                }
                else
                {
                    await UpdateOrganisationStandardWithdrawalDate(organisation.EndPointAssessorOrganisationId, sequenceVm.StandardReference, sequenceVm.Versions[sequenceVm.CurrentVersionIndex.Value], sequenceVm.RequestedWithdrawalDate.Value);
                }


                sequenceVm.IncrementCurrentVersionIndex();
                if (sequenceVm.CurrentVersionIndex.HasValue)
                {
                    return(RedirectToAction(nameof(WithdrawalDateCheck), new { currentVersionIndex = sequenceVm.CurrentVersionIndex.Value }));
                }
            }

            return(RedirectToAction(nameof(Assessment)));
        }