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

#bill_toObject

Who the invoice is being sent to.



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

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

#each_detail(&block) ⇒ Object

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



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

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

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

#overdue?Boolean

Returns:

  • (Boolean)


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

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

#paid?Boolean

Returns:

  • (Boolean)


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

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

#refunded?Boolean

Returns:

  • (Boolean)


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

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

#render_pdfObject

Renders this invoice to pdf as a string



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

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

#render_pdf_to_file(path) ⇒ Object

Renders this invoice to pdf



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

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

#shippingObject

TODO Add a per weight unit shipping cost Calculates the shipping



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

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



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

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

#taxObject

The tax for this invoice, as a BigDecimal



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

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

#totalObject

Calculates the total for this invoice.



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

def total
  subtotal + tax + shipping
end