From 0c39065d686c384557f914acfd79c91bc6434380 Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 14 Mar 2024 10:16:41 +0000 Subject: [PATCH] Update staircase/non staircase validations --- app/models/sales_log.rb | 7 + .../sales/sale_information_validations.rb | 82 +++++++++-- config/locales/en.yml | 4 + .../sale_information_validations_spec.rb | 139 ++++++++++++++++++ 4 files changed, 218 insertions(+), 14 deletions(-) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index c02414a95..5223c249b 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -287,6 +287,13 @@ class SalesLog < Log mortgage_amount + deposit_amount + cashdis_amount end + def deposit_and_discount_total + deposit_amount = deposit || 0 + cashdis_amount = cashdis || 0 + + deposit_amount + cashdis_amount + end + def value_times_equity return unless value && equity diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 883e10d1d..266aa1a9f 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -107,10 +107,59 @@ module Validations::Sales::SaleInformationValidations end def validate_non_staircasing_mortgage(record) + return unless record.saledate && record.form.start_year_after_2024? return unless record.value && record.deposit && record.equity - return unless record.is_not_staircasing? + return unless record.ownershipsch == 1 && record.type && record.mortgageused && record.is_not_staircasing? + + if record.social_homebuy? + add_non_staircasing_socialhomebuy_mortgage_errors(record) + else + add_non_staircasing_non_socialhomebuy_mortgage_errors(record) + end + end + + def validate_staircasing_mortgage(record) return unless record.saledate && record.form.start_year_after_2024? + return unless record.value && record.deposit && record.stairbought + return unless record.is_staircase? && record.ownershipsch == 1 && record.type && record.mortgageused + + if record.social_homebuy? + add_staircasing_socialhomebuy_mortgage_errors(record) + else + add_staircasing_non_socialhomebuy_mortgage_errors(record) + end + end + + def validate_mortgage_used_and_stairbought(record) + return unless record.stairowned && record.mortgageused + + if !record.stairowned_100? && record.mortgageused == 3 + record.errors.add :stairowned, I18n.t("validations.sale_information.stairowned.mortgageused_dont_know") + record.errors.add :mortgageused, I18n.t("validations.sale_information.stairowned.mortgageused_dont_know") + end + end + + def add_non_staircasing_socialhomebuy_mortgage_errors(record) + return unless record.cashdis + + if record.mortgage_used? + return unless record.mortgage + + if record.mortgage_deposit_and_discount_total != record.expected_shared_ownership_deposit_value + %i[mortgage value deposit cashdis equity].each do |field| + record.errors.add field, I18n.t("validations.sale_information.non_staircasing_mortgage.mortgage_used_socialhomebuy", mortgage_deposit_and_discount_total: record.field_formatted_as_currency("mortgage_deposit_and_discount_total"), expected_shared_ownership_deposit_value: record.field_formatted_as_currency("expected_shared_ownership_deposit_value")) + end + end + elsif record.mortgage_not_used? + if record.deposit_and_discount_total != record.expected_shared_ownership_deposit_value + %i[mortgageused value deposit cashdis equity].each do |field| + record.errors.add field, I18n.t("validations.sale_information.non_staircasing_mortgage.mortgage_not_used_socialhomebuy", deposit_and_discount_total: record.field_formatted_as_currency("deposit_and_discount_total"), expected_shared_ownership_deposit_value: record.field_formatted_as_currency("expected_shared_ownership_deposit_value")) + end + end + end + end + def add_non_staircasing_non_socialhomebuy_mortgage_errors(record) if record.mortgage_used? return unless record.mortgage @@ -128,11 +177,25 @@ module Validations::Sales::SaleInformationValidations end end - def validate_staircasing_mortgage(record) - return unless record.mortgageused && record.value && record.deposit && record.stairbought - return unless record.is_staircase? - return unless record.saledate && record.form.start_year_after_2024? + def add_staircasing_socialhomebuy_mortgage_errors(record) + return unless record.cashdis + if record.mortgage_used? + return unless record.mortgage + + if record.mortgage_deposit_and_discount_total != record.stairbought_part_of_value + %i[mortgage value deposit cashdis stairbought].each do |field| + record.errors.add field, I18n.t("validations.sale_information.staircasing_mortgage.mortgage_used_socialhomebuy", mortgage_deposit_and_discount_total: record.field_formatted_as_currency("mortgage_deposit_and_discount_total"), stairbought_part_of_value: record.field_formatted_as_currency("stairbought_part_of_value")) + end + end + elsif record.deposit_and_discount_total != record.stairbought_part_of_value + %i[mortgageused value deposit cashdis stairbought].each do |field| + record.errors.add field, I18n.t("validations.sale_information.staircasing_mortgage.mortgage_not_used_socialhomebuy", deposit_and_discount_total: record.field_formatted_as_currency("deposit_and_discount_total"), stairbought_part_of_value: record.field_formatted_as_currency("stairbought_part_of_value")) + end + end + end + + def add_staircasing_non_socialhomebuy_mortgage_errors(record) if record.mortgage_used? return unless record.mortgage @@ -148,15 +211,6 @@ module Validations::Sales::SaleInformationValidations end end - def validate_mortgage_used_and_stairbought(record) - return unless record.stairowned && record.mortgageused - - if !record.stairowned_100? && record.mortgageused == 3 - record.errors.add :stairowned, I18n.t("validations.sale_information.stairowned.mortgageused_dont_know") - record.errors.add :mortgageused, I18n.t("validations.sale_information.stairowned.mortgageused_dont_know") - end - end - def validate_mortgage_used_dont_know(record) return unless record.mortgageused == 3 diff --git a/config/locales/en.yml b/config/locales/en.yml index 487ceed1a..81c31c824 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -636,9 +636,13 @@ en: non_staircasing_mortgage: mortgage_used: "The mortgage and deposit added together is %{mortgage_and_deposit_total} and the purchase price times by the equity is %{expected_shared_ownership_deposit_value}. These figures should be the same." mortgage_not_used: "The deposit is %{deposit} and the purchase price times by the equity is %{expected_shared_ownership_deposit_value}. As no mortgage was used, these figures should be the same." + mortgage_used_socialhomebuy: "The mortgage, deposit and cash discount added together is %{mortgage_deposit_and_discount_total} and the purchase price times by the equity is %{expected_shared_ownership_deposit_value}. These figures should be the same." + mortgage_not_used_socialhomebuy: "The deposit and cash discount added together is %{deposit_and_discount_total} and the purchase price times by the equity is %{expected_shared_ownership_deposit_value}. As no mortgage was used, these figures should be the same." staircasing_mortgage: mortgage_used: "The mortgage and deposit added together is %{mortgage_and_deposit_total} and the percentage bought times the purchase price is %{stairbought_part_of_value}. These figures should be the same." mortgage_not_used: "The deposit is %{deposit} and the percentage bought times the purchase price is %{stairbought_part_of_value}. As no mortgage was used, these figures should be the same." + mortgage_used_socialhomebuy: "The mortgage, deposit and cash discount added together is %{mortgage_deposit_and_discount_total} and the percentage bought times the purchase price is %{stairbought_part_of_value}. These figures should be the same." + mortgage_not_used_socialhomebuy: "The deposit and cash discount added together is %{deposit_and_discount_total} and the percentage bought times the purchase price is %{stairbought_part_of_value}. As no mortgage was used, these figures should be the same." stairowned: mortgageused_dont_know: "The percentage owned has to be 100% if the mortgage used is 'Don’t know'" merge_request: diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index ca07da6f4..6091c47c4 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -744,6 +744,38 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") expect(record.errors["deposit"]).to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") expect(record.errors["equity"]).to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["cashdis"]).not_to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + end + + context "and it is a social homebuy" do + before do + record.type = 18 + record.cashdis = "200" + end + + it "adds an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["value"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["deposit"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["equity"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["cashdis"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + end + end + + context "and it is not a shared ownership transaction" do + before do + record.ownershipsch = 2 + end + + it "does not add an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["equity"]).to be_empty + expect(record.errors["cashdis"]).to be_empty + end end end @@ -758,6 +790,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["equity"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -771,6 +804,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["equity"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end @@ -791,6 +825,38 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to include("The deposit is £5,000.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") expect(record.errors["deposit"]).to include("The deposit is £5,000.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") expect(record.errors["equity"]).to include("The deposit is £5,000.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["cashdis"]).not_to include("The deposit is £5,000.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") + end + + context "and it is a social homebuy" do + before do + record.type = 18 + record.cashdis = "200" + end + + it "adds an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgageused"]).to include("The deposit and cash discount added together is £5,200.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["value"]).to include("The deposit and cash discount added together is £5,200.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["deposit"]).to include("The deposit and cash discount added together is £5,200.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["equity"]).to include("The deposit and cash discount added together is £5,200.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["cashdis"]).to include("The deposit and cash discount added together is £5,200.00 and the purchase price times by the equity is £8,400.00. As no mortgage was used, these figures should be the same.") + end + end + + context "and it is not a shared ownership transaction" do + before do + record.ownershipsch = 2 + end + + it "does not add an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["equity"]).to be_empty + expect(record.errors["cashdis"]).to be_empty + end end end @@ -805,6 +871,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["equity"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -818,6 +885,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["equity"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -833,6 +901,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["equity"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -864,6 +933,38 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to include("The mortgage and deposit added together is £15,000.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") expect(record.errors["deposit"]).to include("The mortgage and deposit added together is £15,000.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") expect(record.errors["stairbought"]).to include("The mortgage and deposit added together is £15,000.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") + expect(record.errors["cashdis"]).not_to include("The mortgage and deposit added together is £15,000.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") + end + + context "and it is a social homebuy" do + before do + record.type = 18 + record.cashdis = "200" + end + + it "adds an error" do + sale_information_validator.validate_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") + expect(record.errors["value"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") + expect(record.errors["deposit"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") + expect(record.errors["stairbought"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") + expect(record.errors["cashdis"]).to include("The mortgage, deposit and cash discount added together is £15,200.00 and the percentage bought times the purchase price is £8,400.00. These figures should be the same.") + end + end + + context "and it is not a shared ownership transaction" do + before do + record.ownershipsch = 2 + end + + it "does not add an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty + end end end @@ -878,6 +979,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -891,6 +993,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -905,6 +1008,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end @@ -928,6 +1032,38 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to include("The deposit is £5,000.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") expect(record.errors["deposit"]).to include("The deposit is £5,000.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") expect(record.errors["stairbought"]).to include("The deposit is £5,000.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["cashdis"]).not_to include("The deposit is £5,000.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") + end + + context "and it is a social homebuy" do + before do + record.type = 18 + record.cashdis = "200" + end + + it "adds an error" do + sale_information_validator.validate_staircasing_mortgage(record) + expect(record.errors["mortgageused"]).to include("The deposit and cash discount added together is £5,200.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["value"]).to include("The deposit and cash discount added together is £5,200.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["deposit"]).to include("The deposit and cash discount added together is £5,200.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["stairbought"]).to include("The deposit and cash discount added together is £5,200.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") + expect(record.errors["cashdis"]).to include("The deposit and cash discount added together is £5,200.00 and the percentage bought times the purchase price is £8,400.00. As no mortgage was used, these figures should be the same.") + end + end + + context "and it is not a shared ownership transaction" do + before do + record.ownershipsch = 2 + end + + it "does not add an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty + end end end @@ -942,6 +1078,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -955,6 +1092,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end @@ -969,6 +1107,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do expect(record.errors["value"]).to be_empty expect(record.errors["deposit"]).to be_empty expect(record.errors["stairbought"]).to be_empty + expect(record.errors["cashdis"]).to be_empty end end end