app/models/billing/payment.rb in billing-0.0.3 vs app/models/billing/payment.rb in billing-0.0.4a

- old
+ new

@@ -1,39 +1,68 @@ module Billing class Payment < ActiveRecord::Base + PAYMENT_WITH_TYPE = 'Billing::PaymentWithType'.freeze + PAYMENT_EXTERNAL = 'Billing::PaymentExternal'.freeze + PAYPAL_EXPRESS = 'Billing::PaypalExpress'.freeze + PAYMENT_MODELS = [PAYMENT_WITH_TYPE, PAYMENT_EXTERNAL, PAYPAL_EXPRESS].freeze + include AccountItem - belongs_to :account, inverse_of: :payments, validate: true + attr_writer :origin + attr_accessor :origin_id monetize :value_cents + + belongs_to :account, inverse_of: :payments, validate: true - delegate :billable, to: :account + scope :in_period, lambda {|from, to| where(created_at: from..to) } + scope :for_report, -> { joins(:account).where(billing_accounts: { balance_cents: 0 ,report_id: nil }) } - validates_presence_of :value + if defined? Extface + belongs_to :extface_job, class_name: 'Extface::Job' + end + delegate :billable, to: :account + + validates_numericality_of :value, greater_than_or_equal_to: 0 + validates :type, inclusion: { in: PAYMENT_MODELS } + after_initialize on: :create do - self.value = -account.try(:balance).to_money if value.zero? + self.value = -account.try(:balance) if value.zero? end - class << self - def args(*args) - h = { type: 'Billing::PaymentWithType' } - case when args.blank? || args.first.kind_of?(Hash) then - args.blank? ? h : h.merge(*args) - when args.first.kind_of?(String) then - #TODO parse - else - h.merge!(payment_type_id: args.shift.to_param) - if args.any? && (args.first.kind_of?(Hash) || args.first.kind_of?(String)) - h.merge(args(*args)) + before_validation do + account.origin = origin unless account.origin and account.payments.many? + end + + def fiscal?; false; end + def cash?; false; end + def external?; false; end + + def origin + @origin || origins.find_by_id(@origin_id) + end + + private + class << self + def args(*args) + h = {} + case when args.blank? || args.first.kind_of?(Hash) then + args.blank? ? h : h.merge(*args) + when args.first.kind_of?(String) then + #TODO parse else - if args.blank? - h + h.merge!(payment_type_id: args.shift.to_param) + if args.any? && (args.first.kind_of?(Hash) || args.first.kind_of?(String)) + h.merge(args(*args)) else - h.merge!( value: args.shift.to_money ) - args.any? ? h.merge(*args) : h + if args.blank? + h + else + h.merge!( value: args.shift.to_money ) + args.any? ? h.merge(*args) : h + end end end end end - end end end