class PRemise < ApplicationRecord
  belongs_to :p_payment_type
  belongs_to :p_bank_account
  
  has_many :p_payments, :dependent => :nullify
  
  has_many :p_payment_types, :through => :p_payments
  
  accepts_nested_attributes_for :p_payments
  
  validates :p_bank_account_id, :presence => true
  #validates :p_payment_type_id, :presence => true
  validates :date, :presence => true
  
  
  def amount
    self.p_payments.sum(:amount)
  end
  
  def p_payments_count
    self.p_payments.count
  end
  
  after_save do
    self.p_payments.update_all(:remise => true, :p_bank_account_id => self.p_bank_account_id, :paid_at => self.date)
    
  end
  
  after_destroy do
    
  end
  
end
