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, #sub_total=, #total, #total=, #total_tax, #total_tax=

Methods included from Dates

included

Constructor Details

#initialize(params = {}) ⇒ BankTransaction

Returns a new instance of BankTransaction.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xero_gateway/bank_transaction.rb', line 30

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

accessible fields



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

def 
  @bank_account
end

#bank_transaction_idObject

accessible fields



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

def bank_transaction_id
  @bank_transaction_id
end

#contactObject

accessible fields



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

def contact
  @contact
end

#dateObject

accessible fields



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

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

accessible fields



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

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).



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

def line_items
  @line_items
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

accessible fields



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

def reference
  @reference
end

#statusObject

accessible fields



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

def status
  @status
end

#typeObject

accessible fields



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

def type
  @type
end

#urlObject

accessible fields



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

def url
  @url
end

Class Method Details

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/xero_gateway/bank_transaction.rb', line 144

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 "Type" then bank_transaction.type = element.text
      # when "CurrencyCode" then invoice.currency_code = 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 "SubTotal" then invoice.sub_total = BigDecimal.new(element.text)
      # when "TotalTax" then invoice.total_tax = BigDecimal.new(element.text)
      # when "Total" then invoice.total = BigDecimal.new(element.text)
      # when "InvoiceID" then invoice.invoice_id = element.text
      # when "InvoiceNumber" then invoice.invoice_number = element.text
      # when "Payments" then element.children.each { | payment | invoice.payments << Payment.from_xml(payment) }
      # when "AmountDue" then invoice.amount_due = BigDecimal.new(element.text)
      # when "AmountPaid" then invoice.amount_paid = BigDecimal.new(element.text)
      # when "AmountCredited" then invoice.amount_credited = BigDecimal.new(element.text)
      # when "SentToContact" then invoice.sent_to_contact = (element.text.strip.downcase == "true")
      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



47
48
49
50
51
52
53
54
55
56
# File 'lib/xero_gateway/bank_transaction.rb', line 47

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)


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

def line_items_downloaded?
  @line_items_downloaded
end

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/xero_gateway/bank_transaction.rb', line 124

def to_xml(b = Builder::XmlMarkup.new)
  b.BankTransaction {
    b.BankTransactionID bank_transaction_id if bank_transaction_id
    b.Type type
    # b.CurrencyCode self.currency_code if self.currency_code
    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)


64
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
# File 'lib/xero_gateway/bank_transaction.rb', line 64

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