diff --git a/app/models/form/sales/pages/handover_date_check.rb b/app/models/form/sales/pages/handover_date_check.rb new file mode 100644 index 000000000..cc0ce9a9b --- /dev/null +++ b/app/models/form/sales/pages/handover_date_check.rb @@ -0,0 +1,13 @@ +class Form::Sales::Pages::HandoverDateCheck < ::Form::Page + def initialize(id, hsh, subsection) + super + @depends_on = [{ "hodate_3_years_or_more_saledate?" => true }] + @informative_text = {} + end + + def questions + @questions ||= [ + Form::Sales::Questions::HandoverDateCheck.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/questions/handover_date_check.rb b/app/models/form/sales/questions/handover_date_check.rb new file mode 100644 index 000000000..03a516e55 --- /dev/null +++ b/app/models/form/sales/questions/handover_date_check.rb @@ -0,0 +1,23 @@ +class Form::Sales::Questions::HandoverDateCheck < ::Form::Question + def initialize(id, hsh, page) + super + @id = "hodate_check" + @check_answer_label = "Practical completion or handover date check" + @header = "Are you sure practical completion or handover date is more than 3 years before exchange date?" + @type = "interruption_screen" + @answer_options = { + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + } + @hidden_in_check_answers = { + "depends_on" => [ + { + "hodate_check" => 0, + }, + { + "hodate_check" => 1, + }, + ], + } + end +end diff --git a/app/models/form/sales/subsections/shared_ownership_scheme.rb b/app/models/form/sales/subsections/shared_ownership_scheme.rb index 8b5ff0902..8c05cd82d 100644 --- a/app/models/form/sales/subsections/shared_ownership_scheme.rb +++ b/app/models/form/sales/subsections/shared_ownership_scheme.rb @@ -14,6 +14,7 @@ class Form::Sales::Subsections::SharedOwnershipScheme < ::Form::Subsection Form::Sales::Pages::Resale.new(nil, nil, self), Form::Sales::Pages::ExchangeDate.new(nil, nil, self), Form::Sales::Pages::HandoverDate.new(nil, nil, self), + Form::Sales::Pages::HandoverDateCheck.new("handover_date_check", nil, self), Form::Sales::Pages::LaNominations.new(nil, nil, self), Form::Sales::Pages::BuyerPrevious.new(nil, nil, self), Form::Sales::Pages::PreviousBedrooms.new(nil, nil, self), diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 0f660ceb0..10999cbe4 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -2,7 +2,6 @@ class SalesLogValidator < ActiveModel::Validator include Validations::Sales::HouseholdValidations include Validations::Sales::FinancialValidations include Validations::Sales::SaleInformationValidations - include Validations::SharedValidations include Validations::LocalAuthorityValidations diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 4c8747f18..11049e8ff 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -6,4 +6,12 @@ module Validations::Sales::SaleInformationValidations record.errors.add :deposit, "Cash deposit must be £0 - £999,999" end end + + def validate_pratical_completion_date_before_saledate(record) + return if record.saledate.blank? || record.hodate.blank? + + unless record.saledate > record.hodate + record.errors.add :hodate, "Practical completion or handover date must be before exchange date" + end + end end diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index db061704d..3deb92aaa 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -36,4 +36,10 @@ module Validations::Sales::SoftValidations deposit > savings * 4 / 3 end + + def hodate_3_years_or_more_saledate? + return unless hodate && saledate + + ((saledate.to_date - hodate.to_date).to_i / 365) >= 3 + end end diff --git a/db/migrate/20230116151942_add_ho_date_check.rb b/db/migrate/20230116151942_add_ho_date_check.rb new file mode 100644 index 000000000..26c36eeea --- /dev/null +++ b/db/migrate/20230116151942_add_ho_date_check.rb @@ -0,0 +1,7 @@ +class AddHoDateCheck < ActiveRecord::Migration[7.0] + def change + change_table :sales_logs, bulk: true do |t| + t.column :hodate_check, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c133a5935..9e02d77e4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_01_13_125117) do +ActiveRecord::Schema[7.0].define(version: 2023_01_16_151942) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -498,6 +498,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_125117) do t.integer "pcodenk" t.string "postcode_full" t.boolean "is_la_inferred" + t.integer "hodate_check" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id" diff --git a/spec/models/form/sales/pages/handover_date_check_spec.rb b/spec/models/form/sales/pages/handover_date_check_spec.rb new file mode 100644 index 000000000..e7ad99c83 --- /dev/null +++ b/spec/models/form/sales/pages/handover_date_check_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::HandoverDateCheck, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { "" } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[hodate_check]) + end + + it "has the correct id" do + expect(page.id).to eq("") + end + + it "has the correct header" do + expect(page.header).to be_nil + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([ + { + "hodate_3_years_or_more_saledate?" => true, + }, + ]) + end +end diff --git a/spec/models/form/sales/questions/handover_date_check_spec.rb b/spec/models/form/sales/questions/handover_date_check_spec.rb new file mode 100644 index 000000000..0e57abf35 --- /dev/null +++ b/spec/models/form/sales/questions/handover_date_check_spec.rb @@ -0,0 +1,48 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::HandoverDateCheck, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("hodate_check") + end + + it "has the correct header" do + expect(question.header).to eq("Are you sure practical completion or handover date is more than 3 years before exchange date?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Practical completion or handover date check") + end + + it "has the correct type" do + expect(question.type).to eq("interruption_screen") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint" do + expect(question.hint_text).to be_nil + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "0" => { "value" => "Yes" }, + "1" => { "value" => "No" }, + }) + end + + it "has the correct hidden_in_check_answers" do + expect(question.hidden_in_check_answers).to eq({ "depends_on" => [{ "hodate_check" => 0 }, { "hodate_check" => 1 }] }) + end +end diff --git a/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb b/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb index b453ed0d9..1cafa8072 100644 --- a/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb +++ b/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb @@ -20,6 +20,7 @@ RSpec.describe Form::Sales::Subsections::SharedOwnershipScheme, type: :model do resale exchange_contracts handover_date + handover_date_check la_nominations buyer_previous previous_bedrooms diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 40c23a9e3..1e0483fbc 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -52,14 +52,14 @@ RSpec.describe FormHandler do it "is able to load a current sales form" do form = form_handler.get_form("current_sales") expect(form).to be_a(Form) - expect(form.pages.count).to eq(148) + expect(form.pages.count).to eq(149) expect(form.name).to eq("2022_2023_sales") end it "is able to load a previous sales form" do form = form_handler.get_form("previous_sales") expect(form).to be_a(Form) - expect(form.pages.count).to eq(148) + expect(form.pages.count).to eq(149) expect(form.name).to eq("2021_2022_sales") end end diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 575eedfff..a808d2ba8 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -46,4 +46,66 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_pratical_completion_date_before_saledate" do + context "when hodate blank" do + let(:record) { build(:sales_log, hodate: nil) } + + it "does not add an error" do + sale_information_validator.validate_pratical_completion_date_before_saledate(record) + + expect(record.errors).not_to be_present + end + end + + context "when saledate blank" do + let(:record) { build(:sales_log, saledate: nil) } + + it "does not add an error" do + sale_information_validator.validate_pratical_completion_date_before_saledate(record) + + expect(record.errors).not_to be_present + end + end + + context "when saledate and hodate blank" do + let(:record) { build(:sales_log, hodate: nil, saledate: nil) } + + it "does not add an error" do + sale_information_validator.validate_pratical_completion_date_before_saledate(record) + + expect(record.errors).not_to be_present + end + end + + context "when hodate before saledate" do + let(:record) { build(:sales_log, hodate: 2.months.ago, saledate: 1.month.ago) } + + it "does not add the error" do + sale_information_validator.validate_pratical_completion_date_before_saledate(record) + + expect(record.errors).not_to be_present + end + end + + context "when hodate after saledate" do + let(:record) { build(:sales_log, hodate: 1.month.ago, saledate: 2.months.ago) } + + it "adds error" do + sale_information_validator.validate_pratical_completion_date_before_saledate(record) + + expect(record.errors[:hodate]).to be_present + end + end + + context "when hodate == saledate" do + let(:record) { build(:sales_log, hodate: Time.zone.parse("2023-07-01"), saledate: Time.zone.parse("2023-07-01")) } + + it "does not add an error" do + sale_information_validator.validate_pratical_completion_date_before_saledate(record) + + expect(record.errors[:hodate]).to be_present + end + end + end end diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index 132701356..902790009 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" RSpec.describe Validations::Sales::SoftValidations do - let(:record) { FactoryBot.create(:sales_log) } + let(:record) { create(:sales_log) } describe "income1 min validations" do context "when validating soft min" do @@ -255,4 +255,41 @@ RSpec.describe Validations::Sales::SoftValidations do end end end + + describe "hodate_more_than_3_years_before_saledate" do + it "when hodate not set" do + record.saledate = Time.zone.now + record.hodate = nil + + expect(record).not_to be_hodate_3_years_or_more_saledate + end + + it "when saledate not set" do + record.saledate = nil + record.hodate = Time.zone.now + + expect(record).not_to be_hodate_3_years_or_more_saledate + end + + it "when saledate and hodate not set" do + record.saledate = nil + record.hodate = nil + + expect(record).not_to be_hodate_3_years_or_more_saledate + end + + it "when 3 years or more before saledate" do + record.saledate = Time.zone.now + record.hodate = record.saledate - 4.years + + expect(record).to be_hodate_3_years_or_more_saledate + end + + it "when less than 3 years before saledate" do + record.saledate = Time.zone.now + record.hodate = 2.months.ago + + expect(record).not_to be_hodate_3_years_or_more_saledate + end + end end