diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index f6086a918..98517058e 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -279,11 +279,17 @@ class SalesLog < Log mortgage_amount + deposit_amount + cashdis_amount end + def value_times_equity + return unless value && equity + + value * equity / 100 + end + def mortgage_deposit_and_discount_error_fields [ "mortgage", "deposit", - cashdis.present? ? "discount" : nil, + cashdis.present? ? "cash discount" : nil, ].compact.to_sentence end diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index bc1ca95cf..59b84f7e3 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -101,7 +101,10 @@ module Validations::Sales::FinancialValidations if mortgage_value + record.deposit + record.cashdis != record.value * record.equity / 100 %i[mortgage value deposit ownershipsch cashdis equity].each do |field| - record.errors.add field, I18n.t("validations.financial.shared_ownership_deposit", mortgage_deposit_and_discount_error_fields: record.mortgage_deposit_and_discount_error_fields, mortgage_deposit_and_discount_total: record.field_formatted_as_currency("mortgage_deposit_and_discount_total")) + record.errors.add field, I18n.t("validations.financial.shared_ownership_deposit", + mortgage_deposit_and_discount_error_fields: record.mortgage_deposit_and_discount_error_fields, + mortgage_deposit_and_discount_total: record.field_formatted_as_currency("mortgage_deposit_and_discount_total"), + value_times_equity: record.field_formatted_as_currency("value_times_equity")) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index eb2a17ce5..2a8d86951 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -433,7 +433,7 @@ en: 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" - shared_ownership_deposit: "You told us that the %{mortgage_deposit_and_discount_error_fields} add up to %{mortgage_deposit_and_discount_total}" + shared_ownership_deposit: "The %{mortgage_deposit_and_discount_error_fields} added together is %{mortgage_deposit_and_discount_total}. The value times the equity percentage is %{value_times_equity}. These figures should be the same" household: reasonable_preference_reason: diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 873903c56..295b11c2c 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -393,11 +393,11 @@ RSpec.describe Validations::Sales::FinancialValidations do record.equity = 100 financial_validator.validate_shared_ownership_deposit(record) - expect(record.errors["mortgage"]).to include("You told us that the mortgage, deposit, and discount add up to £2,000.00") - expect(record.errors["deposit"]).to include("You told us that the mortgage, deposit, and discount add up to £2,000.00") - expect(record.errors["cashdis"]).to include("You told us that the mortgage, deposit, and discount add up to £2,000.00") - expect(record.errors["value"]).to include("You told us that the mortgage, deposit, and discount add up to £2,000.00") - expect(record.errors["equity"]).to include("You told us that the mortgage, deposit, and discount add up to £2,000.00") + expect(record.errors["mortgage"]).to include("The mortgage, deposit, and cash discount added together is £2,000.00. The value times the equity percentage is £3,000.00. These figures should be the same") + expect(record.errors["deposit"]).to include("The mortgage, deposit, and cash discount added together is £2,000.00. The value times the equity percentage is £3,000.00. These figures should be the same") + expect(record.errors["cashdis"]).to include("The mortgage, deposit, and cash discount added together is £2,000.00. The value times the equity percentage is £3,000.00. These figures should be the same") + expect(record.errors["value"]).to include("The mortgage, deposit, and cash discount added together is £2,000.00. The value times the equity percentage is £3,000.00. These figures should be the same") + expect(record.errors["equity"]).to include("The mortgage, deposit, and cash discount added together is £2,000.00. The value times the equity percentage is £3,000.00. These figures should be the same") end it "does not add an error if no deposit is given" do @@ -487,11 +487,11 @@ RSpec.describe Validations::Sales::FinancialValidations do record.equity = 100 financial_validator.validate_shared_ownership_deposit(record) - expect(record.errors["mortgage"]).to include("You told us that the mortgage, deposit, and discount add up to £3,000.00") - expect(record.errors["deposit"]).to include("You told us that the mortgage, deposit, and discount add up to £3,000.00") - expect(record.errors["cashdis"]).to include("You told us that the mortgage, deposit, and discount add up to £3,000.00") - expect(record.errors["value"]).to include("You told us that the mortgage, deposit, and discount add up to £3,000.00") - expect(record.errors["equity"]).to include("You told us that the mortgage, deposit, and discount add up to £3,000.00") + expect(record.errors["mortgage"]).to include("The mortgage, deposit, and cash discount added together is £3,000.00. The value times the equity percentage is £4,323.00. These figures should be the same") + expect(record.errors["deposit"]).to include("The mortgage, deposit, and cash discount added together is £3,000.00. The value times the equity percentage is £4,323.00. These figures should be the same") + expect(record.errors["cashdis"]).to include("The mortgage, deposit, and cash discount added together is £3,000.00. The value times the equity percentage is £4,323.00. These figures should be the same") + expect(record.errors["value"]).to include("The mortgage, deposit, and cash discount added together is £3,000.00. The value times the equity percentage is £4,323.00. These figures should be the same") + expect(record.errors["equity"]).to include("The mortgage, deposit, and cash discount added together is £3,000.00. The value times the equity percentage is £4,323.00. These figures should be the same") end end