Class: XeroKiwi::Accounting::Overpayment

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

Overview

Represents a Xero Overpayment returned by the Accounting API.

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

Constant Summary collapse

ATTRIBUTES =
{
  overpayment_id:    "OverpaymentID",
  type:              "Type",
  contact:           "Contact",
  date:              "Date",
  status:            "Status",
  line_amount_types: "LineAmountTypes",
  line_items:        "LineItems",
  sub_total:         "SubTotal",
  total_tax:         "TotalTax",
  total:             "Total",
  updated_date_utc:  "UpdatedDateUTC",
  currency_code:     "CurrencyCode",
  currency_rate:     "CurrencyRate",
  remaining_credit:  "RemainingCredit",
  allocations:       "Allocations",
  payments:          "Payments",
  has_attachments:   "HasAttachments",
  reference:         "Reference"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, reference: false) ⇒ Overpayment

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 43

def initialize(attrs, reference: false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  attrs              = attrs.transform_keys(&:to_s)
  @is_reference      = reference
  @overpayment_id    = attrs["OverpaymentID"]
  @type              = attrs["Type"]
  @contact           = attrs["Contact"] ? Contact.new(attrs["Contact"], reference: true) : nil
  @date              = parse_time(attrs["Date"])
  @status            = attrs["Status"]
  @line_amount_types = attrs["LineAmountTypes"]
  @line_items        = (attrs["LineItems"] || []).map { |li| LineItem.new(li) }
  @sub_total         = attrs["SubTotal"]
  @total_tax         = attrs["TotalTax"]
  @total             = attrs["Total"]
  @updated_date_utc  = parse_time(attrs["UpdatedDateUTC"])
  @currency_code     = attrs["CurrencyCode"]
  @currency_rate     = attrs["CurrencyRate"]
  @remaining_credit  = attrs["RemainingCredit"]
  @allocations       = (attrs["Allocations"] || []).map { |a| Allocation.new(a) }
  @payments          = (attrs["Payments"] || []).map { |p| Payment.new(p, reference: true) }
  @has_attachments   = attrs["HasAttachments"]
  @reference         = attrs["Reference"]
end

Class Method Details

.from_response(payload) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 34

def self.from_response(payload)
  return [] if payload.nil?

  items = payload["Overpayments"]
  return [] if items.nil?

  items.map { |attrs| new(attrs) }
end

Instance Method Details

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



76
77
78
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 76

def ==(other)
  other.is_a?(Overpayment) && other.overpayment_id == overpayment_id
end

#hashObject



81
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 81

def hash = [self.class, overpayment_id].hash

#inspectObject



83
84
85
86
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 83

def inspect
  "#<#{self.class} overpayment_id=#{overpayment_id.inspect} " \
    "type=#{type.inspect} status=#{status.inspect} total=#{total.inspect}>"
end

#receive?Boolean

Returns:

  • (Boolean)


66
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 66

def receive? = type == "RECEIVE-OVERPAYMENT"

#reference?Boolean

Returns:

  • (Boolean)


70
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 70

def reference? = @is_reference

#spend?Boolean

Returns:

  • (Boolean)


68
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 68

def spend? = type == "SPEND-OVERPAYMENT"

#to_hObject



72
73
74
# File 'lib/xero_kiwi/accounting/overpayment.rb', line 72

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