public AddEditProviderModel()
 {
     Address = new AddressViewModel
     {
         RegionId = 0
     };
     UKRLPData   = new UKRLPDataModel();
     PublishData = true;
 }
        private UKRLPDataModel GetUkrlpData(int ukprn, bool activeOnly)
        {
            var ukrlpModel = new UKRLPDataModel
            {
                UKRLP = ""
            };

            try
            {
                var ukrlpWebService = new ProviderQueryServiceV3
                {
                    Url = ConfigurationManager.AppSettings["UKRLP_Url"]
                };

                var scs = new SelectionCriteriaStructure
                {
                    StakeholderId = ConfigurationManager.AppSettings["UKRLP_Stakeholder_Id"],
                    UnitedKingdomProviderReferenceNumberList = new[] { ukprn.ToString() },
                    ApprovedProvidersOnly          = YesNoType.No,
                    ApprovedProvidersOnlySpecified = true,
                    ProviderStatus             = "A",
                    CriteriaCondition          = QueryCriteriaConditionType.OR,
                    CriteriaConditionSpecified = true
                };

                var pqs = new ProviderQueryStructure
                {
                    SelectionCriteria = scs,
                    QueryId           = "1"
                };

                string[] statuses = { "A", "V", "PD1", "PD2" };
                if (activeOnly)
                {
                    statuses = new[] { "A", "V" };
                }

                foreach (var status in statuses)
                {
                    scs.ProviderStatus = status;

                    var r = ukrlpWebService.retrieveAllProviders(pqs);
                    if (r.MatchingProviderRecords != null)
                    {
                        foreach (var provider in r.MatchingProviderRecords)
                        {
                            switch (status)
                            {
                            case "A":
                                ukrlpModel.Status = AppGlobal.Language.GetText(this, "StatusActive", "Active");
                                break;

                            case "V":
                                ukrlpModel.Status = AppGlobal.Language.GetText(this, "StatusVerified", "Verified");
                                break;

                            case "PD1":
                                ukrlpModel.Status = AppGlobal.Language.GetText(this, "StatusDeactiviationInProcess",
                                                                               "Provider deactivated, not verified");
                                break;

                            case "PD2":
                                ukrlpModel.Status = AppGlobal.Language.GetText(this, "StatusDeactivationComplete",
                                                                               "Provider deactivated");
                                break;
                            }
                            ukrlpModel.UKRLP       = provider.UnitedKingdomProviderReferenceNumber;
                            ukrlpModel.LegalName   = provider.ProviderName;
                            ukrlpModel.TradingName = provider.ProviderAliases == null || provider.ProviderAliases.Count() == 0 || provider.ProviderAliases[0].ProviderAlias == null ? "" : provider.ProviderAliases[0].ProviderAlias.Substring(0, Math.Min(provider.ProviderAliases[0].ProviderAlias.Length, 255)) ?? "";
                            foreach (var pcs in provider.ProviderContact)
                            {
                                switch (pcs.ContactType)
                                {
                                case "L":     // Legal
                                    ukrlpModel.LegalTelephone = pcs.ContactTelephone1 ?? "";
                                    ukrlpModel.LegalFax       = pcs.ContactFax ?? "";
                                    if (pcs.ContactAddress != null)
                                    {
                                        if (!string.IsNullOrEmpty(pcs.ContactAddress.PAON.Description))
                                        {
                                            ukrlpModel.LegalFullAddress += pcs.ContactAddress.PAON.Description + " ";
                                            ukrlpModel.LegalAddress1     = pcs.ContactAddress.PAON.Description + " ";
                                        }

                                        if (!string.IsNullOrEmpty(pcs.ContactAddress.SAON.Description))
                                        {
                                            ukrlpModel.LegalFullAddress += pcs.ContactAddress.SAON.Description + " ";
                                            ukrlpModel.LegalAddress1    += pcs.ContactAddress.SAON.Description + " ";
                                        }
                                        if (!string.IsNullOrEmpty(ukrlpModel.LegalFullAddress))
                                        {
                                            ukrlpModel.LegalFullAddress += "<br />";
                                        }
                                        ukrlpModel.LegalFullAddress += pcs.ContactAddress.StreetDescription +
                                                                       "<br />";
                                        ukrlpModel.LegalAddress1 += pcs.ContactAddress.StreetDescription;
                                        if (!string.IsNullOrEmpty(pcs.ContactAddress.Locality))
                                        {
                                            ukrlpModel.LegalFullAddress += pcs.ContactAddress.Locality + "<br />";
                                            ukrlpModel.LegalAddress2     = pcs.ContactAddress.Locality;
                                        }
                                        ukrlpModel.LegalFullAddress += pcs.ContactAddress.Items != null &&
                                                                       pcs.ContactAddress.Items.GetLength(0) > 0
                                                ? pcs.ContactAddress.Items[0] + "<br>"
                                                : "";
                                        ukrlpModel.LegalTown += pcs.ContactAddress.Items != null &&
                                                                pcs.ContactAddress.Items.GetLength(0) > 0
                                                ? pcs.ContactAddress.Items[0] + "<br>"
                                                : "";
                                        ukrlpModel.LegalFullAddress += pcs.ContactAddress.PostCode + "<br>";
                                        ukrlpModel.LegalPostcode    += pcs.ContactAddress.PostCode + "<br>";
                                    }
                                    break;

                                case "P":     // Primary
                                    ukrlpModel.ContactTelephone = pcs.ContactTelephone1 ?? "";
                                    ukrlpModel.ContactFax       = pcs.ContactFax ?? "";
                                    ukrlpModel.ContactName      =
                                        string.Format("{0} {1} {2}",
                                                      pcs.ContactPersonalDetails.PersonNameTitle == null
                                                    ? ""
                                                    : pcs.ContactPersonalDetails.PersonNameTitle[0],
                                                      pcs.ContactPersonalDetails.PersonGivenName == null
                                                    ? ""
                                                    : pcs.ContactPersonalDetails.PersonGivenName[0],
                                                      pcs.ContactPersonalDetails.PersonFamilyName).Replace("  ", " ").Trim();
                                    if (pcs.ContactAddress != null)
                                    {
                                        if (!string.IsNullOrEmpty(pcs.ContactAddress.PAON.Description))
                                        {
                                            ukrlpModel.ContactFullAddress += pcs.ContactAddress.PAON.Description +
                                                                             " ";
                                            ukrlpModel.ContactAddress1 = pcs.ContactAddress.PAON.Description + " ";
                                        }

                                        if (!string.IsNullOrEmpty(pcs.ContactAddress.SAON.Description))
                                        {
                                            ukrlpModel.ContactFullAddress += pcs.ContactAddress.SAON.Description +
                                                                             " ";
                                            ukrlpModel.ContactAddress1 += pcs.ContactAddress.SAON.Description + " ";
                                        }
                                        if (!string.IsNullOrEmpty(ukrlpModel.ContactFullAddress))
                                        {
                                            ukrlpModel.ContactFullAddress += "<br />";
                                        }
                                        ukrlpModel.ContactFullAddress += pcs.ContactAddress.StreetDescription +
                                                                         "<br />";
                                        ukrlpModel.ContactAddress1 += pcs.ContactAddress.StreetDescription;
                                        if (!string.IsNullOrEmpty(pcs.ContactAddress.Locality))
                                        {
                                            ukrlpModel.ContactFullAddress += pcs.ContactAddress.Locality + "<br />";
                                            ukrlpModel.ContactAddress2     = pcs.ContactAddress.Locality;
                                        }
                                        ukrlpModel.ContactFullAddress += pcs.ContactAddress.Items != null &&
                                                                         pcs.ContactAddress.Items.GetLength(0) > 0
                                                ? pcs.ContactAddress.Items[0] + "<br>"
                                                : "";
                                        ukrlpModel.ContactTown += pcs.ContactAddress.Items != null &&
                                                                  pcs.ContactAddress.Items.GetLength(0) > 0
                                                ? pcs.ContactAddress.Items[0] + "<br>"
                                                : "";
                                        ukrlpModel.ContactFullAddress += pcs.ContactAddress.PostCode + "<br>";
                                        ukrlpModel.ContactPostcode    += pcs.ContactAddress.PostCode + "<br>";
                                    }
                                    break;
                                }
                            }

                            foreach (var vds in provider.VerificationDetails)
                            {
                                switch (vds.VerificationAuthority.ToLower())
                                {
                                case "companies house":
                                    ukrlpModel.CompanyRegistrationNumber = vds.VerificationID ?? "";
                                    break;

                                case "charity commission":
                                    ukrlpModel.CharityRegistrationNumber = vds.VerificationID ?? "";
                                    break;
                                }
                            }

                            // Check if UKPRN is already in use
                            var providerId = 0;
                            var uc         = UserContext.GetUserContext();
                            if (uc.IsProvider())
                            {
                                providerId = uc.ItemId.Value;
                            }
                            var dbProvider =
                                db.Providers.FirstOrDefault(x => x.Ukprn == ukprn && x.ProviderId != providerId);
                            if (dbProvider != null)
                            {
                                ukrlpModel.InUse = true;
                            }

                            // Check if UKPRN is in use at an organisation
                            if (!ukrlpModel.InUse)
                            {
                                var orgId = 0;
                                if (uc.IsOrganisation())
                                {
                                    orgId = uc.ItemId.Value;
                                }
                                var dbOrganisation =
                                    db.Organisations.FirstOrDefault(x => x.UKPRN == ukprn && x.OrganisationId != orgId);
                                if (dbOrganisation != null)
                                {
                                    ukrlpModel.InUse = true;
                                }
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(ukrlpModel.UKRLP))
                    {
                        break;
                    }
                }
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception)
            {
            }

            return(ukrlpModel);
        }
        public AddEditProviderModel(Provider provider, bool includeDisplayFields = false)
        {
            ProviderId        = provider.ProviderId;
            UKPRN             = provider.Ukprn;
            RecordStatusId    = provider.RecordStatusId;
            IsContractingBody = provider.IsContractingBody;
            ProviderTypeId    = provider.ProviderTypeId;
            ProviderTypeName  = provider.ProviderType.ProviderTypeName;
            ProviderName      = provider.ProviderName;
            TradingName       = provider.TradingName;
            ProviderAlias     = provider.ProviderNameAlias;
            UPIN             = provider.UPIN;
            Loans24Plus      = provider.Loans24Plus;
            ProviderRegionId = provider.ProviderRegionId;
            Email            = provider.Email;
            Website          = provider.Website;
            Telephone        = provider.Telephone;
            Fax = provider.Fax;
            ProviderTrackingUrl    = provider.ProviderTrackingUrl;
            VenueTrackingUrl       = provider.VenueTrackingUrl;
            CourseTrackingUrl      = provider.CourseTrackingUrl;
            BookingTrackingUrl     = provider.BookingTrackingUrl;
            DFE1619Funded          = provider.DFE1619Funded;
            SFAFunded              = provider.SFAFunded;
            DfEProviderTypeId      = DFE1619Funded ? provider.DfEProviderTypeId : null;
            DfEProviderStatusId    = DFE1619Funded ? provider.DfEProviderStatusId : null;
            DfELocalAuthorityId    = DFE1619Funded ? provider.DfELocalAuthorityId : null;
            DfERegionId            = DFE1619Funded ? provider.DfERegionId : null;
            DfEEstablishmentTypeId = DFE1619Funded ? provider.DfEEstablishmentTypeId : null;
            SecureAccessId         = provider.SecureAccessId;

            QualityEmailsPaused  = provider.QualityEmailsPaused;
            QualityEmailStatusId = provider.QualityEmailStatusId;

            Address = provider.AddressId == null
                ? new AddressViewModel {
                RegionId = 0
            }
                : new AddressViewModel(provider.Address)
            {
                RegionId = provider.ProviderRegionId ?? 0
            };

            UKRLPData = new UKRLPDataModel();

            BulkUploadPending = provider.BulkUploadPending;
            PublishData       = provider.PublishData;

            ApprenticeshipContract         = provider.ApprenticeshipContract;
            TASRefreshOverride             = provider.TASRefreshOverride;
            NationalApprenticeshipProvider = provider.NationalApprenticeshipProvider;
            MarketingInformation           = provider.MarketingInformation;

            if (includeDisplayFields)
            {
                RecordStatusName = provider.RecordStatu.RecordStatusName;

                DfEProviderTypeName = provider.DfEProviderType == null
                    ? null
                    : provider.DfEProviderType.DfEProviderTypeName;
                DfEProviderStatusName = provider.DfEProviderStatu == null
                    ? null
                    : provider.DfEProviderStatu.DfEProviderStatusName;
                DfELocalAuthorityName = provider.DfELocalAuthority == null
                    ? null
                    : provider.DfELocalAuthority.DfELocalAuthorityName;
                DfERegionName            = provider.DfERegion == null ? null : provider.DfERegion.DfERegionName;
                DfEEstablishmentTypeName = provider.DfEEstablishmentType == null
                    ? null
                    : provider.DfEEstablishmentType.DfEEstablishmentTypeName;

                if (provider.ProviderRegion != null)
                {
                    ProviderRegionName = provider.ProviderRegion.RegionName;
                }
            }

            this.RoATP            = provider.RoATPFFlag;
            RoATPProviderTypeName = provider.RoATPProviderType == null?AppGlobal.Language.GetText("AddEditProviderModel_RoATP_RoATPProviderTypeNone", "(none)") : provider.RoATPProviderType.Description;

            RoATPStartDate = provider.RoATPStartDate == null?AppGlobal.Language.GetText("AddEditProviderModel_RoATP_RoATPStartDateNA", "n/a") : provider.RoATPStartDate.Value.ToString(Constants.ConfigSettings.ShortDateFormat);

            this.HasBeenQAdForCompliance   = provider.ProviderQACompliances.Any();
            this.LastQAdForComplianceBy    = !provider.ProviderQACompliances.Any() ? null : provider.ProviderQACompliances.OrderByDescending(m => m.CreatedDateTimeUtc).First().AspNetUser.Name;
            this.LastQAdForComplianceOn    = !provider.ProviderQACompliances.Any() ? (DateTime?)null : DateTime.SpecifyKind(provider.ProviderQACompliances.OrderByDescending(m => m.CreatedDateTimeUtc).First().CreatedDateTimeUtc, DateTimeKind.Utc);
            this.HasPassedComplianceChecks = provider.ProviderQACompliances.Any() && provider.ProviderQACompliances.OrderByDescending(m => m.CreatedDateTimeUtc).First().Passed;

            this.HasBeenQAdForStyle   = provider.ProviderQAStyles.Any();
            this.LastQAdForStyleBy    = !provider.ProviderQAStyles.Any() ? null : provider.ProviderQAStyles.OrderByDescending(m => m.CreatedDateTimeUtc).First().AspNetUser.Name;
            this.LastQAdForStyleOn    = !provider.ProviderQAStyles.Any() ? (DateTime?)null : DateTime.SpecifyKind(provider.ProviderQAStyles.OrderByDescending(m => m.CreatedDateTimeUtc).First().CreatedDateTimeUtc, DateTimeKind.Utc);
            this.HasPassedStyleChecks = provider.ProviderQAStyles.Any() && provider.ProviderQAStyles.OrderByDescending(m => m.CreatedDateTimeUtc).First().Passed;

            this.UnableToCompleteProcess = provider.ProviderUnableToCompletes.Where(m => m.Active == true).Count() > 0;
            if (this.UnableToCompleteProcess)
            {
                this.UnableToCompleteDate = DateTime.SpecifyKind(provider.ProviderUnableToCompletes.Where(m => m.Active == true).OrderByDescending(m => m.CreatedDateTimeUtc).First().CreatedDateTimeUtc, DateTimeKind.Utc);
                this.UnableToCompleteName = provider.ProviderUnableToCompletes.Where(m => m.Active == true).OrderByDescending(m => m.CreatedDateTimeUtc).First().AspNetUser.Name;
            }

            this.PassedOverallQAChecks  = provider.PassedOverallQAChecks ? "1": "0";
            this.ApprenticeshipsQAed    = provider.GetQualityAssuredApprenticeshipCount();
            this.ApprenticeshipPassedQA = provider.GetQualityAssuredApprenticeshipPassedCount();
            this.NumberOfApprenticeshipsRequiredToQA = provider.GetNumberOfApprenticeshipsRequiredToQA();

            this.NumberOfLocations                = provider.Locations.Count();
            this.MaxNumberOfLocations             = provider.MaxLocations;
            this.MaxNumberOfLocationsOverriddenBy = provider.MaxLocationsUser != null ? provider.MaxLocationsUser.Name : null;
            this.MaxNumberOfLocationsOverriddenOn = provider.MaxLocationsDateTimeUtc.HasValue ? DateTime.SpecifyKind(provider.MaxLocationsDateTimeUtc.Value, DateTimeKind.Utc) : (DateTime?)null;

            this.DataReadyForQA = provider.DataReadyToQA;

            this.ShowSendFailedQAEmailButton = !provider.PassedOverallQAChecks && this.DataReadyForQA && this.HasBeenQAdForCompliance;

            this.CreatedDateTimeUTC = provider.CreatedDateTimeUtc;

            ImportBatches = provider.ImportBatches.OrderByDescending(x => x.ImportBatch.ImportBatchId).ToList();

            var latestRoATPRefreshed = provider.ProviderTASRefreshes.OrderByDescending(p => p.RefreshTimeUtc).FirstOrDefault();

            if (latestRoATPRefreshed != null)
            {
                RoATPLastRefreshed = DateTime.SpecifyKind(latestRoATPRefreshed.RefreshTimeUtc, DateTimeKind.Utc);
            }
        }