Class: XeroKiwi::Accounting::LineItem

Inherits:
Object
  • Object
show all
Defined in:
lib/xero_kiwi/accounting/line_item.rb

Overview

Represents a line item nested within Xero documents (invoices, credit notes, prepayments, overpayments).

See: developer.xero.com/documentation/api/accounting/invoices

Constant Summary collapse

ATTRIBUTES =
{
  line_item_id:    "LineItemID",
  description:     "Description",
  quantity:        "Quantity",
  unit_amount:     "UnitAmount",
  item_code:       "ItemCode",
  account_code:    "AccountCode",
  account_id:      "AccountId",
  tax_type:        "TaxType",
  tax_amount:      "TaxAmount",
  line_amount:     "LineAmount",
  discount_rate:   "DiscountRate",
  discount_amount: "DiscountAmount",
  tracking:        "Tracking",
  item:            "Item"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ LineItem

rubocop:disable Metrics/AbcSize



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

def initialize(attrs) # rubocop:disable Metrics/AbcSize
  attrs            = attrs.transform_keys(&:to_s)
  @line_item_id    = attrs["LineItemID"]
  @description     = attrs["Description"]
  @quantity        = attrs["Quantity"]
  @unit_amount     = attrs["UnitAmount"]
  @item_code       = attrs["ItemCode"]
  @account_code    = attrs["AccountCode"]
  @account_id      = attrs["AccountId"]
  @tax_type        = attrs["TaxType"]
  @tax_amount      = attrs["TaxAmount"]
  @line_amount     = attrs["LineAmount"]
  @discount_rate   = attrs["DiscountRate"]
  @discount_amount = attrs["DiscountAmount"]
  @tracking        = (attrs["Tracking"] || []).map { |t| TrackingCategory.new(t) }
  @item            = attrs["Item"]
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



51
52
53
# File 'lib/xero_kiwi/accounting/line_item.rb', line 51

def ==(other)
  other.is_a?(LineItem) && to_h == other.to_h
end

#hashObject



56
# File 'lib/xero_kiwi/accounting/line_item.rb', line 56

def hash = to_h.hash

#inspectObject



58
59
60
61
# File 'lib/xero_kiwi/accounting/line_item.rb', line 58

def inspect
  "#<#{self.class} description=#{description.inspect} " \
    "line_amount=#{line_amount.inspect}>"
end

#to_hObject



47
48
49
# File 'lib/xero_kiwi/accounting/line_item.rb', line 47

def to_h
  ATTRIBUTES.keys.to_h { |key| [key, public_send(key)] }
end