class XeroGateway::BankTransaction

Constants

GUID_REGEX
STATUSES
TYPES

Attributes

bank_account[RW]
bank_transaction_id[RW]
contact[RW]
currency_code[RW]
currency_rate[RW]
date[RW]
errors[R]

Any errors that occurred when the valid? method called.

gateway[RW]

Xero::Gateway associated with this invoice.

is_reconciled[RW]
line_items[W]

accessible fields

line_items_downloaded[RW]

Represents whether the line_items have been downloaded when getting from GET /API.XRO/2.0/BankTransactions

reference[RW]
status[RW]
type[RW]
updated_at[RW]
url[RW]

Public Class Methods

from_xml(bank_transaction_element, gateway = nil, options = {}) click to toggle source
# File lib/xero_gateway/bank_transaction.rb, line 152
def self.from_xml(bank_transaction_element, gateway = nil, options = {})
  bank_transaction = BankTransaction.new(options.merge({:gateway => gateway}))
  bank_transaction_element.children.each do |element|
    case(element.name)
      when "BankTransactionID" then bank_transaction.bank_transaction_id = element.text
      when "UpdatedDateUTC" then bank_transaction.updated_at = parse_date_time(element.text)
      when "Type" then bank_transaction.type = element.text
      when "CurrencyCode" then bank_transaction.currency_code = element.text
      when "CurrencyRate" then bank_transaction.currency_rate = BigDecimal(element.text)
      when "Contact" then bank_transaction.contact = Contact.from_xml(element)
      when "BankAccount" then bank_transaction.bank_account = Account.from_xml(element)
      when "Date" then bank_transaction.date = parse_date(element.text)
      when "Status" then bank_transaction.status = element.text
      when "Reference" then bank_transaction.reference = element.text
      when "LineItems" then element.children.each {|line_item| bank_transaction.line_items_downloaded = true; bank_transaction.line_items << LineItem.from_xml(line_item) }
      when "Total" then bank_transaction.total = BigDecimal(element.text)
      when "SubTotal" then bank_transaction.sub_total = BigDecimal(element.text)
      when "TotalTax" then bank_transaction.total_tax = BigDecimal(element.text)
      when "IsReconciled" then bank_transaction.is_reconciled = (element.text.strip.downcase == "true")
      when "Url" then bank_transaction.url = element.text
    end
  end
  bank_transaction
end
new(params = {}) click to toggle source
# File lib/xero_gateway/bank_transaction.rb, line 31
def initialize(params = {})
  @errors ||= []
  @payments ||= []

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

  # params = {
  #   :line_amount_types => "Exclusive"
  # }.merge(params)
  params.each do |k,v|
    self.send("#{k}=", v)
  end

  @line_items ||= []
end

Public Instance Methods

==(other) click to toggle source
# File lib/xero_gateway/bank_transaction.rb, line 48
def ==(other)
  ['type', 'reference', 'status', 'contact', 'line_items', 'bank_account'].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
line_items() 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/bank_transaction.rb, line 109
def line_items
  if line_items_downloaded?
    @line_items

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

    response = @gateway.get_bank_transaction(bank_transaction_id)
    raise BankTransactionNotFoundError, "Bank Transaction with ID #{bank_transaction_id} not found in Xero." unless response.success? && response.bank_transaction.is_a?(XeroGateway::BankTransaction)

    @line_items = response.bank_transaction.line_items
    @line_items_downloaded = true

    @line_items

  # Otherwise, this is a new bank transaction, so return the line_items reference.
  else
    @line_items
  end
end
line_items_downloaded?() click to toggle source
# File lib/xero_gateway/bank_transaction.rb, line 98
def line_items_downloaded?
  @line_items_downloaded
end
to_xml(b = Builder::XmlMarkup.new) click to toggle source
# File lib/xero_gateway/bank_transaction.rb, line 131
def to_xml(b = Builder::XmlMarkup.new)
  b.BankTransaction {
    b.BankTransactionID bank_transaction_id if bank_transaction_id
    b.Type type
    b.CurrencyCode currency_code if currency_code
    b.CurrencyRate currency_rate if currency_rate
    contact.to_xml(b) if contact
    bank_account.to_xml(b, :name => 'BankAccount') if bank_account
    b.Date BankTransaction.format_date(date || Date.today)
    b.Status status if status
    b.Reference reference if reference
    b.IsReconciled true if self.is_reconciled
    b.LineItems {
      self.line_items.each do |line_item|
        line_item.to_xml(b)
      end
    }
    b.Url url if url
  }
end
valid?() click to toggle source

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

Usage:

bank_transaction.valid?     # Returns true/false

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

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

  if type && !TYPES[type]
    @errors << ['type', "must be one of #{TYPES.keys.join('/')}"]
  end

  if status && !STATUSES[status]
    @errors << ['status', "must be one of #{STATUSES.keys.join('/')}"]
  end

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

  # Make sure contact is valid.
  unless @contact && @contact.valid?
    @errors << ['contact', 'is invalid']
  end

  # Make sure all line_items are valid.
  unless line_items.all? { | line_item | line_item.valid? }
    @errors << ['line_items', "at least one line item invalid"]
  end

  @errors.size == 0
end