Browse Source

add hard validation that percentage owned after a staircasing transaction cannot be less than the percentage bought in that transaction, with associated tests

pull/1235/head
Arthur Campbell 3 years ago
parent
commit
a08c1f27a1
  1. 4
      app/models/form/sales/pages/staircase_bought_value_check.rb
  2. 8
      app/models/validations/sales/financial_validations.rb
  3. 3
      config/locales/en.yml
  4. 27
      spec/models/validations/sales/financial_validations_spec.rb

4
app/models/form/sales/pages/staircase_bought_value_check.rb

@ -13,8 +13,8 @@ class Form::Sales::Pages::StaircaseBoughtValueCheck < ::Form::Page
{ {
"key" => "stairbought", "key" => "stairbought",
"i18n_template" => "percentage", "i18n_template" => "percentage",
} },
] ],
} }
@informative_text = {} @informative_text = {}
end end

8
app/models/validations/sales/financial_validations.rb

@ -27,4 +27,12 @@ module Validations::Sales::FinancialValidations
record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid") record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid")
end end
end end
def validate_percentage_bought_not_greater_than_percentage_owned(record)
return unless record.stairbought && record.stairowned
if record.stairbought > record.stairowned
record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned")
end
end
end end

3
config/locales/en.yml

@ -277,6 +277,9 @@ en:
out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}" out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}"
not_provided: "Enter how much rent and other charges the household pays %{period}" not_provided: "Enter how much rent and other charges the household pays %{period}"
cash_discount_invalid: "Cash discount must be £0 - £999,999" cash_discount_invalid: "Cash discount must be £0 - £999,999"
staircasing:
percentage_bought_must_be_greater_than_percentage_owned: "Total percentage buyer now owns must be more than percentage bought in this transaction"
household: household:
reasonpref: reasonpref:
not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting" not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting"

27
spec/models/validations/sales/financial_validations_spec.rb

@ -99,4 +99,31 @@ RSpec.describe Validations::Sales::FinancialValidations do
expect(record.errors["cashdis"]).to be_empty expect(record.errors["cashdis"]).to be_empty
end end
end end
describe "#validate_percentage_bought_not_greater_than_percentage_owned" do
let(:record) { FactoryBot.create(:sales_log) }
it "does not add an error if the percentage bought is less than the percentage owned" do
record.stairbought = 20
record.stairowned = 40
financial_validator.validate_percentage_bought_not_greater_than_percentage_owned(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["stairowned"]).to be_empty
end
it "does not add an error if the percentage bought is equal to the percentage owned" do
record.stairbought = 30
record.stairowned = 30
financial_validator.validate_percentage_bought_not_greater_than_percentage_owned(record)
expect(record.errors["stairbought"]).to be_empty
expect(record.errors["stairowned"]).to be_empty
end
it "adds an error to stairowned and not stairbought if the percentage bought is more than the percentage owned" do
record.stairbought = 50
record.stairowned = 40
financial_validator.validate_percentage_bought_not_greater_than_percentage_owned(record)
expect(record.errors["stairowned"]).to include(match I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned"))
end
end
end end

Loading…
Cancel
Save