class Xeroizer::Record::Invoice

Constants

INVOICE_STATUS
INVOICE_STATUSES
INVOICE_TYPE
INVOICE_TYPES

Public Class Methods

new(parent) click to toggle source
Calls superclass method
# File lib/xeroizer/models/invoice.rb, line 103
def initialize(parent)
  super(parent)
  @sub_total_is_set = false
  @total_tax_is_set = false
  @total_is_set = false
end

Public Instance Methods

accounts_payable?() click to toggle source

Helper method to check if the invoice is accounts payable.

# File lib/xeroizer/models/invoice.rb, line 128
def accounts_payable?
  type == 'ACCPAY'
end
accounts_receivable?() click to toggle source

Helper method to check if the invoice is accounts receivable.

# File lib/xeroizer/models/invoice.rb, line 133
def accounts_receivable?
  type == 'ACCREC'
end
approve!() click to toggle source

Approve a draft invoice

# File lib/xeroizer/models/invoice.rb, line 209
def approve!
  change_status!('AUTHORISED')
end
approved?() click to toggle source

Helper method to check if the invoice has been approved.

# File lib/xeroizer/models/invoice.rb, line 123
def approved?
  [ 'AUTHORISED', 'PAID', 'VOIDED' ].include? status
end
contact_id() click to toggle source

Access the contact ID without forcing a download of an incomplete, summary invoice.

# File lib/xeroizer/models/invoice.rb, line 118
def contact_id
  attributes[:contact] && attributes[:contact][:contact_id]
end
contact_name() click to toggle source

Access the contact name without forcing a download of an incomplete, summary invoice.

# File lib/xeroizer/models/invoice.rb, line 112
def contact_name
  attributes[:contact] && attributes[:contact][:name]
end
delete!() click to toggle source

Delete an approved invoice with no payments.

# File lib/xeroizer/models/invoice.rb, line 199
def delete!
  change_status!('DELETED')
end
email() click to toggle source

Send an email containing the invoice.

# File lib/xeroizer/models/invoice.rb, line 214
def email
  email_url = "#{parent.url}/#{CGI.escape(id)}/Email"
  parent.application.http_post(parent.application.client, email_url, "")
end
loaded_record?() click to toggle source
# File lib/xeroizer/models/invoice.rb, line 187
def loaded_record?
  new_record? ||
    (!new_record? && line_items && line_items.size > 0)
end
not_summary_or_loaded_record(always_summary) click to toggle source
# File lib/xeroizer/models/invoice.rb, line 183
def not_summary_or_loaded_record(always_summary)
  !always_summary && loaded_record?
end
pdf(filename = nil) click to toggle source

Retrieve the PDF version of this invoice. @param [String] filename optional filename to store the PDF in instead of returning the data.

# File lib/xeroizer/models/invoice.rb, line 194
def pdf(filename = nil)
  parent.pdf(id, filename)
end
sub_total(always_summary = false) click to toggle source

Calculate sub_total from line_items.

# File lib/xeroizer/models/invoice.rb, line 153
def sub_total(always_summary = false)
  if !@sub_total_is_set && not_summary_or_loaded_record(always_summary)
    overall_sum = (line_items || []).inject(BigDecimal('0')) { | sum, line_item | sum + line_item.line_amount }

    # If the default amount types are inclusive of 'tax' then remove the tax amount from this sub-total.
    overall_sum -= total_tax if line_amount_types == 'Inclusive'
    overall_sum
  else
    attributes[:sub_total]
  end
end
sub_total=(sub_total) click to toggle source
# File lib/xeroizer/models/invoice.rb, line 137
def sub_total=(sub_total)
  @sub_total_is_set = true
  attributes[:sub_total] = sub_total
end
total(always_summary = false) click to toggle source

Calculate the total from line_items.

# File lib/xeroizer/models/invoice.rb, line 175
def total(always_summary = false)
  if !@total_is_set && not_summary_or_loaded_record(always_summary)
    sub_total + total_tax
  else
    attributes[:total]
  end
end
total=(total) click to toggle source
# File lib/xeroizer/models/invoice.rb, line 147
def total=(total)
  @total_is_set = true
  attributes[:total] = total
end
total_tax(always_summary = false) click to toggle source

Calculate total_tax from line_items.

# File lib/xeroizer/models/invoice.rb, line 166
def total_tax(always_summary = false)
  if !@total_tax_is_set && not_summary_or_loaded_record(always_summary)
    (line_items || []).inject(BigDecimal('0')) { | sum, line_item | sum + line_item.tax_amount }
  else
    attributes[:total_tax]
  end
end
total_tax=(total_tax) click to toggle source
# File lib/xeroizer/models/invoice.rb, line 142
def total_tax=(total_tax)
  @total_tax_is_set = true
  attributes[:total_tax] = total_tax
end
void!() click to toggle source

Void an approved invoice with no payments.

# File lib/xeroizer/models/invoice.rb, line 204
def void!
  change_status!('VOIDED')
end

Protected Instance Methods

change_status!(new_status) click to toggle source
# File lib/xeroizer/models/invoice.rb, line 221
def change_status!(new_status)
  raise CannotChangeInvoiceStatus.new(self, new_status) unless self.payments.size == 0
  self.status = new_status
  self.save
end