Class: XeroGateway::BankTransaction

Inherits:
Object
  • Object
show all
Includes:
Dates, LineItemCalculations
Defined in:
lib/xero_gateway/bank_transaction.rb

Constant Summary collapse

GUID_REGEX =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
TYPES =
{
  'RECEIVE' => 'Receive Bank Transaction',
  'SPEND'   => 'Spend Bank Transaction',
}
STATUSES =
{
  'ACTIVE'  => 'Bank Transaction is active',
  'DELETED' => 'Bank Transaction is deleted',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LineItemCalculations

#add_line_item, #sub_total, #total, #total_tax

Methods included from Dates

included

Constructor Details

#initialize(params = {}) ⇒ BankTransaction

Returns a new instance of BankTransaction.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#bank_accountObject

Returns the value of attribute bank_account.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def 
  @bank_account
end

#bank_transaction_idObject

Returns the value of attribute bank_transaction_id.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def bank_transaction_id
  @bank_transaction_id
end

#contactObject

Returns the value of attribute contact.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def contact
  @contact
end

#currency_codeObject

Returns the value of attribute currency_code.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def currency_code
  @currency_code
end

#currency_rateObject

Returns the value of attribute currency_rate.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def currency_rate
  @currency_rate
end

#dateObject

Returns the value of attribute date.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def date
  @date
end

#errorsObject (readonly)

Any errors that occurred when the #valid? method called.



22
23
24
# File 'lib/xero_gateway/bank_transaction.rb', line 22

def errors
  @errors
end

#gatewayObject

Xero::Gateway associated with this invoice.



19
20
21
# File 'lib/xero_gateway/bank_transaction.rb', line 19

def gateway
  @gateway
end

#is_reconciledObject

Returns the value of attribute is_reconciled.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def is_reconciled
  @is_reconciled
end

#line_itemsObject

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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_downloadedObject

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



25
26
27
# File 'lib/xero_gateway/bank_transaction.rb', line 25

def line_items_downloaded
  @line_items_downloaded
end

#referenceObject

Returns the value of attribute reference.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def reference
  @reference
end

#statusObject

Returns the value of attribute status.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def status
  @status
end

#typeObject

Returns the value of attribute type.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def type
  @type
end

#updated_atObject

Returns the value of attribute updated_at.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def updated_at
  @updated_at
end

#urlObject

Returns the value of attribute url.



29
30
31
# File 'lib/xero_gateway/bank_transaction.rb', line 29

def url
  @url
end

Class Method Details

.from_xml(bank_transaction_element, gateway = nil, options = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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.new(element.text)
      when "Contact" then bank_transaction.contact = Contact.from_xml(element)
      when "BankAccount" then bank_transaction. = 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.new(element.text)
      when "SubTotal" then bank_transaction.sub_total = BigDecimal.new(element.text)
      when "TotalTax" then bank_transaction.total_tax = BigDecimal.new(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

Instance Method Details

#==(other) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# 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_downloaded?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/xero_gateway/bank_transaction.rb', line 98

def line_items_downloaded?
  @line_items_downloaded
end

#to_xml(b = Builder::XmlMarkup.new) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# 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
    .to_xml(b, :name => 'BankAccount') if 
    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?Boolean

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.

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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