diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 208394942..41d24688c 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -50,10 +50,23 @@ class SchemesController < ApplicationController redirect_to scheme_details_path(@scheme) end - def reactivate + def new_reactivation render "toggle_active", locals: { action: "reactivate" } end + def reactivate + @scheme.run_reactivation_validations! + @scheme.reactivation_date = reactivation_date + @scheme.reactivation_date_type = params[:scheme][:reactivation_date_type] + + if @scheme.valid? && @scheme.location_deactivation_periods.deactivations_without_reactivation.update!(reactivation_date:) + flash[:notice] = reactivate_success_notice + redirect_to scheme_details_path(@scheme) + else + render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity + end + end + def new @scheme = Scheme.new end @@ -303,24 +316,36 @@ private end end + def reactivate_success_notice + "#{@scheme.service_name} has been reactivated" + end + def deactivation_date + toggle_date("deactivation_date") + end + + def reactivation_date + toggle_date("reactivation_date") + end + + def toggle_date(key) if params[:scheme].blank? return - elsif params[:scheme][:deactivation_date_type] == "default" + elsif params[:scheme]["#{key}_type".to_sym] == "default" return FormHandler.instance.current_collection_start_date - elsif params[:scheme][:deactivation_date].present? - return params[:scheme][:deactivation_date] + elsif params[:scheme][key.to_sym].present? + return params[:scheme][key.to_sym] end - day = params[:scheme]["deactivation_date(3i)"] - month = params[:scheme]["deactivation_date(2i)"] - year = params[:scheme]["deactivation_date(1i)"] + day = params[:scheme]["#{key}(3i)"] + month = params[:scheme]["#{key}(2i)"] + year = params[:scheme]["#{key}(1i)"] return nil if [day, month, year].any?(&:blank?) Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) end def update_affected_logs - @scheme.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(location: nil, scheme: nil) + @scheme.lettings_logs.filter_by_before_startdate(deactivation_date.to_time).update!(scheme: nil, location: nil) end end diff --git a/app/helpers/toggle_active_scheme_helper.rb b/app/helpers/toggle_active_scheme_helper.rb new file mode 100644 index 000000000..93b9299b5 --- /dev/null +++ b/app/helpers/toggle_active_scheme_helper.rb @@ -0,0 +1,17 @@ +module ToggleActiveSchemeHelper + def toggle_scheme_form_path(action, scheme_id) + if action == "deactivate" + scheme_new_deactivation_path(scheme_id:) + else + scheme_reactivate_path(scheme_id:) + end + end + + def date_type_question(action) + action == "deactivate" ? :deactivation_date_type : :reactivation_date_type + end + + def date_question(action) + action == "deactivate" ? :deactivation_date : :reactivation_date + end +end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 3dfe1ff55..b0e7ade3d 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -20,10 +20,11 @@ class Scheme < ApplicationRecord validate :validate_confirmed validate :deactivation_date_errors + validate :reactivation_date_errors auto_strip_attributes :service_name - attr_accessor :deactivation_date_type, :deactivation_date, :run_deactivation_validations + attr_accessor :deactivation_date_type, :deactivation_date, :run_deactivation_validations, :reactivation_date_type, :reactivation_date, :run_reactivation_validations SENSITIVE = { No: 0, @@ -252,4 +253,25 @@ class Scheme < ApplicationRecord end end end + + def run_reactivation_validations! + @run_reactivation_validations = true + end + + def reactivation_date_errors + return unless @run_reactivation_validations + + if reactivation_date.blank? + if reactivation_date_type.blank? + errors.add(:reactivation_date_type, message: I18n.t("validations.scheme.reactivation_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))) + end + end + end end diff --git a/app/views/schemes/deactivate_confirm.html.erb b/app/views/schemes/deactivate_confirm.html.erb index 45f7d4341..295e0f7f9 100644 --- a/app/views/schemes/deactivate_confirm.html.erb +++ b/app/views/schemes/deactivate_confirm.html.erb @@ -8,7 +8,7 @@ <%= @scheme.service_name %> This change will affect <%= @scheme.lettings_logs.count %> logs - <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %> + <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivate.review_logs") %> <%= f.hidden_field :confirm, value: true %> <%= f.hidden_field :deactivation_date, value: @deactivation_date %> <%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %> diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index 11dd2f276..f0bbd63d4 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -6,24 +6,24 @@ href: scheme_details_path(@scheme), ) %> <% end %> -<%= form_with model: @scheme, url: scheme_new_deactivation_path(@scheme), method: "patch", local: true do |f| %> +<%= form_with model: @scheme, url: toggle_scheme_form_path(action, @scheme.id), method: "patch", local: true do |f| %>
<% collection_start_date = FormHandler.instance.current_collection_start_date %> <%= f.govuk_error_summary %> - <%= f.govuk_radio_buttons_fieldset :deactivation_date_type, - legend: { text: I18n.t("questions.scheme.deactivation.apply_from") }, + <%= f.govuk_radio_buttons_fieldset date_type_question(action), + legend: { text: I18n.t("questions.scheme.deactivate.apply_from") }, caption: { text: title }, - hint: { text: I18n.t("hints.scheme.deactivation", date: collection_start_date.to_formatted_s(:govuk_date)) } do %> - <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.existing_logs") %> - <%= f.govuk_radio_button :deactivation_date_type, + hint: { text: I18n.t("hints.scheme.deactivate", date: collection_start_date.to_formatted_s(:govuk_date)) } do %> + <%= govuk_warning_text text: I18n.t("warnings.scheme.#{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)})" } %> - <%= f.govuk_radio_button :deactivation_date_type, + <%= f.govuk_radio_button date_type_question(action), "other", label: { text: "For tenancies starting after a certain date" }, **basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "scheme") do %> - <%= f.govuk_date_field :deactivation_date, + <%= f.govuk_date_field date_question(action), legend: { text: "Date", size: "m" }, hint: { text: "For example, 27 3 2022" }, width: 20 %> diff --git a/config/locales/en.yml b/config/locales/en.yml index a1c18330a..146128216 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -377,7 +377,7 @@ en: deactivation: apply_from: "When should this change apply?" scheme: - deactivation: + deactivate: apply_from: "When should this change apply?" descriptions: location: @@ -393,7 +393,7 @@ en: units: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff." deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed." scheme: - deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed." + deactivate: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed." warnings: location: @@ -401,9 +401,12 @@ en: existing_logs: "It will not be possible to add logs with this location if their tenancy start date is on or after the date you enter. Any existing logs may be affected." review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." scheme: - deactivation: + deactivate: existing_logs: "It will not be possible to add logs with this scheme if their tenancy start date is on or after the date you enter. Any existing logs may be affected." review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." + reactivate: + existing_logs: "You’ll be able to add logs with this scheme if their tenancy start date is on or after the date you enter." + test: one_argument: "This is based on the tenant’s work situation: %{ecstat1}" diff --git a/config/routes.rb b/config/routes.rb index f90f70534..ba2bfd455 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,8 +52,10 @@ Rails.application.routes.draw do get "new-deactivation", to: "schemes#new_deactivation" get "deactivate-confirm", to: "schemes#deactivate_confirm" get "reactivate", to: "schemes#reactivate" + get "new-reactivation", to: "schemes#new_reactivation" patch "new-deactivation", to: "schemes#new_deactivation" patch "deactivate", to: "schemes#deactivate" + patch "reactivate", to: "schemes#reactivate" resources :locations do get "edit-name", to: "locations#edit_name"