diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index c7a9c0eca..d14ec9825 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -25,17 +25,18 @@ class SchemesController < ApplicationController deactivation_date_value = deactivation_date if @scheme.errors.present? + @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type].to_i render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity elsif deactivation_date_value.blank? render "toggle_active", locals: { action: "deactivate" } elsif params[:scheme][:confirm].present? - if @scheme.update(deactivation_date: deactivation_date_value) + if @scheme.update!(deactivation_date: deactivation_date_value) # update the logs flash[:notice] = "#{@scheme.service_name} has been deactivated" end redirect_to scheme_details_path(@scheme) else - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value } + render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value, deactivation_date_type: params[:scheme][:deactivation_date_type].to_i } end end @@ -147,15 +148,22 @@ class SchemesController < ApplicationController def deactivation_date return if params[:scheme].blank? - return @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_selected")) if params[:scheme][:deactivation_date].blank? - return params[:scheme][:deactivation_date] unless params[:scheme][:deactivation_date] == "other" + return @scheme.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) if params[:scheme][:deactivation_date_type].blank? + return Time.utc(2022, 4, 1) if params[:scheme][:deactivation_date_type].to_i == 1 + return params[:scheme][:deactivation_date] if params[:scheme][:deactivation_date].present? day = params[:scheme]["deactivation_date(3i)"] month = params[:scheme]["deactivation_date(2i)"] year = params[:scheme]["deactivation_date(1i)"] if [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && year.to_i.between?(2000, 2200) - Date.new(year.to_i, month.to_i, day.to_i) + current_collection_start_date = FormHandler.instance.current_collection_start_date + specified_date = Date.new(year.to_i, month.to_i, day.to_i) + if specified_date.before?(current_collection_start_date) + @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.before_current_collection_start", date: current_collection_start_date)) + else + specified_date + end else @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered")) end diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index c6dde13ab..c080e6e9b 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -49,6 +49,10 @@ class FormHandler today < window_end_date ? today.year - 1 : today.year end + def current_collection_start_date + Time.utc(current_collection_start_year, 4, 1) + end + def form_name_from_start_year(year, type) form_mappings = { 0 => "current_#{type}", 1 => "previous_#{type}", -1 => "next_#{type}" } form_mappings[current_collection_start_year - year] diff --git a/app/models/scheme.rb b/app/models/scheme.rb index b8a242a70..f1925a188 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -196,7 +196,7 @@ class Scheme < ApplicationRecord end def validate_confirmed - required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group] + required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group deactivation_date_type] if confirmed == true required_attributes.any? do |attribute| diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index 41e5b831e..c2bf9c8b7 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -9,17 +9,17 @@
<%= f.govuk_error_summary %> - <%= f.govuk_radio_buttons_fieldset :deactivation_date, + <%= f.govuk_radio_buttons_fieldset :deactivation_date_type, legend: { text: "When should this change apply?" }, caption: { text: @scheme.service_name }, hint: { text: "If the date is before 1 April 2022, select ‘From the start of the current collection period’ because the previous period has now closed."} do %> <%= govuk_warning_text text: "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." %> - <%= f.govuk_radio_button :deactivation_date, - Time.utc(2022, 4, 1), + <%= f.govuk_radio_button :deactivation_date_type, + 1, label: { text: "From the start of the current collection period (1 April 2022)" } %> - <%= f.govuk_radio_button :deactivation_date, - "other", + <%= f.govuk_radio_button :deactivation_date_type, + 2, label: { text: "For tenancies starting after a certain date" }, **basic_conditional_html_attributes({"deactivation_date" => ["other"]}, "scheme") do %> <%= f.govuk_date_field :deactivation_date, diff --git a/app/views/schemes/toggle_active_confirm.html.erb b/app/views/schemes/toggle_active_confirm.html.erb index ed4d6203f..bbcc6116b 100644 --- a/app/views/schemes/toggle_active_confirm.html.erb +++ b/app/views/schemes/toggle_active_confirm.html.erb @@ -9,6 +9,7 @@ <%= govuk_warning_text text: "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." %> <%= f.hidden_field :confirm, :value => true %> <%= f.hidden_field :deactivation_date, :value => deactivation_date %> + <%= f.hidden_field :deactivation_date_type, :value => deactivation_date_type %>
<%= f.govuk_submit "Deactivate this scheme" %> <%= govuk_button_link_to "Cancel", scheme_details_path, html: { method: :get }, secondary: true %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 81be77398..5e7e30ca0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -315,6 +315,7 @@ en: deactivation_date: not_selected: "Select one of the options" not_entered: "Enter a date" + before_current_collection_start: "The date must be on or after %{date}" soft_validations: net_income: diff --git a/db/migrate/20221110111447_add_second_deactivation_field_to_schemes.rb b/db/migrate/20221110111447_add_second_deactivation_field_to_schemes.rb new file mode 100644 index 000000000..208c4b013 --- /dev/null +++ b/db/migrate/20221110111447_add_second_deactivation_field_to_schemes.rb @@ -0,0 +1,7 @@ +class AddSecondDeactivationFieldToSchemes < ActiveRecord::Migration[7.0] + def change + change_table :schemes, bulk: true do |t| + t.column :deactivation_date_type, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6ea8c4827..7d0fafc9a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_11_09_095650) do +ActiveRecord::Schema[7.0].define(version: 2022_11_10_111447) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -398,6 +398,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_09_095650) do t.integer "total_units" t.boolean "confirmed" t.datetime "deactivation_date" + t.integer "deactivation_date_type" t.index ["managing_organisation_id"], name: "index_schemes_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_schemes_on_owning_organisation_id" end