64 lines
1.8 KiB
Ruby
64 lines
1.8 KiB
Ruby
class Virement < ActiveRecord::Base
|
|
belongs_to :devise
|
|
validates :amount_ht, :presence => true
|
|
belongs_to :product_customer
|
|
belongs_to :virement_remise
|
|
belongs_to :commission
|
|
belongs_to :account_rib
|
|
|
|
|
|
scope :execute_between, lambda { |start, stop|
|
|
execute_after(start).execute_before(stop)
|
|
}
|
|
|
|
|
|
scope :execute_after, lambda { |date|
|
|
joins(:virement_remise).where("(virement_remises.created_at >= ?)", date )
|
|
}
|
|
scope :execute_before, lambda { |date|
|
|
where("(virement_remises.created_at <= ?)", date )
|
|
}
|
|
|
|
def generate_token
|
|
self.token = loop do
|
|
|
|
token = SecureRandom.hex(6)
|
|
break token unless Virement.exists?(token: token)
|
|
end
|
|
|
|
self.token
|
|
end
|
|
|
|
|
|
def name_for_edi
|
|
value = self.account_rib.name.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').to_s
|
|
|
|
# Remove single quotes from input
|
|
value.gsub!(/[']+/, '')
|
|
|
|
# Replace any non-word character (\W) with a space
|
|
value.gsub!(/\W+/, ' ')
|
|
|
|
# Remove any whitespace before and after the string
|
|
value.strip!
|
|
return value
|
|
end
|
|
|
|
|
|
before_validation do
|
|
if self.product_customer.virements.where("sended is null").count >= 1
|
|
errors.add(:amount_ht, "Vous ne pouvez pas avoir plus d'un virements en attente d'execution.")
|
|
end
|
|
|
|
if !self.product_customer.account_ribs.where(:id => self.account_rib.id, :validated => true).first or !self.account_rib.validated
|
|
errors.add(:account_rib, 'Compte sélectionné invalide')
|
|
|
|
end
|
|
|
|
if self.amount_ht.to_f < 5 or self.amount_ht.to_f > self.product_customer.max_virement
|
|
errors.add(:amount_ht, "Le montant doit être compris entre 5 € et #{self.product_customer.max_virement.to_i} €")
|
|
|
|
end
|
|
end
|
|
end
|