Module: Payday::Invoiceable

Included in:
Invoice
Defined in:
lib/payday/invoiceable.rb

Instance Method Summary collapse

Instance Method Details

#bill_toObject

Who the invoice is being sent to.



24
25
26
# File 'lib/payday/invoiceable.rb', line 24

def bill_to
  "Goofy McGoofison\nYour Invoice Doesn't\nHave It's Own BillTo Method"
end

#each_detailObject

Iterates through the details on this invoiceable. The block given should accept two parameters, the detail name and the actual detail value.



93
94
95
96
97
98
99
# File 'lib/payday/invoiceable.rb', line 93

def each_detail
  return if defined?(invoice_details).nil?

  invoice_details.each do |detail|
    yield(detail[0], detail[1])
  end
end

#overdue?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/payday/invoiceable.rb', line 67

def overdue?
  # rubocop:todo Layout/LineLength
  defined?(due_at) && ((due_at.is_a?(Date) && due_at < Date.today) || (due_at.is_a?(Time) && due_at < Time.now)) && !paid_at
  # rubocop:enable Layout/LineLength
end

#paid?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/payday/invoiceable.rb', line 77

def paid?
  defined?(paid_at) && !!paid_at
end

#refunded?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/payday/invoiceable.rb', line 73

def refunded?
  defined?(refunded_at) && !!refunded_at
end

#render_pdfObject

Renders this invoice to pdf as a string



82
83
84
# File 'lib/payday/invoiceable.rb', line 82

def render_pdf
  Payday::PdfRenderer.render(self)
end

#render_pdf_to_file(path) ⇒ Object

Renders this invoice to pdf



87
88
89
# File 'lib/payday/invoiceable.rb', line 87

def render_pdf_to_file(path)
  Payday::PdfRenderer.render_to_file(self, path)
end

#retentionObject

Calculates the retention (e.g. IRPF) as a percentage of the subtotal. This is a post-tax deduction subtracted from the total.



54
55
56
57
58
59
60
# File 'lib/payday/invoiceable.rb', line 54

def retention
  if defined?(retention_rate)
    subtotal * retention_rate / 100
  else
    0
  end
end

#shippingObject

TODO: Add a per weight unit shipping cost Calculates the shipping



44
45
46
47
48
49
50
# File 'lib/payday/invoiceable.rb', line 44

def shipping
  if defined?(shipping_rate)
    shipping_rate
  else
    0
  end
end

#subtotalObject

Calculates the subtotal of this invoice by adding up all of the line items



29
30
31
# File 'lib/payday/invoiceable.rb', line 29

def subtotal
  line_items.reduce(BigDecimal(0)) { |result, item| result + item.amount }
end

#taxObject

The tax for this invoice, as a BigDecimal



34
35
36
37
38
39
40
# File 'lib/payday/invoiceable.rb', line 34

def tax
  if defined?(tax_rate)
    subtotal * tax_rate / 100
  else
    0
  end
end

#totalObject

Calculates the total for this invoice.



63
64
65
# File 'lib/payday/invoiceable.rb', line 63

def total
  subtotal + tax + shipping - retention
end