diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index a37a0a7cd..f55f79ed3 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -21,21 +21,27 @@ class LocationsController < ApplicationController def show; end def deactivate - deactivation_date_value = deactivation_date - - if @location.errors.present? - @location.deactivation_date_type = params[:location][: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[:location][:confirm].present? - if @location.update(deactivation_date: deactivation_date_value) + if params[:location][:confirm].present? && params[:location][:deactivation_date].present? + if @location.update(deactivation_date: params[:location][:deactivation_date]) # update the logs flash[:notice] = "#{@location.name} has been deactivated" end redirect_to scheme_locations_path(@scheme) else - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value } + + deactivation_date_errors + + if @location.errors.present? + @location.deactivation_date_type = params[:location][:deactivation_date_type] + 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 @@ -162,40 +168,43 @@ private location_params["location_admin_district"] != "Select an option" end + + + def deactivation_date_errors + if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank? + @location.errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) + end + + if params[:location][:deactivation_date_type] == "other" + day = params[:location]["deactivation_date(3i)"] + month = params[:location]["deactivation_date(2i)"] + year = params[:location]["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| + @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s)) if value.blank? + end + elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) + @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) + elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) + @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + end + end + end + + def deactivation_date return if params[:location].blank? collection_start_date = FormHandler.instance.current_collection_start_date return collection_start_date if params[:location][:deactivation_date_type] == "default" - - return @location.errors.add(:deactivation_date_type, message: I18n.t("validations.location.deactivation_date.not_selected")) if params[:location][:deactivation_date].blank? && params[:location][:deactivation_date_type].blank? return params[:location][:deactivation_date] if params[:location][:deactivation_date_type].blank? day = params[:location]["deactivation_date(3i)"] month = params[:location]["deactivation_date(2i)"] year = params[:location]["deactivation_date(1i)"] - collection_start_date = FormHandler.instance.current_collection_start_date - - if !deactivation_date_valid?(day, month, year, collection_start_date) - set_deactivation_date_errors(day, month, year, collection_start_date) - else - Date.new(year.to_i, month.to_i, day.to_i) - end - end - - def deactivation_date_valid?(day, month, year, collection_start_date) - [day, month, year].all?(&:present?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) && Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) - end - - def set_deactivation_date_errors(day, month, year, collection_start_date) - if [day, month, year].any?(&:blank?) - { day:, month:, year: }.each do |period, value| - @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered", period: period.to_s)) if value.blank? - end - elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) - @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) - elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) - @location.errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) - end + Date.new(year.to_i, month.to_i, day.to_i) end end