Browse Source

CLDC-3338: Add tolerance to discounted sale calculations (#2333)

* Rename method

* Update staircase/non staircase validations

* Add errors to type

* Remove validate_shared_ownership_deposit

* Don't add setup BU errors, deduplicate different sale type errors

* Add tolerance

* Reuse method

* Rename methods

* Skip type error completely in BU

* Update validation messages

* Update over tolerance method

* C:DC-3338: Add tolerance to grant calculations

---------

Co-authored-by: Kat <katrina@kosiak.co.uk>
pull/2341/head
Robert Sullivan 9 months ago committed by natdeanlewissoftwire
parent
commit
cdecdfebfe
  1. 2
      app/models/validations/sales/sale_information_validations.rb
  2. 82
      spec/models/validations/sales/sale_information_validations_spec.rb

2
app/models/validations/sales/sale_information_validations.rb

@ -47,7 +47,7 @@ module Validations::Sales::SaleInformationValidations
return unless record.mortgage || record.mortgageused == 2 || record.mortgageused == 3 return unless record.mortgage || record.mortgageused == 2 || record.mortgageused == 3
return unless record.discount || record.grant || record.type == 29 return unless record.discount || record.grant || record.type == 29
if record.mortgage_deposit_and_grant_total != record.value_with_discount && record.discounted_ownership_sale? if over_tolerance?(record.mortgage_deposit_and_grant_total, record.value_with_discount, 1) && record.discounted_ownership_sale?
%i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field| %i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field|
record.errors.add field, I18n.t("validations.sale_information.discounted_ownership_value", mortgage_deposit_and_grant_total: record.field_formatted_as_currency("mortgage_deposit_and_grant_total"), value_with_discount: record.field_formatted_as_currency("value_with_discount")) record.errors.add field, I18n.t("validations.sale_information.discounted_ownership_value", mortgage_deposit_and_grant_total: record.field_formatted_as_currency("mortgage_deposit_and_grant_total"), value_with_discount: record.field_formatted_as_currency("value_with_discount"))
end end

82
spec/models/validations/sales/sale_information_validations_spec.rb

@ -235,21 +235,83 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "and is provided" do context "and is provided" do
it "returns true if mortgage, deposit and grant total does not equal market value" do it "adds an error if mortgage, deposit and grant at least 1 greater than discounted value" do
record.grant = 3_000 record.mortgage = 30_000
record.deposit = 5_000
record.grant = 15_000
record.value = 99_998
record.discount = 50
sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £49,999.00. These figures should be the same")
expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £49,999.00. These figures should be the same")
expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £49,999.00. These figures should be the same")
expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £49,999.00. These figures should be the same")
expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £49,999.00. These figures should be the same")
expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £49,999.00. These figures should be the same")
expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £49,999.00. These figures should be the same")
end
it "adds an error if mortgage, deposit and grant at least 1 less than discounted value" do
record.mortgage = 30_000
record.deposit = 5_000
record.grant = 15_000
record.value = 100_002
record.discount = 50
sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £50,001.00. These figures should be the same")
expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £50,001.00. These figures should be the same")
expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £50,001.00. These figures should be the same")
expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £50,001.00. These figures should be the same")
expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £50,001.00. These figures should be the same")
expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £50,001.00. These figures should be the same")
expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £50,000.00, and the purchase purchase price times by the discount is £50,001.00. These figures should be the same")
end
it "does not add an error if mortgage, deposit and grant less than 1 greater than discounted value" do
record.mortgage = 30_000
record.deposit = 5_000
record.grant = 15_000
record.value = 99_999
record.discount = 50
sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors["mortgage"]).to be_empty
expect(record.errors["value"]).to be_empty
expect(record.errors["deposit"]).to be_empty
expect(record.errors["ownershipsch"]).to be_empty
expect(record.errors["discount"]).to be_empty
expect(record.errors["grant"]).to be_empty
end
it "does not add an error if mortgage, deposit and grant less than 1 less than discounted value" do
record.mortgage = 30_000
record.deposit = 5_000
record.grant = 15_000
record.value = 100_001
record.discount = 50
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same")
expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") expect(record.errors["mortgageused"]).to be_empty
expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") expect(record.errors["mortgage"]).to be_empty
expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") expect(record.errors["value"]).to be_empty
expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") expect(record.errors["deposit"]).to be_empty
expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") expect(record.errors["ownershipsch"]).to be_empty
expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £18,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same") expect(record.errors["discount"]).to be_empty
expect(record.errors["grant"]).to be_empty
end end
it "returns false if mortgage, deposit and grant total equals market value" do it "does not add an error if mortgage, deposit and grant total equals discounted value" do
record.mortgage = 30_000
record.deposit = 5_000
record.grant = 15_000 record.grant = 15_000
record.value = 100_000
record.discount = 50
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty expect(record.errors["mortgageused"]).to be_empty
expect(record.errors["mortgage"]).to be_empty expect(record.errors["mortgage"]).to be_empty
expect(record.errors["value"]).to be_empty expect(record.errors["value"]).to be_empty

Loading…
Cancel
Save