From 6d7a319b4f095109d8b52cc33f54087113bfebb5 Mon Sep 17 00:00:00 2001 From: Robert Sullivan Date: Thu, 21 Mar 2024 14:40:50 +0000 Subject: [PATCH 1/2] 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 --- .../sales/sale_information_validations.rb | 2 +- .../sale_information_validations_spec.rb | 82 ++++++++++++++++--- 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 81054e830..024272ac3 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/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.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| 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 diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 4da297f53..4859aca2d 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -235,21 +235,83 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "and is provided" do - it "returns true if mortgage, deposit and grant total does not equal market value" do - record.grant = 3_000 + it "adds an error if mortgage, deposit and grant at least 1 greater than discounted value" do + 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) - 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["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["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["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["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["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["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 "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.value = 100_000 + 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 From 6841d24e01bfcd87e78bdace19706e5a95529470 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Thu, 21 Mar 2024 15:00:26 +0000 Subject: [PATCH 2/2] feat: add validate_address_fields to lettings for 2024 --- .../bulk_upload/lettings/year2024/row_parser.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/services/bulk_upload/lettings/year2024/row_parser.rb b/app/services/bulk_upload/lettings/year2024/row_parser.rb index b0802df12..32902c4ae 100644 --- a/app/services/bulk_upload/lettings/year2024/row_parser.rb +++ b/app/services/bulk_upload/lettings/year2024/row_parser.rb @@ -414,6 +414,7 @@ class BulkUpload::Lettings::Year2024::RowParser validate :validate_nulls, on: :after_log validate :validate_uprn_exists_if_any_key_address_fields_are_blank, on: :after_log, unless: -> { supported_housing? } + validate :validate_address_fields, on: :after_log validate :validate_incomplete_soft_validations, on: :after_log validate :validate_nationality, on: :after_log @@ -581,6 +582,18 @@ private end end + def validate_address_fields + if field_16.blank? || log.errors.attribute_names.include?(:uprn) + if field_17.blank? + errors.add(:field_17, I18n.t("validations.not_answered", question: "address line 1")) + end + + if field_19.blank? + errors.add(:field_19, I18n.t("validations.not_answered", question: "town or city")) + end + end + end + def validate_incomplete_soft_validations routed_to_soft_validation_questions = log.form.questions.filter { |q| q.type == "interruption_screen" && q.page.routed_to?(log, nil) }.compact routed_to_soft_validation_questions.each do |question|