Sha256: b30f39e17e721e214328a234104fe7b4c14fd633efd1676e34f3a1a9931fab48

Contents?: true

Size: 1.95 KB

Versions: 31

Compression:

Stored size: 1.95 KB

Contents

module Billing
  class Payment < ActiveRecord::Base
    PAYMENT_WITH_TYPE = 'Billing::PaymentWithType'.freeze
    PAYMENT_EXTERNAL = 'Billing::PaymentExternal'.freeze
    PAYPAL_EXPRESS = 'Billing::PaypalExpress'.freeze
    ROOM_TRANSFER = 'Billing::RoomTransfer'.freeze
    PAYMENT_MODELS = [PAYMENT_WITH_TYPE, PAYMENT_EXTERNAL, PAYPAL_EXPRESS, ROOM_TRANSFER].freeze
    
    include BillItem
    
    attr_writer :origin
    attr_accessor :origin_id
    monetize :value_cents

    belongs_to :bill, inverse_of: :payments, validate: true
    
    scope :in_period, lambda {|from, to| where(created_at: from..to) }
    scope :for_report, -> { joins(:bill).where(billing_bills: { balance_cents: 0 ,report_id: nil }) }
    
    if defined? Extface
      belongs_to :extface_job, class_name: 'Extface::Job'
    end
    
    delegate :billable, to: :bill, allow_nil: true

    validates_numericality_of :value, greater_than_or_equal_to: 0
    validates :type, inclusion: { in: PAYMENT_MODELS }
    
    after_initialize on: :create do
      self.value = -bill.try(:balance) if bill && value.zero?
    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 wild_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
            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
              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

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
billing-0.2.2 app/models/billing/payment.rb
billing-0.2.1 app/models/billing/payment.rb
billing-0.2.0 app/models/billing/payment.rb
billing-0.1.9 app/models/billing/payment.rb
billing-0.1.8 app/models/billing/payment.rb
billing-0.1.7 app/models/billing/payment.rb
billing-0.1.6 app/models/billing/payment.rb
billing-0.1.5 app/models/billing/payment.rb
billing-0.1.4 app/models/billing/payment.rb
billing-0.1.3 app/models/billing/payment.rb
billing-0.1.2a app/models/billing/payment.rb
billing-0.1.2 app/models/billing/payment.rb
billing-0.1.1 app/models/billing/payment.rb
billing-0.1.0a app/models/billing/payment.rb
billing-0.1.0 app/models/billing/payment.rb
billing-0.0.9 app/models/billing/payment.rb
billing-0.0.8a app/models/billing/payment.rb
billing-0.0.8 app/models/billing/payment.rb
billing-0.0.7i app/models/billing/payment.rb
billing-0.0.7g app/models/billing/payment.rb