Class: InvoicePDF::Invoice

Inherits:
Object
  • Object
show all
Defined in:
lib/invoice/invoice.rb

Overview

Invoice model to create your PDF invoices.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Invoice

Creates a new Invoice object with the specified options

Options

  • :generator - An InvoicePDF::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 of LineItems. (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 taxable LineItems. 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( options = {} )
  options = { :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(options)
  options.each { |k,v| send("#{k}=", v) }
end

Instance Attribute Details

#bill_toObject

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_addressObject

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

#companyObject

Returns the value of attribute company.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def company
  @company
end

#company_addressObject

Returns the value of attribute company_address.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def company_address
  @company_address
end

#currencyObject

Returns the value of attribute currency.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def currency
  @currency
end

#currency_textObject

Returns the value of attribute currency_text.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def currency_text
  @currency_text
end

#decimalObject

Returns the value of attribute decimal.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def decimal
  @decimal
end

#discountObject

Returns the value of attribute discount.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def discount
  @discount
end

#due_dateObject

Returns the value of attribute due_date.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def due_date
  @due_date
end

#file_nameObject

Returns the value of attribute file_name.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def file_name
  @file_name
end

#file_pathObject

Returns the value of attribute file_path.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def file_path
  @file_path
end

#generatorObject

Returns the value of attribute generator.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def generator
  @generator
end

#invoice_dateObject

Returns the value of attribute invoice_date.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def invoice_date
  @invoice_date
end

#itemsObject

Returns the value of attribute items.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def items
  @items
end

#notesObject

Returns the value of attribute notes.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def notes
  @notes
end

#numberObject

Returns the value of attribute number.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def number
  @number
end

Returns the value of attribute paid.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def paid
  @paid
end

#po_numberObject

Returns the value of attribute po_number.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def po_number
  @po_number
end

#separatorObject

Returns the value of attribute separator.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def separator
  @separator
end

#taxObject

Returns the value of attribute tax.



8
9
10
# File 'lib/invoice/invoice.rb', line 8

def tax
  @tax
end

Instance Method Details

#discount_amountObject

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

#saveObject

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

#subtotalObject

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_amountObject

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

#totalObject

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_dueObject

The total amount due (total - paid)



72
73
74
# File 'lib/invoice/invoice.rb', line 72

def total_due
  total - paid
end