Browse Source

Update shortfall validation

pull/708/head
baarkerlounger 3 years ago
parent
commit
adb30b22ba
  1. 8
      app/models/derived_variables/case_log_variables.rb
  2. 10
      app/models/validations/financial_validations.rb
  3. 5
      config/locales/en.yml
  4. 44
      spec/models/validations/financial_validations_spec.rb

8
app/models/derived_variables/case_log_variables.rb

@ -34,15 +34,11 @@ module DerivedVariables::CaseLogVariables
self.wpschrge = weekly_value(pscharge) if pscharge.present? self.wpschrge = weekly_value(pscharge) if pscharge.present?
self.wsupchrg = weekly_value(supcharg) if supcharg.present? self.wsupchrg = weekly_value(supcharg) if supcharg.present?
self.wtcharge = weekly_value(tcharge) if tcharge.present? self.wtcharge = weekly_value(tcharge) if tcharge.present?
if is_supported_housing? && chcharge.present? self.wtshortfall = weekly_value(tshortfall) if tshortfall && receives_housing_related_benefits?
self.wchchrg = weekly_value(chcharge) self.wchchrg = weekly_value(chcharge) if is_supported_housing? && chcharge.present?
end
end end
self.has_benefits = get_has_benefits self.has_benefits = get_has_benefits
self.tshortfall_known = 0 if tshortfall self.tshortfall_known = 0 if tshortfall
self.wtshortfall = if tshortfall && receives_housing_related_benefits?
weekly_value(tshortfall)
end
self.nocharge = household_charge&.zero? ? 1 : 0 self.nocharge = household_charge&.zero? ? 1 : 0
self.housingneeds = get_housingneeds self.housingneeds = get_housingneeds
if is_renewal? if is_renewal?

10
app/models/validations/financial_validations.rb

@ -71,9 +71,13 @@ module Validations::FinancialValidations
end end
def validate_rent_amount(record) def validate_rent_amount(record)
if record.brent.present? && record.tshortfall.present? && record.brent < record.tshortfall * 2 if record.wtshortfall
record.errors.add :brent, I18n.t("validations.financial.rent.less_than_double_shortfall", tshortfall: record.tshortfall * 2) if record.wrent && (record.wtshortfall > record.wrent)
record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent") record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent")
record.errors.add :brent, I18n.t("validations.financial.rent.less_than_shortfall")
elsif record.wtshortfall < 0.01
record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.must_be_positive")
end
end end
if record.tcharge.present? && weekly_value_in_range(record, "tcharge", 0, 9.99) if record.tcharge.present? && weekly_value_in_range(record, "tcharge", 0, 9.99)

5
config/locales/en.yml

@ -97,7 +97,8 @@ en:
financial: financial:
tshortfall: tshortfall:
outstanding_amount_not_required: "You must not answer the outstanding amount question if you don’t have outstanding rent or charges" outstanding_amount_not_required: "You must not answer the outstanding amount question if you don’t have outstanding rent or charges"
more_than_rent: "Answer must be less than half of the basic rent amount" more_than_rent: "Answer must be less than the basic rent amount"
must_be_positive: "Answer must be more than £0.01 as you told us there is an outstanding amount"
hbrentshortfall: hbrentshortfall:
outstanding_no_benefits: "Answer cannot be ‘yes’ to outstanding amount for basic rent or charges if tenant does not receive housing benefit or Universal Credit or you‘re not sure" outstanding_no_benefits: "Answer cannot be ‘yes’ to outstanding amount for basic rent or charges if tenant does not receive housing benefit or Universal Credit or you‘re not sure"
benefits: benefits:
@ -109,7 +110,7 @@ en:
earnings_missing: "Enter how much income the household has in total" earnings_missing: "Enter how much income the household has in total"
negative_currency: "Enter an amount above 0" negative_currency: "Enter an amount above 0"
rent: rent:
less_than_double_shortfall: "Answer must be more than double the shortfall in basic rent" less_than_shortfall: "Answer must be more than the shortfall in basic rent"
scharge: scharge:
private_registered_provider: private_registered_provider:
general_needs: "Service charge must be between £0 and £55 per week if the landlord is a private registered provider and it is a general needs letting" general_needs: "Service charge must be between £0 and £55 per week if the landlord is a private registered provider and it is a general needs letting"

44
spec/models/validations/financial_validations_spec.rb

@ -77,14 +77,43 @@ RSpec.describe Validations::FinancialValidations do
end end
context "when outstanding rent or charges is yes" do context "when outstanding rent or charges is yes" do
let(:record) { FactoryBot.create(:case_log, :about_completed) }
it "expects that a shortfall is provided" do it "expects that a shortfall is provided" do
record.hbrentshortfall = 1 record.hbrentshortfall = 1
record.tshortfall = 99 record.tshortfall = 99
financial_validator.validate_outstanding_rent_amount(record) financial_validator.validate_outstanding_rent_amount(record)
expect(record.errors["tshortfall"]).to be_empty expect(record.errors["tshortfall"]).to be_empty
end end
it "validates that the shortfall is a positive £ amount" do
record.hb = 6
record.hbrentshortfall = 1
record.tshortfall_known = 0
record.tshortfall = 0
record.period = 2
record.set_derived_fields!
financial_validator.validate_rent_amount(record)
expect(record.errors["tshortfall"])
.to include(match I18n.t("validations.financial.tshortfall.must_be_positive"))
end
it "validates that basic rent is no less than the shortfall" do
record.hb = 6
record.hbrentshortfall = 1
record.tshortfall_known = 0
record.tshortfall = 299.50
record.brent = 198
record.period = 2
record.set_derived_fields!
financial_validator.validate_rent_amount(record)
expect(record.errors["brent"])
.to include(match I18n.t("validations.financial.rent.less_than_shortfall"))
expect(record.errors["tshortfall"])
.to include(match I18n.t("validations.financial.tshortfall.more_than_rent"))
end
end
end end
end
describe "rent period validations" do describe "rent period validations" do
let(:organisation) { FactoryBot.create(:organisation) } let(:organisation) { FactoryBot.create(:organisation) }
@ -213,19 +242,6 @@ RSpec.describe Validations::FinancialValidations do
end end
describe "rent and charges validations" do describe "rent and charges validations" do
context "when shortfall amount is provided" do
it "validates that basic rent is no less than double the shortfall" do
record.hbrentshortfall = 2
record.tshortfall = 99.50
record.brent = 198
financial_validator.validate_rent_amount(record)
expect(record.errors["brent"])
.to include(match I18n.t("validations.financial.rent.less_than_double_shortfall", shortfall: 198))
expect(record.errors["tshortfall"])
.to include(match I18n.t("validations.financial.tshortfall.more_than_rent"))
end
end
context "when the owning organisation is a private registered provider" do context "when the owning organisation is a private registered provider" do
before { record.owning_organisation.provider_type = 2 } before { record.owning_organisation.provider_type = 2 }

Loading…
Cancel
Save