diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index ce7a812d2..2cf4742d8 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -96,8 +96,9 @@ module Validations::Sales::SoftValidations def purchase_price_out_of_soft_range? return unless value && beds && la && sale_range + return if is_staircase? && !stairbought&.positive? - !value.between?(sale_range.soft_min, sale_range.soft_max) + !full_purchase_price.between?(sale_range.soft_min, sale_range.soft_max) end def staircase_owned_out_of_soft_range? @@ -136,11 +137,11 @@ module Validations::Sales::SoftValidations end def purchase_price_higher_or_lower_text - value < sale_range.soft_min ? "lower" : "higher" + full_purchase_price < sale_range.soft_min ? "lower" : "higher" end def purchase_price_soft_min_or_soft_max - value < sale_range.soft_min ? sale_range.soft_min : sale_range.soft_max + full_purchase_price < sale_range.soft_min ? sale_range.soft_min : sale_range.soft_max end def grant_outside_common_range? @@ -203,6 +204,14 @@ module Validations::Sales::SoftValidations private + def full_purchase_price + if is_staircase? && stairbought&.positive? + value * 100 / stairbought + else + value + end + end + def sale_range LaSaleRange.find_by( start_year: collection_start_year, diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index 0f8ff16c4..1466d2b8e 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -808,6 +808,63 @@ RSpec.describe Validations::Sales::SoftValidations do expect(record).not_to be_purchase_price_out_of_soft_range end + + context "when staircasing" do + it "is not out of range when stairbought is 10% and value is 20,000 (full price 200,000 is in soft range)" do + record.staircase = 1 + record.stairbought = 10 + record.value = 20_000 + record.beds = 2 + record.la = "E07000223" + record.saledate = Time.zone.local(2023, 1, 1) + + expect(record).not_to be_purchase_price_out_of_soft_range + end + + it "is out of range when stairbought is 10% and value is 17,000 (full price 170,000 is below soft min of 177,000)" do + record.staircase = 1 + record.stairbought = 10 + record.value = 17_000 + record.beds = 2 + record.la = "E07000223" + record.saledate = Time.zone.local(2023, 1, 1) + + expect(record).to be_purchase_price_out_of_soft_range + end + + it "is out of range when stairbought is 10% and value is 40,000 (full price 400,000 is above soft max of 384,000)" do + record.staircase = 1 + record.stairbought = 10 + record.value = 40_000 + record.beds = 2 + record.la = "E07000223" + record.saledate = Time.zone.local(2023, 1, 1) + + expect(record).to be_purchase_price_out_of_soft_range + end + + it "does not trigger when stairbought is not set" do + record.staircase = 1 + record.stairbought = nil + record.value = 20_000 + record.beds = 2 + record.la = "E07000223" + record.saledate = Time.zone.local(2023, 1, 1) + + expect(record).not_to be_purchase_price_out_of_soft_range + end + + it "does not trigger when stairbought is 0" do + record.staircase = 1 + record.stairbought = 0 + record.value = 20_000 + record.beds = 2 + record.la = "E07000223" + record.saledate = Time.zone.local(2023, 1, 1) + + expect(record).not_to be_purchase_price_out_of_soft_range + end + end end describe "#grant_outside_common_range?" do