Sha256: 0fcb9e7c4748764264a484117bb4824febcb3e07c3f0fc5a115487f74edf1d90

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module Billing
  class Charge < ActiveRecord::Base
    include AccountItem
    belongs_to :account, inverse_of: :charges, validate: true
    belongs_to :chargable, polymorphic: true
    belongs_to :origin, inverse_of: :charges
    has_one :modifier, inverse_of: :charge

    monetize :price_cents
    monetize :value_cents
    
    delegate :paid?, to: :account
    
    scope :unpaid, -> { joins(:account).where.not(billing_accounts: { balance_cents: 0}) }
    scope :paid, -> { joins(:account).where(billing_accounts: { balance_cents: 0}) }
    scope :in_period, lambda {|from, to| where(revenue_at: from..to) }
    
    validates_presence_of :price
    validates_numericality_of :value, greater_than_or_equal_to: 0
    
    before_save do
      self.value = price unless modifier.present? #FIXME global account modifier lost
    end
    
    class << self
      def args(*args)
        case when args.blank? || args.first.is_a?(Hash) then
          {}.merge(*args)
        else
          h = { price: args.shift.to_money }
          args.any? ? h.merge(*args) : h
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
billing-0.0.4b app/models/billing/charge.rb
billing-0.0.4a app/models/billing/charge.rb
billing-0.0.4 app/models/billing/charge.rb