class XeroGateway::ManualJournal

Constants

GUID_REGEX
STATUSES

Attributes

date[RW]

accessible fields

errors[R]

Any errors that occurred when the valid? method called.

gateway[RW]

Xero::Gateway associated with this invoice.

journal_lines[W]
journal_lines_downloaded[RW]

Represents whether the journal lines have been downloaded when getting from GET /API.XRO/2.0/ManualJournals

manual_journal_id[RW]

accessible fields

narration[RW]

accessible fields

show_on_cash_basis_reports[RW]

accessible fields

status[RW]

accessible fields

url[RW]

accessible fields

Public Class Methods

from_xml(manual_journal_element, gateway = nil, options = {}) click to toggle source
# File lib/xero_gateway/manual_journal.rb, line 140
def self.from_xml(manual_journal_element, gateway = nil, options = {})
  manual_journal = ManualJournal.new(options.merge({:gateway => gateway}))
  manual_journal_element.children.each do |element|
    case(element.name)
      when "ManualJournalID" then manual_journal.manual_journal_id = element.text
      when "Date" then manual_journal.date = parse_date(element.text)
      when "Status" then manual_journal.status = element.text
      when "Narration" then manual_journal.narration = element.text
      when "JournalLines" then element.children.each {|journal_line| manual_journal.journal_lines_downloaded = true; manual_journal.journal_lines << JournalLine.from_xml(journal_line) }
      when "Url" then manual_journal.url = element.text
    end
  end
  manual_journal
end
new(params = {}) click to toggle source
# File lib/xero_gateway/manual_journal.rb, line 30
def initialize(params = {})
  @errors ||= []
  @payments ||= []

  # Check if the line items have been downloaded.
  @journal_lines_downloaded = (params.delete(:journal_lines_downloaded) == true)

  params.each do |k,v|
    self.send("#{k}=", v)
  end

  @journal_lines ||= []
end

Public Instance Methods

==(other) click to toggle source
# File lib/xero_gateway/manual_journal.rb, line 44
def ==(other)
  ['narration', 'status', 'journal_lines', 'show_on_cash_basis_reports'].each do |field|
    return false if send(field) != other.send(field)
  end

  ["date"].each do |field|
    return false if send(field).to_s != other.send(field).to_s
  end
  return true
end
add_journal_line(params = {}) click to toggle source
# File lib/xero_gateway/manual_journal.rb, line 155
def add_journal_line(params = {})
  journal_line = nil
  case params
    when Hash                        then  journal_line = JournalLine.new(params)
    when JournalLine then  journal_line = params
    else             raise InvalidLineItemError
  end
  @journal_lines << journal_line
  journal_line
end
journal_lines() click to toggle source

If line items are not downloaded, then attempt a download now (if this record was found to begin with).

# File lib/xero_gateway/manual_journal.rb, line 103
def journal_lines
  if journal_lines_downloaded?
    @journal_lines

  elsif manual_journal_id =~ GUID_REGEX && @gateway
    # There is a manual_journal_id so we can assume this record was loaded from Xero.
    # Let's attempt to download the journal_line records (if there is a gateway)

    response = @gateway.get_manual_journal(manual_journal_id)
    raise ManualJournalNotFoundError, "Manual Journal with ID #{manual_journal_id} not found in Xero." unless response.success? && response.manual_journal.is_a?(XeroGateway::ManualJournal)

    @journal_lines = response.manual_journal.journal_lines
    @journal_lines_downloaded = true

    @journal_lines

  # Otherwise, this is a new manual journal, so return the journal_lines reference.
  else
    @journal_lines
  end
end
journal_lines_downloaded?() click to toggle source
# File lib/xero_gateway/manual_journal.rb, line 98
def journal_lines_downloaded?
  @journal_lines_downloaded
end
to_xml(b = Builder::XmlMarkup.new) click to toggle source
# File lib/xero_gateway/manual_journal.rb, line 125
def to_xml(b = Builder::XmlMarkup.new)
  b.ManualJournal {
    b.ManualJournalID manual_journal_id if manual_journal_id
    b.Narration narration
    b.JournalLines {
      self.journal_lines.each do |journal_line|
        journal_line.to_xml(b)
      end
    }
    b.Date ManualJournal.format_date(date || Date.today)
    b.Status status if status
    b.Url url if url
  }
end
valid?() click to toggle source

Validate the ManualJournal record according to what will be valid by the gateway.

Usage:

manual_journal.valid?     # Returns true/false

Additionally sets manual_journal.errors array to an array of field/error.
# File lib/xero_gateway/manual_journal.rb, line 61
def valid?
  @errors = []

  if !manual_journal_id.nil? && manual_journal_id !~ GUID_REGEX
    @errors << ['manual_journal_id', 'must be blank or a valid Xero GUID']
  end

  if narration.blank?
    @errors << ['narration', "can't be blank"]
  end

  unless date
    @errors << ['date', "can't be blank"]
  end

  # Make sure all journal_items are valid.
  unless journal_lines.all? { | journal_line | journal_line.valid? }
    @errors << ['journal_lines', "at least one journal line invalid"]
  end

  # make sure there are at least 2 journal lines
  unless journal_lines.length > 1
    @errors << ['journal_lines', "journal must contain at least two individual journal lines"]
  end

  if journal_lines.length > 100
    @errors << ['journal_lines', "journal must contain less than one hundred journal lines"]
  end

  unless journal_lines.sum(&:line_amount).to_f == 0.0
    @errors << ['journal_lines', "the total debits must be equal to total credits"]
  end

  @errors.size == 0
end