Class: Xeroizer::Record::CreditNote

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

Constant Summary collapse

CREDIT_NOTE_STATUS =
{
  'AUTHORISED' =>       'Approved credit_notes awaiting payment',
  'DELETED' =>          'Draft credit_notes that are deleted',
  'DRAFT' =>            'CreditNotes saved as draft or entered via API',
  'PAID' =>             'CreditNotes approved and fully paid',
  'SUBMITTED' =>        'CreditNotes entered by an employee awaiting approval',
  'VOIDED' =>           'Approved credit_notes that are voided'
}
CREDIT_NOTE_STATUSES =
CREDIT_NOTE_STATUS.keys.sort
CREDIT_NOTE_TYPE =
{
  'ACCRECCREDIT' =>           'Accounts Receivable',
  'ACCPAYCREDIT' =>           'Accounts Payable'
}
CREDIT_NOTE_TYPES =
CREDIT_NOTE_TYPE.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

#contact_idObject

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



85
86
87
# File 'lib/xeroizer/models/credit_note.rb', line 85

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

#contact_nameObject

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



79
80
81
# File 'lib/xeroizer/models/credit_note.rb', line 79

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

#currency_rateObject



89
90
91
92
93
94
95
# File 'lib/xeroizer/models/credit_note.rb', line 89

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

#pdf(filename = nil) ⇒ Object

Retrieve the PDF version of this credit note.

Parameters:

  • filename (String) (defaults to: nil)

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



135
136
137
# File 'lib/xeroizer/models/credit_note.rb', line 135

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

#sub_total(always_summary = false) ⇒ Object

Calculate sub_total from line_items.



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xeroizer/models/credit_note.rb', line 103

def sub_total(always_summary = false)
  if !always_summary && (new_record? || (!new_record? && line_items && line_items.size > 0))
    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=(value) ⇒ Object

Swallow assignment of attributes that should only be calculated automatically.



98
# File 'lib/xeroizer/models/credit_note.rb', line 98

def sub_total=(value);  raise SettingTotalDirectlyNotSupported.new(:sub_total);   end

#total(always_summary = false) ⇒ Object

Calculate the total from line_items.



125
126
127
128
129
130
131
# File 'lib/xeroizer/models/credit_note.rb', line 125

def total(always_summary = false)
  unless always_summary
    sub_total + total_tax
  else
    attributes[:total]
  end
end

#total=(value) ⇒ Object



100
# File 'lib/xeroizer/models/credit_note.rb', line 100

def total=(value);      raise SettingTotalDirectlyNotSupported.new(:total);       end

#total_tax(always_summary = false) ⇒ Object

Calculate total_tax from line_items.



116
117
118
119
120
121
122
# File 'lib/xeroizer/models/credit_note.rb', line 116

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

#total_tax=(value) ⇒ Object



99
# File 'lib/xeroizer/models/credit_note.rb', line 99

def total_tax=(value);  raise SettingTotalDirectlyNotSupported.new(:total_tax);   end