diff --git a/app/models/location.rb b/app/models/location.rb index f788f72ff..7248d6424 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -180,6 +180,11 @@ class Location < ApplicationRecord status_at(6.months.from_now) == :deactivating_soon end + def active_on_date?(date) + status = status_at(date) + %i[active deactivating_soon].include?(status) + end + def validate_postcode if !postcode&.match(POSTCODE_REGEXP) error_message = I18n.t("validations.postcode") diff --git a/app/models/scheme.rb b/app/models/scheme.rb index b33136e5c..07f4f8c9f 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -298,6 +298,13 @@ class Scheme < ApplicationRecord def all_locations_inactive? active? && locations.all? { |location| !location.active? } end + + def any_location_active_on_date?(date) + return false unless date + + locations.any? { |location| date && location.active_on_date?(date) } + end + def reactivating_soon? status == :reactivating_soon end diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index df2a2fc5e..04b4af76e 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -93,6 +93,10 @@ module Validations::SetupValidations scheme_during_startdate_validation(record) end + def validate_tenancy(record) + tenancy_during_startdate_validation(record) + end + def validate_managing_organisation_data_sharing_agremeent_signed(record) return if record.skip_dpo_validation diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index 79fc95158..a5734aa11 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -84,6 +84,17 @@ module Validations::SharedValidations end end + def tenancy_during_startdate_validation(record) + return if record.startdate.blank? + + if record.scheme.present? && !record.scheme.any_location_active_on_date?(record.startdate) + record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme.locations_inactive.startdate", name: record.scheme.service_name, date: record.startdate) + end + if record.location.present? && !record.location.active_on_date?(record.startdate) + record.errors.add :startdate, I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: record.location.postcode) + end + end + def inactive_status(date, resource) return if date.blank? || resource.blank?