2019-01-09 16:54:09 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Validates the current invoice is not generated within a closed accounting period
|
|
|
|
class ClosedPeriodValidator < ActiveModel::Validator
|
|
|
|
def validate(record)
|
|
|
|
date = if record.is_a?(Avoir)
|
|
|
|
record.avoir_date
|
|
|
|
else
|
2023-02-14 13:10:58 +01:00
|
|
|
Time.current
|
2019-01-09 16:54:09 +01:00
|
|
|
end
|
|
|
|
|
2023-03-22 16:05:25 +01:00
|
|
|
AccountingPeriod.find_each do |period|
|
2023-02-14 13:10:58 +01:00
|
|
|
record.errors.add(:date, I18n.t('errors.messages.in_closed_period')) if date >= period.start_at && date <= period.end_at
|
2019-01-09 16:54:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|