Class: InvoicePDF::Invoice
- Inherits:
-
Object
- Object
- InvoicePDF::Invoice
- Defined in:
- lib/invoice/invoice.rb
Overview
Invoice model to create your PDF invoices.
Instance Attribute Summary collapse
-
#bill_to ⇒ Object
Returns the value of attribute bill_to.
-
#bill_to_address ⇒ Object
Returns the value of attribute bill_to_address.
-
#company ⇒ Object
Returns the value of attribute company.
-
#company_address ⇒ Object
Returns the value of attribute company_address.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#currency_text ⇒ Object
Returns the value of attribute currency_text.
-
#decimal ⇒ Object
Returns the value of attribute decimal.
-
#discount ⇒ Object
Returns the value of attribute discount.
-
#due_date ⇒ Object
Returns the value of attribute due_date.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#generator ⇒ Object
Returns the value of attribute generator.
-
#invoice_date ⇒ Object
Returns the value of attribute invoice_date.
-
#items ⇒ Object
Returns the value of attribute items.
-
#notes ⇒ Object
Returns the value of attribute notes.
-
#number ⇒ Object
Returns the value of attribute number.
-
#paid ⇒ Object
Returns the value of attribute paid.
-
#po_number ⇒ Object
Returns the value of attribute po_number.
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#tax ⇒ Object
Returns the value of attribute tax.
Instance Method Summary collapse
-
#discount_amount ⇒ Object
Calculates the discount amount if a discount is specified greater than 0.
-
#initialize(options = {}) ⇒ Invoice
constructor
Creates a new Invoice object with the specified options ==== Options *
:generator
- AnInvoicePDF::Generator
that designs and creates the PDF. -
#save ⇒ Object
Generates the PDF invoice and saves it to the specified destination.
-
#subtotal ⇒ Object
Calculates the subtotal of the invoice.
-
#tax_amount ⇒ Object
Calculates the tax amount of the invoice based on taxable items.
-
#total ⇒ Object
Calculates the total of the invoice with the discount and tax amount applied.
-
#total_due ⇒ Object
The total amount due (total - paid).
Constructor Details
#initialize(options = {}) ⇒ Invoice
Creates a new Invoice object with the specified options
Options
-
:generator
- AnInvoicePDF::Generator
that designs and creates the PDF. (default is InvoicePDF::Generators::Standard.new) -
:file_path
- The directory to store the generated invoice. (default is ‘./’) -
:file_name
- The file name of the PDF to save. (default is ‘invoice.pdf’) -
:company
- The company name that will appear on the top of the invoice and in the ‘Bill To’ section. (default is ‘Company Name’) -
:company_address
- Your company’s address. Displayed in the ‘Pay To’ section. Use n for line separation. (default is nil) -
:bill_to
- Person/company you’re billing. (default is nil) -
:bill_to_address
- Address of the person/company you’re billing to. Displayed in the ‘Bill To’ section. (default is nil) -
:number
- The invoice number. (default is ‘100’) -
:po_number
- The purchase order number for the invoice. Excluded from the invoice if nil. (default is nil) -
:invoice_date
- The invoice date. (default is Time.now) -
:due_date
- The invoice due_date. You need your money by this time. (default is 15 days after Time.now) -
:items
- Array ofLineItem
s. (default is an empty array). -
:discount
- Discount to apply to the invoice. The value should not include the percent symbol. (default is nil) -
:tax
- Tax amount to apply to taxableLineItem
s. The value should not include the percent symbol. (default is nil) -
:paid
- The amount already paid on the invoice. (default is 0) -
:notes
- Notes to display at the end of the invoice. (default is nil) -
:currency
- The currency symbol to use for formatting prices and totals. (default is ‘$’) -
:separator
- The thousands separator for prices/totals. (default is ‘,’) -
:decimal
- The separator between the integer and fractional value. Think dollars and cents. (default is ‘.’) -
:currency_text
- Displays text after the formatted currency. Useful if you wanted to include the currency code (example: ‘ USD’). (default is ”)
Examples
invoice = InvoicePDF::Invoice.new( :company => "Drew Tempelmeyer", :company_address => "555 55th St\nNew York, NY 00000", :bill_to => "John Doe", :number => "AZ-100", :notes => "Test invoice")
invoice.items << InvoicePDF::LineItem.new(:description => "Here is a line item", :price => 495.00, :quantity => 5)
invoice.save
38 39 40 41 42 43 |
# File 'lib/invoice/invoice.rb', line 38 def initialize( = {} ) = { :generator => InvoicePDF::Generators::Standard.new, :file_path => './', :file_name => 'invoice.pdf', :company => 'Company Name', :number => '100', :po_number => nil, :invoice_date => Time.now, :due_date => Time.now + ((60 * 60 * 24) * 15), :items => [], :discount => nil, :tax => nil, :paid => 0, :notes => nil, :currency => '$', :separator => ',', :decimal => '.', :currency_text => '' }.merge() .each { |k,v| send("#{k}=", v) } end |
Instance Attribute Details
#bill_to ⇒ Object
Returns the value of attribute bill_to.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def bill_to @bill_to end |
#bill_to_address ⇒ Object
Returns the value of attribute bill_to_address.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def bill_to_address @bill_to_address end |
#company ⇒ Object
Returns the value of attribute company.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def company @company end |
#company_address ⇒ Object
Returns the value of attribute company_address.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def company_address @company_address end |
#currency ⇒ Object
Returns the value of attribute currency.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def currency @currency end |
#currency_text ⇒ Object
Returns the value of attribute currency_text.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def currency_text @currency_text end |
#decimal ⇒ Object
Returns the value of attribute decimal.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def decimal @decimal end |
#discount ⇒ Object
Returns the value of attribute discount.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def discount @discount end |
#due_date ⇒ Object
Returns the value of attribute due_date.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def due_date @due_date end |
#file_name ⇒ Object
Returns the value of attribute file_name.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def file_name @file_name end |
#file_path ⇒ Object
Returns the value of attribute file_path.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def file_path @file_path end |
#generator ⇒ Object
Returns the value of attribute generator.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def generator @generator end |
#invoice_date ⇒ Object
Returns the value of attribute invoice_date.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def invoice_date @invoice_date end |
#items ⇒ Object
Returns the value of attribute items.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def items @items end |
#notes ⇒ Object
Returns the value of attribute notes.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def notes @notes end |
#number ⇒ Object
Returns the value of attribute number.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def number @number end |
#paid ⇒ Object
Returns the value of attribute paid.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def paid @paid end |
#po_number ⇒ Object
Returns the value of attribute po_number.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def po_number @po_number end |
#separator ⇒ Object
Returns the value of attribute separator.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def separator @separator end |
#tax ⇒ Object
Returns the value of attribute tax.
8 9 10 |
# File 'lib/invoice/invoice.rb', line 8 def tax @tax end |
Instance Method Details
#discount_amount ⇒ Object
Calculates the discount amount if a discount is specified greater than 0
61 62 63 64 |
# File 'lib/invoice/invoice.rb', line 61 def discount_amount return 0 if discount.nil? || discount <= 0 subtotal * ( discount / 100 ) end |
#save ⇒ Object
Generates the PDF invoice and saves it to the specified destination
77 78 79 |
# File 'lib/invoice/invoice.rb', line 77 def save generator.create_pdf( self ) end |
#subtotal ⇒ Object
Calculates the subtotal of the invoice
46 47 48 49 50 |
# File 'lib/invoice/invoice.rb', line 46 def subtotal amount = 0 items.each { |item| amount += item.total } amount end |
#tax_amount ⇒ Object
Calculates the tax amount of the invoice based on taxable items
53 54 55 56 57 58 |
# File 'lib/invoice/invoice.rb', line 53 def tax_amount return 0 if tax.nil? || tax <= 0 amount = 0 items.each { |item| amount += item.total if item.taxable? } amount * ( tax / 100 ) end |
#total ⇒ Object
Calculates the total of the invoice with the discount and tax amount applied
67 68 69 |
# File 'lib/invoice/invoice.rb', line 67 def total (subtotal - discount_amount) + tax_amount end |
#total_due ⇒ Object
The total amount due (total - paid)
72 73 74 |
# File 'lib/invoice/invoice.rb', line 72 def total_due total - paid end |