Class: Eboshi::Invoice
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Eboshi::Invoice
- Defined in:
- app/models/eboshi/invoice.rb
Class Method Summary collapse
Instance Method Summary collapse
- #adjustments ⇒ Object
- #balance ⇒ Object
- #consistant_rate ⇒ Object
- #paid? ⇒ Boolean
- #status ⇒ Object
- #total ⇒ Object
- #total=(value) ⇒ Object
- #total_for_user(user) ⇒ Object
- #total_hours ⇒ Object
- #total_hours_for_user(user) ⇒ Object
- #users ⇒ Object
- #works ⇒ Object
Class Method Details
.paid ⇒ Object
29 30 31 |
# File 'app/models/eboshi/invoice.rb', line 29 def self.paid order(date: :desc).select(&:paid?) end |
.unpaid ⇒ Object
25 26 27 |
# File 'app/models/eboshi/invoice.rb', line 25 def self.unpaid order(date: :desc).reject(&:paid?) end |
Instance Method Details
#adjustments ⇒ Object
37 38 39 |
# File 'app/models/eboshi/invoice.rb', line 37 def adjustments line_items.where(type: "Eboshi::Adjustment") end |
#balance ⇒ Object
55 56 57 |
# File 'app/models/eboshi/invoice.rb', line 55 def balance total - payments.to_a.sum(&:total) end |
#consistant_rate ⇒ Object
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
64 65 66 |
# File 'app/models/eboshi/invoice.rb', line 64 def paid? !line_items.empty? && !payments.empty? && balance == 0 end |
#status ⇒ Object
59 60 61 62 |
# File 'app/models/eboshi/invoice.rb', line 59 def status return 'unbilled' if new_record? paid? ? 'paid' : 'unpaid' end |
#total ⇒ Object
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_hours ⇒ Object
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 |
#users ⇒ Object
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 |
#works ⇒ Object
33 34 35 |
# File 'app/models/eboshi/invoice.rb', line 33 def works line_items.where(type: "Eboshi::Work") end |