2019-01-08 17:32:45 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Validates the current accounting period does not overlap an existing one
|
|
|
|
class PeriodOverlapValidator < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
the_end = record.end_at
|
|
|
|
the_start = record.start_at
|
|
|
|
|
2023-03-22 16:05:25 +01:00
|
|
|
AccountingPeriod.find_each do |period|
|
2023-02-24 17:26:55 +01:00
|
|
|
record.errors.add(:start_at, I18n.t('errors.messages.cannot_overlap')) if the_start >= period.start_at && the_start <= period.end_at
|
|
|
|
record.errors.add(:end_at, I18n.t('errors.messages.cannot_overlap')) if the_end >= period.start_at && the_end <= period.end_at
|
|
|
|
record.errors.add(:end_at, I18n.t('errors.messages.cannot_encompass')) if period.start_at >= the_start && period.end_at <= the_end
|
2019-01-08 17:32:45 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|