diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index f39c17af7..fc1c44585 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -188,7 +188,12 @@ private collection_year = record.collection_start_year - rent_range = LaRentRange.find_by(start_year: collection_year, la: record.la, beds: record.beds_for_la_rent_range, lettype: record.lettype) + rent_range = LaRentRange.find_by( + start_year: collection_year, + la: record.la, + beds: record.beds_for_la_rent_range, + lettype: record.lettype, + ) if rent_range.present? && !weekly_value_in_range(record, "brent", rent_range.hard_min, rent_range.hard_max) && record.brent.present? && record.period.present? if record.weekly_value(record["brent"]) < rent_range.hard_min @@ -201,7 +206,9 @@ private record.errors.add :rent_type, I18n.t("validations.financial.brent.rent_type.below_hard_min") record.errors.add :needstype, I18n.t("validations.financial.brent.needstype.below_hard_min") record.errors.add :period, I18n.t("validations.financial.brent.period.below_hard_min") - elsif record.beds.blank? || record.beds < LaRentRange::MAX_BEDS + end + + if record.weekly_value(record["brent"]) > rent_range.hard_max record.errors.add :brent, I18n.t("validations.financial.brent.above_hard_max") record.errors.add :beds, I18n.t("validations.financial.brent.beds.above_hard_max") record.errors.add :la, I18n.t("validations.financial.brent.la.above_hard_max") diff --git a/app/services/bulk_upload/lettings/year2022/row_parser.rb b/app/services/bulk_upload/lettings/year2022/row_parser.rb index d9cc99505..432897d0b 100644 --- a/app/services/bulk_upload/lettings/year2022/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2022/row_parser.rb @@ -520,7 +520,7 @@ private end def validate_only_one_housing_needs_type - if [field_55, field_56, field_57].compact.count.positive? + if [field_55, field_56, field_57].compact.count > 1 errors.add(:field_55, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) errors.add(:field_56, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) errors.add(:field_57, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) @@ -866,28 +866,28 @@ private attributes["tenancylength"] = field_11 attributes["declaration"] = field_132 - attributes["age1_known"] = (field_12 == "R" || field_12.blank? ? 1 : 0) + attributes["age1_known"] = age1_known? attributes["age1"] = field_12 if attributes["age1_known"].zero? && field_12&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age2_known"] = (field_13 == "R" || field_13.blank? ? 1 : 0) + attributes["age2_known"] = age2_known? attributes["age2"] = field_13 if attributes["age2_known"].zero? && field_13&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age3_known"] = (field_14 == "R" || field_14.blank? ? 1 : 0) + attributes["age3_known"] = age3_known? attributes["age3"] = field_14 if attributes["age3_known"].zero? && field_14&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age4_known"] = (field_15 == "R" || field_15.blank? ? 1 : 0) + attributes["age4_known"] = age4_known? attributes["age4"] = field_15 if attributes["age4_known"].zero? && field_15&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age5_known"] = (field_16 == "R" || field_16.blank? ? 1 : 0) + attributes["age5_known"] = age5_known? attributes["age5"] = field_16 if attributes["age5_known"].zero? && field_16&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age6_known"] = (field_17 == "R" || field_17.blank? ? 1 : 0) + attributes["age6_known"] = age6_known? attributes["age6"] = field_17 if attributes["age6_known"].zero? && field_17&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age7_known"] = (field_18 == "R" || field_18.blank? ? 1 : 0) + attributes["age7_known"] = age7_known? attributes["age7"] = field_18 if attributes["age7_known"].zero? && field_18&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age8_known"] = (field_19 == "R" || field_19.blank? ? 1 : 0) + attributes["age8_known"] = age8_known? attributes["age8"] = field_19 if attributes["age8_known"].zero? && field_19&.match(/\A\d{1,3}\z|\AR\z/) attributes["sex1"] = field_20 @@ -920,13 +920,13 @@ private attributes["ecstat7"] = field_41 attributes["ecstat8"] = field_42 - 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["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_45 attributes["leftreg"] = leftreg @@ -1112,7 +1112,32 @@ private end end - def details_known(person_n) + def age1_known? + return 1 if field_12 == "R" + return 1 if field_12.blank? + + 0 + end + + [ + { person: 2, field: :field_13 }, + { person: 3, field: :field_14 }, + { person: 4, field: :field_15 }, + { person: 5, field: :field_16 }, + { person: 6, field: :field_17 }, + { person: 7, field: :field_18 }, + { person: 8, field: :field_19 }, + ].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 details_known?(person_n) send("person_#{person_n}_present?") ? 0 : 1 end @@ -1129,31 +1154,31 @@ private end def person_2_present? - field_13.present? && field_21.present? && field_28.present? + field_13.present? || field_21.present? || field_28.present? end def person_3_present? - field_14.present? && field_22.present? && field_29.present? + field_14.present? || field_22.present? || field_29.present? end def person_4_present? - field_15.present? && field_23.present? && field_30.present? + field_15.present? || field_23.present? || field_30.present? end def person_5_present? - field_16.present? && field_24.present? && field_31.present? + field_16.present? || field_24.present? || field_31.present? end def person_6_present? - field_17.present? && field_25.present? && field_32.present? + field_17.present? || field_25.present? || field_32.present? end def person_7_present? - field_18.present? && field_26.present? && field_33.present? + field_18.present? || field_26.present? || field_33.present? end def person_8_present? - field_19.present? && field_27.present? && field_34.present? + field_19.present? || field_27.present? || field_34.present? end def tshortfall_known diff --git a/app/services/bulk_upload/lettings/year2023/row_parser.rb b/app/services/bulk_upload/lettings/year2023/row_parser.rb index dc2a04a66..d1689b2dd 100644 --- a/app/services/bulk_upload/lettings/year2023/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2023/row_parser.rb @@ -419,7 +419,7 @@ private end def validate_only_one_housing_needs_type - if [field_83, field_84, field_85].compact.count.positive? + if [field_83, field_84, field_85].compact.count > 1 errors.add(:field_83, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) errors.add(:field_84, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) errors.add(:field_85, I18n.t("validations.household.housingneeds_type.only_one_option_permitted")) @@ -805,28 +805,28 @@ private attributes["tenancylength"] = field_43 attributes["declaration"] = field_45 - attributes["age1_known"] = (field_46 == "R" || field_46.blank? ? 1 : 0) + attributes["age1_known"] = age1_known? attributes["age1"] = field_46 if attributes["age1_known"].zero? && field_46&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age2_known"] = (field_52 == "R" || field_52.blank? ? 1 : 0) + attributes["age2_known"] = age2_known? attributes["age2"] = field_52 if attributes["age2_known"].zero? && field_52&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age3_known"] = (field_56 == "R" || field_56.blank? ? 1 : 0) + attributes["age3_known"] = age3_known? attributes["age3"] = field_56 if attributes["age3_known"].zero? && field_56&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age4_known"] = (field_60 == "R" || field_60.blank? ? 1 : 0) + attributes["age4_known"] = age4_known? attributes["age4"] = field_60 if attributes["age4_known"].zero? && field_60&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age5_known"] = (field_64 == "R" || field_64.blank? ? 1 : 0) + attributes["age5_known"] = age5_known? attributes["age5"] = field_64 if attributes["age5_known"].zero? && field_64&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age6_known"] = (field_68 == "R" || field_68.blank? ? 1 : 0) + attributes["age6_known"] = age6_known? attributes["age6"] = field_68 if attributes["age6_known"].zero? && field_68&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age7_known"] = (field_72 == "R" || field_72.blank? ? 1 : 0) + attributes["age7_known"] = age7_known? attributes["age7"] = field_72 if attributes["age7_known"].zero? && field_72&.match(/\A\d{1,3}\z|\AR\z/) - attributes["age8_known"] = (field_76 == "R" || field_76.blank? ? 1 : 0) + attributes["age8_known"] = age8_known? attributes["age8"] = field_76 if attributes["age8_known"].zero? && field_76&.match(/\A\d{1,3}\z|\AR\z/) attributes["sex1"] = field_47 @@ -859,13 +859,13 @@ private 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["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 @@ -1077,36 +1077,61 @@ private end end - def details_known(person_n) + def age1_known? + return 1 if field_46 == "R" + return 1 if field_46.blank? + + 0 + end + + [ + { person: 2, field: :field_52 }, + { person: 3, field: :field_56 }, + { person: 4, field: :field_60 }, + { person: 5, field: :field_64 }, + { person: 6, field: :field_68 }, + { person: 7, field: :field_72 }, + { person: 8, field: :field_76 }, + ].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 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? + field_51.present? || field_52.present? || field_53.present? end def person_3_present? - field_55.present? && field_56.present? && field_57.present? + field_55.present? || field_56.present? || field_57.present? end def person_4_present? - field_59.present? && field_60.present? && field_61.present? + field_59.present? || field_60.present? || field_61.present? end def person_5_present? - field_63.present? && field_64.present? && field_65.present? + field_63.present? || field_64.present? || field_65.present? end def person_6_present? - field_67.present? && field_68.present? && field_69.present? + field_67.present? || field_68.present? || field_69.present? end def person_7_present? - field_71.present? && field_72.present? && field_73.present? + field_71.present? || field_72.present? || field_73.present? end def person_8_present? - field_75.present? && field_76.present? && field_77.present? + field_75.present? || field_76.present? || field_77.present? end def leftreg diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 1f40f7cbc..8cb777029 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -788,7 +788,7 @@ RSpec.describe Validations::FinancialValidations do LaRentRange.create!( ranges_rent_id: "1", la: "E07000223", - beds: 1, + beds: 4, lettype: 1, soft_min: 12.41, soft_max: 89.54, @@ -814,7 +814,7 @@ RSpec.describe Validations::FinancialValidations do record.lettype = 1 record.period = 1 record.la = "E07000223" - record.beds = 1 + record.beds = 4 record.startdate = Time.zone.local(2021, 9, 17) record.brent = 9.17 @@ -846,7 +846,7 @@ RSpec.describe Validations::FinancialValidations do record.lettype = 1 record.period = 1 record.la = "E07000223" - record.beds = 1 + record.beds = 4 record.startdate = Time.zone.local(2021, 9, 17) record.brent = 200 @@ -883,7 +883,7 @@ RSpec.describe Validations::FinancialValidations do record.period = 1 record.la = "E07000223" record.startdate = Time.zone.local(2022, 2, 5) - record.beds = 1 + record.beds = 4 record.brent = 200 financial_validator.validate_rent_amount(record) diff --git a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb index 6355bb3bd..bada35dad 100644 --- a/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2022/row_parser_spec.rb @@ -446,6 +446,16 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do end end + describe "#field_14" do # age3 + context "when blank but gender given" do + let(:attributes) { valid_attributes.merge(field_14: "", field_22: "F") } + + it "returns an error" do + expect(parser.errors[:field_14]).to be_present + end + end + end + describe "#field_52" do # leaving reason context "when field_134 is 1 meaning it is a renewal" do context "when field_52 is 40" do @@ -475,6 +485,16 @@ RSpec.describe BulkUpload::Lettings::Year2022::RowParser do end describe "#field_55, #field_56, #field_57" do + context "when one item selected" do + let(:attributes) { { bulk_upload:, field_55: "1" } } + + it "is permitted" do + expect(parser.errors[:field_55]).to be_blank + expect(parser.errors[:field_56]).to be_blank + expect(parser.errors[:field_57]).to be_blank + end + end + context "when more than one item selected" do let(:attributes) { { bulk_upload:, field_55: "1", field_56: "1" } } 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 4d745f21e..7dc1e177b 100644 --- a/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb +++ b/spec/services/bulk_upload/lettings/year2023/row_parser_spec.rb @@ -438,6 +438,16 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end describe "#field_83, #field_84, #field_85" do + context "when one item selected" do + let(:attributes) { { bulk_upload:, field_83: "1" } } + + it "is permitted" do + expect(parser.errors[:field_83]).to be_blank + expect(parser.errors[:field_84]).to be_blank + expect(parser.errors[:field_85]).to be_blank + end + end + context "when more than one item selected" do let(:attributes) { { bulk_upload:, field_83: "1", field_84: "1" } } @@ -723,6 +733,16 @@ RSpec.describe BulkUpload::Lettings::Year2023::RowParser do end end end + + describe "#field_56" do # age3 + context "when null but gender given" do + let(:attributes) { setup_section_params.merge({ field_56: "", field_57: "F" }) } + + it "returns an error" do + expect(parser.errors[:field_56]).to be_present + end + end + end end describe "#log" do