qi6_app/app/models/p_payment_document.rb
2019-05-17 12:30:45 +02:00

77 lines
1.7 KiB
Ruby

class PPaymentDocument < ApplicationRecord
belongs_to :p_document
belongs_to :p_payment
has_one :p_customer, :through => :p_payment
validates :p_document_id, :presence => true
#validates :p_payment_id, :presence => true
validates :amount, :presence => true
attr_accessor :skip_validation
after_save do
if !skip_validation
self.p_document.save
self.p_customer.update_caches if self.p_customer
end
true
end
after_destroy do
p_document = self.p_document
#self.restore_attributes
#self.save(:validate => false)
if p_document
#ddf
p_document.save
end
self.p_payment.save if self.p_payment
self.p_customer.update_caches if self.p_customer
true
end
def siblings_payments
siblings_payments = self.p_document.p_payment_documents.joins(:p_payment).where(:p_payments => {:canceled => false})
siblings_payments = siblings_payments.where("p_payment_documents.id != ?", self.id) if self.id
return siblings_payments
end
def siblings_payments_total
self.siblings_payments.sum(:amount)
end
before_validation do
if !skip_validation
if self.p_document
sum = self.siblings_payments_total + self.amount.to_f
if sum > (self.p_document.totals[:ok_total_ttc].to_f + 0.99) and false #désactivé pour gérer les impayés
errors.add(:amount, "Ne doit pas dépasser le montant de la somme restant dues (#{self.p_document.totals[:ok_total_ttc].to_f - siblings_payments_total})")
end
end
end
true
end
end