class DonatorTransaction < ActiveRecord::Base belongs_to :donator belongs_to :recurrent_payment belongs_to :tr_occasion belongs_to :tr_activity #has_many :particulars, :as => :owner belongs_to :particular_bill, :class_name => "Particular" accepts_nested_attributes_for :particular_bill belongs_to :particular_send, :class_name => "Particular" accepts_nested_attributes_for :particular_send BANK_ACCOUNT = ["Crédit coopératif", "La banque postale"] has_one :tr_payment accepts_nested_attributes_for :tr_payment has_one :tr_payment_type, :through => :tr_payment has_many :transaction_items accepts_nested_attributes_for :transaction_items has_one :generated_recurrent_payment, :foreign_key => :origine_transaction_id, :class_name => "RecurrentPayment" accepts_nested_attributes_for :generated_recurrent_payment def total self.transaction_items.sum(:amount) end before_create do self.generated_recurrent_payment = RecurrentPayment.new() update_recurrent_payment() end before_update do update_recurrent_payment() end def update_recurrent_payment if self.recurrent self.generated_recurrent_payment.active = true self.generated_recurrent_payment.start_at = self.tr_payment.paid_at else self.generated_recurrent_payment.active = false self.generated_recurrent_payment.start_at = nil end end def valid_particulars? true if self.particular_bill.valid? and particular_send.valid? end def tr_payment_type_id self.tr_payment_type.id if self.tr_payment_type end end