Module: Payday::Invoiceable

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

Overview

Include Invoiceable in your Invoice class to make it Payday compatible. Payday expects that a line_items method containing an Enumerable of LineItem compatible elements exists. Those LineItem objects primarily need to include quantity, price, and description methods.

The bill_to method should always be overwritten by your class. Otherwise, it'll say that your invoice should be billed to Goofy McGoofison. ship_to is also available, but will not be used in rendered invoices if it doesn't exist.

Although not required, if a tax_rate method exists, Invoiceable will use it to calculate tax when generating an invoice. We include a simple tax method that calculates tax, but it's probably wiser to override this in your class (our calculated tax won't be stored to a database by default, for example).

If the due_at, paid_at, and refunded_at methods are available, Invoiceable will use them to show due dates, paid dates, and refunded dates, as well as stamps showing if the invoice is paid or due.

Instance Method Summary (collapse)

Instance Method Details

- (Object) bill_to

Who the invoice is being sent to.



18
19
20
# File 'lib/payday/invoiceable.rb', line 18

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

- (Object) each_detail(&block)

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



77
78
79
80
81
82
83
# File 'lib/payday/invoiceable.rb', line 77

def each_detail(&block)
  return if defined?(invoice_details).nil?
  
  invoice_details.each do |detail|
    block.call(detail[0], detail[1])
  end
end

- (Boolean) overdue?

Returns:

  • (Boolean)


53
54
55
# File 'lib/payday/invoiceable.rb', line 53

def overdue?
  defined?(:due_at) && ((due_at.is_a?(Date) && due_at < Date.today) || (due_at.is_a?(Time) && due_at < Time.now))  && !paid_at
end

- (Boolean) paid?

Returns:

  • (Boolean)


61
62
63
# File 'lib/payday/invoiceable.rb', line 61

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

- (Boolean) refunded?

Returns:

  • (Boolean)


57
58
59
# File 'lib/payday/invoiceable.rb', line 57

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

- (Object) render_pdf

Renders this invoice to pdf as a string



66
67
68
# File 'lib/payday/invoiceable.rb', line 66

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

- (Object) render_pdf_to_file(path)

Renders this invoice to pdf



71
72
73
# File 'lib/payday/invoiceable.rb', line 71

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

- (Object) shipping

TODO Add a per weight unit shipping cost Calculates the shipping



40
41
42
43
44
45
46
# File 'lib/payday/invoiceable.rb', line 40

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

- (Object) subtotal

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



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

def subtotal
  line_items.inject(BigDecimal.new("0")) { |result, item| result += item.amount }
end

- (Object) tax

The tax for this invoice, as a BigDecimal



28
29
30
31
32
33
34
35
36
# File 'lib/payday/invoiceable.rb', line 28

def tax
  if defined?(tax_rate)
    calculated = subtotal * tax_rate
    return 0 if calculated < 0
    calculated
  else
    0
  end
end

- (Object) total

Calculates the total for this invoice.



49
50
51
# File 'lib/payday/invoiceable.rb', line 49

def total
  subtotal + tax + shipping
end