diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 21f3743ca..255f12bf2 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -52,6 +52,21 @@ module Validations::Sales::FinancialValidations end end + def validate_percentage_bought_at_least_threshold(record) + return unless record.stairbought && record.type + + threshold = if [2, 16, 18, 24].include? record.type + 10 + else + 1 + end + + if threshold && record.stairbought < threshold + record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_must_be_at_least_threshold", threshold:) + record.errors.add :type, I18n.t("validations.setup.type.percentage_bought_must_be_at_least_threshold", threshold:) + end + end + def validate_child_income(record) return unless record.income2 && record.ecstat2 diff --git a/config/locales/en.yml b/config/locales/en.yml index d21bb5b5c..e17c0ab9b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -159,6 +159,8 @@ en: Enter a date within the %{current_start_year_short}/%{current_end_year_short} financial year, which is between %{current_start_year_long} and %{current_end_year_long} previous_and_current_financial_year: "Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}" + type: + percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}% for this shared ownership type" startdate: later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date" @@ -302,6 +304,7 @@ en: staircasing: percentage_bought_must_be_greater_than_percentage_owned: "Total percentage buyer now owns must be more than percentage bought in this transaction" older_person_percentage_owned_maximum_75: "Percentage cannot be above 75% under Older Person's Shared Ownership" + percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}%" household: reasonpref: diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index fae0e4536..d9406aec9 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -160,6 +160,48 @@ RSpec.describe Validations::Sales::FinancialValidations do end end + describe "#validate_percentage_bought_at_least_threshold" do + let(:record) { FactoryBot.create(:sales_log) } + + it "adds an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do + record.stairbought = 9 + [2, 16, 18, 24].each do |type| + record.type = type + financial_validator.validate_percentage_bought_at_least_threshold(record) + expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 10%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 10% for this shared ownership type"]) + record.errors.clear + end + + record.stairbought = 0 + [28, 30, 31, 32].each do |type| + record.type = type + financial_validator.validate_percentage_bought_at_least_threshold(record) + expect(record.errors["stairbought"]).to eq(["The minimum increase in equity while staircasing is 1%"]) + expect(record.errors["type"]).to eq(["The minimum increase in equity while staircasing is 1% for this shared ownership type"]) + record.errors.clear + end + end + + it "doesn't add an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do + record.stairbought = 10 + [2, 16, 18, 24].each do |type| + record.type = type + financial_validator.validate_percentage_bought_at_least_threshold(record) + expect(record.errors).to be_empty + record.errors.clear + end + + record.stairbought = 1 + [28, 30, 31, 32].each do |type| + record.type = type + financial_validator.validate_percentage_bought_at_least_threshold(record) + expect(record.errors).to be_empty + record.errors.clear + end + end + end + describe "#validate_percentage_owned_not_too_much_if_older_person" do let(:record) { FactoryBot.create(:sales_log) }