diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index 6d12f6675..ce5b7a42c 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -28,11 +28,14 @@ module SchemesHelper end def scheme_availability(scheme) - availability = "Active from #{scheme.available_from.to_formatted_s(:govuk_date)}" - scheme.scheme_deactivation_periods.each do |deactivation| - availability << " to #{(deactivation.deactivation_date - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{deactivation.deactivation_date.to_formatted_s(:govuk_date)}" - availability << "\nActive from #{deactivation.reactivation_date.to_formatted_s(:govuk_date)}" if deactivation.reactivation_date.present? + availability = "" + scheme.active_periods.each do |period| + if period.from.present? + availability << "\nActive from #{period.from.to_formatted_s(:govuk_date)}" + availability << " to #{(period.to - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{period.to.to_formatted_s(:govuk_date)}" if period.to.present? + end end - availability + availability.strip end end + diff --git a/app/helpers/toggle_active_scheme_helper.rb b/app/helpers/toggle_active_scheme_helper.rb index 93b9299b5..9f93939fd 100644 --- a/app/helpers/toggle_active_scheme_helper.rb +++ b/app/helpers/toggle_active_scheme_helper.rb @@ -1,9 +1,9 @@ module ToggleActiveSchemeHelper - def toggle_scheme_form_path(action, scheme_id) + def toggle_scheme_form_path(action, scheme) if action == "deactivate" - scheme_new_deactivation_path(scheme_id:) + scheme_new_deactivation_path(scheme) else - scheme_reactivate_path(scheme_id:) + scheme_reactivate_path(scheme) end end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 09fe504f1..3f88c0044 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -214,7 +214,7 @@ class Scheme < ApplicationRecord end def available_from - created_at + [created_at, FormHandler.instance.current_collection_start_date].min end def status @@ -227,6 +227,19 @@ class Scheme < ApplicationRecord :active end + ActivePeriod = Struct.new(:from, :to) + def active_periods + periods = [ActivePeriod.new(available_from, nil)] + + sorted_deactivation_periods = scheme_deactivation_periods.sort_by(&:deactivation_date) + sorted_deactivation_periods.each do |deactivation| + periods.find { |x| x.to.nil? }.to = deactivation.deactivation_date + periods << ActivePeriod.new(deactivation.reactivation_date, nil) + end + + periods.select { |period| (period.from != period.to) && (period.to.nil? || (period.from.present? && period.from <= period.to)) } + end + def active? status == :active end @@ -252,10 +265,11 @@ class Scheme < ApplicationRecord elsif deactivation_date_type == "other" errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.invalid")) end + elsif scheme_deactivation_periods.any? { |period| deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) } + errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation.during_deactivated_period")) else - collection_start_date = FormHandler.instance.current_collection_start_date - unless deactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1)) - errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + unless deactivation_date.between?(available_from, Time.zone.local(2200, 1, 1)) + errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: available_from.to_formatted_s(:govuk_date))) end end end @@ -267,17 +281,17 @@ class Scheme < ApplicationRecord def reactivation_date_errors return unless @run_reactivation_validations + recent_deactivation = scheme_deactivation_periods.deactivations_without_reactivation.first if reactivation_date.blank? if reactivation_date_type.blank? - errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.reactivation_date.not_selected")) + errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.toggle_date.not_selected")) elsif reactivation_date_type == "other" - errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation_date.invalid")) - end - else - collection_start_date = FormHandler.instance.current_collection_start_date - unless reactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1)) - errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + errors.add(:reactivation_date, message: I18n.t("validations.scheme.toggle_date.invalid")) end + elsif !reactivation_date.between?(available_from, Time.zone.local(2200, 1, 1)) + errors.add(:reactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: available_from.to_formatted_s(:govuk_date))) + elsif reactivation_date < recent_deactivation.deactivation_date + errors.add(:reactivation_date, message: I18n.t("validations.scheme.reactivation.before_deactivation", date: recent_deactivation.deactivation_date.to_formatted_s(:govuk_date))) end end end diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index 6cae1a4bf..1d4ff2ecf 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -6,7 +6,7 @@ href: scheme_details_path(@scheme), ) %> <% end %> -<%= form_with model: @scheme, url: toggle_scheme_form_path(action, @scheme.id), method: "patch", local: true do |f| %> +<%= form_with model: @scheme, url: toggle_scheme_form_path(action, @scheme), method: "patch", local: true do |f| %>