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
and paid_at
methods are available, Invoiceable will use them to show due dates and paid dates, as well as stamps showing if the invoice is paid or due.
Instance Method Summary collapse
-
#bill_to ⇒ Object
Who the invoice is being sent to.
-
#each_detail(&block) ⇒ Object
Iterates through the details on this invoiceable.
- #overdue? ⇒ Boolean
- #paid? ⇒ Boolean
-
#render_pdf ⇒ Object
Renders this invoice to pdf as a string.
-
#render_pdf_to_file(path) ⇒ Object
Renders this invoice to pdf.
-
#shipping ⇒ Object
TODO Add a per weight unit shipping cost Calculates the shipping.
-
#subtotal ⇒ Object
Calculates the subtotal of this invoice by adding up all of the line items.
-
#tax ⇒ Object
The tax for this invoice, as a BigDecimal.
-
#total ⇒ Object
Calculates the total for this invoice.
Instance Method Details
#bill_to ⇒ Object
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 |
#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.
73 74 75 76 77 78 79 |
# File 'lib/payday/invoiceable.rb', line 73 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
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 |
#paid? ⇒ Boolean
57 58 59 |
# File 'lib/payday/invoiceable.rb', line 57 def paid? defined?(:paid_at) && !!paid_at end |
#render_pdf ⇒ Object
Renders this invoice to pdf as a string
62 63 64 |
# File 'lib/payday/invoiceable.rb', line 62 def render_pdf Payday::PdfRenderer.render(self) end |
#render_pdf_to_file(path) ⇒ Object
Renders this invoice to pdf
67 68 69 |
# File 'lib/payday/invoiceable.rb', line 67 def render_pdf_to_file(path) Payday::PdfRenderer.render_to_file(self, path) end |
#shipping ⇒ Object
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 |
#subtotal ⇒ Object
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 |
#tax ⇒ Object
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 |
#total ⇒ Object
Calculates the total for this invoice.
49 50 51 |
# File 'lib/payday/invoiceable.rb', line 49 def total subtotal + tax + shipping end |