diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 199c295dd..e55ee10d6 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -280,7 +280,7 @@ private if params[:location_deactivation_period].blank? return elsif params[:location_deactivation_period]["#{key}_type".to_sym] == "default" - return FormHandler.instance.start_date_of_earliest_open_collection_period + return FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period elsif params[:location_deactivation_period][key.to_sym].present? return params[:location_deactivation_period][key.to_sym] end diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 7f858c645..ab3b2853f 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -103,10 +103,18 @@ class FormHandler in_crossover_period? ? previous_collection_start_date : current_collection_start_date end + def start_date_of_earliest_open_for_editing_collection_period + in_edit_crossover_period? ? previous_collection_start_date : current_collection_start_date + end + def in_crossover_period?(now: Time.zone.now) lettings_in_crossover_period?(now:) || sales_in_crossover_period?(now:) end + def in_edit_crossover_period?(now: Time.zone.now) + lettings_in_edit_crossover_period?(now:) || sales_in_edit_crossover_period?(now:) + end + def lettings_in_crossover_period?(now: Time.zone.now) forms = lettings_forms.values forms.count { |form| now.between?(form.start_date, form.new_logs_end_date) } > 1 @@ -145,6 +153,14 @@ class FormHandler end end + def earliest_open_for_editing_collection_start_date(now: Time.zone.now) + if in_edit_crossover_period?(now:) + collection_start_date(now) - 1.year + else + collection_start_date(now) + end + end + private def get_all_forms diff --git a/app/models/location_deactivation_period.rb b/app/models/location_deactivation_period.rb index be635a975..3f222509e 100644 --- a/app/models/location_deactivation_period.rb +++ b/app/models/location_deactivation_period.rb @@ -34,8 +34,8 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator end elsif location.location_deactivation_periods.any? { |period| period.reactivation_date.present? && record.deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) } record.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation.during_deactivated_period")) - elsif record.deactivation_date.before? FormHandler.instance.start_date_of_earliest_open_collection_period - record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: FormHandler.instance.start_date_of_earliest_open_collection_period.to_formatted_s(:govuk_date))) + elsif record.deactivation_date.before? FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period + record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period.to_formatted_s(:govuk_date))) elsif record.deactivation_date.before? location.available_from record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.before_creation", date: location.available_from.to_formatted_s(:govuk_date))) end diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index c7c8270b5..138f04fae 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -10,7 +10,7 @@ <%= form_with model: @location_deactivation_period, url: toggle_location_form_path(action, @location), method: "patch", local: true do |f| %>
- <% start_date = FormHandler.instance.earliest_open_collection_start_date(now: @location.available_from) %> + <% start_date = FormHandler.instance.earliest_open_for_editing_collection_start_date %> <%= f.govuk_error_summary %> <%= f.govuk_radio_buttons_fieldset date_type_question(action), legend: { text: I18n.t("questions.location.toggle_active.apply_from") }, diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 48eb703ca..7410c05b2 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1832,7 +1832,7 @@ RSpec.describe LocationsController, type: :request do let(:startdate) { Time.utc(2022, 9, 11) } before do - Timecop.freeze(Time.utc(2023, 1, 10)) + Timecop.freeze(Time.utc(2023, 9, 10)) sign_in user create(:location_deactivation_period, deactivation_date:, location:) location.save!