diff --git a/app/models/form/sales/pages/mortgage_amount.rb b/app/models/form/sales/pages/mortgage_amount.rb index 793570b46..73e306c2a 100644 --- a/app/models/form/sales/pages/mortgage_amount.rb +++ b/app/models/form/sales/pages/mortgage_amount.rb @@ -3,9 +3,7 @@ class Form::Sales::Pages::MortgageAmount < ::Form::Page super(id, hsh, subsection) @ownershipsch = ownershipsch @header = "Mortgage Amount" - @depends_on = [{ - "mortgageused" => 1, - }] + @depends_on = [{ "mortgage_used?" => true }] end def questions diff --git a/app/models/form/sales/questions/mortgage_amount.rb b/app/models/form/sales/questions/mortgage_amount.rb index 12959709a..9b199b2aa 100644 --- a/app/models/form/sales/questions/mortgage_amount.rb +++ b/app/models/form/sales/questions/mortgage_amount.rb @@ -5,7 +5,7 @@ class Form::Sales::Questions::MortgageAmount < ::Form::Question @check_answer_label = "Mortgage amount" @header = "What is the mortgage amount?" @type = "numeric" - @min = 0 + @min = 1 @width = 5 @prefix = "£" @hint_text = "Enter the amount of mortgage agreed with the mortgage lender. Exclude any deposits or cash payments. Numeric in pounds. Rounded to the nearest pound." diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index ba2ea4fa8..2a6e1a6d3 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -243,6 +243,10 @@ class SalesLog < Log ownershipsch == 2 end + def mortgage_used? + mortgageused == 1 + end + def mortgage_not_used? mortgageused == 2 end diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index faf436afa..61772be93 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -36,6 +36,10 @@ module Validations::Sales::FinancialValidations end end + def validate_mortgage(record) + record.errors.add :mortgage, I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero? + end + def validate_cash_discount(record) return unless record.cashdis diff --git a/config/locales/en.yml b/config/locales/en.yml index 36d8b93f8..56caa6a59 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -316,6 +316,7 @@ en: 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}%" + mortgage: "Mortgage value cannot be £0 if a mortgage was used for the purchase of this property" household: reasonpref: diff --git a/spec/models/form/sales/pages/mortgage_amount_spec.rb b/spec/models/form/sales/pages/mortgage_amount_spec.rb index 551788fbb..c6f6f924e 100644 --- a/spec/models/form/sales/pages/mortgage_amount_spec.rb +++ b/spec/models/form/sales/pages/mortgage_amount_spec.rb @@ -28,8 +28,6 @@ RSpec.describe Form::Sales::Pages::MortgageAmount, type: :model do end it "has correct depends_on" do - expect(page.depends_on).to eq([{ - "mortgageused" => 1, - }]) + expect(page.depends_on).to eq([{ "mortgage_used?" => true }]) end end diff --git a/spec/models/form/sales/questions/mortgage_amount_spec.rb b/spec/models/form/sales/questions/mortgage_amount_spec.rb index 328406ecd..758101867 100644 --- a/spec/models/form/sales/questions/mortgage_amount_spec.rb +++ b/spec/models/form/sales/questions/mortgage_amount_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do let(:page) { instance_double(Form::Page) } it "has correct page" do - expect(question.page).to eq(page) + expect(question.page).to be(page) end it "has the correct id" do @@ -36,7 +36,7 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do end it "has correct width" do - expect(question.width).to eq(5) + expect(question.width).to be(5) end it "has correct prefix" do @@ -44,6 +44,6 @@ RSpec.describe Form::Sales::Questions::MortgageAmount, type: :model do end it "has correct min" do - expect(question.min).to eq(0) + expect(question.min).to be(1) end end diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index b60ef5e6a..e1eb81dea 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -113,6 +113,24 @@ RSpec.describe Validations::Sales::FinancialValidations do end end + describe "#validate_mortgage" do + let(:record) { FactoryBot.create(:sales_log) } + + it "adds an error is the mortgage is zero" do + record.mortgageused = 1 + record.mortgage = 0 + financial_validator.validate_mortgage(record) + expect(record.errors[:mortgage]).to include I18n.t("validations.financial.mortgage") + end + + it "does not add an error is the mortgage is positive" do + record.mortgageused = 1 + record.mortgage = 234 + financial_validator.validate_mortgage(record) + expect(record.errors).to be_empty + end + end + describe "#validate_cash_discount" do let(:record) { FactoryBot.create(:sales_log) }