diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index da79b20a6..06332a5a7 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -292,9 +292,493 @@ class BulkUpload::Lettings::Year2023::RowParser .empty? end + def log + @log ||= LettingsLog.new(attributes_for_log) + end + private def attribute_set @attribute_set ||= instance_variable_get(:@attributes) end + + def questions + log.form.subsections.flat_map { |ss| ss.applicable_questions(log) } + end + + # TODO: whole method needs re-mapping + def attributes_for_log + attributes = {} + + attributes["lettype"] = field_5 + attributes["tenancycode"] = field_13 + attributes["la"] = field_25 + attributes["postcode_known"] = postcode_known + attributes["postcode_full"] = postcode_full + attributes["owning_organisation_id"] = owning_organisation_id + attributes["managing_organisation_id"] = managing_organisation_id + attributes["renewal"] = renewal + attributes["scheme"] = scheme + attributes["location"] = location + attributes["created_by"] = bulk_upload.user + attributes["needstype"] = field_4 + attributes["rent_type"] = rent_type + attributes["startdate"] = startdate + attributes["unittype_gn"] = field_29 + attributes["builtype"] = field_30 + attributes["wchair"] = field_31 + attributes["beds"] = field_32 + attributes["joint"] = field_39 + attributes["startertenancy"] = field_40 + attributes["tenancy"] = field_41 + attributes["tenancyother"] = field_42 + attributes["tenancylength"] = field_43 + attributes["declaration"] = field_45 + + attributes["age1_known"] = field_46 == "R" ? 1 : 0 + attributes["age1"] = field_46 if attributes["age1_known"].zero? + + attributes["age2_known"] = field_52 == "R" ? 1 : 0 + attributes["age2"] = field_52 if attributes["age2_known"].zero? + + attributes["age3_known"] = field_56 == "R" ? 1 : 0 + attributes["age3"] = field_56 if attributes["age3_known"].zero? + + attributes["age4_known"] = field_60 == "R" ? 1 : 0 + attributes["age4"] = field_60 if attributes["age4_known"].zero? + + attributes["age5_known"] = field_64 == "R" ? 1 : 0 + attributes["age5"] = field_64 if attributes["age5_known"].zero? + + attributes["age6_known"] = field_68 == "R" ? 1 : 0 + attributes["age6"] = field_68 if attributes["age6_known"].zero? + + attributes["age7_known"] = field_72 == "R" ? 1 : 0 + attributes["age7"] = field_72 if attributes["age7_known"].zero? + + attributes["age8_known"] = field_76 == "R" ? 1 : 0 + attributes["age8"] = field_76 if attributes["age8_known"].zero? + + attributes["sex1"] = field_47 + attributes["sex2"] = field_53 + attributes["sex3"] = field_57 + attributes["sex4"] = field_61 + attributes["sex5"] = field_65 + attributes["sex6"] = field_69 + attributes["sex7"] = field_73 + attributes["sex8"] = field_77 + + attributes["ethnic_group"] = ethnic_group_from_ethnic + attributes["ethnic"] = field_48 + attributes["national"] = field_49 + + attributes["relat2"] = field_51 + attributes["relat3"] = field_55 + attributes["relat4"] = field_59 + attributes["relat5"] = field_63 + attributes["relat6"] = field_67 + attributes["relat7"] = field_71 + attributes["relat8"] = field_75 + + attributes["ecstat1"] = field_50 + attributes["ecstat2"] = field_54 + attributes["ecstat3"] = field_58 + attributes["ecstat4"] = field_62 + attributes["ecstat5"] = field_66 + attributes["ecstat6"] = field_70 + attributes["ecstat7"] = field_74 + attributes["ecstat8"] = field_78 + + 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["details_known_7"] = details_known(7) + attributes["details_known_8"] = details_known(8) + + attributes["armedforces"] = field_79 + attributes["leftreg"] = leftreg + attributes["reservist"] = field_81 + + attributes["preg_occ"] = field_82 + + attributes["housingneeds"] = housingneeds + attributes["housingneeds_type"] = housingneeds_type + attributes["housingneeds_other"] = housingneeds_other + + attributes["illness"] = field_89 + + attributes["layear"] = field_100 + attributes["waityear"] = field_101 + attributes["reason"] = field_102 + attributes["reasonother"] = field_103 + attributes["prevten"] = field_104 + attributes["homeless"] = homeless + + attributes["prevloc"] = prevloc + attributes["previous_la_known"] = previous_la_known + attributes["ppcodenk"] = ppcodenk + attributes["ppostcode_full"] = ppostcode_full + + attributes["reasonpref"] = field_110 + attributes["rp_homeless"] = field_111 + attributes["rp_insan_unsat"] = field_112 + attributes["rp_medwel"] = field_113 + attributes["rp_hardship"] = field_114 + attributes["rp_dontknow"] = field_115 + + attributes["cbl"] = cbl + attributes["chr"] = chr + attributes["cap"] = cap + attributes["letting_allocation_unknown"] = letting_allocation_unknown + + attributes["referral"] = field_119 + + attributes["net_income_known"] = net_income_known + attributes["earnings"] = earnings + attributes["incfreq"] = field_121 + attributes["hb"] = field_123 + attributes["benefits"] = field_124 + + attributes["period"] = field_126 + attributes["brent"] = field_128 + attributes["scharge"] = field_129 + attributes["pscharge"] = field_130 + attributes["supcharg"] = field_131 + attributes["tcharge"] = field_132 + attributes["chcharge"] = field_127 + attributes["household_charge"] = field_125 + attributes["hbrentshortfall"] = field_133 + attributes["tshortfall_known"] = tshortfall_known + attributes["tshortfall"] = field_134 + + attributes["hhmemb"] = hhmemb + + attributes["unitletas"] = field_26 + attributes["rsnvac"] = rsnvac + attributes["sheltered"] = field_44 + + attributes["illness_type_1"] = field_98 + attributes["illness_type_2"] = field_92 + attributes["illness_type_3"] = field_95 + attributes["illness_type_4"] = field_90 + attributes["illness_type_5"] = field_91 + attributes["illness_type_6"] = field_93 + attributes["illness_type_7"] = field_94 + attributes["illness_type_8"] = field_97 + attributes["illness_type_9"] = field_96 + attributes["illness_type_10"] = field_99 + + attributes["irproduct_other"] = field_12 + + attributes["offered"] = field_28 + + attributes["propcode"] = field_14 + + attributes["majorrepairs"] = majorrepairs + + attributes["mrcdate"] = mrcdate + + attributes["voiddate"] = voiddate + + attributes["first_time_property_let_as_social_housing"] = first_time_property_let_as_social_housing + + attributes + end + + def postcode_known + if postcode_full.present? + 1 + elsif field_25.present? + 0 + end + end + + def postcode_full + "#{field_23} #{field_24}" if field_23 && field_24 + end + + def owning_organisation + Organisation.find_by_id_on_mulitple_fields(field_1) + end + + def owning_organisation_id + owning_organisation&.id + end + + def managing_organisation + Organisation.find_by_id_on_mulitple_fields(field_2) + end + + def managing_organisation_id + managing_organisation&.id + end + + def renewal + case field_6 + when 1 + 1 + when 2 + 0 + when nil + rsnvac == 14 ? 1 : 0 + end + end + + def rsnvac + field_27 + end + + def scheme + @scheme ||= Scheme.find_by_id_on_mulitple_fields(field_15) + end + + def location + return if scheme.nil? + + @location ||= scheme.locations.find_by_id_on_mulitple_fields(field_16) + end + + def renttype + case field_5 + when 1, 2, 3, 4 + :social + when 5, 6, 7, 8 + :affordable + when 9, 10, 11, 12 + :intermediate + end + end + + def rent_type + case renttype + when :social + Imports::LettingsLogsImportService::RENT_TYPE[:social_rent] + when :affordable + if field_10 == 1 + Imports::LettingsLogsImportService::RENT_TYPE[:london_affordable_rent] + else + Imports::LettingsLogsImportService::RENT_TYPE[:affordable_rent] + end + when :intermediate + case field_11 + when 1 + Imports::LettingsLogsImportService::RENT_TYPE[:rent_to_buy] + when 2 + Imports::LettingsLogsImportService::RENT_TYPE[:london_living_rent] + when 3 + Imports::LettingsLogsImportService::RENT_TYPE[:other_intermediate_rent_product] + end + end + end + + def startdate + Date.new(field_9 + 2000, field_8, field_7) if field_9.present? && field_8.present? && field_7.present? + rescue Date::Error + Date.new + end + + def ethnic_group_from_ethnic + return nil if field_48.blank? + + case field_48 + 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 details_known(person_n) + send("person_#{person_n}_present?") ? 0 : 1 + end + + def person_2_present? + field_51.present? && field_52.present? && field_53.present? + end + + def person_3_present? + field_55.present? && field_56.present? && field_57.present? + end + + def person_4_present? + field_59.present? && field_60.present? && field_61.present? + end + + def person_5_present? + field_63.present? && field_64.present? && field_65.present? + end + + def person_6_present? + field_67.present? && field_68.present? && field_69.present? + end + + def person_7_present? + field_71.present? && field_72.present? && field_73.present? + end + + def person_8_present? + field_75.present? && field_76.present? && field_77.present? + end + + def leftreg + case field_80 + when 3 + 3 + when 4 + 1 + when 5 + 2 + when 6 + 0 + end + end + + def housingneeds + if field_87 == 1 + 2 + elsif field_88 == 1 + 3 + else + 2 + end + end + + def housingneeds_type + if field_83 == 1 + 0 + elsif field_84 == 1 + 1 + elsif field_85 == 1 + 2 + end + end + + def housingneeds_other + return 1 if field_86 == 1 + end + + def homeless + case field_105 + when 1 + 1 + when 12 + 11 + end + end + + def prevloc + field_109 + end + + def previous_la_known + prevloc.present? ? 1 : 0 + end + + def ppcodenk + case field_106 + when 1 + 1 + when 2 + 0 + end + end + + def ppostcode_full + "#{field_107} #{field_108}".strip.gsub(/\s+/, " ") + end + + def cbl + case field_116 + when 2 + 0 + when 1 + 1 + end + end + + def chr + case field_117 + when 2 + 0 + when 1 + 1 + end + end + + def cap + case field_118 + when 2 + 0 + when 1 + 1 + end + end + + def letting_allocation_unknown + [cbl, chr, cap].all?(0) ? 1 : 0 + end + + def net_income_known + case field_120 + when 1 + 0 + when 2 + 1 + when 3 + 1 + when 4 + 2 + end + end + + def earnings + field_122.round if field_122.present? + end + + def tshortfall_known + field_133 == 1 ? 0 : 1 + end + + def hhmemb + [ + person_2_present?, + person_3_present?, + person_4_present?, + person_5_present?, + person_6_present?, + person_7_present?, + person_8_present?, + ].count(true) + 1 + end + + def majorrepairs + mrcdate.present? ? 1 : 0 + end + + def mrcdate + Date.new(field_38 + 2000, field_37, field_36) if field_38.present? && field_37.present? && field_36.present? + end + + def voiddate + Date.new(field_35 + 2000, field_34, field_33) if field_35.present? && field_34.present? && field_33.present? + end + + def first_time_property_let_as_social_housing + case rsnvac + when 15, 16, 17 + 1 + else + 0 + end + end end diff --git a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb index b85665c78..b7e0d3974 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -6,7 +6,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do let(:now) { Time.zone.today } let(:attributes) { { bulk_upload: } } - let(:bulk_upload) { create(:bulk_upload, :lettings, user:) } + let(:bulk_upload) { create(:bulk_upload, :lettings, user:, needstype: nil) } let(:user) { create(:user, organisation: owning_org) } let(:owning_org) { create(:organisation, :with_old_visible_id) } @@ -82,133 +82,139 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end end - # context "when valid row" do - # let(:attributes) do - # { - # bulk_upload:, - # field_1: "1", - # field_4: scheme.old_visible_id, - # field_7: "123", - # field_96: now.day.to_s, - # field_97: now.month.to_s, - # field_98: now.strftime("%g"), - # field_108: "EC1N", - # field_109: "2TD", - # field_111: owning_org.old_visible_id, - # field_113: managing_org.old_visible_id, - # field_130: "1", - # field_134: "2", - # field_102: "2", - # field_103: "1", - # field_104: "1", - # field_101: "1", - # field_133: "2", - # field_8: "1", - # field_9: "2", - # field_132: "1", - - # field_12: "42", - # field_13: "41", - # field_14: "20", - # field_15: "18", - # field_16: "16", - # field_17: "14", - # field_18: "12", - # field_19: "20", - - # field_20: "F", - # field_21: "M", - # field_22: "F", - # field_23: "M", - # field_24: "F", - # field_25: "M", - # field_26: "F", - # field_27: "M", - - # field_43: "17", - # field_44: "18", - - # field_28: "P", - # field_29: "C", - # field_30: "X", - # field_31: "R", - # field_32: "C", - # field_33: "C", - # field_34: "X", - - # field_35: "1", - # field_36: "2", - # field_37: "6", - # field_38: "7", - # field_39: "8", - # field_40: "9", - # field_41: "0", - # field_42: "10", - - # field_45: "1", - # field_114: "4", - # field_46: "1", - - # field_47: "1", - - # field_118: "2", - - # field_66: "5", - # field_67: "2", - # field_52: "31", - # field_61: "3", - # field_68: "12", - - # field_65: "1", - # field_63: "EC1N", - # field_64: "2TD", - - # field_69: "1", - # field_70: "1", - # field_71: "", - # field_72: "1", - # field_73: "", - # field_74: "", - - # field_75: "1", - # field_76: "2", - # field_77: "2", - - # field_78: "2", - - # field_51: "1", - # field_50: "2000", - # field_116: "2", - # field_48: "1", - # field_49: "1", - - # field_79: "4", - # field_80: "1234.56", - # field_87: "1", - # field_88: "234.56", - - # field_106: "15", - # field_99: "0", - # field_89: now.day.to_s, - # field_90: now.month.to_s, - # field_91: now.strftime("%g"), - # } - # end + context "when valid row" do + let(:attributes) do + { + bulk_upload:, + field_5: "1", + field_15: scheme.old_visible_id, + field_13: "123", + field_7: now.day.to_s, + field_8: now.month.to_s, + field_9: now.strftime("%g"), + field_23: "EC1N", + field_24: "2TD", + field_1: owning_org.old_visible_id, + field_2: managing_org.old_visible_id, + field_11: "1", + field_6: "2", + field_29: "2", + field_30: "1", + field_31: "1", + field_32: "1", + field_39: "2", + field_40: "1", + field_41: "2", + field_45: "1", + + field_46: "42", + field_52: "41", + field_56: "20", + field_60: "18", + field_64: "16", + field_68: "14", + field_72: "12", + field_76: "20", + + field_47: "F", + field_53: "M", + field_57: "F", + field_61: "M", + field_65: "F", + field_69: "M", + field_73: "F", + field_77: "M", + + field_48: "17", + field_49: "18", + + field_51: "P", + field_55: "C", + field_59: "X", + field_63: "R", + field_67: "C", + field_71: "C", + field_75: "X", + + field_50: "1", + field_54: "2", + field_58: "6", + field_62: "7", + field_66: "8", + field_70: "9", + field_74: "0", + field_78: "10", + + field_79: "1", + field_80: "4", + field_81: "1", + + field_82: "1", + + field_89: "2", + + field_100: "5", + field_101: "2", + field_102: "31", + field_104: "3", + field_105: "12", + + field_106: "1", + field_107: "EC1N", + field_108: "2TD", + + field_110: "1", + field_111: "1", + field_112: "", + field_113: "1", + field_114: "", + field_115: "", + + field_116: "1", + field_117: "2", + field_118: "2", + + field_119: "2", + + field_120: "1", + field_122: "2000", + field_121: "2", + field_123: "1", + field_124: "1", + + field_126: "4", + field_128: "1234.56", + field_129: "321.32", + field_130: "13.14", + field_131: "101.11", + field_132: "1500.19", + field_133: "1", + field_134: "234.56", + + field_27: "15", + field_28: "0", + field_33: now.day.to_s, + field_34: now.month.to_s, + field_35: now.strftime("%g"), + + field_4: "1", + } + end - # it "returns true" do - # expect(parser).to be_valid - # end + 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 + 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 + expect(questions.map(&:id).size).to eq(0) + expect(questions.map(&:id)).to eql([]) + end + end + end # context "when setup section not complete" do # let(:attributes) { { bulk_upload:, field_7: "123" } } @@ -686,7 +692,7 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do # expect(parser.errors[:field_103]).to be_present # end # end - end + # end end # describe "#log" do