Browse Source

CLDC-3184 Add value/discount validation (#2222)

* Add value/discount validation

* fix postcode field
pull/2223/head
kosiakkatrina 11 months ago committed by GitHub
parent
commit
140e55d313
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      app/models/sales_log.rb
  2. 15
      app/models/validations/sales/sale_information_validations.rb
  3. 3
      config/locales/en.yml
  4. 77
      spec/models/validations/sales/sale_information_validations_spec.rb

6
app/models/sales_log.rb

@ -485,4 +485,10 @@ class SalesLog < Log
def is_staircase? def is_staircase?
staircase == 1 staircase == 1
end end
def discount_value
return unless discount && value
value * discount / 100
end
end end

15
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)) 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
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 end

3
config/locales/en.yml

@ -619,6 +619,9 @@ en:
out_of_range: "Loan, grants or subsidies must be between £9,000 and £16,000" out_of_range: "Loan, grants or subsidies must be between £9,000 and £16,000"
stairbought: stairbought:
over_max: "The percentage bought in this staircasing transaction cannot be higher than %{max_stairbought}% for %{type} sales." 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: merge_request:
organisation_part_of_another_merge: "This organisation is part of another merge - select a different one" 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" organisation_not_selected: "Select an organisation from the search list"

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

@ -608,4 +608,81 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end 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 end

Loading…
Cancel
Save