diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 02f3a9d70..9a10dc894 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -36,6 +36,10 @@ class FormHandler forms["next_lettings"] end + def archived_lettings_form + forms["archived_lettings"] + end + def current_sales_form forms["current_sales"] end @@ -44,6 +48,10 @@ class FormHandler forms["previous_sales"] end + def archived_sales_form + forms["archived_sales"] + end + def next_sales_form forms["next_sales"] end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 3064cba31..d03425ee4 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -61,13 +61,14 @@ class SalesLog < Log [by_postcode, param_without_spaces, param_without_spaces]) } scope :age1_answered, -> { where.not(age1: nil).or(where(age1_known: [1, 2])) } + scope :ecstat1_answered, -> { where.not(ecstat1: nil).or(where("saledate >= ?", Time.zone.local(2025, 4, 1))) } scope :duplicate_logs, lambda { |log| visible.where(log.slice(*DUPLICATE_LOG_ATTRIBUTES)) .where.not(id: log.id) .where.not(saledate: nil) .where.not(sex1: nil) - .where.not(ecstat1: nil) .where.not(postcode_full: nil) + .ecstat1_answered .age1_answered } scope :after_date, ->(date) { where("saledate >= ?", date) } @@ -77,9 +78,9 @@ class SalesLog < Log .group(*DUPLICATE_LOG_ATTRIBUTES) .where.not(saledate: nil) .where.not(sex1: nil) - .where.not(ecstat1: nil) .where.not(postcode_full: nil) .age1_answered + .ecstat1_answered .having("COUNT(*) > 1") if assigned_to_id diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index 687b042a4..d59a03907 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -33,6 +33,7 @@ FactoryBot.define do noint { 2 } privacynotice { 1 } purchid { rand(999_999_999).to_s } + staircase { 1 } end trait :discounted_ownership_setup_complete do saledate_today @@ -66,6 +67,7 @@ FactoryBot.define do postcode_full { "A1 1AA" } noint { 2 } uprn_known { 0 } + staircase { 1 } end trait :completed do purchid { rand(999_999_999).to_s } diff --git a/spec/features/organisation_spec.rb b/spec/features/organisation_spec.rb index 15bfecf39..336ae51af 100644 --- a/spec/features/organisation_spec.rb +++ b/spec/features/organisation_spec.rb @@ -132,6 +132,7 @@ RSpec.describe "User Features" do let!(:other_general_needs_logs) { FactoryBot.create_list(:lettings_log, 4, assigned_to: user, needstype: 1) } let!(:other_supported_housing_logs) { FactoryBot.create_list(:lettings_log, 4, assigned_to: user, needstype: 2) } let(:number_of_lettings_logs) { LettingsLog.count } + let(:previous_year) { FormHandler.instance.previous_lettings_form.start_date.year } before do allow(FormHandler.instance).to receive(:in_crossover_period?).and_return(true) @@ -204,9 +205,9 @@ RSpec.describe "User Features" do end it "can filter lettings logs by year" do - check("years-2022-field") + check("years-#{previous_year}-field") click_button("Apply filters") - expect(page).to have_current_path("/organisations/#{org_id}/lettings-logs?%5Byears%5D[]=&years[]=2022&%5Bstatus%5D[]=&%5Bneedstypes%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=") + expect(page).to have_current_path("/organisations/#{org_id}/lettings-logs?%5Byears%5D[]=&years[]=#{previous_year}&%5Bstatus%5D[]=&%5Bneedstypes%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=") expect(page).not_to have_link first_log.id.to_s, href: "/lettings-logs/#{first_log.id}" end @@ -226,6 +227,7 @@ RSpec.describe "User Features" do context "when viewing sales logs for specific organisation" do let(:first_log) { organisation.sales_logs.first } let(:number_of_sales_logs) { SalesLog.count } + let(:previous_year) { FormHandler.instance.previous_sales_form.start_date.year } before do allow(FormHandler.instance).to receive(:in_crossover_period?).and_return(true) @@ -254,9 +256,9 @@ RSpec.describe "User Features" do organisation.sales_logs.map(&:id).each do |sales_log_id| expect(page).to have_link sales_log_id.to_s, href: "/sales-logs/#{sales_log_id}" end - check("years-2022-field") + check("years-#{previous_year}-field") click_button("Apply filters") - expect(page).to have_current_path("/organisations/#{org_id}/sales-logs?%5Byears%5D[]=&years[]=2022&%5Bstatus%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=") + expect(page).to have_current_path("/organisations/#{org_id}/sales-logs?%5Byears%5D[]=&years[]=#{previous_year}&%5Bstatus%5D[]=&assigned_to=all&user_text_search=&user=&owning_organisation_select=all&owning_organisation_text_search=&owning_organisation=&managing_organisation_select=all&managing_organisation_text_search=&managing_organisation=") expect(page).not_to have_link first_log.id.to_s, href: "/sales-logs/#{first_log.id}" end end diff --git a/spec/features/sales_log_spec.rb b/spec/features/sales_log_spec.rb index 663a4f7ac..c226c6bd3 100644 --- a/spec/features/sales_log_spec.rb +++ b/spec/features/sales_log_spec.rb @@ -142,6 +142,7 @@ RSpec.describe "Sales Log Features" do context "when downloading logs" do let(:user) { create(:user, :support) } let(:other_user) { create(:user, organisation: user.organisation) } + let(:current_year) { FormHandler.instance.current_sales_form.start_date.year } context "when I am signed in" do before do @@ -191,7 +192,7 @@ RSpec.describe "Sales Log Features" do context "when one year filter is selected" do before do - check("2024 to 2025") + check("#{current_year} to #{current_year + 1}") click_button("Apply filters") end diff --git a/spec/helpers/collection_resources_helper_spec.rb b/spec/helpers/collection_resources_helper_spec.rb index 05c164fc1..a146e43cc 100644 --- a/spec/helpers/collection_resources_helper_spec.rb +++ b/spec/helpers/collection_resources_helper_spec.rb @@ -4,10 +4,18 @@ RSpec.describe CollectionResourcesHelper do let(:current_user) { create(:user, :data_coordinator) } let(:user) { create(:user, :data_coordinator) } let(:storage_service) { instance_double(Storage::S3Service, get_file_metadata: nil) } + let(:current_date) { Time.zone.local(2024, 8, 8) } before do allow(Storage::S3Service).to receive(:new).and_return(storage_service) allow(storage_service).to receive(:configuration).and_return(OpenStruct.new(bucket_name: "core-test-collection-resources")) + Timecop.travel(current_date) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + Singleton.__init__(FormHandler) end describe "when displaying file metadata" do @@ -34,9 +42,10 @@ RSpec.describe CollectionResourcesHelper do describe "#editable_collection_resource_years" do context "when in crossover period" do + let(:current_date) { Time.zone.local(2024, 4, 8) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 4, 8)) end it "returns previous and current years" do @@ -50,9 +59,7 @@ RSpec.describe CollectionResourcesHelper do end context "and after 1st January" do - before do - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 2, 1)) - end + let(:current_date) { Time.zone.local(2025, 2, 1) } it "returns current and next years" do expect(editable_collection_resource_years).to match_array([2024, 2025]) @@ -60,9 +67,7 @@ RSpec.describe CollectionResourcesHelper do end context "and before 1st January" do - before do - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 12, 1)) - end + let(:current_date) { Time.zone.local(2024, 12, 1) } it "returns current year" do expect(editable_collection_resource_years).to eq([2024]) @@ -73,9 +78,10 @@ RSpec.describe CollectionResourcesHelper do describe "#displayed_collection_resource_years" do context "when in crossover period" do + let(:current_date) { Time.zone.local(2024, 4, 8) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true) - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2024, 4, 8)) end it "returns previous and current years" do @@ -188,9 +194,10 @@ RSpec.describe CollectionResourcesHelper do end context "when next year is editable" do + let(:current_date) { Time.zone.local(2025, 1, 1) } + before do allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false) - allow(Time.zone).to receive(:today).and_return(Time.zone.local(2025, 1, 1)) end it "returns true" do diff --git a/spec/models/sales_log_derived_fields_spec.rb b/spec/models/sales_log_derived_fields_spec.rb index ce34c8ca5..8eda0c36f 100644 --- a/spec/models/sales_log_derived_fields_spec.rb +++ b/spec/models/sales_log_derived_fields_spec.rb @@ -67,13 +67,13 @@ RSpec.describe SalesLog, type: :model do end it "clears mortgage value if mortgage used is changed from no to don't know" do - log = create(:sales_log, :outright_sale_setup_complete, mortgage: 0, mortgageused: 2) + log = create(:sales_log, :shared_ownership_setup_complete, stairowned: 100, mortgage: 0, mortgageused: 2) log.mortgageused = 3 expect { log.set_derived_fields! }.to change(log, :mortgage).from(0).to(nil) end it "clears mortgage value if mortgage used is changed from yes to don't know" do - log = create(:sales_log, :outright_sale_setup_complete, mortgage: 50_000, mortgageused: 1) + log = create(:sales_log, :shared_ownership_setup_complete, staircase: 2, mortgage: 50_000, mortgageused: 1) log.mortgageused = 3 expect { log.set_derived_fields! }.to change(log, :mortgage).from(50_000).to(nil) end @@ -101,30 +101,36 @@ RSpec.describe SalesLog, type: :model do expect { log.set_derived_fields! }.to change(log, :deposit).from(0).to(nil) end - it "clears derived deposit when setting mortgage used to yes" do - log = create(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 123_400, mortgageused: 2) - log.mortgageused = 1 - expect { log.set_derived_fields! }.to change(log, :deposit).from(123_400).to(nil) + context "with outright sales log" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2024, 5, 4)) + end + + it "clears derived deposit when setting mortgage used to yes" do + log = create(:sales_log, :outright_sale_setup_complete, value: 123_400, deposit: 123_400, mortgageused: 2) + log.mortgageused = 1 + expect { log.set_derived_fields! }.to change(log, :deposit).from(123_400).to(nil) + end + + it "clears that buyer 1 will live in the property if joint purchase is updated" do + log = create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) + log.jointpur = 1 + expect { log.set_derived_fields! }.to change(log, :buy1livein).from(1).to(nil) + end end context "when buyers will live in the property" do context "and the sale is not a joint purchase" do it "derives that buyer 1 will live in the property" do - log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) + log = build(:sales_log, :shared_ownership_setup_complete, staircase: 2, buylivein: 1, jointpur: 2) expect { log.set_derived_fields! }.to change(log, :buy1livein).from(nil).to(1) end it "does not derive a value for whether buyer 2 will live in the property" do - log = build(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) + log = build(:sales_log, :shared_ownership_setup_complete, staircase: 2, buylivein: 1, jointpur: 2) log.set_derived_fields! expect(log.buy2livein).to be_nil end - - it "clears that buyer 1 will live in the property if joint purchase is updated" do - log = create(:sales_log, :outright_sale_setup_complete, buylivein: 1, jointpur: 2) - log.jointpur = 1 - expect { log.set_derived_fields! }.to change(log, :buy1livein).from(1).to(nil) - end end context "and the sale is a joint purchase" do diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 0c854c221..20eb0b2a6 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -281,9 +281,18 @@ RSpec.describe SalesLog, type: :model do end end - context "when there is a log with a different ecstat1" do + context "when there is a 2024 log with a different ecstat1" do let!(:different_ecstat1) { create(:sales_log, :duplicate, ecstat1: 0, owning_organisation: organisation) } + before do + Timecop.freeze(Time.zone.local(2024, 5, 2)) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + end + it "does not return a log with a different ecstat1 as a duplicate" do expect(described_class.duplicate_logs(log)).not_to include(different_ecstat1) end diff --git a/spec/models/validations/sales/setup_validations_spec.rb b/spec/models/validations/sales/setup_validations_spec.rb index 659cde23f..45579ca42 100644 --- a/spec/models/validations/sales/setup_validations_spec.rb +++ b/spec/models/validations/sales/setup_validations_spec.rb @@ -4,6 +4,8 @@ RSpec.describe Validations::Sales::SetupValidations do subject(:setup_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::Sales::SetupValidations } } + let(:current_year) { FormHandler.instance.current_sales_form.start_date.year } + let(:previous_year) { FormHandler.instance.previous_sales_form.start_date.year } describe "#validate_saledate_collection_year" do context "with sales_in_crossover_period == false" do @@ -68,7 +70,7 @@ RSpec.describe Validations::Sales::SetupValidations do end context "when saledate is in an open previous collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2024, 1, 1)) } + let(:record) { build(:sales_log, saledate: Time.zone.local(current_year, 1, 1)) } before do allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) @@ -91,12 +93,12 @@ RSpec.describe Validations::Sales::SetupValidations do it "adds error" do setup_validator.validate_saledate_collection_year(record) - expect(record.errors[:saledate]).to include("Enter a date within the 2023 to 2024 or 2024 to 2025 collection years, which is between 1st April 2023 and 31st March 2025.") + expect(record.errors[:saledate]).to include("Enter a date within the #{previous_year} to #{previous_year + 1} or #{current_year} to #{current_year + 1} collection years, which is between 1st April #{previous_year} and 31st March #{current_year + 1}.") end end context "when saledate is after an open collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) } + let(:record) { build(:sales_log, saledate: Time.zone.local(current_year + 1, 4, 1)) } before do allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) @@ -105,21 +107,17 @@ RSpec.describe Validations::Sales::SetupValidations do it "adds error" do setup_validator.validate_saledate_collection_year(record) - expect(record.errors[:saledate]).to include("Enter a date within the 2023 to 2024 or 2024 to 2025 collection years, which is between 1st April 2023 and 31st March 2025.") + expect(record.errors[:saledate]).to include("Enter a date within the #{previous_year} to #{previous_year + 1} or #{current_year} to #{current_year + 1} collection years, which is between 1st April #{previous_year} and 31st March #{current_year + 1}.") end end context "when current time is after the new logs end date but before edit end date for the previous period" do let(:record) { build(:sales_log, saledate: nil) } - before do - allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8)) - end - it "cannot create new logs for the archived collection year" do record.saledate = Time.zone.local(2023, 1, 1) setup_validator.validate_saledate_collection_year(record) - expect(record.errors["saledate"]).to include(match "Enter a date within the 2023 to 2024 or 2024 to 2025 collection years, which is between 1st April 2023 and 31st March 2025.") + expect(record.errors["saledate"]).to include(match "Enter a date within the #{previous_year} to #{previous_year + 1} or #{current_year} to #{current_year + 1} collection years, which is between 1st April #{previous_year} and 31st March #{current_year + 1}.") end it "can edit already created logs for the previous collection year" do @@ -127,7 +125,7 @@ RSpec.describe Validations::Sales::SetupValidations do record.save!(validate: false) record.saledate = Time.zone.local(2024, 1, 1) setup_validator.validate_saledate_collection_year(record) - expect(record.errors["saledate"]).not_to include(match "Enter a date within the 2024 to 2025 collection year, which is between 1st April 2024 and 31st March 2025.") + expect(record.errors["saledate"]).not_to include(match "Enter a date within the #{current_year} to #{current_year + 1} collection year, which is between 1st April #{current_year} and 31st March #{current_year + 1}.") end end