Submit social housing lettings and sales data (CORE)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
5.3 KiB

CLDC-2810 Send missing addresses template (#1952) * Add missing addresses csv job * Update missing addresses csv service methods * Add rake task * Update the job to send missing town or city templates * Update service to create mising town or city templates * Add send missing town or city csv rake task * Add IDs to the CSVs * Put all log in the same csv * Add issue type column * Write wrong uprn logs to csv * Add mailer methods * Skip uprn issue for specified orgs * Add sales csv rake task * set SKIP_UPRN_ISSUE_ORG_IDS on review apps * test * Update notify template IDs for testing * Initialize service with organisation instead of a hash * Add expiration time to url * Add optional tags and remove LA from csv * Extract log to csv methods * Update casing * Update old IDs in factories * Move constant * Extract some repeating scopes * Pass in organisations to skip instead of using an env var * update template id for sales * Update link expiry time and headers * Lower the threshold for testing * Add issue explanation to the email * Add how to fix * update emails * CLDC-2810 Create all addresses CSV (#1953) * Add rake tasks for creating all addresses CSV * Write headers if logs don't exist, update header names * Rename method * CLDC-2810 Correct addresses from csv (#1957) * Updating importing lettings addresses form csv * Add import_sales_addresses_from_csv rake * Allow correcting addresses from both templates * escape . * Reinfer LA if the postcode hasn't changed * Update labels and email content * Update missing addresses threshold * Remove unused env var
1 year ago
module Csv
class MissingAddressesCsvService
def initialize(organisation, skip_uprn_issue_organisations)
@organisation = organisation
@skip_uprn_issue_organisations = skip_uprn_issue_organisations
end
def create_missing_lettings_addresses_csv
logs_with_missing_addresses = @organisation.managed_lettings_logs
.imported_2023_with_old_form_id
.where(needstype: 1, address_line1: nil, town_or_city: nil, uprn_known: [0, nil])
logs_with_missing_town_or_city = @organisation.managed_lettings_logs
.imported_2023_with_old_form_id
.where(needstype: 1, town_or_city: nil, uprn_known: [0, nil])
.where.not(address_line1: nil)
logs_with_wrong_uprn = if @skip_uprn_issue_organisations.include?(@organisation.id)
[]
else
@organisation.managed_lettings_logs
.imported_2023
.where(needstype: 1)
.where.not(uprn: nil)
.where("uprn = propcode OR uprn = tenancycode OR town_or_city = 'Bristol'")
end
return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? && logs_with_wrong_uprn.empty?
CSV.generate(headers: true) do |csv|
csv << ["Issue type", "Log ID", "Tenancy start date", "Tenant code", "Property reference", "Log owner", "Owning organisation", "Managing organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"]
logs_with_missing_addresses.each do |log|
csv << ["Full address required"] + lettings_log_to_csv_row(log)
end
logs_with_missing_town_or_city.each do |log|
csv << ["Missing town or city"] + lettings_log_to_csv_row(log)
end
logs_with_wrong_uprn.each do |log|
csv << ["Incorrect UPRN"] + lettings_log_to_csv_row(log)
end
end
end
def create_missing_sales_addresses_csv
logs_with_missing_addresses = @organisation.sales_logs
.imported_2023_with_old_form_id
.where(address_line1: nil, town_or_city: nil, uprn_known: [0, nil])
logs_with_missing_town_or_city = @organisation.sales_logs
.imported_2023_with_old_form_id
.where(town_or_city: nil, uprn_known: [0, nil])
.where.not(address_line1: nil)
logs_with_wrong_uprn = if @skip_uprn_issue_organisations.include?(@organisation.id)
[]
else
@organisation.sales_logs
.imported_2023
.where.not(uprn: nil)
.where("uprn = purchid OR town_or_city = 'Bristol'")
end
return if logs_with_missing_addresses.empty? && logs_with_missing_town_or_city.empty? && logs_with_wrong_uprn.empty?
CSV.generate(headers: true) do |csv|
csv << ["Issue type", "Log ID", "Sale completion date", "Purchaser code", "Log owner", "Owning organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"]
logs_with_missing_addresses.each do |log|
csv << ["Full address required"] + sales_log_to_csv_row(log)
end
logs_with_missing_town_or_city.each do |log|
csv << ["Missing town or city"] + sales_log_to_csv_row(log)
end
logs_with_wrong_uprn.each do |log|
csv << ["Incorrect UPRN"] + sales_log_to_csv_row(log)
end
end
end
def create_lettings_addresses_csv
logs = @organisation.managed_lettings_logs.filter_by_year(2023)
CSV.generate(headers: true) do |csv|
csv << ["Log ID", "Tenancy start date", "Tenant code", "Property reference", "Log owner", "Owning organisation", "Managing organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"]
logs.each do |log|
csv << lettings_log_to_csv_row(log)
end
end
end
def create_sales_addresses_csv
logs = @organisation.sales_logs.filter_by_year(2023)
CSV.generate(headers: true) do |csv|
csv << ["Log ID", "Sale completion date", "Purchaser code", "Log owner", "Owning organisation", "UPRN", "Address Line 1", "Address Line 2 (optional)", "Town or City", "County (optional)", "Property's postcode"]
logs.each do |log|
csv << sales_log_to_csv_row(log)
end
end
end
private
def sales_log_to_csv_row(log)
[log.id,
log.saledate&.to_date,
log.purchid,
log.created_by&.email,
log.owning_organisation&.name,
log.uprn,
log.address_line1,
log.address_line2,
log.town_or_city,
log.county,
log.postcode_full]
end
def lettings_log_to_csv_row(log)
[log.id,
log.startdate&.to_date,
log.tenancycode,
log.propcode,
log.created_by&.email,
log.owning_organisation&.name,
log.managing_organisation&.name,
log.uprn,
log.address_line1,
log.address_line2,
log.town_or_city,
log.county,
log.postcode_full]
end
end
end