Class: Eboshi::Invoice

Inherits:
ApplicationRecord show all
Defined in:
app/models/eboshi/invoice.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



29
30
31
# File 'app/models/eboshi/invoice.rb', line 29

def self.paid
  order(date: :desc).select(&:paid?)
end

.unpaidObject



25
26
27
# File 'app/models/eboshi/invoice.rb', line 25

def self.unpaid
  order(date: :desc).reject(&:paid?)
end

Instance Method Details

#adjustmentsObject



37
38
39
# File 'app/models/eboshi/invoice.rb', line 37

def adjustments
  line_items.where(type: "Eboshi::Adjustment")
end

#balanceObject



55
56
57
# File 'app/models/eboshi/invoice.rb', line 55

def balance
  total - payments.to_a.sum(&:total)
end

#consistant_rateObject



76
77
78
79
80
# File 'app/models/eboshi/invoice.rb', line 76

def consistant_rate
  return true if works.empty?
  rates = works.collect(&:rate).uniq
  rates.length == 1 ? rates.first : false
end

#paid?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/eboshi/invoice.rb', line 64

def paid?
  !line_items.empty? && !payments.empty? && balance == 0
end

#statusObject



59
60
61
62
# File 'app/models/eboshi/invoice.rb', line 59

def status
  return 'unbilled' if new_record?
  paid? ? 'paid' : 'unpaid'
end

#totalObject



41
42
43
# File 'app/models/eboshi/invoice.rb', line 41

def total
  line_items.to_a.sum(&:total)
end

#total=(value) ⇒ Object



49
50
51
52
53
# File 'app/models/eboshi/invoice.rb', line 49

def total=(value)
  difference = value.to_f - total
  return total if difference.abs < 0.01
  line_items << Adjustment.new(total: difference, client_id: client_id)
end

#total_for_user(user) ⇒ Object



45
46
47
# File 'app/models/eboshi/invoice.rb', line 45

def total_for_user(user)
  line_items.to_a.select { |li| li.user == user }.sum(&:total)
end

#total_hoursObject



68
69
70
# File 'app/models/eboshi/invoice.rb', line 68

def total_hours
  line_items.to_a.sum(&:hours)
end

#total_hours_for_user(user) ⇒ Object



72
73
74
# File 'app/models/eboshi/invoice.rb', line 72

def total_hours_for_user(user)
  line_items.to_a.select { |li| li.user == user }.sum(&:hours)
end

#usersObject



82
83
84
# File 'app/models/eboshi/invoice.rb', line 82

def users
  line_items.collect(&:user).uniq.sort_by { |user| user ? user.name : "" }
end

#worksObject



33
34
35
# File 'app/models/eboshi/invoice.rb', line 33

def works
  line_items.where(type: "Eboshi::Work")
end