From 98faef75b7894404ae841e5810f94c7d778333b1 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 17 Apr 2023 16:13:52 +0100 Subject: [PATCH] feat: add hard income validations to uprn question --- app/models/form/lettings/questions/uprn.rb | 2 +- app/models/form/sales/questions/uprn.rb | 2 +- app/models/lettings_log.rb | 4 ++-- app/models/sales_log.rb | 4 ++-- app/models/validations/financial_validations.rb | 2 ++ .../validations/sales/financial_validations.rb | 6 +++--- config/locales/en.yml | 3 +++ db/schema.rb | 15 ++++++++------- spec/models/form/lettings/questions/uprn_spec.rb | 2 +- spec/models/form/sales/questions/uprn_spec.rb | 2 +- 10 files changed, 24 insertions(+), 18 deletions(-) diff --git a/app/models/form/lettings/questions/uprn.rb b/app/models/form/lettings/questions/uprn.rb index efe9a4ea4..5ce6ce9c1 100644 --- a/app/models/form/lettings/questions/uprn.rb +++ b/app/models/form/lettings/questions/uprn.rb @@ -3,7 +3,7 @@ class Form::Lettings::Questions::Uprn < ::Form::Question super @id = "uprn" @check_answer_label = "UPRN" - @header = "What is the property's UPRN" + @header = "What is the property's UPRN?" @type = "text" @hint_text = "The Unique Property Reference Number (UPRN) is a unique number system created by Ordnance Survey and used by housing providers and sectors UK-wide. For example 10010457355." @width = 10 diff --git a/app/models/form/sales/questions/uprn.rb b/app/models/form/sales/questions/uprn.rb index 7967a2035..8bc0072bd 100644 --- a/app/models/form/sales/questions/uprn.rb +++ b/app/models/form/sales/questions/uprn.rb @@ -3,7 +3,7 @@ class Form::Sales::Questions::Uprn < ::Form::Question super @id = "uprn" @check_answer_label = "UPRN" - @header = "What is the property's UPRN" + @header = "What is the property's UPRN?" @type = "text" @hint_text = "The Unique Property Reference Number (UPRN) is a unique number system created by Ordnance Survey and used by housing providers and sectors UK-wide. For example 10010457355." @width = 10 diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 0ddc921be..896cc4503 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -33,7 +33,7 @@ class LettingsLog < Log before_validation :reset_location_fields!, unless: :postcode_known? before_validation :reset_previous_location_fields!, unless: :previous_postcode_known? before_validation :set_derived_fields! - after_validation :process_uprn_change!, if: :should_process_uprn_change? + before_validation :process_uprn_change!, if: :should_process_uprn_change? belongs_to :scheme, optional: true belongs_to :location, optional: true @@ -671,6 +671,6 @@ private end def should_process_uprn_change? - uprn_changed? && startdate && startdate.year >= 2023 + uprn && stardate && (uprn_changed? || stardate_changed?) && stardate.year >= 2023 end end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index ebb3ef4e3..b0e96ed7f 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -32,7 +32,7 @@ class SalesLog < Log before_validation :reset_location_fields!, unless: :postcode_known? before_validation :reset_previous_location_fields!, unless: :previous_postcode_known? before_validation :set_derived_fields! - after_validation :process_uprn_change!, if: :should_process_uprn_change? + before_validation :process_uprn_change!, if: :should_process_uprn_change? scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_purchaser_code, ->(purchid) { where("purchid ILIKE ?", "%#{purchid}%") } @@ -324,7 +324,7 @@ class SalesLog < Log end def should_process_uprn_change? - uprn_changed? && saledate && saledate.year >= 2023 + uprn && saledate && (uprn_changed? || saledate_changed?) && saledate.year >= 2023 end def value_with_discount diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 82fdf56f3..5b39dd3ad 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -214,6 +214,7 @@ private if record.weekly_value(record["brent"]) < rent_range.hard_min record.errors.add :brent, I18n.t("validations.financial.brent.below_hard_min") record.errors.add :beds, I18n.t("validations.financial.brent.beds.below_hard_min") + record.errors.add :uprn, I18n.t("validations.financial.brent.uprn.below_hard_min") record.errors.add :la, I18n.t("validations.financial.brent.la.below_hard_min") record.errors.add :postcode_known, I18n.t("validations.financial.brent.postcode_known.below_hard_min") record.errors.add :scheme_id, I18n.t("validations.financial.brent.scheme_id.below_hard_min") @@ -226,6 +227,7 @@ private if record.weekly_value(record["brent"]) > rent_range.hard_max record.errors.add :brent, :over_hard_max, message: I18n.t("validations.financial.brent.above_hard_max") record.errors.add :beds, I18n.t("validations.financial.brent.beds.above_hard_max") + record.errors.add :uprn, I18n.t("validations.financial.brent.uprn.above_hard_max") record.errors.add :la, I18n.t("validations.financial.brent.la.above_hard_max") record.errors.add :postcode_known, I18n.t("validations.financial.brent.postcode_known.above_hard_max") record.errors.add :scheme_id, I18n.t("validations.financial.brent.scheme_id.above_hard_max") diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index f725cd41e..c3b1446c9 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -5,7 +5,7 @@ module Validations::Sales::FinancialValidations def validate_income1(record) return unless record.income1 && record.la && record.shared_ownership_scheme? - relevant_fields = %i[income1 ownershipsch la postcode_full] + relevant_fields = %i[income1 ownershipsch uprn la postcode_full] if record.london_property? && record.income1 > 90_000 relevant_fields.each { |field| record.errors.add field, I18n.t("validations.financial.income.over_hard_max_for_london") } elsif record.property_not_in_london? && record.income1 > 80_000 @@ -16,7 +16,7 @@ module Validations::Sales::FinancialValidations def validate_income2(record) return unless record.income2 && record.la && record.shared_ownership_scheme? - relevant_fields = %i[income2 ownershipsch la postcode_full] + relevant_fields = %i[income2 ownershipsch uprn la postcode_full] if record.london_property? && record.income2 > 90_000 relevant_fields.each { |field| record.errors.add field, I18n.t("validations.financial.income.over_hard_max_for_london") } elsif record.property_not_in_london? && record.income2 > 80_000 @@ -28,7 +28,7 @@ module Validations::Sales::FinancialValidations return unless record.income1 && record.income2 && record.la && record.shared_ownership_scheme? combined_income = record.income1 + record.income2 - relevant_fields = %i[income1 income2 ownershipsch la postcode_full] + relevant_fields = %i[income1 income2 ownershipsch uprn la postcode_full] if record.london_property? && combined_income > 90_000 relevant_fields.each { |field| record.errors.add field, I18n.t("validations.financial.income.combined_over_hard_max_for_london") } elsif record.property_not_in_london? && combined_income > 80_000 diff --git a/config/locales/en.yml b/config/locales/en.yml index 074a00147..a18e1c2bc 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -296,6 +296,9 @@ en: postcode_known: below_hard_min: "Rent is below the absolute minimum expected for a property of this type. Please check the rent, rent period, local authority and number of bedrooms" above_hard_max: "Rent is higher than the absolute maximum expected for a property of this type. Please check the rent, rent period, local authority and number of bedrooms" + uprn: + below_hard_min: "Rent is below the absolute minimum expected for a property of this type based on this UPRN" + above_hard_max: "Rent is higher than the absolute maximum expected for a property of this type based on this UPRN" la: below_hard_min: "Rent is below the absolute minimum expected for a property of this type based on this local authority" above_hard_max: "Rent is higher than the absolute maximum expected for a property of this type based on this local authority" diff --git a/db/schema.rb b/db/schema.rb index e121fda09..eff7debad 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -519,6 +519,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_12_143245) do t.integer "prevten" t.integer "mortgageused" t.integer "wchair" + t.integer "income2_value_check" t.integer "armedforcesspouse" t.datetime "hodate", precision: nil t.integer "hoday" @@ -543,14 +544,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_12_143245) do t.integer "retirement_value_check" t.integer "hodate_check" t.integer "extrabor_value_check" - t.integer "grant_value_check" - t.integer "staircase_bought_value_check" t.integer "deposit_and_mortgage_value_check" t.integer "shared_ownership_deposit_value_check" + t.integer "grant_value_check" + t.integer "value_value_check" t.integer "old_persons_shared_ownership_value_check" - t.integer "income2_value_check" + t.integer "staircase_bought_value_check" t.integer "monthly_charges_value_check" - t.integer "value_value_check" t.integer "details_known_5" t.integer "details_known_6" t.integer "saledate_check" @@ -560,10 +560,9 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_12_143245) do t.integer "ethnicbuy2" t.integer "proplen_asked" t.string "old_id" - t.integer "pregblank" t.integer "buy2living" t.integer "prevtenbuy2" - t.integer "nationalbuy2" + t.integer "pregblank" t.string "uprn" t.integer "uprn_known" t.integer "uprn_confirmed" @@ -571,10 +570,12 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_12_143245) do t.string "address_line2" t.string "town_or_city" t.string "county" + t.integer "nationalbuy2" t.integer "discounted_sale_value_check" t.integer "student_not_child_value_check" - t.integer "buyer_livein_value_check" + t.integer "combined_income_value_check" t.integer "percentage_discount_value_check" + t.integer "buyer_livein_value_check" t.index ["bulk_upload_id"], name: "index_sales_logs_on_bulk_upload_id" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["old_id"], name: "index_sales_logs_on_old_id", unique: true diff --git a/spec/models/form/lettings/questions/uprn_spec.rb b/spec/models/form/lettings/questions/uprn_spec.rb index 8f6ba047a..06611bee5 100644 --- a/spec/models/form/lettings/questions/uprn_spec.rb +++ b/spec/models/form/lettings/questions/uprn_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Form::Lettings::Questions::Uprn, type: :model do end it "has the correct header" do - expect(question.header).to eq("What is the property's UPRN") + expect(question.header).to eq("What is the property's UPRN?") end it "has the correct check_answer_label" do diff --git a/spec/models/form/sales/questions/uprn_spec.rb b/spec/models/form/sales/questions/uprn_spec.rb index f8fd75942..562650083 100644 --- a/spec/models/form/sales/questions/uprn_spec.rb +++ b/spec/models/form/sales/questions/uprn_spec.rb @@ -16,7 +16,7 @@ RSpec.describe Form::Sales::Questions::Uprn, type: :model do end it "has the correct header" do - expect(question.header).to eq("What is the property's UPRN") + expect(question.header).to eq("What is the property's UPRN?") end it "has the correct check_answer_label" do