1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

validates invoice&avoir creation to prevent any creation in closed period

This commit is contained in:
Sylvain 2019-01-09 16:54:09 +01:00
parent 5e8172af35
commit 35638d611b
6 changed files with 23 additions and 0 deletions

View File

@ -17,6 +17,8 @@ class Invoice < ActiveRecord::Base
after_create :update_reference
after_commit :generate_and_send_invoice, on: [:create], if: :persisted?
validates_with ClosedPeriodValidator
def file
dir = "invoices/#{user.id}"

View File

@ -0,0 +1,17 @@
# 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
DateTime.now
end
AccountingPeriod.all.each do |period|
record.errors[:date] << I18n.t('errors.messages.in_closed_period') if date >= period.start_at && date <= period.end_at
end
end
end

View File

@ -38,6 +38,7 @@ en:
cannot_be_before_previous_value: "cannot be before the previous value"
cannot_overlap: "can't overlap an existing accounting period"
cannot_encompass: "can't encompass an existing accounting period"
in_closed_period: "can't be within a closed accounting period"
activemodel:
errors:

View File

@ -38,6 +38,7 @@ es:
cannot_be_before_previous_value: "No puede estar antes del valor anterior."
cannot_overlap: "can't overlap an existing accounting period" # missing translation
cannot_encompass: "can't encompass an existing accounting period" # missing translation
in_closed_period: "can't be within a closed accounting period" # missing translation
activemodel:
errors:

View File

@ -38,6 +38,7 @@ fr:
cannot_be_before_previous_value: "ne peut pas être antérieur(e) à la valeur précédente"
cannot_overlap: "ne peut pas chevaucher une période comptable existante"
cannot_encompass: "ne peut pas englober une période comptable existante"
in_closed_period: "ne peut pas être dans une période comptable fermée"
activemodel:
errors:

View File

@ -38,6 +38,7 @@ pt:
cannot_be_before_previous_value: "não pode ser antes do valor anterior"
cannot_overlap: "can't overlap an existing accounting period" # missing translation
cannot_encompass: "can't encompass an existing accounting period" # missing translation
in_closed_period: "can't be within a closed accounting period" # missing translation
activemodel:
errors: