class Xeroizer::Record::CreditNote

Constants

CREDIT_NOTE_STATUS
CREDIT_NOTE_STATUSES
CREDIT_NOTE_TYPE
CREDIT_NOTE_TYPES

Public Instance Methods

allocate() click to toggle source
# File lib/xeroizer/models/credit_note.rb, line 149
def allocate
  if self.class.possible_primary_keys && self.class.possible_primary_keys.all? { | possible_key | self[possible_key].nil? }
    raise RecordKeyMustBeDefined.new(self.class.possible_primary_keys)
  end

  request = association_to_xml(:allocations)
  allocations_url = "#{parent.url}/#{CGI.escape(id)}/Allocations"

  log "[ALLOCATION SENT] (#{__FILE__}:#{__LINE__}) \r\n#{request}"
  response = parent.application.http_put(parent.application.client, allocations_url, request)
  log "[ALLOCATION RECEIVED] (#{__FILE__}:#{__LINE__}) \r\n#{response}"
  parse_save_response(response)
end
contact_id() click to toggle source

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

# File lib/xeroizer/models/credit_note.rb, line 92
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 credit note.

# File lib/xeroizer/models/credit_note.rb, line 86
def contact_name
  attributes[:contact] && attributes[:contact][:name]
end
pdf(filename = nil) click to toggle source

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

# File lib/xeroizer/models/credit_note.rb, line 134
def pdf(filename = nil)
  parent.pdf(id, filename)
end
save() click to toggle source
Calls superclass method
# File lib/xeroizer/models/credit_note.rb, line 138
def save
  # Calling parse_save_response() on the credit note will wipe out
  # the allocations, so we have to manually preserve them.
  allocations_backup = self.allocations
  if super
    self.allocations = allocations_backup
    allocate unless self.allocations.empty?
    true
  end
end
sub_total(always_summary = false) click to toggle source

Calculate sub_total from line_items.

# File lib/xeroizer/models/credit_note.rb, line 102
def sub_total(always_summary = false)
  if !always_summary && (new_record? || (!new_record? && line_items && line_items.size > 0))
    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=(value) click to toggle source

Swallow assignment of attributes that should only be calculated automatically.

# File lib/xeroizer/models/credit_note.rb, line 97
def sub_total=(value);  raise SettingTotalDirectlyNotSupported.new(:sub_total);   end
total(always_summary = false) click to toggle source

Calculate the total from line_items.

# File lib/xeroizer/models/credit_note.rb, line 124
def total(always_summary = false)
  unless always_summary
    sub_total + total_tax
  else
    attributes[:total]
  end
end
total=(value) click to toggle source
# File lib/xeroizer/models/credit_note.rb, line 99
def total=(value);      raise SettingTotalDirectlyNotSupported.new(:total);       end
total_tax(always_summary = false) click to toggle source

Calculate total_tax from line_items.

# File lib/xeroizer/models/credit_note.rb, line 115
def total_tax(always_summary = false)
  if !always_summary && (new_record? || (!new_record? && line_items && line_items.size > 0))
    (line_items || []).inject(BigDecimal('0')) { | sum, line_item | sum + line_item.tax_amount }
  else
    attributes[:total_tax]
  end
end
total_tax=(value) click to toggle source
# File lib/xeroizer/models/credit_note.rb, line 98
def total_tax=(value);  raise SettingTotalDirectlyNotSupported.new(:total_tax);   end