From 140e55d313166160d5b9590cc33e916f55e0506b Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 8 Feb 2024 08:11:28 +0000 Subject: [PATCH] CLDC-3184 Add value/discount validation (#2222) * Add value/discount validation * fix postcode field --- app/models/sales_log.rb | 6 ++ .../sales/sale_information_validations.rb | 15 ++++ config/locales/en.yml | 3 + .../sale_information_validations_spec.rb | 77 +++++++++++++++++++ 4 files changed, 101 insertions(+) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 3e15c832d..dae70c729 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -485,4 +485,10 @@ class SalesLog < Log def is_staircase? staircase == 1 end + + def discount_value + return unless discount && value + + value * discount / 100 + end end diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 17fbc95b4..c2036227a 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -83,4 +83,19 @@ module Validations::Sales::SaleInformationValidations record.errors.add :type, I18n.t("validations.sale_information.stairbought.over_max", max_stairbought:, type: record.form.get_question("type", record).answer_label(record)) end end + + def validate_discount_and_value(record) + return unless record.saledate && record.form.start_year_after_2024? + return unless record.discount && record.value && record.la + + if record.london_property? && record.discount_value > 136_400 + %i[discount value la postcode_full uprn].each do |field| + record.errors.add field, I18n.t("validations.sale_information.value.over_discounted_london_max", discount_value: record.field_formatted_as_currency("discount_value")) + end + elsif record.property_not_in_london? && record.discount_value > 102_400 + %i[discount value la postcode_full uprn].each do |field| + record.errors.add field, I18n.t("validations.sale_information.value.over_discounted_max", discount_value: record.field_formatted_as_currency("discount_value")) + end + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c7d8c9da2..cd2f373ec 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -619,6 +619,9 @@ en: out_of_range: "Loan, grants or subsidies must be between £9,000 and £16,000" stairbought: over_max: "The percentage bought in this staircasing transaction cannot be higher than %{max_stairbought}% for %{type} sales." + value: + over_discounted_london_max: "The percentage discount multiplied by the purchase price is %{discount_value}. This figure should not be more than £136,400 for properties in London." + over_discounted_max: "The percentage discount multiplied by the purchase price is %{discount_value}. This figure should not be more than £102,400 for properties outside of London." merge_request: organisation_part_of_another_merge: "This organisation is part of another merge - select a different one" organisation_not_selected: "Select an organisation from the search list" diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 25474e55b..24f2e22ee 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -608,4 +608,81 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_discount_and_value" do + let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate: now) } + let(:now) { Time.zone.local(2024, 4, 1) } + + around do |example| + Timecop.freeze(now) do + example.run + end + Timecop.return + end + + context "with a log in the 24/25 collection year" do + context "when in London" do + before do + record.la = "E09000001" + end + + it "adds an error if value * discount is more than 136,400" do + record.discount = 80 + sale_information_validator.validate_discount_and_value(record) + expect(record.errors["value"]).to include("The percentage discount multiplied by the purchase price is £160,000.00. This figure should not be more than £136,400 for properties in London.") + expect(record.errors["discount"]).to include("The percentage discount multiplied by the purchase price is £160,000.00. This figure should not be more than £136,400 for properties in London.") + expect(record.errors["la"]).to include("The percentage discount multiplied by the purchase price is £160,000.00. This figure should not be more than £136,400 for properties in London.") + expect(record.errors["postcode_full"]).to include("The percentage discount multiplied by the purchase price is £160,000.00. This figure should not be more than £136,400 for properties in London.") + expect(record.errors["uprn"]).to include("The percentage discount multiplied by the purchase price is £160,000.00. This figure should not be more than £136,400 for properties in London.") + end + + it "does not add an error value * discount is less than 136,400" do + sale_information_validator.validate_discount_and_value(record) + expect(record.errors["value"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["la"]).to be_empty + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["uprn"]).to be_empty + end + end + + context "when in outside of London" do + before do + record.la = "E06000015" + end + + it "adds an error if value * discount is more than 136,400" do + record.discount = 52 + sale_information_validator.validate_discount_and_value(record) + expect(record.errors["value"]).to include("The percentage discount multiplied by the purchase price is £104,000.00. This figure should not be more than £102,400 for properties outside of London.") + expect(record.errors["discount"]).to include("The percentage discount multiplied by the purchase price is £104,000.00. This figure should not be more than £102,400 for properties outside of London.") + expect(record.errors["la"]).to include("The percentage discount multiplied by the purchase price is £104,000.00. This figure should not be more than £102,400 for properties outside of London.") + expect(record.errors["postcode_full"]).to include("The percentage discount multiplied by the purchase price is £104,000.00. This figure should not be more than £102,400 for properties outside of London.") + expect(record.errors["uprn"]).to include("The percentage discount multiplied by the purchase price is £104,000.00. This figure should not be more than £102,400 for properties outside of London.") + end + + it "does not add an error value * discount is less than 136,400" do + sale_information_validator.validate_discount_and_value(record) + expect(record.errors["value"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["la"]).to be_empty + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["uprn"]).to be_empty + end + end + end + + context "when it is a 2023 log" do + let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 80, ownershipsch: 2, type: 9, saledate: Time.zone.local(2023, 4, 1), la: "E06000015") } + + it "does not add an error" do + sale_information_validator.validate_discount_and_value(record) + expect(record.errors["value"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["la"]).to be_empty + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["uprn"]).to be_empty + end + end + end end