Class: Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/payment.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.merge_banknotes(payments = nil) ⇒ Object



47
48
49
50
51
52
# File 'app/models/payment.rb', line 47

def self.merge_banknotes(payments=nil)
  payments ||= self.all
  payments.reject{|l| l.banknotes.nil? }.map(&:banknotes).reduce({}) do |result, entry|
    result.merge(entry) {|key, left, right| left.to_i+right.to_i }
  end
end

.merge_cash(payments = nil) ⇒ Object



54
55
56
57
# File 'app/models/payment.rb', line 54

def self.merge_cash(payments=nil)
  payments ||= self.all
  payments.map(&:paid_amount).compact.sum
end

Instance Method Details

#checkObject



77
78
79
80
81
82
# File 'app/models/payment.rb', line 77

def check
  Payments::CheckWorker.new.perform(id)
  reload

  checked?
end

#commission_for(amount) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/payment.rb', line 63

def commission_for(amount)
  return 0 if commissions.blank?

  commission = commissions.select{|x| x[:max].to_f >= amount && amount >= x[:min].to_f }.
    sort_by{|x| x[:weight]}.first

  return 0 if commission.blank?

  static  = commission[:static_fee].try(:to_f) || 0
  percent = ((commission[:percent_fee].try(:to_f) || 0)/100*amount).round(2)

  static+percent
end

#payObject



84
85
86
# File 'app/models/payment.rb', line 84

def pay
  Payments::PayWorker.perform_async(id)
end

#titleObject



59
60
61
# File 'app/models/payment.rb', line 59

def title
  provider.title rescue "-"
end