Browse Source

add compound validation to ensure that logs of type older person shared ownership may not have a stairbought value of greater than 75% \n associated tests included

pull/1235/head
Arthur Campbell 3 years ago
parent
commit
ef90eef644
  1. 4
      app/models/sales_log.rb
  2. 9
      app/models/validations/sales/financial_validations.rb
  3. 1
      config/locales/en.yml
  4. 32
      spec/models/validations/sales/financial_validations_spec.rb

4
app/models/sales_log.rb

@ -136,6 +136,10 @@ class SalesLog < Log
type == 18
end
def is_older_persons_shared_ownership?
type == 24
end
def ppostcode_full=(postcode)
if postcode
super UKPostcode.parse(postcode).to_s

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

@ -35,4 +35,13 @@ module Validations::Sales::FinancialValidations
record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned")
end
end
def validate_percentage_owned_not_too_much_if_older_person(record)
return unless record.is_older_persons_shared_ownership? && record.stairowned
if record.stairowned > 75
record.errors.add :stairowned, I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75")
record.errors.add :type, I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75")
end
end
end

1
config/locales/en.yml

@ -279,6 +279,7 @@ en:
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"
older_person_percentage_owned_maximum_75: "Percentage cannot be above 75% under Older Person's Shared Ownership"
household:
reasonpref:

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

@ -126,4 +126,36 @@ RSpec.describe Validations::Sales::FinancialValidations do
expect(record.errors["stairowned"]).to include(match I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned"))
end
end
describe "#validate_percentage_owned_not_too_much_if_older_person" do
let(:record) { FactoryBot.create(:sales_log) }
context "when log type is not older persons shared ownership" do
it "does not add an error when percentage owned after staircasing transaction exceeds 75%" do
record.type = 2
record.stairowned = 80
financial_validator.validate_percentage_owned_not_too_much_if_older_person(record)
expect(record.errors["stairowned"]).to be_empty
expect(record.errors["type"]).to be_empty
end
end
context "when log type is older persons shared ownership" do
it "does not add an error when percentage owned after staircasing transaction is less than 75%" do
record.type = 24
record.stairowned = 50
financial_validator.validate_percentage_owned_not_too_much_if_older_person(record)
expect(record.errors["stairowned"]).to be_empty
expect(record.errors["type"]).to be_empty
end
it "adds an error when percentage owned after staircasing transaction exceeds 75%" do
record.type = 24
record.stairowned = 90
financial_validator.validate_percentage_owned_not_too_much_if_older_person(record)
expect(record.errors["stairowned"]).to include(match I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75"))
expect(record.errors["type"]).to include(match I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75"))
end
end
end
end

Loading…
Cancel
Save