diff --git a/app/models/location_deactivation_period.rb b/app/models/location_deactivation_period.rb index dcf347d24..58b694d72 100644 --- a/app/models/location_deactivation_period.rb +++ b/app/models/location_deactivation_period.rb @@ -1,4 +1,6 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator + include CollectionTimeHelper + def validate(record) location = record.location recent_deactivation = location.location_deactivation_periods.deactivations_without_reactivation.first @@ -24,6 +26,8 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator end def validate_deactivation(record, location) + earliest_possible_deactivation = FormHandler.instance.in_crossover_period? ? previous_collection_start_date : current_collection_start_date + if record.deactivation_date.blank? if record.deactivation_date_type.blank? record.errors.add(:deactivation_date_type, message: I18n.t("validations.location.toggle_date.not_selected")) @@ -32,10 +36,10 @@ 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")) - else - unless record.deactivation_date.between?(location.available_from, Time.zone.local(2200, 1, 1)) - record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: location.available_from.to_formatted_s(:govuk_date))) - end + elsif record.deactivation_date.before? earliest_possible_deactivation + record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: earliest_possible_deactivation.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 end end diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index 5c030de89..53cc0d1d8 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -20,7 +20,7 @@ <%= govuk_warning_text text: I18n.t("warnings.location.#{action}.existing_logs") %> <%= f.govuk_radio_button date_type_question(action), "default", - label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> + label: { text: "From the start of the open collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> <%= f.govuk_radio_button date_type_question(action), "other", diff --git a/config/locales/en.yml b/config/locales/en.yml index b691d61a7..8dba02d30 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -470,6 +470,7 @@ en: toggle_date: not_selected: "Select one of the options" invalid: "Enter a valid day, month and year" + before_creation: "The location cannot be deactivated before %{date}, the start of the collection year when it was created" out_of_range: "The date must be on or after the %{date}" reactivation: before_deactivation: "This location was deactivated on %{date}. The reactivation date must be on or after deactivation date"