Browse Source

CLDC-2017-monthly-charges-validation (#1448)

* add a validation against the case where monthly leashold charges are zero, test this, remove unnecessary validation on cash discount as this is covered by validate_numeric_min_max

* remove focus: true from describe block

* minor changes for testing and linting

* correct minor bug found in PO

* make minor amendments to failing tests
pull/1500/head
Arthur Campbell 2 years ago committed by GitHub
parent
commit
a08c9d6134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/derived_variables/sales_log_variables.rb
  2. 2
      app/models/form/sales/questions/leasehold_charges.rb
  3. 2
      app/models/form/sales/questions/leasehold_charges_known.rb
  4. 4
      app/models/sales_log.rb
  5. 8
      app/models/validations/sales/financial_validations.rb
  6. 2
      config/locales/en.yml
  7. 2
      spec/fixtures/imports/sales_logs/discounted_ownership_sales_log.xml
  8. 2
      spec/models/form/sales/questions/leasehold_charges_known_spec.rb
  9. 4
      spec/models/form/sales/questions/leasehold_charges_spec.rb
  10. 2
      spec/models/validations/financial_validations_spec.rb
  11. 38
      spec/models/validations/sales/financial_validations_spec.rb
  12. 4
      spec/requests/lettings_logs_controller_spec.rb

4
app/models/derived_variables/sales_log_variables.rb

@ -1,6 +1,7 @@
module DerivedVariables::SalesLogVariables module DerivedVariables::SalesLogVariables
def set_derived_fields! def set_derived_fields!
self.ethnic = 17 if ethnic_refused? self.ethnic = 17 if ethnic_refused?
self.mscharge = nil if monthly_leasehold_charges_unknown?
if exdate.present? if exdate.present?
self.exday = exdate.day self.exday = exdate.day
self.exmonth = exdate.month self.exmonth = exdate.month
@ -12,9 +13,6 @@ module DerivedVariables::SalesLogVariables
self.hoyear = hodate.year self.hoyear = hodate.year
end end
self.deposit = value if outright_sale? && mortgage_not_used? self.deposit = value if outright_sale? && mortgage_not_used?
if mscharge_known.present? && mscharge_known.zero?
self.mscharge = 0
end
if mortgage_not_used? if mortgage_not_used?
self.mortgage = 0 self.mortgage = 0
end end

2
app/models/form/sales/questions/leasehold_charges.rb

@ -5,7 +5,7 @@ class Form::Sales::Questions::LeaseholdCharges < ::Form::Question
@check_answer_label = "Monthly leasehold charges" @check_answer_label = "Monthly leasehold charges"
@header = "Enter the total monthly charge" @header = "Enter the total monthly charge"
@type = "numeric" @type = "numeric"
@min = 0 @min = 1
@width = 5 @width = 5
@prefix = "£" @prefix = "£"
@ownershipsch = ownershipsch @ownershipsch = ownershipsch

2
app/models/form/sales/questions/leasehold_charges_known.rb

@ -3,7 +3,7 @@ class Form::Sales::Questions::LeaseholdChargesKnown < ::Form::Question
super(id, hsh, subsection) super(id, hsh, subsection)
@id = "mscharge_known" @id = "mscharge_known"
@check_answer_label = "Monthly leasehold charges known?" @check_answer_label = "Monthly leasehold charges known?"
@header = "Does the property have any monthly leasehold charges?" @header = "Do you know the monthly leasehold charges for the property?"
@hint_text = "For example, service and management charges" @hint_text = "For example, service and management charges"
@type = "radio" @type = "radio"
@answer_options = ANSWER_OPTIONS @answer_options = ANSWER_OPTIONS

4
app/models/sales_log.rb

@ -299,6 +299,10 @@ class SalesLog < Log
companybuy == 1 companybuy == 1
end end
def monthly_leasehold_charges_unknown?
mscharge_known&.zero?
end
def buyers_age_for_old_persons_shared_ownership_invalid? def buyers_age_for_old_persons_shared_ownership_invalid?
return unless old_persons_shared_ownership? return unless old_persons_shared_ownership?

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

@ -40,12 +40,8 @@ module Validations::Sales::FinancialValidations
record.errors.add :mortgage, :cannot_be_0, message: I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero? record.errors.add :mortgage, :cannot_be_0, message: I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero?
end end
def validate_cash_discount(record) def validate_monthly_leasehold_charges(record)
return unless record.cashdis record.errors.add :mscharge, I18n.t("validations.financial.monthly_leasehold_charges.not_zero") if record.mscharge&.zero?
unless record.cashdis.between?(0, 999_999)
record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid")
end
end end
def validate_percentage_bought_not_greater_than_percentage_owned(record) def validate_percentage_bought_not_greater_than_percentage_owned(record)

2
config/locales/en.yml

@ -320,6 +320,8 @@ en:
percentage_bought_must_be_greater_than_percentage_owned: "Total percentage buyer now owns must be more than percentage bought in this transaction" 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" 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}%" percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}%"
monthly_leasehold_charges:
not_zero: "Monthly leasehold charges cannot be £0 if the property has monthly charges"
equity: equity:
under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%" under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%"
over_max: "The maximum initial equity stake is %{max_equity}%" over_max: "The maximum initial equity stake is %{max_equity}%"

2
spec/fixtures/imports/sales_logs/discounted_ownership_sales_log.xml vendored

@ -280,7 +280,7 @@
<Q34b>33</Q34b> <Q34b>33</Q34b>
<Q35Borrowing>2 No</Q35Borrowing> <Q35Borrowing>2 No</Q35Borrowing>
<Q36CashDeposit>0</Q36CashDeposit> <Q36CashDeposit>0</Q36CashDeposit>
<Q37MonthlyCharges>0.00</Q37MonthlyCharges> <Q37MonthlyCharges>10.00</Q37MonthlyCharges>
</Group> </Group>
<Group> <Group>
<Q40PurchasePrice override-field=""/> <Q40PurchasePrice override-field=""/>

2
spec/models/form/sales/questions/leasehold_charges_known_spec.rb

@ -16,7 +16,7 @@ RSpec.describe Form::Sales::Questions::LeaseholdChargesKnown, type: :model do
end end
it "has the correct header" do it "has the correct header" do
expect(question.header).to eq("Does the property have any monthly leasehold charges?") expect(question.header).to eq("Do you know the monthly leasehold charges for the property?")
end end
it "has the correct check_answer_label" do it "has the correct check_answer_label" do

4
spec/models/form/sales/questions/leasehold_charges_spec.rb

@ -36,11 +36,11 @@ RSpec.describe Form::Sales::Questions::LeaseholdCharges, type: :model do
end end
it "has the correct width" do it "has the correct width" do
expect(question.width).to eq(5) expect(question.width).to be 5
end end
it "has the correct min" do it "has the correct min" do
expect(question.min).to eq(0) expect(question.min).to be 1
end end
it "has the correct prefix" do it "has the correct prefix" do

2
spec/models/validations/financial_validations_spec.rb

@ -86,7 +86,7 @@ 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(:lettings_log, :about_completed, startdate: Time.zone.now) } let(:record) { FactoryBot.create(:lettings_log, :about_completed, startdate: Time.zone.local(2023, 1, 1)) }
it "expects that a shortfall is provided" do it "expects that a shortfall is provided" do
record.hbrentshortfall = 1 record.hbrentshortfall = 1

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

@ -131,28 +131,6 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
end end
describe "#validate_cash_discount" do
let(:record) { FactoryBot.create(:sales_log) }
it "adds an error if the cash discount is below zero" do
record.cashdis = -1
financial_validator.validate_cash_discount(record)
expect(record.errors["cashdis"]).to include(match I18n.t("validations.financial.cash_discount_invalid"))
end
it "adds an error if the cash discount is one million or more" do
record.cashdis = 1_000_000
financial_validator.validate_cash_discount(record)
expect(record.errors["cashdis"]).to include(match I18n.t("validations.financial.cash_discount_invalid"))
end
it "does not add an error if the cash discount is in the expected range" do
record.cashdis = 10_000
financial_validator.validate_cash_discount(record)
expect(record.errors).to be_empty
end
end
describe "#validate_percentage_bought_not_greater_than_percentage_owned" do describe "#validate_percentage_bought_not_greater_than_percentage_owned" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.create(:sales_log) }
@ -178,6 +156,22 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
end end
describe "#validate_monthly_leasehold_charges" do
let(:record) { FactoryBot.create(:sales_log) }
it "does not add an error if monthly leasehold charges are positive" do
record.mscharge = 2345
financial_validator.validate_monthly_leasehold_charges(record)
expect(record.errors).to be_empty
end
it "adds an error if monthly leasehold charges are zero" do
record.mscharge = 0
financial_validator.validate_monthly_leasehold_charges(record)
expect(record.errors[:mscharge]).to include I18n.t("validations.financial.monthly_leasehold_charges.not_zero")
end
end
describe "#validate_percentage_bought_at_least_threshold" do describe "#validate_percentage_bought_at_least_threshold" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.create(:sales_log) }

4
spec/requests/lettings_logs_controller_spec.rb

@ -891,14 +891,14 @@ RSpec.describe LettingsLogsController, type: :request do
end end
end end
context "when a lettings log is for a renewal of supported housing, so property information does not need to show" do context "when a lettings log is for a renewal of supported housing, property information does not need to show" do
let(:lettings_log) do let(:lettings_log) do
FactoryBot.create( FactoryBot.create(
:lettings_log, :lettings_log,
owning_organisation: user.organisation, owning_organisation: user.organisation,
managing_organisation: user.organisation, managing_organisation: user.organisation,
created_by: user, created_by: user,
startdate: Time.zone.now, startdate: Time.zone.local(2023, 1, 1),
renewal: 1, renewal: 1,
needstype: 2, needstype: 2,
rent_type: 3, rent_type: 3,

Loading…
Cancel
Save