Module: Validations::SetupValidations

Includes:
CollectionTimeHelper, SharedValidations
Defined in:
setup_validations.rb

Instance Method Summary collapse

Instance Method Details

#validate_irproduct_other(record) ⇒ Object



69
70
71
72
73
# File 'setup_validations.rb', line 69

def validate_irproduct_other(record)
  if intermediate_product_rent_type?(record) && record.irproduct_other.blank?
    record.errors.add :irproduct_other, I18n.t("validations.setup.intermediate_rent_product_name.blank")
  end
end

#validate_location(record) ⇒ Object



75
76
77
78
79
80
81
82
# File 'setup_validations.rb', line 75

def validate_location(record)
  location_during_startdate_validation(record)

  if record.location&.status == :incomplete
    record.errors.add :location_id, :incomplete, message: I18n.t("validations.setup.location.incomplete")
    record.errors.add :scheme_id, :incomplete, message: I18n.t("validations.setup.location.incomplete")
  end
end

#validate_managing_organisation_data_sharing_agremeent_signed(record) ⇒ Object



96
97
98
99
100
101
102
# File 'setup_validations.rb', line 96

def validate_managing_organisation_data_sharing_agremeent_signed(record)
  return if record.skip_dpo_validation

  if record.managing_organisation_id_changed? && record.managing_organisation.present? && !record.managing_organisation.data_protection_confirmed?
    record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.data_sharing_agreement_not_signed")
  end
end

#validate_merged_organisations_start_date(record) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'setup_validations.rb', line 59

def validate_merged_organisations_start_date(record)
  return unless record.startdate && date_valid?("startdate", record)

  return add_same_merge_organisation_error(record) if record.owning_organisation == record.managing_organisation
  return add_same_merge_error(record) if organisations_belong_to_same_merge?(record.owning_organisation, record.managing_organisation)

  add_merged_organisations_errors(record)
  add_absorbing_organisations_errors(record)
end

#validate_organisation(record) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'setup_validations.rb', line 23

def validate_organisation(record)
  created_by, managing_organisation, owning_organisation = record.values_at("created_by", "managing_organisation", "owning_organisation")
  unless [created_by, managing_organisation, owning_organisation].any?(&:blank?) || ((created_by.organisation.absorbed_organisations + [created_by.organisation]) & [managing_organisation, owning_organisation]).present?
    record.errors.add :created_by, I18n.t("validations.setup.created_by.invalid")
    record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.invalid")
    record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.invalid")
  end
  return unless record.startdate

  if owning_organisation.present?
    if owning_organisation&.merge_date.present? && owning_organisation.merge_date <= record.startdate
      record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_merged_organisation",
                                                        owning_organisation: record.owning_organisation.name,
                                                        owning_organisation_merge_date: record.owning_organisation.merge_date.to_formatted_s(:govuk_date),
                                                        owning_absorbing_organisation: record.owning_organisation.absorbing_organisation.name)
    elsif owning_organisation&.absorbed_organisations.present? && owning_organisation.available_from.present? && owning_organisation.available_from.to_date > record.startdate.to_date
      record.errors.add :owning_organisation_id, I18n.t("validations.setup.owning_organisation.inactive_absorbing_organisation",
                                                        owning_organisation: record.owning_organisation.name,
                                                        owning_organisation_available_from: record.owning_organisation.available_from.to_formatted_s(:govuk_date))
    end
  end

  if managing_organisation.present?
    if managing_organisation&.merge_date.present? && managing_organisation.merge_date <= record.startdate
      record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.inactive_merged_organisation",
                                                          managing_organisation: record.managing_organisation.name,
                                                          managing_organisation_merge_date: record.managing_organisation.merge_date.to_formatted_s(:govuk_date),
                                                          managing_absorbing_organisation: record.managing_organisation.absorbing_organisation.name)
    elsif managing_organisation&.absorbed_organisations.present? && managing_organisation.available_from.present? && managing_organisation.available_from.to_date > record.startdate.to_date
      record.errors.add :managing_organisation_id, I18n.t("validations.setup.managing_organisation.inactive_absorbing_organisation",
                                                          managing_organisation: record.managing_organisation.name,
                                                          managing_organisation_available_from: record.managing_organisation.available_from.to_formatted_s(:govuk_date))
    end
  end
end

#validate_scheme(record) ⇒ Object



92
93
94
# File 'setup_validations.rb', line 92

def validate_scheme(record)
  scheme_during_startdate_validation(record)
end

#validate_scheme_has_confirmed_locations_validation(record) ⇒ Object



84
85
86
87
88
89
90
# File 'setup_validations.rb', line 84

def validate_scheme_has_confirmed_locations_validation(record)
  return unless record.scheme

  unless record.scheme.locations.confirmed.any?
    record.errors.add :scheme_id, :no_completed_locations, message: I18n.t("validations.scheme.no_completed_locations")
  end
end

#validate_startdate_setup(record) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'setup_validations.rb', line 5

def validate_startdate_setup(record)
  return unless record.startdate && date_valid?("startdate", record) && !FeatureToggle.allow_future_form_use?

  first_collection_start_date = if record.startdate_was.present?
                                  editable_collection_start_date
                                else
                                  active_collection_start_date
                                end

  unless record.startdate.between?(first_collection_start_date, current_collection_end_date)
    record.errors.add :startdate, startdate_validation_error_message
  end

  if record.startdate > Time.zone.today + 14.days
    record.errors.add :startdate, I18n.t("validations.setup.startdate.later_than_14_days_after")
  end
end