Browse Source

CLDC-3173 Add equity and staircase validation (#2201)

* Add equity and staircase validation

* Update BU tests
pull/2200/head^2
kosiakkatrina 11 months ago committed by GitHub
parent
commit
5302889417
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 12
      app/models/validations/sales/financial_validations.rb
  2. 1
      config/locales/en.yml
  3. 79
      spec/models/validations/sales/financial_validations_spec.rb
  4. 2
      spec/services/bulk_upload/sales/year2024/row_parser_spec.rb

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

@ -119,6 +119,18 @@ module Validations::Sales::FinancialValidations
end
end
def validate_equity_less_than_staircase_difference(record)
return unless record.equity && record.stairbought && record.stairowned
return unless record.saledate && record.form.start_year_after_2024?
if record.equity > record.stairowned - record.stairbought
formatted_equity = sprintf("%g", record.equity)
record.errors.add :equity, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought)
record.errors.add :stairowned, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought)
record.errors.add :stairbought, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought)
end
end
private
def is_relationship_child?(relationship)

1
config/locales/en.yml

@ -433,6 +433,7 @@ en:
equity:
under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%"
over_max: "The maximum initial equity stake is %{max_equity}%"
over_stairowned_minus_stairbought: "The initial equity stake is %{equity}% and the percentage owned in total minus the percentage bought is %{staircase_difference}%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought."
mortgage: "Mortgage value cannot be £0 if a mortgage was used for the purchase of this property"
shared_ownership_deposit: "The %{mortgage_deposit_and_discount_error_fields} added together is %{mortgage_deposit_and_discount_total}. The value times the equity percentage is %{value_times_equity}. These figures should be the same"

79
spec/models/validations/sales/financial_validations_spec.rb

@ -561,4 +561,83 @@ RSpec.describe Validations::Sales::FinancialValidations do
end
end
end
describe "#validate_equity_less_than_staircase_difference" do
let(:record) { FactoryBot.create(:sales_log, saledate: now) }
around do |example|
Timecop.freeze(now) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
context "with a log in the 23/24 collection year" do
let(:now) { Time.zone.local(2023, 4, 1) }
it "does not add an error" do
record.stairbought = 2
record.stairowned = 3
record.equity = 2
financial_validator.validate_equity_less_than_staircase_difference(record)
expect(record.errors).to be_empty
end
end
context "with a log in 24/25 collection year" do
let(:now) { Time.zone.local(2024, 4, 1) }
it "adds errors if equity is more than stairowned - stairbought" do
record.stairbought = 2
record.stairowned = 3
record.equity = 2
financial_validator.validate_equity_less_than_staircase_difference(record)
expect(record.errors["equity"]).to include("The initial equity stake is 2% and the percentage owned in total minus the percentage bought is 1%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought.")
expect(record.errors["stairowned"]).to include("The initial equity stake is 2% and the percentage owned in total minus the percentage bought is 1%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought.")
expect(record.errors["stairbought"]).to include("The initial equity stake is 2% and the percentage owned in total minus the percentage bought is 1%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought.")
end
it "does not add errors if equity is less than stairowned - stairbought" do
record.stairbought = 2
record.stairowned = 10
record.equity = 2
financial_validator.validate_equity_less_than_staircase_difference(record)
expect(record.errors).to be_empty
end
it "does not add errors if equity is equal stairowned - stairbought" do
record.stairbought = 2
record.stairowned = 10
record.equity = 8
financial_validator.validate_equity_less_than_staircase_difference(record)
expect(record.errors).to be_empty
end
it "does not add errors if stairbought is not given" do
record.stairbought = nil
record.stairowned = 10
record.equity = 2
financial_validator.validate_equity_less_than_staircase_difference(record)
expect(record.errors).to be_empty
end
it "does not add errors if stairowned is not given" do
record.stairbought = 2
record.stairowned = nil
record.equity = 2
financial_validator.validate_equity_less_than_staircase_difference(record)
expect(record.errors).to be_empty
end
it "does not add errors if equity is not given" do
record.stairbought = 2
record.stairowned = 10
record.equity = 0
financial_validator.validate_equity_less_than_staircase_difference(record)
expect(record.errors).to be_empty
end
end
end
end

2
spec/services/bulk_upload/sales/year2024/row_parser_spec.rb

@ -88,7 +88,7 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do
field_85: "5",
field_86: "1",
field_87: "10",
field_88: "11",
field_88: "40",
field_89: "1",
field_91: "30",
field_92: "3",

Loading…
Cancel
Save