diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index 2b3542813..88bad228f 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -1,5 +1,10 @@ module DerivedVariables::SalesLogVariables def set_derived_fields! self.ethnic = 17 if ethnic_refused? + if exdate.present? + self.exday = exdate.day + self.exmonth = exdate.month + self.exyear = exdate.year + end end end diff --git a/app/models/form/sales/pages/exchange_date.rb b/app/models/form/sales/pages/exchange_date.rb new file mode 100644 index 000000000..59e23ab01 --- /dev/null +++ b/app/models/form/sales/pages/exchange_date.rb @@ -0,0 +1,18 @@ +class Form::Sales::Pages::ExchangeDate < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "exchange_contracts" + @header = "" + @description = "" + @subsection = subsection + @depends_on = [{ + "resale" => 2, + }] + end + + def questions + @questions ||= [ + Form::Sales::Questions::ExchangeDate.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/questions/exchange_date.rb b/app/models/form/sales/questions/exchange_date.rb new file mode 100644 index 000000000..2c726e441 --- /dev/null +++ b/app/models/form/sales/questions/exchange_date.rb @@ -0,0 +1,10 @@ +class Form::Sales::Questions::ExchangeDate < ::Form::Question + def initialize(id, hsh, page) + super + @id = "exdate" + @check_answer_label = "Exchange of contracts date" + @header = "What is the exchange of contracts date?" + @type = "date" + @page = page + 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 fda791298..f4b02a9e5 100644 --- a/app/models/form/sales/subsections/shared_ownership_scheme.rb +++ b/app/models/form/sales/subsections/shared_ownership_scheme.rb @@ -13,6 +13,7 @@ class Form::Sales::Subsections::SharedOwnershipScheme < ::Form::Subsection Form::Sales::Pages::AboutStaircase.new(nil, nil, self), Form::Sales::Pages::PreviousBedrooms.new(nil, nil, self), Form::Sales::Pages::MonthlyRent.new(nil, nil, self), + Form::Sales::Pages::ExchangeDate.new(nil, nil, self), ] end diff --git a/db/migrate/20221221122233_add_exchange_contracts_to_sales.rb b/db/migrate/20221221122233_add_exchange_contracts_to_sales.rb new file mode 100644 index 000000000..51976d7ca --- /dev/null +++ b/db/migrate/20221221122233_add_exchange_contracts_to_sales.rb @@ -0,0 +1,11 @@ +class AddExchangeContractsToSales < ActiveRecord::Migration[7.0] + def change + change_table :sales_logs, bulk: true do |t| + t.column :exdate, :datetime + t.column :exday, :integer + t.column :exmonth, :integer + t.column :exyear, :integer + t.column :resale, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 455c8927d..d616fd6d5 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: 2022_12_21_105502) do +ActiveRecord::Schema[7.0].define(version: 2022_12_21_122233) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -420,6 +420,11 @@ ActiveRecord::Schema[7.0].define(version: 2022_12_21_105502) do t.integer "stairbought" t.integer "stairowned" t.decimal "mrent", precision: 10, scale: 2 + t.datetime "exdate", precision: nil + t.integer "exday" + t.integer "exmonth" + t.integer "exyear" + t.integer "resale" 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/exchange_date_spec.rb b/spec/models/form/sales/pages/exchange_date_spec.rb new file mode 100644 index 000000000..21c4c3aab --- /dev/null +++ b/spec/models/form/sales/pages/exchange_date_spec.rb @@ -0,0 +1,35 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::ExchangeDate, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + 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[exdate]) + end + + it "has the correct id" do + expect(page.id).to eq("exchange_contracts") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has the correct depends_on" do + expect(page.depends_on).to eq([{ + "resale" => 2, + }]) + end +end diff --git a/spec/models/form/sales/questions/exchange_date_spec.rb b/spec/models/form/sales/questions/exchange_date_spec.rb new file mode 100644 index 000000000..53d5a8975 --- /dev/null +++ b/spec/models/form/sales/questions/exchange_date_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::ExchangeDate, 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("exdate") + end + + it "has the correct header" do + expect(question.header).to eq("What is the exchange of contracts date?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Exchange of contracts date") + end + + it "has the correct type" do + expect(question.type).to eq("date") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + 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 3a4e654b4..644485ca5 100644 --- a/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb +++ b/spec/models/form/sales/subsections/shared_ownership_scheme_spec.rb @@ -18,6 +18,7 @@ RSpec.describe Form::Sales::Subsections::SharedOwnershipScheme, type: :model do about_staircasing previous_bedrooms monthly_rent + exchange_contracts ], ) end diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index e69ffc524..d2bcd26f0 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(72) + expect(form.pages.count).to eq(73) 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(72) + expect(form.pages.count).to eq(73) expect(form.name).to eq("2021_2022_sales") end end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 3ee033971..9e618a10e 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -90,4 +90,19 @@ RSpec.describe SalesLog, type: :model do expect(described_class.filter_by_organisation([organisation_3]).count).to eq(0) end end + + describe "derived variables" do + let!(:sales_log) do + described_class.create({ + exdate: Time.gm(2022, 5, 4), + }) + end + + it "correctly derives and saves exday, exmonth and exyear" do + record_from_db = ActiveRecord::Base.connection.execute("select exday, exmonth, exyear from sales_logs where id=#{sales_log.id}").to_a[0] + expect(record_from_db["exday"]).to eq(4) + expect(record_from_db["exmonth"]).to eq(5) + expect(record_from_db["exyear"]).to eq(2022) + end + end end