Class: Xeroizer::Record::Invoice

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

Constant Summary collapse

INVOICE_TYPE =
{
  'ACCREC' =>           'Accounts Receivable',
  'ACCPAY' =>           'Accounts Payable'
}
INVOICE_TYPES =
INVOICE_TYPE.keys.sort
INVOICE_STATUS =
{
  'AUTHORISED' =>       'Approved invoices awaiting payment',
  'DELETED' =>          'Draft invoices that are deleted',
  'DRAFT' =>            'Invoices saved as draft or entered via API',
  'PAID' =>             'Invoices approved and fully paid',
  'SUBMITTED' =>        'Invoices entered by an employee awaiting approval',
  'VOIDED' =>           'Approved invoices that are voided'
}
INVOICE_STATUSES =
INVOICE_STATUS.keys.sort

Instance Attribute Summary

Attributes inherited from Base

#attributes, #complete_record_downloaded, #errors, #model, #parent

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #as_json, build, #complete_record_downloaded?, #download_complete_record!, #initialize, #inspect, #new_model_class, #new_record?, #non_calculated_attributes, #save, #saved!, #to_h, #to_json, #update_attributes

Methods included from XmlHelper

included

Methods included from ValidationHelper

included

Methods included from RecordAssociationHelper

included

Methods included from ModelDefinitionHelper

included

Methods included from ClassLevelInheritableAttributes

included

Constructor Details

This class inherits a constructor from Xeroizer::Record::Base

Instance Method Details

#accounts_payable?Boolean

Helper method to check if the invoice is accounts payable.

Returns:

  • (Boolean)


105
106
107
# File 'lib/xeroizer/models/invoice.rb', line 105

def accounts_payable?
  type == 'ACCPAY'
end

#accounts_receivable?Boolean

Helper method to check if the invoice is accounts receivable.

Returns:

  • (Boolean)


110
111
112
# File 'lib/xeroizer/models/invoice.rb', line 110

def accounts_receivable?
  type == 'ACCREC'
end

#approve!Object

Approve a draft invoice



198
199
200
# File 'lib/xeroizer/models/invoice.rb', line 198

def approve!
  change_status!('AUTHORISED')
end

#approved?Boolean

Helper method to check if the invoice has been approved.

Returns:

  • (Boolean)


100
101
102
# File 'lib/xeroizer/models/invoice.rb', line 100

def approved?
  [ 'AUTHORISED', 'PAID', 'VOIDED' ].include? status
end

#contact_idObject

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



95
96
97
# File 'lib/xeroizer/models/invoice.rb', line 95

def contact_id
  attributes[:contact] && attributes[:contact][:contact_id]
end

#contact_nameObject

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



89
90
91
# File 'lib/xeroizer/models/invoice.rb', line 89

def contact_name
  attributes[:contact] && attributes[:contact][:name]
end

#currency_rateObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/xeroizer/models/invoice.rb', line 114

def currency_rate
  if attributes[:currency_rate]
    if attributes[:currency_rate].is_a? BigDecimal
      attributes[:currency_rate]
    else
      BigDecimal.new(attributes[:currency_rate])
    end
  else
    BigDecimal.new('1.0')
  end
end

#delete!Object

Delete an approved invoice with no payments.



188
189
190
# File 'lib/xeroizer/models/invoice.rb', line 188

def delete!
  change_status!('DELETED')
end

#loaded_record?Boolean

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/xeroizer/models/invoice.rb', line 176

def loaded_record?
  new_record? ||
    (!new_record? && line_items && line_items.size > 0)
end

#not_summary_or_loaded_record(always_summary) ⇒ Object



172
173
174
# File 'lib/xeroizer/models/invoice.rb', line 172

def not_summary_or_loaded_record(always_summary)
  !always_summary && loaded_record?
end

#pdf(filename = nil) ⇒ Object

Retrieve the PDF version of this invoice.

Parameters:

  • filename (String) (defaults to: nil)

    optional filename to store the PDF in instead of returning the data.



183
184
185
# File 'lib/xeroizer/models/invoice.rb', line 183

def pdf(filename = nil)
  parent.pdf(id, filename)
end

#sub_total(always_summary = false) ⇒ Object

Calculate sub_total from line_items.



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/xeroizer/models/invoice.rb', line 142

def sub_total(always_summary = false)
  if !@sub_total_is_set && not_summary_or_loaded_record(always_summary)
    sum = (line_items || []).inject(BigDecimal.new('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.
    sum -= total_tax if line_amount_types == 'Inclusive'
    sum
  else
    attributes[:sub_total]
  end
end

#sub_total=(sub_total) ⇒ Object



126
127
128
129
# File 'lib/xeroizer/models/invoice.rb', line 126

def sub_total=(sub_total)
  @sub_total_is_set = true
  attributes[:sub_total] = sub_total
end

#total(always_summary = false) ⇒ Object

Calculate the total from line_items.



164
165
166
167
168
169
170
# File 'lib/xeroizer/models/invoice.rb', line 164

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) ⇒ Object



136
137
138
139
# File 'lib/xeroizer/models/invoice.rb', line 136

def total=(total)
  @total_is_set = true
  attributes[:total] = total
end

#total_tax(always_summary = false) ⇒ Object

Calculate total_tax from line_items.



155
156
157
158
159
160
161
# File 'lib/xeroizer/models/invoice.rb', line 155

def total_tax(always_summary = false)
  if !@total_tax_is_set && not_summary_or_loaded_record(always_summary)
    (line_items || []).inject(BigDecimal.new('0')) { | sum, line_item | sum + line_item.tax_amount }
  else
    attributes[:total_tax]
  end
end

#total_tax=(total_tax) ⇒ Object



131
132
133
134
# File 'lib/xeroizer/models/invoice.rb', line 131

def total_tax=(total_tax)
  @total_tax_is_set = true
  attributes[:total_tax] = total_tax
end

#void!Object

Void an approved invoice with no payments.



193
194
195
# File 'lib/xeroizer/models/invoice.rb', line 193

def void!
  change_status!('VOIDED')
end