Class: XeroGateway::LineItem

Inherits:
Object
  • Object
show all
Includes:
Money
Defined in:
lib/xero_gateway/line_item.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 = {}) ⇒ LineItem

Returns a new instance of LineItem.



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

def initialize(params = {})
  @errors ||= []
  @tracking ||= []
  @quantity = 1
  @unit_amount = BigDecimal.new('0')
  
  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/line_item.rb', line 13

def 
  @account_code
end

#descriptionObject

All accessible fields



13
14
15
# File 'lib/xero_gateway/line_item.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/line_item.rb', line 10

def errors
  @errors
end

#item_codeObject

All accessible fields



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

def item_code
  @item_code
end

#line_item_idObject

All accessible fields



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

def line_item_id
  @line_item_id
end

#quantityObject

All accessible fields



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

def quantity
  @quantity
end

#tax_amountObject

All accessible fields



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

def tax_amount
  @tax_amount
end

#tax_typeObject

All accessible fields



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

def tax_type
  @tax_type
end

#trackingObject

All accessible fields



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

def tracking
  @tracking
end

#unit_amountObject

All accessible fields



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

def unit_amount
  @unit_amount
end

Class Method Details

.from_xml(line_item_element) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/xero_gateway/line_item.rb', line 96

def self.from_xml(line_item_element)
  line_item = LineItem.new
  line_item_element.children.each do |element|
    case(element.name)
      when "LineItemID" then line_item.line_item_id = element.text
      when "Description" then line_item.description = element.text
      when "Quantity" then line_item.quantity = BigDecimal(element.text)
      when "UnitAmount" then line_item.unit_amount = BigDecimal.new(element.text)
      when "ItemCode" then line_item.item_code = element.text
      when "TaxType" then line_item.tax_type = element.text
      when "TaxAmount" then line_item.tax_amount = BigDecimal.new(element.text)
      when "LineAmount" then line_item.line_amount = BigDecimal.new(element.text)
      when "AccountCode" then line_item. = element.text
      when "Tracking" then
        element.children.each do | tracking_element |
          line_item.tracking << TrackingCategory.from_xml(tracking_element)
        end
    end
  end
  line_item
end

Instance Method Details

#==(other) ⇒ Object



118
119
120
121
122
123
# File 'lib/xero_gateway/line_item.rb', line 118

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

#has_tracking?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
# File 'lib/xero_gateway/line_item.rb', line 50

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

#line_amountObject

Calculate the line_amount as quantity * unit_amount as this value must be correct for the API call to succeed.



69
70
71
# File 'lib/xero_gateway/line_item.rb', line 69

def line_amount
  quantity * unit_amount
end

#line_amount=(value) ⇒ Object

Deprecated (but API for setter remains).

As line_amount must equal quantity * unit_amount for the API call to pass, this is now automatically calculated in the line_amount method.



64
65
# File 'lib/xero_gateway/line_item.rb', line 64

def line_amount=(value)
end

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



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/line_item.rb', line 73

def to_xml(b = Builder::XmlMarkup.new)
  b.LineItem {
    b.Description description
    b.Quantity quantity if quantity
    b.UnitAmount LineItem.format_money(unit_amount)
    b.ItemCode item_code if item_code
    b.TaxType tax_type if tax_type
    b.TaxAmount tax_amount if tax_amount
    b.LineAmount line_amount if line_amount
    b.AccountCode  if 
    if has_tracking?
      b.Tracking {
        # 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 LineItem record according to what will be valid by the gateway.

Usage:

line_item.valid?     # Returns true/false

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

Returns:

  • (Boolean)


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

def valid?
  @errors = []
  
  if !line_item_id.nil? && line_item_id !~ GUID_REGEX
    @errors << ['line_item_id', 'must be blank or a valid Xero GUID']
  end
  
  unless description
    @errors << ['description', "can't be blank"]
  end
        
  if tax_type && !TAX_TYPE[tax_type]
    @errors << ['tax_type', "must be one of #{TAX_TYPE.keys.join('/')}"]
  end
  
  @errors.size == 0
end