From 3856a175afd263bc684bf3776839c4d645010e24 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 26 Feb 2024 17:02:55 +0000 Subject: [PATCH] Add tolerance to soft validation (#2240) --- .../validations/sales/soft_validations.rb | 8 ++++--- .../sales/soft_validations_spec.rb | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index ae9ee66cc..14c0afae4 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -89,9 +89,11 @@ module Validations::Sales::SoftValidations return unless cashdis || !is_type_discount? return unless deposit && value && equity - cash_discount = cashdis || 0 - mortgage_value = mortgage || 0 - mortgage_value + deposit + cash_discount != value * equity / 100 + !within_tolerance?(mortgage_deposit_and_discount_total, value * equity / 100, 1) + end + + def within_tolerance?(expected, actual, tolerance) + (expected - actual).abs <= tolerance end def mortgage_plus_deposit_less_than_discounted_value? diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index e417fbd8e..299968436 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -415,6 +415,17 @@ RSpec.describe Validations::Sales::SoftValidations do .not_to be_shared_ownership_deposit_invalid end + it "returns false if MORTGAGE + DEPOSIT + CASHDIS are within 1£ of VALUE * EQUITY/100" do + record.mortgage = 500 + record.deposit = 500 + record.cashdis = 500 + record.value = 3001 + record.equity = 50 + + expect(record) + .not_to be_shared_ownership_deposit_invalid + end + it "returns false if mortgage is used and no mortgage is given" do record.mortgage = nil record.deposit = 1000 @@ -473,6 +484,18 @@ RSpec.describe Validations::Sales::SoftValidations do .to be_shared_ownership_deposit_invalid end + it "returns false if no cashdis not routed to and MORTGAGE + DEPOSIT are within 1£ of VALUE * EQUITY/100" do + record.mortgage = 500 + record.deposit = 500 + record.type = 2 + record.cashdis = nil + record.value = 1999 + record.equity = 50 + + expect(record) + .not_to be_shared_ownership_deposit_invalid + end + it "returns false if no value is given" do record.mortgage = 1000 record.deposit = 1000