Browse Source

CLDC-3164 Change discounted ownership value to hard validation (#2176)

* Move soft validation to hard for 2024

* Update a date on a test

* Update validation message

* Deduplicate validations
pull/2205/head
kosiakkatrina 11 months ago committed by GitHub
parent
commit
8f5acc55ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      app/models/validations/sales/sale_information_validations.rb
  2. 2
      config/locales/en.yml
  3. 220
      spec/models/validations/sales/sale_information_validations_spec.rb
  4. 9
      spec/models/validations/sales/soft_validations_spec.rb

12
app/models/validations/sales/sale_information_validations.rb

@ -35,20 +35,14 @@ module Validations::Sales::SaleInformationValidations
end end
def validate_discounted_ownership_value(record) def validate_discounted_ownership_value(record)
return unless record.saledate && collection_start_year_for_date(record.saledate) >= 2024 return unless record.saledate && record.form.start_year_after_2024?
return unless record.value && record.deposit && record.ownershipsch return unless record.value && record.deposit && record.ownershipsch
return unless record.mortgage || record.mortgageused == 2 || record.mortgageused == 3 return unless record.mortgage || record.mortgageused == 2 || record.mortgageused == 3
return unless record.discount || record.grant || record.type == 29 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 record.mortgage_deposit_and_grant_total != record.value_with_discount && record.discounted_ownership_sale?
%i[mortgage deposit grant value discount ownershipsch].each do |field| %i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field|
record.errors.add( 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"))
field,
I18n.t(
"validations.sale_information.discounted_ownership_value",
value_with_discount: format_as_currency(record.value_with_discount),
),
)
end end
end end
end end

2
config/locales/en.yml

@ -609,7 +609,7 @@ en:
must_be_after_hodate: "Sale completion date must be after practical completion or handover date" must_be_after_hodate: "Sale completion date must be after practical completion or handover date"
previous_property_type: previous_property_type:
property_type_bedsit: "A bedsit cannot have more than 1 bedroom" property_type_bedsit: "A bedsit cannot have more than 1 bedroom"
discounted_ownership_value: "Mortgage, deposit, and grant total must equal %{value_with_discount}" discounted_ownership_value: "The mortgage, deposit, and grant when added together is %{mortgage_deposit_and_grant_total}, and the purchase purchase price times by the discount is %{value_with_discount}. These figures should be the same"
monthly_rent: monthly_rent:
higher_than_expected: "Basic monthly rent must be between £0.00 and £9,999.00" higher_than_expected: "Basic monthly rent must be between £0.00 and £9,999.00"
merge_request: merge_request:

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

@ -172,179 +172,241 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
describe "#validate_discounted_ownership_value" do describe "#validate_discounted_ownership_value" do
context "when sale is on or after 24/25 collection window" do let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 8, saledate: now) }
context "when grant is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 8, saledate: Time.zone.local(2024, 4, 3)) } around do |example|
Timecop.freeze(now) do
example.run
end
Timecop.return
end
context "with a log in the 24/25 collection year" do
let(:now) { Time.zone.local(2024, 4, 1) }
context "when grant is routed to" do
context "and not provided" do context "and not provided" do
before do before do
record.grant = nil record.grant = nil
end end
it "does not add an error" do it "returns false" do
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors).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 end
end end
context "and is provided" do context "and is provided" do
it "adds an error if mortgage, deposit and grant total does not equal market value" do it "returns true if mortgage, deposit and grant total does not equal market value" do
record.grant = 3_000 record.grant = 3_000
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expected_message = ["Mortgage, deposit, and grant total must equal £30,000.00"] 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[:mortgage]).to eq(expected_message) 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 eq(expected_message) 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[:grant]).to eq(expected_message) 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[:value]).to eq(expected_message) 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[:discount]).to eq(expected_message) 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")
end end
it "does not add an error if mortgage, deposit and grant total equals market value" do it "returns false if mortgage, deposit and grant total equals market value" do
record.grant = 15_000 record.grant = 15_000
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors).to be_empty 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 end
end end
end end
context "when discount is routed to" do context "when discount is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 9, saledate: Time.zone.local(2024, 4, 3)) } let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 9, saledate: now) }
context "and not provided" do context "and not provided" do
before do before do
record.discount = nil record.discount = nil
end end
it "does not add an error" do it "returns false" do
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors).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 end
end end
context "and is provided" do context "and is provided" do
it "adds an error if mortgage and deposit total does not equal market value - discount" do it "returns true if mortgage and deposit total does not equal market value - discount" do
record.discount = 10 record.discount = 10
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same")
expected_message = ["Mortgage, deposit, and grant total must equal £27,000.00"] expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same")
expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same")
expect(record.errors[:mortgage]).to eq(expected_message) expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same")
expect(record.errors[:deposit]).to eq(expected_message) expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same")
expect(record.errors[:grant]).to eq(expected_message) expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same")
expect(record.errors[:value]).to eq(expected_message) expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £15,000.00, and the purchase purchase price times by the discount is £27,000.00. These figures should be the same")
expect(record.errors[:discount]).to eq(expected_message)
end end
it "does not add an error if mortgage and deposit total equals market value - discount" do it "returns false if mortgage and deposit total equals market value - discount" do
record.discount = 50 record.discount = 50
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors).to be_empty 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 end
end end
end end
context "when neither discount nor grant is routed to" do context "when neither discount nor grant is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, value: 30_000, ownershipsch: 2, type: 29, saledate: Time.zone.local(2024, 4, 3)) } let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, value: 30_000, ownershipsch: 2, type: 29, saledate: now) }
it "adds an error if mortgage and deposit total does not equal market value" do it "returns true if mortgage and deposit total does not equal market value" do
record.deposit = 2_000 record.deposit = 2_000
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £12,000.00, and the purchase purchase price times by the discount is £30,000.00. These figures should be the same")
expected_message = ["Mortgage, deposit, and grant total must equal £30,000.00"] expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £12,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 £12,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 eq(expected_message) expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £12,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 eq(expected_message) expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £12,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 eq(expected_message) expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £12,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 eq(expected_message) expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £12,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 eq(expected_message)
end end
it "does not add an error if mortgage and deposit total equals market value" do it "returns false if mortgage and deposit total equals market value" do
record.deposit = 20_000 record.deposit = 20_000
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors).to be_empty 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 end
end end
context "when mortgage is routed to" do context "when mortgage is routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2024, 4, 3)) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) }
context "and not provided" do context "and not provided" do
before do before do
record.mortgage = nil record.mortgage = nil
end end
it "does not add an error" do it "returns false" do
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors).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 end
end end
context "and is provided" do context "and is provided" do
it "adds an error if mortgage, grant and deposit total does not equal market value - discount" do it "returns true if mortgage, grant and deposit total does not equal market value - discount" do
record.mortgage = 10 record.mortgage = 10
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expected_message = ["Mortgage, deposit, and grant total must equal £18,000.00"] expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:mortgage]).to eq(expected_message) expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:deposit]).to eq(expected_message) expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:grant]).to eq(expected_message) expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:value]).to eq(expected_message) expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £8,010.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:discount]).to eq(expected_message)
end end
it "does not add an error if mortgage, grant and deposit total equals market value - discount" do it "returns false if mortgage, grant and deposit total equals market value - discount" do
record.mortgage = 10_000 record.mortgage = 10_000
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors).to be_empty 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 end
end end
end end
context "when mortgage is not routed to" do context "when mortgage is not routed to" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2024, 4, 3)) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) }
it "adds an error if grant and deposit total does not equal market value - discount" do it "returns true if grant and deposit total does not equal market value - discount" do
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expected_message = ["Mortgage, deposit, and grant total must equal £18,000.00"] expect(record.errors["mortgage"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:mortgage]).to eq(expected_message) expect(record.errors["value"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:deposit]).to eq(expected_message) expect(record.errors["deposit"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:grant]).to eq(expected_message) expect(record.errors["ownershipsch"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:value]).to eq(expected_message) expect(record.errors["discount"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
expect(record.errors[:discount]).to eq(expected_message) expect(record.errors["grant"]).to include("The mortgage, deposit, and grant when added together is £8,000.00, and the purchase purchase price times by the discount is £18,000.00. These figures should be the same")
end end
it "does not add an error if mortgage, grant and deposit total equals market value - discount" do it "returns false if mortgage, grant and deposit total equals market value - discount" do
record.grant = 13_000 record.grant = 13_000
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors).to be_empty 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 end
end end
context "when ownership is not discounted" do context "when ownership is not discounted" do
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 1, saledate: Time.zone.local(2024, 4, 3)) } let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 1, saledate: now) }
it "does not add an error" do it "returns false" do
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors).to be_empty expect(record.errors["mortgage"]).to be_empty
end 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 end
end end
context "when sale is before 24/25 collection" do context "when it is a 2023 log" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 4)) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 1)) }
it "does not add an error" do it "returns false" do
record.mortgage = 10
sale_information_validator.validate_discounted_ownership_value(record) sale_information_validator.validate_discounted_ownership_value(record)
expect(record.errors["mortgageused"]).to be_empty
expect(record.errors).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
end end
end end
end end

9
spec/models/validations/sales/soft_validations_spec.rb

@ -940,6 +940,15 @@ RSpec.describe Validations::Sales::SoftValidations do
expect(record).not_to be_discounted_ownership_value_invalid expect(record).not_to be_discounted_ownership_value_invalid
end end
end end
context "when it is a 2024 log" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2024, 4, 3)) }
it "returns true if mortgage, grant and deposit total does not equal market value - discount" do
record.mortgage = 10
expect(record).not_to be_discounted_ownership_value_invalid
end
end
end end
describe "#buyer1_livein_wrong_for_ownership_type?" do describe "#buyer1_livein_wrong_for_ownership_type?" do

Loading…
Cancel
Save