diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index d673389f0..7e0eb1a58 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -22,22 +22,22 @@ class SchemesController < ApplicationController end def deactivate - deactivation_date_value = deactivation_date - - if @scheme.errors.present? - @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type] - @scheme.deactivation_date = nil - 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) - # update the logs - flash[:notice] = "#{@scheme.service_name} has been deactivated" - end - redirect_to scheme_details_path(@scheme) + if params[:scheme].present? && params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present? + confirm_deactivation else - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value, deactivation_date_type: params[:scheme][:deactivation_date_type] } + deactivation_date_errors + if @scheme.errors.present? + @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type] + # @scheme.deactivation_date = deactivation_date_errors + render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity + else + deactivation_date_value = deactivation_date + if deactivation_date_value.blank? + render "toggle_active", locals: { action: "deactivate" } + else + render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value } + end + end end end @@ -147,29 +147,6 @@ class SchemesController < ApplicationController render "schemes/support_services_provider" end - def deactivation_date - return if params[:scheme].blank? - 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] == "default" - 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) - 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 - end - private def validation_errors(scheme_params) @@ -303,4 +280,52 @@ private def redirect_if_scheme_confirmed redirect_to @scheme if @scheme.confirmed? end + + def confirm_deactivation + if @scheme.update(deactivation_date: params[:scheme][:deactivation_date]) + @scheme.lettings_logs.filter_by_before_startdate(params[:scheme][:deactivation_date]).update!(scheme: nil) + flash[:notice] = "#{@scheme.service_name} has been deactivated" + end + redirect_to scheme_details_path(@scheme) + nil + end + + def deactivation_date_errors + if params[:scheme].present? && params[:scheme][:deactivation_date].blank? && params[:scheme][:deactivation_date_type].blank? + @scheme.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) + end + + if params[:scheme].present? && params[:scheme][:deactivation_date_type] == "other" + day = params[:scheme]["deactivation_date(3i)"] + month = params[:scheme]["deactivation_date(2i)"] + year = params[:scheme]["deactivation_date(1i)"] + + collection_start_date = FormHandler.instance.current_collection_start_date + + if [day, month, year].any?(&:blank?) + { day:, month:, year: }.each do |period, value| + @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered", period: period.to_s)) if value.blank? + end + # { 1 => year.to_i, 2 => month.to_i, 3 => day.to_i } + elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) + @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) + elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) + @scheme.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + end + end + end + + def deactivation_date + return if params[:scheme].blank? + + collection_start_date = FormHandler.instance.current_collection_start_date + return collection_start_date if params[:scheme][:deactivation_date_type] == "default" + return params[:scheme][:deactivation_date] if params[:scheme][:deactivation_date_type].blank? + + day = params[:scheme]["deactivation_date(3i)"] + month = params[:scheme]["deactivation_date(2i)"] + year = params[:scheme]["deactivation_date(1i)"] + + Date.new(year.to_i, month.to_i, day.to_i) + end end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index f09fd1ebd..76b43e511 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -38,6 +38,7 @@ class LettingsLog < Log scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } + scope :filter_by_before_startdate, ->(date) { left_joins(:location).where("lettings_logs.startdate >= ?", date) } scope :search_by, lambda { |param| filter_by_location_postcode(param) .or(filter_by_tenant_code(param)) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 784605a9d..a4ae000fb 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -22,6 +22,7 @@ class Scheme < ApplicationRecord auto_strip_attributes :service_name attr_accessor :deactivation_date_type + attr_accessor :deactivation_date SENSITIVE = { No: 0, diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index 1c3c5abc9..d6897571d 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -11,7 +11,7 @@ <%= f.govuk_error_summary %> <%= f.govuk_radio_buttons_fieldset :deactivation_date_type, legend: { text: "When should this change apply?" }, - caption: { text: @scheme.service_name }, + caption: { text: "Deactivate #{@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_type, diff --git a/app/views/schemes/toggle_active_confirm.html.erb b/app/views/schemes/toggle_active_confirm.html.erb index bbcc6116b..ed4d6203f 100644 --- a/app/views/schemes/toggle_active_confirm.html.erb +++ b/app/views/schemes/toggle_active_confirm.html.erb @@ -9,7 +9,6 @@ <%= 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 5e7e30ca0..5f51593dd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -314,8 +314,9 @@ en: scheme: 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}" + not_entered: "Enter a %{period}" + invalid: "Enter a valid date" + out_of_range: "The date must be on or after the %{date}" soft_validations: net_income: @@ -367,6 +368,8 @@ en: startdate: "When did the first property in this location become available under this scheme? (optional)" add_another_location: "Do you want to add another location?" mobility_type: "What are the mobility standards for the majority of units in this location?" + deactivation: + apply_from: "When should this change apply?" descriptions: location: mobility_type: @@ -379,6 +382,13 @@ en: postcode: "For example, SW1P 4DF." name: "This is how you refer to this location within your organisation" 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." + + warnings: + location: + deactivation: + 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." test: one_argument: "This is based on the tenant’s work situation: %{ecstat1}"