public OrganisationAddress CreateOrganisationAddressFromCompaniesHouseAddress(
            CompaniesHouseAddress companiesHouseAddress)
        {
            var  premisesAndLine1 = GetAddressLineFromPremisesAndAddressLine1(companiesHouseAddress);
            bool?isUkAddress      = null;

            if (_PostcodeChecker.IsValidPostcode(companiesHouseAddress?.PostalCode).Result)
            {
                isUkAddress = true;
            }

            return(new OrganisationAddress
            {
                Address1 = FirstHundredChars(companiesHouseAddress?.CareOf ?? premisesAndLine1),
                Address2 =
                    FirstHundredChars(companiesHouseAddress?.CareOf != null
                        ? premisesAndLine1
                        : companiesHouseAddress?.AddressLine2),
                Address3 = FirstHundredChars(companiesHouseAddress?.CareOf != null
                    ? companiesHouseAddress?.AddressLine2
                    : null),
                TownCity = FirstHundredChars(companiesHouseAddress?.Locality),
                County = FirstHundredChars(companiesHouseAddress?.Region),
                Country = companiesHouseAddress?.Country,
                PostCode = companiesHouseAddress?.PostalCode,
                PoBox = companiesHouseAddress?.PoBox,
                Status = AddressStatuses.Active,
                StatusDate = VirtualDateTime.Now,
                StatusDetails = DetailsOfChange,
                Modified = VirtualDateTime.Now,
                Created = VirtualDateTime.Now,
                Source = SourceOfChange,
                IsUkAddress = isUkAddress
            });
        }
        private void AddOrganisationAddress(Organisation organisation, CompaniesHouseAddress companiesHouseAddress)
        {
            OrganisationAddress organisationAddress =
                UpdateFromCompaniesHouseService.CreateOrganisationAddressFromCompaniesHouseAddress(companiesHouseAddress);

            organisationAddress.StatusDetails = "Initial import from CoHo";

            organisationAddress.Organisation = organisation;
            organisation.OrganisationAddresses.Add(organisationAddress);

            dataRepository.Insert(organisationAddress);
        }
        private void PopulateViewModelBasedOnCompanyNumber(AddOrganisationFoundViewModel viewModel)
        {
            CompaniesHouseCompany organisationFromCompaniesHouse = companiesHouseApi.GetCompany(viewModel.CompanyNumber);

            // Name
            viewModel.Name = organisationFromCompaniesHouse.CompanyName;

            // Address
            CompaniesHouseAddress coHoAddress         = organisationFromCompaniesHouse.RegisteredOfficeAddress;
            OrganisationAddress   organisationAddress = UpdateFromCompaniesHouseService.CreateOrganisationAddressFromCompaniesHouseAddress(coHoAddress);
            string addressString = organisationAddress?.GetAddressString() ?? "";

            viewModel.AddressLines = addressString.Split(",").ToList();

            // IsUkAddress
            string postCode = organisationFromCompaniesHouse.RegisteredOfficeAddress.PostalCode;

            viewModel.IsUkAddress = PostcodesIoApi.IsValidPostcode(postCode)
                ? AddOrganisationIsUkAddress.Yes
                : (AddOrganisationIsUkAddress?)null;
        }
        public OSPlacesAddress MatchCompaniesHouseAddress(CompaniesHouseAddress companiesHouseAddress)
        {
            int maxresults = 1;

            string matchedAddress = string.Format("{0}, {1}, {2}, {3}", companiesHouseAddress.address_line_1, companiesHouseAddress.address_line_2, companiesHouseAddress.locality, companiesHouseAddress.postal_code);

            string URL = string.Format("{0}/places/v1/addresses/find?query={1}&maxresults={2}&key={3}", this.TargetURL, matchedAddress, maxresults, this.APIKey);

            var response = this._httpclient.GetAsync(URL).Result;

            if (response.IsSuccessStatusCode)
            {
                var responseContent = response.Content;

                // by calling .Result you are synchronously reading the result
                string responseString = responseContent.ReadAsStringAsync().Result;

                //Deserialise
                OSPlacesResults OSPlacesResults = new OSPlacesResults();

                //Set up the object...
                MemoryStream stream1 = new MemoryStream();

                StreamWriter writer = new StreamWriter(stream1);
                writer.Write(responseString);
                writer.Flush();
                stream1.Position = 0;

                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(OSPlacesResults));
                OSPlacesResults = (OSPlacesResults)ser.ReadObject(stream1);

                if (OSPlacesResults != null && OSPlacesResults.DPA != null && OSPlacesResults.DPA.Length > 0)
                {
                    return(OSPlacesResults.DPA[0].Result);
                }
            }

            return(null);
        }
Esempio n. 5
0
        public void UpdateAddress(Organisation organisation, CompaniesHouseCompany organisationFromCompaniesHouse)
        {
            CompaniesHouseAddress companiesHouseAddress = organisationFromCompaniesHouse.RegisteredOfficeAddress;
            OrganisationAddress   newOrganisationAddressFromCompaniesHouse =
                CreateOrganisationAddressFromCompaniesHouseAddress(companiesHouseAddress);
            OrganisationAddress oldOrganisationAddress = organisation.GetLatestAddress();

            if (oldOrganisationAddress != null)
            {
                if (oldOrganisationAddress.AddressMatches(newOrganisationAddressFromCompaniesHouse) ||
                    IsNewOrganisationAddressNullOrEmpty(newOrganisationAddressFromCompaniesHouse))
                {
                    return;
                }
                oldOrganisationAddress.Status     = AddressStatuses.Retired;
                oldOrganisationAddress.StatusDate = VirtualDateTime.Now;
            }

            newOrganisationAddressFromCompaniesHouse.OrganisationId = organisation.OrganisationId;
            organisation.OrganisationAddresses.Add(newOrganisationAddressFromCompaniesHouse);

            dataRepository.Insert(newOrganisationAddressFromCompaniesHouse);
        }
 private string GetAddressLineFromPremisesAndAddressLine1(CompaniesHouseAddress companiesHouseAddress)
 {
     return(companiesHouseAddress?.Premises == null
         ? companiesHouseAddress?.AddressLine1
         : companiesHouseAddress?.Premises + "," + companiesHouseAddress?.AddressLine1);
 }