Example #1
0
        public IActionResult ChangeAddressGet(long id)
        {
            Organisation organisation = dataRepository.Get <Organisation>(id);

            if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber))
            {
                try
                {
                    CompaniesHouseCompany organisationFromCompaniesHouse =
                        companiesHouseApi.GetCompany(organisation.CompanyNumber);

                    OrganisationAddress addressFromCompaniesHouse =
                        UpdateFromCompaniesHouseService.CreateOrganisationAddressFromCompaniesHouseAddress(
                            organisationFromCompaniesHouse.RegisteredOfficeAddress);

                    if (!organisation.GetLatestAddress().AddressMatches(addressFromCompaniesHouse))
                    {
                        return(OfferNewCompaniesHouseAddress(organisation, addressFromCompaniesHouse));
                    }
                }
                catch (Exception ex)
                {
                    // Use Manual Change page instead
                    CustomLogger.Warning("Error from Companies House API", ex);
                }
            }

            // In all other cases...
            // * Organisation doesn't have a Companies House number
            // * CoHo API returns an error
            // * CoHo address matches Organisation address
            // ... send to the Manual Change page
            return(SendToManualChangePage(organisation));
        }
        private Organisation SaveNewOrganisation(string companyNumber, User requestingUser)
        {
            CompaniesHouseCompany companiesHouseCompany = companiesHouseApi.GetCompany(companyNumber);

            Organisation organisation = new Organisation
            {
                SectorType        = SectorTypes.Private, // All companies imported from CoHo are private-sector
                CompanyNumber     = companyNumber,
                EmployerReference = GenerateUniqueEmployerReference()
            };

            SetInitialStatus(organisation, requestingUser, SourceOfData.CompaniesHouse);

            AddOrganisationName(organisation, companiesHouseCompany.CompanyName, SourceOfData.CompaniesHouse);

            AddOrganisationAddress(organisation, companiesHouseCompany.RegisteredOfficeAddress);

            AddOrganisationSicCodes(organisation, companiesHouseCompany.SicCodes, SourceOfData.CompaniesHouse);

            SetInitialScopes(organisation);

            dataRepository.Insert(organisation);
            dataRepository.SaveChanges();
            return(organisation);
        }
        public void UpdateSicCode(Organisation organisation, CompaniesHouseCompany organisationFromCompaniesHouse)
        {
            var companySicCodes = organisationFromCompaniesHouse.SicCodes ?? new List <string>();

            RetireExtraSicCodes(organisation, companySicCodes);
            AddNewSicCodes(organisation, companySicCodes);
        }
        public IActionResult ChangeSicCodesGet(long id)
        {
            Organisation organisation = dataRepository.Get <Organisation>(id);

            if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber))
            {
                try
                {
                    CompaniesHouseCompany organisationFromCompaniesHouse =
                        companiesHouseApi.GetCompany(organisation.CompanyNumber);

                    List <string> sicCodeIdsFromCompaniesHouse = organisationFromCompaniesHouse.SicCodes;
                    List <string> sicCodesFromDatabase         = organisation.GetSicCodes().Select(osc => osc.SicCodeId.ToString()).ToList();

                    if (!sicCodesFromDatabase.ToHashSet().SetEquals(sicCodeIdsFromCompaniesHouse))
                    {
                        return(OfferNewCompaniesHouseSicCodes(organisation, sicCodeIdsFromCompaniesHouse));
                    }
                }
                catch (Exception ex)
                {
                    // Use Manual Change page instead
                    CustomLogger.Warning("Error from Companies House API", ex);
                }
            }

            // In all other cases...
            // * Organisation doesn't have a Companies House number
            // * CoHo API returns an error
            // * CoHo SIC codes match Organisation SIC codes
            // ... send to the Manual Change page
            return(SendToManualChangePage(organisation));
        }
        public IActionResult ChangeNameGet(long id)
        {
            Organisation organisation = dataRepository.Get <Organisation>(id);

            if (!string.IsNullOrWhiteSpace(organisation.CompanyNumber))
            {
                try
                {
                    CompaniesHouseCompany organisationFromCompaniesHouse =
                        companiesHouseApi.GetCompany(organisation.CompanyNumber);

                    string nameFromCompaniesHouse = organisationFromCompaniesHouse.CompanyName;

                    if (!string.Equals(organisation.OrganisationName, nameFromCompaniesHouse, StringComparison.Ordinal))
                    {
                        return(OfferNewCompaniesHouseName(organisation, nameFromCompaniesHouse));
                    }
                }
                catch (Exception ex)
                {
                    // Use Manual Change page instead
                    CustomLogger.Warning("Error from Companies House API", ex);
                }
            }

            // In all other cases...
            // * Organisation doesn't have a Companies House number
            // * CoHo API returns an error
            // * CoHo name matches Organisation name
            // ... send to the Manual Change page
            return(SendToManualChangePage(organisation));
        }
Example #6
0
        public void UpdateOrganisationDetails(long organisationId)
        {
            CustomLogger.Debug($"Loading organisation - OrganisationId({organisationId})");
            var organisation = dataRepository.Get <Organisation>(organisationId);

            CustomLogger.Debug($"Updating LastCheckedAgainstCompaniesHouse - OrganisationId({organisationId})");
            organisation.LastCheckedAgainstCompaniesHouse = VirtualDateTime.Now;
            dataRepository.SaveChanges();

            try
            {
                CustomLogger.Debug($"Calling CoHo API - OrganisationId({organisationId})");
                CompaniesHouseCompany organisationFromCompaniesHouse =
                    companiesHouseApi.GetCompany(organisation.CompanyNumber);

                try
                {
                    CustomLogger.Debug($"Updating SIC codes - OrganisationId({organisationId})");
                    UpdateSicCode(organisation, organisationFromCompaniesHouse);

                    CustomLogger.Debug($"Updating Address - OrganisationId({organisationId})");
                    UpdateAddress(organisation, organisationFromCompaniesHouse);

                    CustomLogger.Debug($"Updating Name - OrganisationId({organisationId})");
                    UpdateName(organisation, organisationFromCompaniesHouse);

                    CustomLogger.Debug($"Saving - OrganisationId({organisationId})");
                    dataRepository.SaveChanges();

                    CustomLogger.Debug($"Saved - OrganisationId({organisationId})");
                }
                catch (Exception ex)
                {
                    string message =
                        $"Update from Companies House: Failed to update database, organisation id = {organisationId}";
                    CustomLogger.Error(message, ex);
                }
            }
            catch (Exception ex)
            {
                string message =
                    $"Update from Companies House: Failed to get company data from companies house, organisation id = {organisationId}";
                CustomLogger.Error(message, ex);
            }
        }
Example #7
0
        public void UpdateName(Organisation organisation, CompaniesHouseCompany organisationFromCompaniesHouse)
        {
            string companyNameFromCompaniesHouse = organisationFromCompaniesHouse.CompanyName;

            companyNameFromCompaniesHouse = FirstHundredChars(companyNameFromCompaniesHouse);

            if (IsCompanyNameEqual(organisation.GetName(), companyNameFromCompaniesHouse))
            {
                return;
            }

            var nameToAdd = new OrganisationName {
                Organisation = organisation, Name = companyNameFromCompaniesHouse, Source = SourceOfChange, Created = VirtualDateTime.Now
            };

            organisation.OrganisationNames.Add(nameToAdd);
            organisation.OrganisationName = companyNameFromCompaniesHouse;
            dataRepository.Insert(nameToAdd);
        }
        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 void UpdateAddress(Organisation organisation, CompaniesHouseCompany organisationFromCompaniesHouse)
        {
            var companiesHouseAddress = organisationFromCompaniesHouse.RegisteredOfficeAddress;
            var newOrganisationAddressFromCompaniesHouse =
                CreateOrganisationAddressFromCompaniesHouseAddress(companiesHouseAddress);
            var oldOrganisationAddress = _organisationBusinessLogic.GetOrganisationAddress(organisation);

            if (oldOrganisationAddress.AddressMatches(newOrganisationAddressFromCompaniesHouse) ||
                IsNewOrganisationAddressNullOrEmpty(newOrganisationAddressFromCompaniesHouse))
            {
                return;
            }

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

            oldOrganisationAddress.Status     = AddressStatuses.Retired;
            oldOrganisationAddress.StatusDate = VirtualDateTime.Now;
            oldOrganisationAddress.Modified   = VirtualDateTime.Now;

            _DataRepository.Insert(newOrganisationAddressFromCompaniesHouse);
        }
Example #10
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);
        }