From d939e88435aa2e646fa395c675a642ca02726a7d Mon Sep 17 00:00:00 2001 From: Phil Lee Date: Thu, 11 May 2023 12:40:36 +0100 Subject: [PATCH] CLDC-2316 Bulk upload sales 2023 (#1603) # Context - https://digital.dclg.gov.uk/jira/browse/CLDC-2316 - Implement bulk upload sales for new collection year 2023 - This is a first pass implementation and will probably have some bugs in it and we can address over time # Changes - Add CSV parser for sales 2023 to handle CSV structure - Tweak collection window validation so error now contextual to year selected for upload - Handle arbitrary ordering of CSV columns - Fix ordering of errors in report and now ordered by cell - Added `Upload your file again` link styled as button on error report to match lettings experience - Update tooling to convert logs to 2023 csv rows with support for random column ordering # Known issues - There seem to be some issues with how UPRN is handled if the UPRN cannot be validated. - For the above I think there is dependency on https://github.com/communitiesuk/submit-social-housing-lettings-and-sales-data/pull/1570 which should clear any errored fields so users can continue to create logs and fix within the service --- app/controllers/lettings_logs_controller.rb | 2 +- app/models/bulk_upload.rb | 4 + .../lettings/year2022/row_parser.rb | 6 +- .../lettings/year2023/row_parser.rb | 6 +- .../bulk_upload/sales/year2022/row_parser.rb | 8 +- .../bulk_upload/sales/year2023/csv_parser.rb | 91 ++ .../bulk_upload/sales/year2023/row_parser.rb | 1159 +++++++++++++++++ .../bulk_upload_sales_results/show.html.erb | 4 +- config/locales/en.yml | 2 +- .../lettings_logs_controller_spec.rb | 21 + spec/factories/sales_log.rb | 3 + .../requests/lettings_logs_controller_spec.rb | 6 +- .../sales/year2023/csv_parser_spec.rb | 148 +++ .../sales/year2023/row_parser_spec.rb | 704 ++++++++++ spec/support/bulk_upload/sales_log_to_csv.rb | 41 + 15 files changed, 2189 insertions(+), 16 deletions(-) create mode 100644 app/services/bulk_upload/sales/year2023/csv_parser.rb create mode 100644 app/services/bulk_upload/sales/year2023/row_parser.rb create mode 100644 spec/controllers/lettings_logs_controller_spec.rb create mode 100644 spec/services/bulk_upload/sales/year2023/csv_parser_spec.rb create mode 100644 spec/services/bulk_upload/sales/year2023/row_parser_spec.rb diff --git a/app/controllers/lettings_logs_controller.rb b/app/controllers/lettings_logs_controller.rb index 169ff42e3..6b1d4a531 100644 --- a/app/controllers/lettings_logs_controller.rb +++ b/app/controllers/lettings_logs_controller.rb @@ -118,7 +118,7 @@ private end def redirect_if_bulk_upload_resolved - if @bulk_upload && @bulk_upload.lettings_logs.in_progress.count.zero? + if @bulk_upload && @bulk_upload.lettings? && @bulk_upload.lettings_logs.in_progress.count.zero? redirect_to resume_bulk_upload_lettings_result_path(@bulk_upload) end end diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index 66e83cd85..ed1e9ab21 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -14,6 +14,10 @@ class BulkUpload < ApplicationRecord "#{year}/#{year - 2000 + 1}" end + def end_year + year + 1 + end + def logs if lettings? lettings_logs diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index 953487cd7..2eac1d6c4 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -691,9 +691,9 @@ private return if start_date.blank? || bulk_upload.form.blank? unless bulk_upload.form.valid_start_date_for_form?(start_date) - errors.add(:field_96, I18n.t("validations.date.outside_collection_window"), category: :setup) - errors.add(:field_97, I18n.t("validations.date.outside_collection_window"), category: :setup) - errors.add(:field_98, I18n.t("validations.date.outside_collection_window"), category: :setup) + errors.add(:field_96, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_97, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_98, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) end end diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index 85abd180d..bd870d748 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -635,9 +635,9 @@ private return if start_date.blank? || bulk_upload.form.blank? unless bulk_upload.form.valid_start_date_for_form?(start_date) - errors.add(:field_7, I18n.t("validations.date.outside_collection_window"), category: :setup) - errors.add(:field_8, I18n.t("validations.date.outside_collection_window"), category: :setup) - errors.add(:field_9, I18n.t("validations.date.outside_collection_window"), category: :setup) + errors.add(:field_7, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_8, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_9, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) end end diff --git a/app/services/bulk_upload/sales/year2022/row_parser.rb b/app/services/bulk_upload/sales/year2022/row_parser.rb index 4383898fb..9cc2f1f02 100644 --- a/app/services/bulk_upload/sales/year2022/row_parser.rb +++ b/app/services/bulk_upload/sales/year2022/row_parser.rb @@ -811,7 +811,7 @@ private def soctenant return unless field_39 && field_113 - if (field_39 == 1 || fields_39 == 2) && field_113 == 1 + if (field_39 == 1 || field_39 == 2) && field_113 == 1 1 elsif field_113 == 1 2 @@ -941,9 +941,9 @@ private return if saledate.blank? || bulk_upload.form.blank? unless bulk_upload.form.valid_start_date_for_form?(saledate) - errors.add(:field_2, I18n.t("validations.date.outside_collection_window"), category: :setup) - errors.add(:field_3, I18n.t("validations.date.outside_collection_window"), category: :setup) - errors.add(:field_4, I18n.t("validations.date.outside_collection_window"), category: :setup) + errors.add(:field_2, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_3, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_4, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) end end diff --git a/app/services/bulk_upload/sales/year2023/csv_parser.rb b/app/services/bulk_upload/sales/year2023/csv_parser.rb new file mode 100644 index 000000000..2080dc4a1 --- /dev/null +++ b/app/services/bulk_upload/sales/year2023/csv_parser.rb @@ -0,0 +1,91 @@ +require "csv" + +class BulkUpload::Sales::Year2023::CsvParser + MIN_COLUMNS = 135 + MAX_COLUMNS = 142 + + attr_reader :path + + def initialize(path:) + @path = path + end + + def row_offset + if with_headers? + rows.find_index { |row| row[0].match(/field number/i) } + 1 + else + 0 + end + end + + def col_offset + with_headers? ? 1 : 0 + end + + def cols + @cols ||= ("A".."EK").to_a + end + + def row_parsers + @row_parsers ||= body_rows.map do |row| + stripped_row = row[col_offset..] + hash = Hash[field_numbers.zip(stripped_row)] + + BulkUpload::Sales::Year2023::RowParser.new(hash) + end + end + + def body_rows + rows[row_offset..] + end + + def rows + @rows ||= CSV.parse(normalised_string, row_sep:) + end + + def column_for_field(field) + cols[field_numbers.find_index(field) + col_offset] + end + +private + + def default_field_numbers + [6, 3, 4, 5, nil, 28, 30, 38, 47, 51, 55, 59, 31, 39, 48, 52, 56, 60, 37, 46, 50, 54, 58, 35, 43, 49, 53, 57, 61, 32, 33, 78, 80, 79, 81, 83, 84, nil, 62, 66, 64, 65, 63, 67, 69, 70, 68, 76, 77, 16, 17, 18, 26, 24, 25, 27, 8, 91, 95, 96, 97, 92, 93, 94, 98, 100, 101, 103, 104, 106, 110, 111, 112, 113, 114, 9, 116, 117, 118, 120, 124, 125, 126, 10, 11, nil, 127, 129, 133, 134, 135, 1, 2, nil, 73, nil, 75, 107, 108, 121, 122, 130, 131, 82, 109, 123, 132, 115, 15, 86, 87, 29, 7, 12, 13, 14, 36, 44, 45, 88, 89, 102, 105, 119, 128, 19, 20, 21, 22, 23, 34, 40, 41, 42, 71, 72, 74, 85, 90, 99].map do |number| + if number.to_s.match?(/^[0-9]+$/) + "field_#{number}" + else + "field_blank" + end + end + end + + def field_numbers + @field_numbers ||= if with_headers? + rows[row_offset - 1][col_offset..].map { |number| number.to_s.match?(/^[0-9]+$/) ? "field_#{number}" : "field_blank" } + else + default_field_numbers + end + end + + def headers + @headers ||= ("field_1".."field_135").to_a + end + + def with_headers? + rows.map { |r| r[0] }.any? { |cell| cell&.match?(/field number/i) } + end + + def row_sep + "\n" + end + + def normalised_string + return @normalised_string if @normalised_string + + @normalised_string = File.read(path, encoding: "bom|utf-8") + @normalised_string.gsub!("\r\n", "\n") + @normalised_string.scrub!("") + + @normalised_string + end +end diff --git a/app/services/bulk_upload/sales/year2023/row_parser.rb b/app/services/bulk_upload/sales/year2023/row_parser.rb new file mode 100644 index 000000000..67cf9a284 --- /dev/null +++ b/app/services/bulk_upload/sales/year2023/row_parser.rb @@ -0,0 +1,1159 @@ +class BulkUpload::Sales::Year2023::RowParser + include ActiveModel::Model + include ActiveModel::Attributes + + QUESTIONS = { + field_1: "Which organisation owned this property before the sale?", + field_2: "Username", + field_3: "What is the day of the sale completion date? - DD", + field_4: "What is the month of the sale completion date? - MM", + field_5: "What is the year of the sale completion date? - YY", + field_6: "What is the purchaser code?", + field_7: "Was this purchase made through an ownership scheme?", + field_8: "What is the type of shared ownership sale?", + field_9: "What is the type of discounted ownership sale?", + field_10: "What is the type of outright sale?", + + field_11: "If 'other', what is the 'other' type?", + field_12: "Is the buyer a company?", + field_13: "Will the buyers live in the property?", + field_14: "Is this a joint purchase?", + field_15: "Are there more than two joint purchasers of this property?", + field_16: "How many bedrooms does the property have?", + field_17: "What type of unit is the property?", + field_18: "Which type of bulding is the property?", + field_19: "If known, enter this property's UPRN", + field_20: "Address line 1", + + field_21: "Address line 2", + field_22: "Town or city", + field_23: "County", + field_24: "Part 1 of postcode of property", + field_25: "Part 2 of postcode of property", + field_26: "What is the local authority of the property?", + field_27: "Is the property built or adapted to wheelchair user standards?", + field_28: "Was the buyer interviewed for any of the answers you will provide on this log?", + field_29: "Data Protection question", + field_30: "Age of buyer 1", + + field_31: "Gender identity of buyer 1", + field_32: "What is buyer 1's ethnic group?", + field_33: "What is buyer 1's nationality?", + field_34: "If 'Any other country', what is buyer 1's nationality?", + field_35: "Working situation of buyer 1", + field_36: "Will buyer 1 live in the property?", + field_37: "Relationship to buyer 1 for person 2", + field_38: "Age of person 2", + field_39: "Gender identity of person 2", + field_40: "Which of the following best describes buyer 2's ethnic background?", + + field_41: "What is buyer 2's nationality?", + field_42: "If 'Any other country', what is buyer 2's nationality?", + field_43: "What is buyer 2 or person 2's working situation?", + field_44: "Will buyer 2 live in the property?", + field_45: "Besides the buyers, how many people will live in the property?", + field_46: "Relationship to buyer 1 for person 3", + field_47: "Age of person 3", + field_48: "Gender identity of person 3", + field_49: "Working situation of person 3", + field_50: "Relationship to buyer 1 for person 4", + + field_51: "Age of person 4", + field_52: "Gender identity of person 4", + field_53: "Working situation of person 4", + field_54: "Relationship to buyer 1 for person 5", + field_55: "Age of person 5", + field_56: "Gender identity of person 5", + field_57: "Working situation of person 5", + field_58: "Relationship to buyer 1 for person 6", + field_59: "Age of person 6", + field_60: "Gender identity of person 6", + + field_61: "Working situation of person 6", + field_62: "What was buyer 1's previous tenure?", + field_63: "Do you know the postcode of buyer 1's last settled home?", + field_64: "Part 1 of postcode of buyer 1's last settled home", + field_65: "Part 2 of postcode of buyer 1's last settled home", + field_66: "What is the local authority of buyer 1's last settled home?", + field_67: "Was the buyer registered with their PRP (HA)?", + field_68: "Was the buyer registered with another PRP (HA)?", + field_69: "Was the buyer registered with the local authority?", + field_70: "Was the buyer registered with a Help to Buy agent?", + + field_71: "At the time of purchase, was buyer 2 living at the same address as buyer 1?", + field_72: "What was buyer 2's previous tenure?", + field_73: "Has the buyer ever served in the UK Armed Forces and for how long?", + field_74: "Is the buyer still serving in the UK armed forces?", + field_75: "Are any of the buyers a spouse or civil partner of a UK Armed Forces regular who died in service within the last 2 years?", + field_76: "Does anyone in the household consider themselves to have a disability?", + field_77: "Does anyone in the household use a wheelchair?", + field_78: "What is buyer 1's gross annual income?", + field_79: "Was buyer 1's income used for a mortgage application?", + field_80: "What is buyer 2's gross annual income?", + + field_81: "Was buyer 2's income used for a mortgage application?", + field_82: "Were the buyers receiving any of these housing-related benefits immediately before buying this property?", + field_83: "What is the total amount the buyers had in savings before they paid any deposit for the property?", + field_84: "Have any of the purchasers previously owned a property?", + field_85: "Was the previous property under shared ownership?", + field_86: "How long have the buyers been living in the property before the purchase? - Shared ownership", + field_87: "Is this a staircasing transaction?", + field_88: "What percentage of the property has been bought in this staircasing transaction?", + field_89: "What percentage of the property does the buyer now own in total?", + field_90: "Was this transaction part of a back-to-back staircasing transaction to facilitate sale of the home on the open market?", + + field_91: "Is this a resale?", + field_92: "What is the day of the practical completion or handover date?", + field_93: "What is the month of the practical completion or handover date?", + field_94: "What is the year of the practical completion or handover date?", + field_95: "What is the day of the practical completion or handover date?", + field_96: "What is the month of the practical completion or handover date?", + field_97: "What is the day of the exchange of contracts date?", + field_98: "Was the household re-housed under a local authority nominations agreement?", + field_99: "Was the buyer a private registered provider, housing association or local authority tenant immediately before this sale?", + field_100: "How many bedrooms did the buyer's previous property have?", + + field_101: "What was the type of the buyer's previous property?", + field_102: "What was the rent type of the buyer's previous property?", + field_103: "What was the full purchase price?", + field_104: "What was the initial percentage equity stake purchased?", + field_105: "Was a mortgage used for the purchase of this property? - Shared ownership", + field_106: "What is the mortgage amount?", + field_107: "What is the name of the mortgage lender? - Shared ownership", + field_108: "If 'other', what is the name of the mortgage lender?", + field_109: "What is the length of the mortgage in years? - Shared ownership", + field_110: "Does this include any extra borrowing?", + + field_111: "How much was the cash deposit paid on the property?", + field_112: "How much cash discount was given through Social Homebuy?", + field_113: "What is the basic monthly rent?", + field_114: "What are the total monthly leasehold charges for the property?", + field_115: "How long have the buyers been living in the property before the purchase? - Discounted ownership", + field_116: "What was the full purchase price?", + field_117: "What was the amount of any loan, grant, discount or subsidy given?", + field_118: "What was the percentage discount?", + field_119: "Was a mortgage used for the purchase of this property? - Discounted ownership", + field_120: "What is the mortgage amount?", + + field_121: "What is the name of the mortgage lender? - Discounted ownership", + field_122: "If 'other', what is the name of the mortgage lender?", + field_123: "What is the length of the mortgage in years? - Discounted ownership", + field_124: "Does this include any extra borrowing?", + field_125: "How much was the cash deposit paid on the property?", + field_126: "What are the total monthly leasehold charges for the property?", + field_127: "What is the full purchase price?", + field_128: "Was a mortgage used for the purchase of this property? - Outright sale", + field_129: "What is the mortgage amount?", + field_130: "What is the name of the mortgage lender? - Outright sale", + + field_131: "If 'other', what is the name of the mortgage lender?", + field_132: "What is the length of the mortgage in years? - Outright sale", + field_133: "Does this include any extra borrowing?", + field_134: "How much was the cash deposit paid on the property?", + field_135: "What are the total monthly leasehold charges for the property?", + }.freeze + + attribute :bulk_upload + attribute :block_log_creation, :boolean, default: -> { false } + + attribute :field_blank + + attribute :field_1, :string + attribute :field_2, :string + attribute :field_3, :integer + attribute :field_4, :integer + attribute :field_5, :integer + attribute :field_6, :string + attribute :field_7, :integer + attribute :field_8, :integer + attribute :field_9, :integer + attribute :field_10, :integer + + attribute :field_11, :string + attribute :field_12, :integer + attribute :field_13, :integer + attribute :field_14, :integer + attribute :field_15, :integer + attribute :field_16, :integer + attribute :field_17, :integer + attribute :field_18, :integer + attribute :field_19, :string + attribute :field_20, :string + + attribute :field_21, :string + attribute :field_22, :string + attribute :field_23, :string + attribute :field_24, :string + attribute :field_25, :string + attribute :field_26, :string + attribute :field_27, :integer + attribute :field_28, :integer + attribute :field_29, :integer + attribute :field_30, :string + + attribute :field_31, :string + attribute :field_32, :integer + attribute :field_33, :integer + attribute :field_34, :string + attribute :field_35, :integer + attribute :field_36, :integer + attribute :field_37, :string + attribute :field_38, :string + attribute :field_39, :string + attribute :field_40, :integer + + attribute :field_41, :integer + attribute :field_42, :string + attribute :field_43, :integer + attribute :field_44, :integer + attribute :field_45, :integer + attribute :field_46, :string + attribute :field_47, :string + attribute :field_48, :string + attribute :field_49, :integer + attribute :field_50, :string + + attribute :field_51, :string + attribute :field_52, :string + attribute :field_53, :integer + attribute :field_54, :string + attribute :field_55, :string + attribute :field_56, :string + attribute :field_57, :integer + attribute :field_58, :string + attribute :field_59, :string + attribute :field_60, :string + + attribute :field_61, :integer + attribute :field_62, :integer + attribute :field_63, :integer + attribute :field_64, :string + attribute :field_65, :string + attribute :field_66, :string + attribute :field_67, :integer + attribute :field_68, :integer + attribute :field_69, :integer + attribute :field_70, :integer + + attribute :field_71, :integer + attribute :field_72, :string + attribute :field_73, :integer + attribute :field_74, :integer + attribute :field_75, :integer + attribute :field_76, :integer + attribute :field_77, :integer + attribute :field_78, :integer + attribute :field_79, :integer + attribute :field_80, :integer + + attribute :field_81, :integer + attribute :field_82, :integer + attribute :field_83, :integer + attribute :field_84, :integer + attribute :field_85, :integer + attribute :field_86, :integer + attribute :field_87, :integer + attribute :field_88, :integer + attribute :field_89, :integer + attribute :field_90, :integer + + attribute :field_91, :integer + attribute :field_92, :integer + attribute :field_93, :integer + attribute :field_94, :integer + attribute :field_95, :integer + attribute :field_96, :integer + attribute :field_97, :integer + attribute :field_98, :integer + attribute :field_99, :integer + attribute :field_100, :integer + + attribute :field_101, :integer + attribute :field_102, :integer + attribute :field_103, :integer + attribute :field_104, :integer + attribute :field_105, :integer + attribute :field_106, :integer + attribute :field_107, :integer + attribute :field_108, :string + attribute :field_109, :integer + attribute :field_110, :integer + + attribute :field_111, :integer + attribute :field_112, :integer + attribute :field_113, :decimal + attribute :field_114, :decimal + attribute :field_115, :integer + attribute :field_116, :integer + attribute :field_117, :integer + attribute :field_118, :integer + attribute :field_119, :integer + attribute :field_120, :integer + + attribute :field_121, :integer + attribute :field_122, :string + attribute :field_123, :integer + attribute :field_124, :integer + attribute :field_125, :integer + attribute :field_126, :integer + attribute :field_127, :integer + attribute :field_128, :integer + attribute :field_129, :integer + attribute :field_130, :integer + + attribute :field_131, :string + attribute :field_132, :integer + attribute :field_133, :integer + attribute :field_134, :integer + attribute :field_135, :integer + + validates :field_3, + presence: { + message: I18n.t("validations.not_answered", question: "sale completion date (day)"), + category: :setup, + }, + on: :after_log + + validates :field_4, + presence: { + message: I18n.t("validations.not_answered", question: "sale completion date (month)"), + category: :setup, + }, on: :after_log + + validates :field_5, + presence: { + message: I18n.t("validations.not_answered", question: "sale completion date (year)"), + category: :setup, + }, + format: { + with: /\A\d{2}\z/, + message: I18n.t("validations.setup.saledate.year_not_two_digits"), + category: :setup, + if: proc { field_5.present? }, + }, on: :after_log + + validates :field_7, + presence: { + message: I18n.t("validations.not_answered", question: "ownership type"), + category: :setup, + }, + on: :after_log + + validates :field_8, + presence: { + message: I18n.t("validations.not_answered", question: "shared ownership type"), + category: :setup, + if: :shared_ownership?, + }, + on: :after_log + + validates :field_9, + presence: { + message: I18n.t("validations.not_answered", question: "shared ownership type"), + category: :setup, + if: :discounted_ownership?, + }, + on: :after_log + + validates :field_10, + presence: { + message: I18n.t("validations.not_answered", question: "shared ownership type"), + category: :setup, + if: :outright_sale?, + }, + on: :after_log + + validates :field_13, + presence: { + message: I18n.t("validations.not_answered", question: "will the buyers live in the property"), + category: :setup, + if: :outright_sale?, + }, + on: :after_log + + validates :field_12, + presence: { + message: I18n.t("validations.not_answered", question: "company buyer"), + category: :setup, + if: :outright_sale?, + }, + on: :after_log + + validates :field_14, + presence: { + message: I18n.t("validations.not_answered", question: "joint purchase"), + category: :setup, + if: :joint_purchase_asked?, + }, + on: :after_log + + validates :field_15, + presence: { + message: I18n.t("validations.not_answered", question: "more than 2 buyers"), + category: :setup, + if: :joint_purchase?, + }, + on: :after_log + + validate :validate_nulls, on: :after_log + validate :validate_valid_radio_option, on: :before_log + + validate :validate_owning_org_data_given, on: :after_log + validate :validate_owning_org_exists, on: :after_log + validate :validate_owning_org_permitted, on: :after_log + + validate :validate_created_by_exists, on: :after_log + validate :validate_created_by_related, on: :after_log + validate :validate_relevant_collection_window, on: :after_log + + validate :validate_uprn_exists_if_any_key_adddress_fields_are_blank, on: :after_log + validate :validate_address_line_1, on: :after_log + validate :validate_town_or_city, on: :after_log + validate :validate_postcode, on: :after_log + + def self.question_for_field(field) + QUESTIONS[field] + end + + def attribute_set + @attribute_set ||= instance_variable_get(:@attributes) + end + + def blank_row? + attribute_set + .to_hash + .reject { |k, _| %w[bulk_upload block_log_creation].include?(k) } + .values + .compact + .empty? + end + + def log + @log ||= SalesLog.new(attributes_for_log) + end + + def valid? + errors.clear + + return true if blank_row? + + super(:before_log) + before_errors = errors.dup + + log.valid? + + super(:after_log) + errors.merge!(before_errors) + + log.errors.each do |error| + fields = field_mapping_for_errors[error.attribute] || [] + + fields.each do |field| + unless errors.include?(field) + errors.add(field, error.message) + end + end + end + + errors.blank? + end + + def block_log_creation? + block_log_creation + end + + def inspect + "#" + end + +private + + def prevtenbuy2 + case field_72 + when "R" + 0 + else + field_72 + end + end + + def infer_buyer2_ethnic_group_from_ethnic + case field_40 + when 1, 2, 3, 18 + 0 + when 4, 5, 6, 7 + 1 + when 8, 9, 10, 11, 15 + 2 + when 12, 13, 14 + 3 + when 16, 19 + 4 + else + field_40 + end + end + + def validate_uprn_exists_if_any_key_adddress_fields_are_blank + if field_19.blank? && (field_20.blank? || field_22.blank? || field_24.blank? || field_25.blank?) + errors.add(:field_19, I18n.t("validations.not_answered", question: "UPRN")) + end + end + + def validate_address_line_1 + if field_19.blank? && field_20.blank? + errors.add(:field_20, I18n.t("validations.not_answered", question: "address line 1")) + end + end + + def validate_town_or_city + if field_19.blank? && field_22.blank? + errors.add(:field_22, I18n.t("validations.not_answered", question: "town or city")) + end + end + + def validate_postcode + if field_19.blank? && field_24.blank? + errors.add(:field_24, I18n.t("validations.not_answered", question: "part 1 of the property's postcode")) + end + + if field_19.blank? && field_25.blank? + errors.add(:field_25, I18n.t("validations.not_answered", question: "part 2 of the property's postcode")) + end + end + + def shared_ownership? + field_7 == 1 + end + + def discounted_ownership? + field_7 == 2 + end + + def outright_sale? + field_7 == 3 + end + + def joint_purchase? + field_116 == 1 + end + + def joint_purchase_asked? + shared_ownership? || discounted_ownership? || field_114 == 2 + end + + def field_mapping_for_errors + { + purchid: %i[field_6], + saledate: %i[field_3 field_4 field_5], + noint: %i[field_28], + age1_known: %i[field_30], + age1: %i[field_30], + age2_known: %i[field_38], + age2: %i[field_38], + age3_known: %i[field_47], + age3: %i[field_47], + age4_known: %i[field_51], + age4: %i[field_51], + age5_known: %i[field_55], + age5: %i[field_55], + age6_known: %i[field_59], + age6: %i[field_59], + sex1: %i[field_31], + sex2: %i[field_39], + sex3: %i[field_48], + + sex4: %i[field_52], + sex5: %i[field_56], + sex6: %i[field_60], + relat2: %i[field_37], + relat3: %i[field_46], + relat4: %i[field_50], + relat5: %i[field_54], + relat6: %i[field_58], + + ecstat1: %i[field_35], + ecstat2: %i[field_43], + ecstat3: %i[field_49], + + ecstat4: %i[field_53], + ecstat5: %i[field_57], + ecstat6: %i[field_61], + ethnic_group: %i[field_32], + ethnic: %i[field_32], + national: %i[field_33], + income1nk: %i[field_78], + income1: %i[field_78], + income2nk: %i[field_80], + income2: %i[field_80], + inc1mort: %i[field_79], + inc2mort: %i[field_81], + savingsnk: %i[field_83], + savings: %i[field_83], + prevown: %i[field_84], + prevten: %i[field_62], + prevloc: %i[field_66], + previous_la_known: %i[field_66], + ppcodenk: %i[field_63], + ppostcode_full: %i[field_64 field_65], + pregyrha: %i[field_67], + pregla: %i[field_69], + pregghb: %i[field_70], + pregother: %i[field_68], + pregblank: %i[field_67 field_69 field_70 field_68], + disabled: %i[field_76], + + wheel: %i[field_77], + beds: %i[field_16], + proptype: %i[field_17], + builtype: %i[field_18], + la_known: %i[field_26], + la: %i[field_26], + + is_la_inferred: %i[field_26], + pcodenk: %i[field_24 field_25], + postcode_full: %i[field_24 field_25], + wchair: %i[field_27], + + type: %i[field_8 field_9 field_10 field_7], + resale: %i[field_91], + hodate: %i[field_95 field_96 field_97], + exdate: %i[field_92 field_93 field_94], + + lanomagr: %i[field_98], + frombeds: %i[field_100], + fromprop: %i[field_101], + value: %i[field_103 field_116 field_127], + equity: %i[field_104], + mortgage: %i[field_106 field_120 field_129], + extrabor: %i[field_110 field_124 field_133], + deposit: %i[field_111 field_125 field_134], + cashdis: %i[field_112], + mrent: %i[field_113], + + has_mscharge: %i[field_114 field_126 field_135], + mscharge: %i[field_114 field_126 field_135], + grant: %i[field_117], + discount: %i[field_118], + othtype: %i[field_11], + owning_organisation_id: %i[field_1], + created_by: %i[field_2], + hhregres: %i[field_73], + hhregresstill: %i[field_73], + armedforcesspouse: %i[field_75], + + mortgagelender: %i[field_107 field_121 field_130], + mortgagelenderother: %i[field_108 field_122 field_131], + + hb: %i[field_82], + mortlen: %i[field_109 field_123 field_132], + proplen: %i[field_115 field_86], + + jointmore: %i[field_15], + staircase: %i[field_87], + privacynotice: %i[field_29], + ownershipsch: %i[field_7], + companybuy: %i[field_12], + buylivein: %i[field_13], + + jointpur: %i[field_14], + buy1livein: %i[field_36], + buy2livein: %i[field_44], + hholdcount: %i[field_45], + stairbought: %i[field_88], + stairowned: %i[field_89], + socprevten: %i[field_102], + mortgageused: %i[field_105 field_119 field_128], + soctenant: %i[field_99], + } + end + + def attributes_for_log + attributes = {} + + attributes["purchid"] = field_6 + attributes["saledate"] = saledate + attributes["noint"] = 2 if field_28 == 1 + + attributes["age1_known"] = age1_known? + attributes["age1"] = field_30 if attributes["age1_known"].zero? && field_30&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age2_known"] = age2_known? + attributes["age2"] = field_38 if attributes["age2_known"].zero? && field_38&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age3_known"] = age3_known? + attributes["age3"] = field_47 if attributes["age3_known"].zero? && field_47&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age4_known"] = age4_known? + attributes["age4"] = field_51 if attributes["age4_known"].zero? && field_51&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age5_known"] = age5_known? + attributes["age5"] = field_55 if attributes["age5_known"].zero? && field_55&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["age6_known"] = age6_known? + attributes["age6"] = field_59 if attributes["age6_known"].zero? && field_59&.match(/\A\d{1,3}\z|\AR\z/) + + attributes["sex1"] = field_31 + attributes["sex2"] = field_39 + attributes["sex3"] = field_48 + attributes["sex4"] = field_52 + attributes["sex5"] = field_56 + attributes["sex6"] = field_60 + + attributes["relat2"] = field_37 + attributes["relat3"] = field_46 + attributes["relat4"] = field_50 + attributes["relat5"] = field_54 + attributes["relat6"] = field_58 + + attributes["ecstat1"] = field_35 + attributes["ecstat2"] = field_43 + attributes["ecstat3"] = field_49 + attributes["ecstat4"] = field_53 + attributes["ecstat5"] = field_57 + attributes["ecstat6"] = field_61 + + attributes["details_known_2"] = details_known?(2) + attributes["details_known_3"] = details_known?(3) + attributes["details_known_4"] = details_known?(4) + attributes["details_known_5"] = details_known?(5) + attributes["details_known_6"] = details_known?(6) + + attributes["ethnic_group"] = ethnic_group_from_ethnic + attributes["ethnic"] = field_32 + attributes["national"] = field_33 + + attributes["income1nk"] = field_78.present? ? 0 : 1 + attributes["income1"] = field_78 + + attributes["income2nk"] = field_80.present? ? 0 : 1 + attributes["income2"] = field_80 + + attributes["inc1mort"] = field_79 + attributes["inc2mort"] = field_81 + + attributes["savingsnk"] = field_83.present? ? 0 : 1 + attributes["savings"] = field_83 + attributes["prevown"] = field_84 + + attributes["prevten"] = field_62 + attributes["prevloc"] = field_66 + attributes["previous_la_known"] = previous_la_known + attributes["ppcodenk"] = field_63 + attributes["ppostcode_full"] = ppostcode_full + + attributes["pregyrha"] = field_67 + attributes["pregla"] = field_69 + attributes["pregghb"] = field_70 + attributes["pregother"] = field_68 + attributes["pregblank"] = 1 if [field_67, field_69, field_70, field_78].all?(&:blank?) + + attributes["disabled"] = field_76 + attributes["wheel"] = field_77 + attributes["beds"] = field_16 + attributes["proptype"] = field_17 + attributes["builtype"] = field_18 + attributes["la_known"] = field_26.present? ? 1 : 0 + attributes["la"] = field_24 + attributes["is_la_inferred"] = false + attributes["pcodenk"] = 0 if postcode_full.present? + attributes["postcode_full"] = postcode_full + attributes["wchair"] = field_27 + + attributes["type"] = sale_type + attributes["resale"] = field_91 + + attributes["hodate"] = hodate + attributes["exdate"] = exdate + + attributes["lanomagr"] = field_98 + + attributes["frombeds"] = field_100 + attributes["fromprop"] = field_101 + + attributes["value"] = value + attributes["equity"] = field_104 + attributes["mortgage"] = mortgage + attributes["extrabor"] = extrabor + attributes["deposit"] = deposit + + attributes["cashdis"] = field_112 + attributes["mrent"] = field_113 + attributes["has_mscharge"] = mscharge.present? ? 1 : 0 + attributes["mscharge"] = mscharge + attributes["grant"] = field_117 + attributes["discount"] = field_118 + + attributes["othtype"] = field_11 + + attributes["owning_organisation_id"] = owning_organisation_id + attributes["created_by"] = created_by || bulk_upload.user + attributes["hhregres"] = hhregres + attributes["hhregresstill"] = hhregresstill + attributes["armedforcesspouse"] = field_75 + + attributes["mortgagelender"] = mortgagelender + attributes["mortgagelenderother"] = mortgagelenderother + + attributes["hb"] = field_82 + + attributes["mortlen"] = mortlen + + attributes["proplen"] = proplen + attributes["jointmore"] = field_15 + attributes["staircase"] = field_87 + attributes["privacynotice"] = field_29 + attributes["ownershipsch"] = field_7 + attributes["companybuy"] = field_12 + attributes["buylivein"] = field_13 + attributes["jointpur"] = field_14 + attributes["buy1livein"] = field_36 + attributes["buy2livein"] = field_44 + attributes["hholdcount"] = field_45 + attributes["stairbought"] = field_88 + attributes["stairowned"] = field_89 + attributes["socprevten"] = field_102 + attributes["mortgageused"] = mortgageused + + attributes["uprn"] = field_19 + attributes["address_line1"] = field_20 + attributes["address_line2"] = field_21 + attributes["town_or_city"] = field_22 + attributes["county"] = field_23 + + attributes["ethnic_group2"] = infer_buyer2_ethnic_group_from_ethnic + attributes["ethnicbuy2"] = field_40 + attributes["nationalbuy2"] = field_41 + + attributes["buy2living"] = field_71 + attributes["prevtenbuy2"] = prevtenbuy2 + + attributes["hhregresstill"] = field_74 + + attributes["prevshared"] = field_85 + + attributes["staircasesale"] = field_90 + + attributes["soctenant"] = field_99 + + attributes + end + + def saledate + Date.new(field_5 + 2000, field_4, field_3) if field_5.present? && field_4.present? && field_3.present? + rescue Date::Error + Date.new + end + + def hodate + Date.new(field_97 + 2000, field_96, field_95) if field_97.present? && field_96.present? && field_95.present? + rescue Date::Error + Date.new + end + + def exdate + Date.new(field_94 + 2000, field_93, field_92) if field_94.present? && field_93.present? && field_92.present? + rescue Date::Error + Date.new + end + + def age1_known? + return 1 if field_30 == "R" + return 1 if field_30.blank? + + 0 + end + + [ + { person: 2, field: :field_38 }, + { person: 3, field: :field_47 }, + { person: 4, field: :field_51 }, + { person: 5, field: :field_55 }, + { person: 6, field: :field_59 }, + ].each do |hash| + define_method("age#{hash[:person]}_known?") do + return 1 if public_send(hash[:field]) == "R" + return 0 if send("person_#{hash[:person]}_present?") + return 1 if public_send(hash[:field]).blank? + + 0 + end + end + + def person_2_present? + field_38.present? || field_39.present? || field_37.present? + end + + def person_3_present? + field_47.present? || field_48.present? || field_46.present? + end + + def person_4_present? + field_51.present? || field_52.present? || field_50.present? + end + + def person_5_present? + field_55.present? || field_56.present? || field_54.present? + end + + def person_6_present? + field_59.present? || field_60.present? || field_58.present? + end + + def details_known?(person_n) + send("person_#{person_n}_present?") ? 1 : 2 + end + + def ethnic_group_from_ethnic + return nil if field_32.blank? + + case field_32 + when 1, 2, 3, 18 + 0 + when 4, 5, 6, 7 + 1 + when 8, 9, 10, 11, 15 + 2 + when 12, 13, 14 + 3 + when 16, 19 + 4 + when 17 + 17 + end + end + + def postcode_full + "#{field_24} #{field_25}" if field_24 && field_25 + end + + def ppostcode_full + "#{field_64} #{field_65}" if field_64 && field_65 + end + + def sale_type + return field_8 if shared_ownership? + return field_9 if discounted_ownership? + return field_10 if outright_sale? + end + + def value + return field_103 if shared_ownership? + return field_116 if discounted_ownership? + return field_127 if outright_sale? + end + + def mortgage + return field_106 if shared_ownership? + return field_120 if discounted_ownership? + return field_129 if outright_sale? + end + + def extrabor + return field_110 if shared_ownership? + return field_124 if discounted_ownership? + return field_133 if outright_sale? + end + + def deposit + return field_111 if shared_ownership? + return field_125 if discounted_ownership? + return field_134 if outright_sale? + end + + def mscharge + return field_114 if shared_ownership? + return field_126 if discounted_ownership? + return field_135 if outright_sale? + end + + def mortgagelender + return field_107 if shared_ownership? + return field_121 if discounted_ownership? + return field_130 if outright_sale? + end + + def mortgagelenderother + return field_108 if shared_ownership? + return field_122 if discounted_ownership? + return field_131 if outright_sale? + end + + def mortlen + return field_109 if shared_ownership? + return field_123 if discounted_ownership? + return field_132 if outright_sale? + end + + def proplen + return field_86 if shared_ownership? + return field_115 if discounted_ownership? + end + + def mortgageused + return field_105 if shared_ownership? + return field_119 if discounted_ownership? + return field_128 if outright_sale? + end + + def owning_organisation + Organisation.find_by_id_on_multiple_fields(field_1) + end + + def owning_organisation_id + owning_organisation&.id + end + + def created_by + @created_by ||= User.find_by(email: field_2) + end + + def hhregres + case field_73 + when 3 then 3 + when 4, 5, 6 then 1 + when 7 then 7 + when 8 then 8 + end + end + + def hhregresstill + return unless hhregres == 1 + + field_73 + end + + def previous_la_known + field_66.present? ? 1 : 0 + end + + def block_log_creation! + self.block_log_creation = true + end + + def questions + @questions ||= log.form.subsections.flat_map { |ss| ss.applicable_questions(log) } + end + + def validate_owning_org_data_given + if field_1.blank? + block_log_creation! + + if errors[:field_1].blank? + errors.add(:field_1, "The owning organisation code is incorrect", category: :setup) + end + end + end + + def validate_owning_org_exists + if owning_organisation.nil? + block_log_creation! + + if errors[:field_1].blank? + errors.add(:field_1, "The owning organisation code is incorrect", category: :setup) + end + end + end + + def validate_owning_org_owns_stock + if owning_organisation && !owning_organisation.holds_own_stock? + block_log_creation! + + if errors[:field_1].blank? + errors.add(:field_1, "The owning organisation code provided is for an organisation that does not own stock", category: :setup) + end + end + end + + def validate_owning_org_permitted + if owning_organisation && !bulk_upload.user.organisation.affiliated_stock_owners.include?(owning_organisation) + block_log_creation! + + if errors[:field_1].blank? + errors.add(:field_1, "You do not have permission to add logs for this owning organisation", category: :setup) + end + end + end + + def validate_created_by_exists + return if field_2.blank? + + unless created_by + errors.add(:field_2, "User with the specified email could not be found") + end + end + + def validate_created_by_related + return unless created_by + + unless created_by.organisation == owning_organisation + block_log_creation! + errors.add(:field_2, "User must be related to owning organisation", category: :setup) + end + end + + def setup_question?(question) + log.form.setup_sections[0].subsections[0].questions.include?(question) + end + + def validate_nulls + field_mapping_for_errors.each do |error_key, fields| + question_id = error_key.to_s + question = questions.find { |q| q.id == question_id } + + next unless question + next if log.optional_fields.include?(question.id) + next if question.completed?(log) + + if setup_question?(question) + fields.each do |field| + unless errors.any? { |e| fields.include?(e.attribute) } + errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase), category: :setup) + end + end + else + fields.each do |field| + unless errors.any? { |e| fields.include?(e.attribute) } + errors.add(field, I18n.t("validations.not_answered", question: question.check_answer_label&.downcase)) + end + end + end + end + end + + def validate_valid_radio_option + log.attributes.each do |question_id, _v| + question = log.form.get_question(question_id, log) + + next unless question&.type == "radio" + next if log[question_id].blank? || question.answer_options.key?(log[question_id].to_s) || !question.page.routed_to?(log, nil) + + fields = field_mapping_for_errors[question_id.to_sym] || [] + + if setup_question?(question) + fields.each do |field| + if errors[field].present? + errors.add(field, I18n.t("validations.invalid_option", question: QUESTIONS[field]), category: :setup) + end + end + else + fields.each do |field| + unless errors.any? { |e| fields.include?(e.attribute) } + errors.add(field, I18n.t("validations.invalid_option", question: QUESTIONS[field])) + end + end + end + end + end + + def validate_relevant_collection_window + return if saledate.blank? || bulk_upload.form.blank? + return if errors.key?(:field_3) || errors.key?(:field_4) || errors.key?(:field_5) + + unless bulk_upload.form.valid_start_date_for_form?(saledate) + errors.add(:field_3, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_4, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + errors.add(:field_5, I18n.t("validations.date.outside_collection_window", year_combo: bulk_upload.year_combo, start_year: bulk_upload.year, end_year: bulk_upload.end_year), category: :setup) + end + end +end diff --git a/app/views/bulk_upload_sales_results/show.html.erb b/app/views/bulk_upload_sales_results/show.html.erb index 6361c17ed..161edb013 100644 --- a/app/views/bulk_upload_sales_results/show.html.erb +++ b/app/views/bulk_upload_sales_results/show.html.erb @@ -13,8 +13,10 @@
- <% @bulk_upload.bulk_upload_errors.group_by(&:row).each do |_row, errors_for_row| %> + <% @bulk_upload.bulk_upload_errors.order_by_cell.group_by(&:row).each do |_row, errors_for_row| %> <%= render BulkUploadErrorRowComponent.new(bulk_upload_errors: errors_for_row) %> <% end %>
+ +<%= govuk_button_link_to "Upload your file again", start_bulk_upload_sales_logs_path %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 0e2500dce..1ac1fb6c3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -165,7 +165,7 @@ en: date: invalid_date: "Enter a date in the correct format, for example 31 1 2022" - outside_collection_window: Enter a date within the 22/23 collection year, which is between 1st April 2022 and 31st March 2023 + outside_collection_window: Enter a date within the %{year_combo} collection year, which is between 1st April %{start_year} and 31st March %{end_year} postcode: "Enter a postcode in the correct format, for example AA1 1AA" location_admin_district: "Select a local authority" email: diff --git a/spec/controllers/lettings_logs_controller_spec.rb b/spec/controllers/lettings_logs_controller_spec.rb new file mode 100644 index 000000000..0874649e7 --- /dev/null +++ b/spec/controllers/lettings_logs_controller_spec.rb @@ -0,0 +1,21 @@ +require "rails_helper" + +RSpec.describe LettingsLogsController do + before do + sign_in bulk_upload.user + end + + describe "#index" do + context "when a sales bulk upload filter is applied" do + let(:bulk_upload) { create(:bulk_upload, :sales) } + + it "does not redirect to resume path" do + session[:logs_filters] = { bulk_upload_id: [bulk_upload.id.to_s] }.to_json + + get :index + + expect(response).to be_successful + end + end + end +end diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index b17efec90..0ea3787dc 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -126,5 +126,8 @@ FactoryBot.define do mortgagelender { 5 } extrabor { 1 } end + trait :with_uprn do + uprn { rand(999_999_999_999).to_s } + end end end diff --git a/spec/requests/lettings_logs_controller_spec.rb b/spec/requests/lettings_logs_controller_spec.rb index 6de5c4db2..7f1dda118 100644 --- a/spec/requests/lettings_logs_controller_spec.rb +++ b/spec/requests/lettings_logs_controller_spec.rb @@ -438,7 +438,7 @@ RSpec.describe LettingsLogsController, type: :request do let(:organisation) { create(:organisation) } let(:user) { create(:user, organisation:) } - let(:bulk_upload) { create(:bulk_upload, user:) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } let!(:included_log) { create(:lettings_log, :in_progress, bulk_upload:, owning_organisation: organisation) } let!(:excluded_log) { create(:lettings_log, :in_progress, owning_organisation: organisation) } @@ -492,7 +492,7 @@ RSpec.describe LettingsLogsController, type: :request do let(:user) { create(:user, organisation:) } let(:other_user) { create(:user, organisation:) } - let(:bulk_upload) { create(:bulk_upload, user: other_user) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user: other_user) } let!(:excluded_log) { create(:lettings_log, bulk_upload:, owning_organisation: organisation) } let!(:also_excluded_log) { create(:lettings_log, owning_organisation: organisation) } @@ -509,7 +509,7 @@ RSpec.describe LettingsLogsController, type: :request do let(:organisation) { create(:organisation) } let(:user) { create(:user, organisation:) } - let(:bulk_upload) { create(:bulk_upload, user:) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } it "redirects to resume the bulk upload" do get "/lettings-logs?bulk_upload_id[]=#{bulk_upload.id}" diff --git a/spec/services/bulk_upload/sales/year2023/csv_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/csv_parser_spec.rb new file mode 100644 index 000000000..54af60e3d --- /dev/null +++ b/spec/services/bulk_upload/sales/year2023/csv_parser_spec.rb @@ -0,0 +1,148 @@ +require "rails_helper" + +RSpec.describe BulkUpload::Sales::Year2023::CsvParser do + subject(:service) { described_class.new(path:) } + + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) { build(:sales_log, :completed, :with_uprn) } + + context "when parsing csv with headers" do + before do + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::SalesLogToCsv.new(log:).default_2023_field_numbers_row) + file.write(BulkUpload::SalesLogToCsv.new(log:).to_2023_csv_row) + file.rewind + end + + it "returns correct offsets" do + expect(service.row_offset).to eq(7) + expect(service.col_offset).to eq(1) + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_19).to eql(log.uprn) + end + end + + context "when parsing csv with headers in arbitrary order" do + let(:seed) { rand } + + before do + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::SalesLogToCsv.new(log:).default_2023_field_numbers_row(seed:)) + file.write(BulkUpload::SalesLogToCsv.new(log:).to_2023_csv_row(seed:)) + file.rewind + end + + it "returns correct offsets" do + expect(service.row_offset).to eq(7) + expect(service.col_offset).to eq(1) + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_19).to eql(log.uprn) + end + end + + context "when parsing csv without headers" do + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) { build(:sales_log, :completed, :with_uprn) } + + before do + file.write(BulkUpload::SalesLogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "returns correct offsets" do + expect(service.row_offset).to eq(0) + expect(service.col_offset).to eq(0) + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_19).to eql(log.uprn) + end + end + + context "when parsing with BOM aka byte order mark" do + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) { build(:sales_log, :completed, :with_uprn) } + let(:bom) { "\uFEFF" } + + before do + file.write(bom) + file.write(BulkUpload::SalesLogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.close + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_19).to eql(log.uprn) + end + end + + context "when an invalid byte sequence" do + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) { build(:sales_log, :completed, :with_uprn) } + let(:invalid_sequence) { "\x81" } + + before do + file.write(invalid_sequence) + file.write(BulkUpload::SalesLogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.close + end + + it "parses csv correctly" do + expect(service.row_parsers[0].field_19).to eql(log.uprn) + end + end + + describe "#column_for_field", aggregate_failures: true do + context "when headers present" do + before do + file.write("Question\n") + file.write("Additional info\n") + file.write("Values\n") + file.write("Can be empty?\n") + file.write("Type of letting the question applies to\n") + file.write("Duplicate check field?\n") + file.write(BulkUpload::SalesLogToCsv.new(log:).default_2023_field_numbers_row) + file.write(BulkUpload::SalesLogToCsv.new(log:).to_2023_csv_row) + file.rewind + end + + it "returns correct column" do + expect(service.column_for_field("field_1")).to eql("CO") + expect(service.column_for_field("field_99")).to eql("EK") + end + end + + context "when no headers" do + let(:file) { Tempfile.new } + let(:path) { file.path } + let(:log) { build(:sales_log, :completed, :with_uprn) } + + before do + file.write(BulkUpload::SalesLogToCsv.new(log:, col_offset: 0).to_2023_csv_row) + file.rewind + end + + it "returns correct column" do + expect(service.column_for_field("field_1")).to eql("CN") + expect(service.column_for_field("field_99")).to eql("EJ") + end + end + end +end diff --git a/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb new file mode 100644 index 000000000..e49e47db7 --- /dev/null +++ b/spec/services/bulk_upload/sales/year2023/row_parser_spec.rb @@ -0,0 +1,704 @@ +require "rails_helper" + +RSpec.describe BulkUpload::Sales::Year2023::RowParser do + subject(:parser) { described_class.new(attributes) } + + let(:now) { Time.zone.parse("01/03/2023") } + + let(:attributes) { { bulk_upload: } } + let(:bulk_upload) { create(:bulk_upload, :sales, user:) } + let(:user) { create(:user, organisation: owning_org) } + + let(:owning_org) { create(:organisation, :with_old_visible_id) } + + let(:setup_section_params) do + { + bulk_upload:, + field_1: owning_org.old_visible_id, # organisation + field_2: user.email, # user + field_3: now.day.to_s, # sale day + field_4: now.month.to_s, # sale month + field_5: now.strftime("%g"), # sale year + field_6: "test id", # purchase id + field_7: "1", # owhershipsch + field_8: "2", # shared ownership sale type + field_13: "1", # will the buyers live in the property + field_14: "2", # joint purchase + } + end + + let(:valid_attributes) do + { + bulk_upload:, + + field_1: owning_org.old_visible_id, + field_3: "22", + field_4: "2", + field_5: "23", + field_6: "test id", + field_7: "1", + field_8: "2", + field_13: "1", + field_14: "1", + field_15: "2", + field_16: "2", + field_17: "1", + field_18: "1", + field_19: "100023336956", + field_24: "CR0", + field_25: "4BB", + field_26: "E09000008", + field_27: "3", + field_28: "1", + field_29: "1", + field_30: "32", + field_31: "M", + field_32: "12", + field_33: "18", + field_35: "1", + field_36: "1", + field_37: "R", + field_38: "32", + field_39: "F", + field_43: "2", + field_44: "1", + field_45: "0", + field_62: "1", + field_63: "1", + field_64: "A1", + field_65: "1AA", + field_66: "E09000008", + field_69: "1", + field_70: "1", + field_73: "3", + field_75: "5", + field_76: "3", + field_77: "3", + field_78: "30000", + field_79: "1", + field_80: "15000", + field_81: "1", + field_82: "4", + field_83: "20000", + field_84: "3", + field_86: "5", + field_87: "1", + field_88: "10", + field_89: "10", + field_91: "2", + field_92: "30", + field_93: "3", + field_94: "22", + field_95: "23", + field_96: "3", + field_97: "22", + field_98: "3", + field_99: "1", + field_100: "1", + field_101: "1", + field_102: "1", + field_103: "250000", + field_104: "25", + field_105: "1", + field_106: "42500", + field_107: "1", + field_109: "20", + field_110: "3", + field_111: "20000", + field_113: "800", + field_114: "200", + } + end + + around do |example| + FormHandler.instance.use_real_forms! + + example.run + + FormHandler.instance.use_fake_forms! + end + + describe "#blank_row?" do + context "when a new object" do + it "returns true" do + expect(parser).to be_blank_row + end + end + + context "when any field is populated" do + before do + parser.field_1 = "1" + end + + it "returns false" do + expect(parser).not_to be_blank_row + end + end + end + + describe "validations" do + before do + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\", \"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) + + body = { + results: [ + { + DPA: { + "POSTCODE": "EC1N 2TD", + "POST_TOWN": "Newcastle", + "ORGANISATION_NAME": "Some place", + }, + }, + ], + }.to_json + + stub_request(:get, "https://api.os.uk/search/places/v1/uprn?key=OS_DATA_KEY&uprn=100023336956") + .to_return(status: 200, body:, headers: {}) + + parser.valid? + end + + describe "#valid?" do + context "when the row is blank" do + let(:attributes) { { bulk_upload: } } + + it "returns true" do + expect(parser).to be_valid + end + end + + context "when calling the method multiple times" do + let(:attributes) { { bulk_upload:, field_7: 2 } } + + it "does not add keep adding errors to the pile" do + expect { parser.valid? }.not_to change(parser.errors, :count) + end + end + + context "when valid row" do + let(:attributes) { valid_attributes } + + it "returns true" do + expect(parser).to be_valid + end + + it "instantiates a log with everything completed", aggregate_failures: true do + questions = parser.send(:questions).reject do |q| + parser.send(:log).optional_fields.include?(q.id) || q.completed?(parser.send(:log)) + end + + expect(questions.map(&:id).size).to eq(0) + expect(questions.map(&:id)).to eql([]) + end + end + end + + context "when setup section not complete and type is not given" do + let(:attributes) do + { + bulk_upload:, + field_6: "test id", + } + end + + it "has errors on correct setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute).sort + + expect(errors).to eql(%i[field_1 field_3 field_4 field_5 field_7]) + end + end + + context "when setup section not complete and type is shared ownership" do + let(:attributes) do + { + bulk_upload:, + field_6: "test id", + field_7: "1", + } + end + + it "has errors on correct setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute).sort + + expect(errors).to eql(%i[field_1 field_14 field_3 field_4 field_5 field_8]) + end + end + + context "when setup section not complete it's shared ownership joint purchase" do + let(:attributes) do + { + bulk_upload:, + field_6: "test id", + field_7: "1", + field_8: "2", + field_14: "1", + } + end + + it "has errors on correct setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute).sort + + expect(errors).to eql(%i[field_1 field_15 field_3 field_4 field_5]) + end + end + + context "when setup section not complete and type is discounted ownership" do + let(:attributes) do + { + bulk_upload:, + field_6: "test id", + field_7: "2", + } + end + + it "has errors on correct setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute).sort + + expect(errors).to eql(%i[field_1 field_14 field_3 field_4 field_5 field_9]) + end + end + + context "when setup section not complete it's discounted ownership joint purchase" do + let(:attributes) do + { + bulk_upload:, + field_28: "test id", + field_30: "2", + field_47: "8", + field_39: "1", + } + end + + it "has errors on correct setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute).sort + + expect(errors).to eql(%i[field_1 field_3 field_4 field_5 field_7]) + end + end + + context "when setup section not complete and type is outright sale" do + let(:attributes) do + { + bulk_upload:, + field_6: "test id", + field_7: "3", + } + end + + it "has errors on correct setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute).sort + + expect(errors).to eql(%i[field_1 field_10 field_12 field_13 field_3 field_4 field_5]) + end + end + + context "when setup section not complete outright sale buyer is not company" do + let(:attributes) do + { + bulk_upload:, + field_6: "test id", + field_7: "3", + field_10: "12", + field_12: "2", + } + end + + it "has errors on correct setup fields" do + errors = parser.errors.select { |e| e.options[:category] == :setup }.map(&:attribute).sort + + expect(errors).to eql(%i[field_1 field_13 field_14 field_3 field_4 field_5]) + end + end + + describe "#field_1" do # owning org + context "when no data given" do + let(:attributes) { setup_section_params.merge(field_1: nil) } + + it "is not permitted as setup error" do + expect(parser.errors.where(:field_1, category: :setup).map(&:message)).to eql(["The owning organisation code is incorrect"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + + context "when cannot find owning org" do + let(:attributes) { { bulk_upload:, field_1: "donotexist" } } + + it "is not permitted as a setup error" do + expect(parser.errors.where(:field_1, category: :setup).map(&:message)).to eql(["The owning organisation code is incorrect"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + + context "when not affiliated with owning org" do + let(:unaffiliated_org) { create(:organisation, :with_old_visible_id) } + + let(:attributes) { { bulk_upload:, field_1: unaffiliated_org.old_visible_id } } + + it "is not permitted as setup error" do + expect(parser.errors.where(:field_1, category: :setup).map(&:message)).to eql(["You do not have permission to add logs for this owning organisation"]) + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + end + + describe "#field_2" do # username for created_by + context "when blank" do + let(:attributes) { setup_section_params.merge(bulk_upload:, field_2: nil) } + + it "is permitted" do + expect(parser.errors[:field_2]).to be_blank + end + end + + context "when user could not be found" do + let(:attributes) { { bulk_upload:, field_2: "idonotexist@example.com" } } + + it "is not permitted" do + expect(parser.errors[:field_2]).to be_present + end + end + + context "when an unaffiliated user" do + let(:other_user) { create(:user) } + + let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id, field_2: other_user.email } } + + it "is not permitted as a setup error" do + expect(parser.errors.where(:field_2, category: :setup)).to be_present + end + + it "blocks log creation" do + expect(parser).to be_block_log_creation + end + end + + context "when an user part of owning org" do + let(:other_user) { create(:user, organisation: owning_org) } + + let(:attributes) { { bulk_upload:, field_1: owning_org.old_visible_id, field_2: other_user.email } } + + it "is permitted" do + expect(parser.errors[:field_2]).to be_blank + end + end + end + + describe "fields 3, 4, 5 => saledate" do + context "when all of these fields are blank" do + let(:attributes) { setup_section_params.merge({ field_3: nil, field_4: nil, field_5: nil }) } + + it "returns them as setup errors" do + expect(parser.errors.where(:field_3, category: :setup)).to be_present + expect(parser.errors.where(:field_4, category: :setup)).to be_present + expect(parser.errors.where(:field_5, category: :setup)).to be_present + end + end + + context "when one of these fields is blank" do + let(:attributes) { setup_section_params.merge({ field_3: "1", field_4: "1", field_5: nil }) } + + it "returns an error only on blank field as setup error" do + expect(parser.errors[:field_3]).to be_blank + expect(parser.errors[:field_4]).to be_blank + expect(parser.errors.where(:field_5, category: :setup)).to be_present + end + end + + context "when field 5 is 4 digits instead of 2" do + let(:attributes) { setup_section_params.merge({ bulk_upload:, field_5: "2022" }) } + + it "returns a setup error" do + expect(parser.errors.where(:field_5, category: :setup).map(&:message)).to include("Sale completion year must be 2 digits") + end + end + + context "when invalid date given" do + let(:attributes) { setup_section_params.merge({ field_3: "a", field_4: "12", field_5: "2022" }) } + + it "does not raise an error" do + expect { parser.valid? }.not_to raise_error + end + end + + context "when inside of collection year" do + around do |example| + Timecop.freeze(Date.new(2023, 10, 1)) do + example.run + end + end + + let(:attributes) { setup_section_params.merge({ field_3: "1", field_4: "10", field_5: "23" }) } + + let(:bulk_upload) { create(:bulk_upload, :sales, user:, year: 2023) } + + it "does not return errors" do + expect(parser.errors[:field_3]).not_to be_present + expect(parser.errors[:field_4]).not_to be_present + expect(parser.errors[:field_5]).not_to be_present + end + end + + context "when outside of collection year" do + around do |example| + Timecop.freeze(Date.new(2022, 4, 2)) do + example.run + end + end + + let(:attributes) { setup_section_params.merge({ field_3: "1", field_4: "1", field_5: "22" }) } + + let(:bulk_upload) { create(:bulk_upload, :sales, user:, year: 2022) } + + it "returns setup errors" do + expect(parser.errors.where(:field_3, category: :setup)).to be_present + expect(parser.errors.where(:field_4, category: :setup)).to be_present + expect(parser.errors.where(:field_5, category: :setup)).to be_present + end + end + end + + describe "#field_19" do # UPRN + context "when UPRN known" do + let(:attributes) { setup_section_params.merge({ field_19: "100023336956" }) } + + it "is valid" do + expect(parser.errors[:field_19]).to be_blank + end + end + + context "when UPRN not known but address known" do + let(:attributes) { setup_section_params.merge({ field_19: nil, field_20: "some street", field_22: "some town", field_24: "EC1N", field_25: "2TD" }) } + + it "is valid" do + expect(parser.errors[:field_19]).to be_blank + end + end + + context "when neither UPRN or address known" do + let(:attributes) { setup_section_params.merge({ field_19: nil, field_20: nil, field_22: nil, field_24: nil, field_25: nil }) } + + it "is not valid" do + expect(parser.errors[:field_19]).to be_present + end + end + end + + [ + { field: :field_20, name: "address line 1" }, + { field: :field_22, name: "town or city" }, + { field: :field_24, name: "postcode part 1" }, + { field: :field_25, name: "postcode part 2" }, + ].each do |data| + describe "##{data[:field]} (#{data[:name]})" do + context "when UPRN present" do + let(:attributes) { setup_section_params.merge({ field_19: "100023336956", data[:field] => nil }) } + + it "can be blank" do + expect(parser.errors[data[:field]]).to be_blank + end + end + + context "when UPRN not present" do + let(:attributes) { setup_section_params.merge({ field_19: nil, data[:field] => nil }) } + + it "cannot be blank" do + expect(parser.errors[data[:field]]).to be_present + end + end + end + end + + [ + %w[age1_known age1 field_30], + %w[age2_known age2 field_38], + %w[age3_known age3 field_47], + %w[age4_known age4 field_51], + %w[age5_known age5 field_55], + %w[age6_known age6 field_59], + ].each do |known, age, field| + describe "##{known} and ##{age}" do + context "when #{field} is blank" do + let(:attributes) { { bulk_upload:, field.to_s => nil } } + + it "sets ##{known} 1" do + expect(parser.log.public_send(known)).to be(1) + end + + it "sets ##{age} to nil" do + expect(parser.log.public_send(age)).to be_nil + end + end + + context "when #{field} is R" do + let(:attributes) { setup_section_params.merge({ field.to_s => "R", field_28: "1", field_45: "5", field_29: "1" }) } + + it "sets ##{known} 1" do + expect(parser.log.public_send(known)).to be(1) + end + + it "sets ##{age} to nil" do + expect(parser.log.public_send(age)).to be_nil + end + end + + context "when #{field} is a number" do + let(:attributes) { setup_section_params.merge({ field.to_s => "50", field_28: "1", field_45: "5", field_29: "1" }) } + + it "sets ##{known} to 0" do + expect(parser.log.public_send(known)).to be(0) + end + + it "sets ##{age} to given age" do + expect(parser.log.public_send(age)).to be(50) + end + end + + context "when #{field} is a non-sensical value" do + let(:attributes) { setup_section_params.merge({ field.to_s => "A", field_28: "1", field_45: "5", field_29: "1" }) } + + it "sets ##{known} to 0" do + expect(parser.log.public_send(known)).to be(0) + end + + it "sets ##{age} to nil" do + expect(parser.log.public_send(age)).to be_nil + end + end + end + end + + describe "#field_36" do # will buyer1 live in property? + context "when not a possible value" do + let(:attributes) { valid_attributes.merge({ field_36: "3" }) } + + it "is not valid" do + expect(parser.errors).to include(:field_36) + end + end + end + end + + describe "#log" do + describe "#uprn" do + let(:attributes) { setup_section_params.merge({ field_19: "100023336956" }) } + + it "is correctly set" do + expect(parser.log.uprn).to eql("100023336956") + end + end + + describe "#address_line1" do + let(:attributes) { setup_section_params.merge({ field_20: "some street" }) } + + it "is correctly set" do + expect(parser.log.address_line1).to eql("some street") + end + end + + describe "#address_line2" do + let(:attributes) { setup_section_params.merge({ field_21: "some other street" }) } + + it "is correctly set" do + expect(parser.log.address_line2).to eql("some other street") + end + end + + describe "#town_or_city" do + let(:attributes) { setup_section_params.merge({ field_22: "some town" }) } + + it "is correctly set" do + expect(parser.log.town_or_city).to eql("some town") + end + end + + describe "#county" do + let(:attributes) { setup_section_params.merge({ field_23: "some county" }) } + + it "is correctly set" do + expect(parser.log.county).to eql("some county") + end + end + + describe "#ethnic_group2" do + let(:attributes) { setup_section_params.merge({ field_40: "1" }) } + + it "is correctly set" do + expect(parser.log.ethnic_group2).to be(0) + end + end + + describe "#ethnicbuy2" do + let(:attributes) { setup_section_params.merge({ field_40: "1" }) } + + it "is correctly set" do + expect(parser.log.ethnicbuy2).to be(1) + end + end + + describe "#nationalbuy2" do + let(:attributes) { setup_section_params.merge({ field_41: "18" }) } + + it "is correctly set" do + expect(parser.log.nationalbuy2).to be(18) + end + end + + describe "#buy2living" do + let(:attributes) { setup_section_params.merge({ field_71: "1" }) } + + it "is correctly set" do + expect(parser.log.buy2living).to be(1) + end + end + + describe "#prevtenbuy2" do + let(:attributes) { setup_section_params.merge({ field_72: "R" }) } + + it "is correctly set" do + expect(parser.log.prevtenbuy2).to be(0) + end + end + + describe "#hhregresstill" do + let(:attributes) { setup_section_params.merge({ field_74: "4" }) } + + it "is correctly set" do + expect(parser.log.hhregresstill).to be(4) + end + end + + describe "#prevshared" do + let(:attributes) { setup_section_params.merge({ field_85: "3" }) } + + it "is correctly set" do + expect(parser.log.prevshared).to be(3) + end + end + + describe "#staircasesale" do + let(:attributes) { setup_section_params.merge({ field_90: "1" }) } + + it "is correctly set" do + expect(parser.log.staircasesale).to be(1) + end + end + + describe "#soctenant" do + let(:attributes) { setup_section_params.merge({ field_99: "1" }) } + + it "is correctly set" do + expect(parser.log.soctenant).to be(1) + end + end + end +end diff --git a/spec/support/bulk_upload/sales_log_to_csv.rb b/spec/support/bulk_upload/sales_log_to_csv.rb index 650eaad65..eb32bb822 100644 --- a/spec/support/bulk_upload/sales_log_to_csv.rb +++ b/spec/support/bulk_upload/sales_log_to_csv.rb @@ -16,6 +16,15 @@ class BulkUpload::SalesLogToCsv (row_prefix + to_2022_row).flatten.join(",") + line_ending end + def to_2023_csv_row(seed: nil) + if seed + row = to_2023_row.shuffle(random: Random.new(seed)) + (row_prefix + row).flatten.join(",") + line_ending + else + (row_prefix + to_2023_row).flatten.join(",") + line_ending + end + end + def default_2022_field_numbers (1..125).to_a end @@ -28,6 +37,34 @@ class BulkUpload::SalesLogToCsv end.flatten.join(",") + line_ending end + def default_2023_field_numbers_row(seed: nil) + if seed + ["Bulk upload field number"] + default_2023_field_numbers.shuffle(random: Random.new(seed)) + else + ["Bulk upload field number"] + default_2023_field_numbers + end.flatten.join(",") + line_ending + end + + def to_2023_row + to_2022_row + [ + log.uprn, + log.address_line1, + log.address_line2, + log.town_or_city, + log.county, + nil, # mistake in template. field is ignored + log.ethnic_group2, + log.nationalbuy2, + nil, # mistake in template. field is ignored + log.buy2living, + log.prevtenbuy2, + log.hhregresstill, + log.prevshared, + log.staircasesale, + log.soctenant, + ] + end + def to_2022_row [ log.purchid, # 1 @@ -177,6 +214,10 @@ class BulkUpload::SalesLogToCsv private + def default_2023_field_numbers + [6, 3, 4, 5, nil, 28, 30, 38, 47, 51, 55, 59, 31, 39, 48, 52, 56, 60, 37, 46, 50, 54, 58, 35, 43, 49, 53, 57, 61, 32, 33, 78, 80, 79, 81, 83, 84, nil, 62, 66, 64, 65, 63, 67, 69, 70, 68, 76, 77, 16, 17, 18, 26, 24, 25, 27, 8, 91, 95, 96, 97, 92, 93, 94, 98, 100, 101, 103, 104, 106, 110, 111, 112, 113, 114, 9, 116, 117, 118, 120, 124, 125, 126, 10, 11, nil, 127, 129, 133, 134, 135, 1, 2, nil, 73, nil, 75, 107, 108, 121, 122, 130, 131, 82, 109, 123, 132, 115, 15, 86, 87, 29, 7, 12, 13, 14, 36, 44, 45, 88, 89, 102, 105, 119, 128, 19, 20, 21, 22, 23, 34, 40, 41, 42, 71, 72, 74, 85, 90, 99] + end + def hhregres if log.hhregres == 1 log.hhregresstill