Class: XeroKiwi::Accounting::Prepayment

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

Overview

Represents a Xero Prepayment returned by the Accounting API.

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

Constant Summary collapse

ATTRIBUTES =
{
  prepayment_id:      "PrepaymentID",
  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",
  invoice_number:     "InvoiceNumber",
  remaining_credit:   "RemainingCredit",
  allocations:        "Allocations",
  payments:           "Payments",
  has_attachments:    "HasAttachments",
  fully_paid_on_date: "FullyPaidOnDate"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, reference: false) ⇒ Prepayment

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



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

def initialize(attrs, reference: false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  attrs               = attrs.transform_keys(&:to_s)
  @is_reference       = reference
  @prepayment_id      = attrs["PrepaymentID"]
  @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"]
  @invoice_number     = attrs["InvoiceNumber"]
  @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"]
  @fully_paid_on_date = parse_time(attrs["FullyPaidOnDate"])
end

Class Method Details

.from_response(payload) ⇒ Object



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

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

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

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

Instance Method Details

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



78
79
80
# File 'lib/xero_kiwi/accounting/prepayment.rb', line 78

def ==(other)
  other.is_a?(Prepayment) && other.prepayment_id == prepayment_id
end

#hashObject



83
# File 'lib/xero_kiwi/accounting/prepayment.rb', line 83

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

#inspectObject



85
86
87
88
# File 'lib/xero_kiwi/accounting/prepayment.rb', line 85

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

#receive?Boolean

Returns:

  • (Boolean)


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

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

#reference?Boolean

Returns:

  • (Boolean)


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

def reference? = @is_reference

#spend?Boolean

Returns:

  • (Boolean)


72
# File 'lib/xero_kiwi/accounting/prepayment.rb', line 72

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

#to_hObject



74
75
76
# File 'lib/xero_kiwi/accounting/prepayment.rb', line 74

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