Class: XeroGateway::JournalLine

Inherits:
Object
  • Object
show all
Includes:
Money
Defined in:
lib/xero_gateway/journal_line.rb

Constant Summary collapse

TAX_TYPE =
Account::TAX_TYPE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Money

included

Constructor Details

#initialize(params = {}) ⇒ JournalLine

Returns a new instance of JournalLine.



15
16
17
18
19
20
21
22
# File 'lib/xero_gateway/journal_line.rb', line 15

def initialize(params = {})
  @errors ||= []
  @tracking ||= []
  
  params.each do |k,v|
    self.send("#{k}=", v)
  end
end

Instance Attribute Details

#account_codeObject

All accessible fields



13
14
15
# File 'lib/xero_gateway/journal_line.rb', line 13

def 
  @account_code
end

#descriptionObject

All accessible fields



13
14
15
# File 'lib/xero_gateway/journal_line.rb', line 13

def description
  @description
end

#errorsObject (readonly)

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



10
11
12
# File 'lib/xero_gateway/journal_line.rb', line 10

def errors
  @errors
end

#journal_line_idObject

All accessible fields



13
14
15
# File 'lib/xero_gateway/journal_line.rb', line 13

def journal_line_id
  @journal_line_id
end

#line_amountObject

All accessible fields



13
14
15
# File 'lib/xero_gateway/journal_line.rb', line 13

def line_amount
  @line_amount
end

#tax_typeObject

All accessible fields



13
14
15
# File 'lib/xero_gateway/journal_line.rb', line 13

def tax_type
  @tax_type
end

#trackingObject

All accessible fields



13
14
15
# File 'lib/xero_gateway/journal_line.rb', line 13

def tracking
  @tracking
end

Class Method Details

.from_xml(journal_line_element) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/xero_gateway/journal_line.rb', line 77

def self.from_xml(journal_line_element)
  journal_line = JournalLine.new
  journal_line_element.children.each do |element|
    case(element.name)
      when "LineAmount" then journal_line.line_amount = BigDecimal.new(element.text)
      when "AccountCode" then journal_line. = element.text
      when "JournalLineID" then journal_line.journal_line_id = element.text
      when "Description" then journal_line.description = element.text
      when "TaxType" then journal_line.tax_type = element.text
      when "Tracking" then
        element.children.each do | tracking_element |
          journal_line.tracking << TrackingCategory.from_xml(tracking_element)
        end
    end
  end
  journal_line
end

Instance Method Details

#==(other) ⇒ Object



95
96
97
98
99
100
# File 'lib/xero_gateway/journal_line.rb', line 95

def ==(other)
  [:description, :line_amount, :account_code, :tax_type].each do |field|
    return false if send(field) != other.send(field)
  end
  return true
end

#has_tracking?Boolean

Returns:

  • (Boolean)


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

def has_tracking?
  return false if tracking.nil?
  
  if tracking.is_a?(Array)
    return tracking.any?
  else
    return tracking.is_a?(TrackingCategory)
  end
end

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/xero_gateway/journal_line.rb', line 58

def to_xml(b = Builder::XmlMarkup.new)
  b.JournalLine {
    b.LineAmount line_amount # mandatory
    b.AccountCode  # mandatory
    b.Description description if description # optional
    b.TaxType tax_type if tax_type # optional
    if has_tracking?
      b.Tracking { # optional
        # Due to strange retardness in the Xero API, the XML structure for a tracking category within
        # an invoice is different to a standalone tracking category.
        # This means rather than going category.to_xml we need to call the special category.to_xml_for_invoice_messages
        (tracking.is_a?(TrackingCategory) ? [tracking] : tracking).each do |category|
          category.to_xml_for_invoice_messages(b)
        end
      }
    end
  }
end

#valid?Boolean

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

Usage:

journal_line_item.valid?     # Returns true/false

Additionally sets journal_line_item.errors array to an array of field/error.

Returns:

  • (Boolean)


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

def valid?
  @errors = []
  
  if !journal_line_id.nil? && journal_line_id !~ GUID_REGEX
    @errors << ['journal_line_id', 'must be blank or a valid Xero GUID']
  end
  
  unless line_amount
    @errors << ['line_amount', "can't be blank"]
  end

  unless 
    @errors << ['account_code', "can't be blank"]
  end
  
  @errors.size == 0
end