Class: XeroKiwi::Accounting::Contact

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

Overview

Represents a Xero Contact returned by the Accounting API.

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

Constant Summary collapse

ATTRIBUTES =
{
  contact_id:                         "ContactID",
  contact_number:                     "ContactNumber",
  account_number:                     "AccountNumber",
  contact_status:                     "ContactStatus",
  name:                               "Name",
  first_name:                         "FirstName",
  last_name:                          "LastName",
  email_address:                      "EmailAddress",
  bank_account_details:               "BankAccountDetails",
  company_number:                     "CompanyNumber",
  tax_number:                         "TaxNumber",
  tax_number_type:                    "TaxNumberType",
  accounts_receivable_tax_type:       "AccountsReceivableTaxType",
  accounts_payable_tax_type:          "AccountsPayableTaxType",
  addresses:                          "Addresses",
  phones:                             "Phones",
  is_supplier:                        "IsSupplier",
  is_customer:                        "IsCustomer",
  default_currency:                   "DefaultCurrency",
  updated_date_utc:                   "UpdatedDateUTC",
  contact_persons:                    "ContactPersons",
  xero_network_key:                   "XeroNetworkKey",
  merged_to_contact_id:               "MergedToContactID",
  sales_default_account_code:         "SalesDefaultAccountCode",
  purchases_default_account_code:     "PurchasesDefaultAccountCode",
  sales_tracking_categories:          "SalesTrackingCategories",
  purchases_tracking_categories:      "PurchasesTrackingCategories",
  sales_default_line_amount_type:     "SalesDefaultLineAmountType",
  purchases_default_line_amount_type: "PurchasesDefaultLineAmountType",
  tracking_category_name:             "TrackingCategoryName",
  tracking_option_name:               "TrackingOptionName",
  payment_terms:                      "PaymentTerms",
  contact_groups:                     "ContactGroups",
  website:                            "Website",
  branding_theme:                     "BrandingTheme",
  batch_payments:                     "BatchPayments",
  discount:                           "Discount",
  balances:                           "Balances",
  has_attachments:                    "HasAttachments"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, reference: false) ⇒ Contact

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/xero_kiwi/accounting/contact.rb', line 64

def initialize(attrs, reference: false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  attrs                               = attrs.transform_keys(&:to_s)
  @is_reference                       = reference
  @contact_id                         = attrs["ContactID"]
  @contact_number                     = attrs["ContactNumber"]
  @account_number                     = attrs["AccountNumber"]
  @contact_status                     = attrs["ContactStatus"]
  @name                               = attrs["Name"]
  @first_name                         = attrs["FirstName"]
  @last_name                          = attrs["LastName"]
  @email_address                      = attrs["EmailAddress"]
  @bank_account_details               = attrs["BankAccountDetails"]
  @company_number                     = attrs["CompanyNumber"]
  @tax_number                         = attrs["TaxNumber"]
  @tax_number_type                    = attrs["TaxNumberType"]
  @accounts_receivable_tax_type       = attrs["AccountsReceivableTaxType"]
  @accounts_payable_tax_type          = attrs["AccountsPayableTaxType"]
  @addresses                          = (attrs["Addresses"] || []).map { |a| Address.new(a) }
  @phones                             = (attrs["Phones"] || []).map { |p| Phone.new(p) }
  @is_supplier                        = attrs["IsSupplier"]
  @is_customer                        = attrs["IsCustomer"]
  @default_currency                   = attrs["DefaultCurrency"]
  @updated_date_utc                   = parse_time(attrs["UpdatedDateUTC"])
  @contact_persons                    = (attrs["ContactPersons"] || []).map { |cp| ContactPerson.new(cp) }
  @xero_network_key                   = attrs["XeroNetworkKey"]
  @merged_to_contact_id               = attrs["MergedToContactID"]
  @sales_default_account_code         = attrs["SalesDefaultAccountCode"]
  @purchases_default_account_code     = attrs["PurchasesDefaultAccountCode"]
  @sales_tracking_categories          = (attrs["SalesTrackingCategories"] || []).map { |t| TrackingCategory.new(t) }
  @purchases_tracking_categories      = (attrs["PurchasesTrackingCategories"] || []).map { |t| TrackingCategory.new(t) }
  @sales_default_line_amount_type     = attrs["SalesDefaultLineAmountType"]
  @purchases_default_line_amount_type = attrs["PurchasesDefaultLineAmountType"]
  @tracking_category_name             = attrs["TrackingCategoryName"]
  @tracking_option_name               = attrs["TrackingOptionName"]
  @payment_terms                      = PaymentTerms.from_hash(attrs["PaymentTerms"])
  @contact_groups                     = (attrs["ContactGroups"] || []).map { |cg| ContactGroup.new(cg, reference: true) }
  @website                            = attrs["Website"]
  @branding_theme                     = attrs["BrandingTheme"] ? BrandingTheme.new(attrs["BrandingTheme"]) : nil
  @batch_payments                     = attrs["BatchPayments"]
  @discount                           = attrs["Discount"]
  @balances                           = attrs["Balances"]
  @has_attachments                    = attrs["HasAttachments"]
end

Class Method Details

.from_response(payload) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/xero_kiwi/accounting/contact.rb', line 55

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

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

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

Instance Method Details

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



122
123
124
# File 'lib/xero_kiwi/accounting/contact.rb', line 122

def ==(other)
  other.is_a?(Contact) && other.contact_id == contact_id
end

#active?Boolean

Returns:

  • (Boolean)


114
# File 'lib/xero_kiwi/accounting/contact.rb', line 114

def active? = contact_status == "ACTIVE"

#archived?Boolean

Returns:

  • (Boolean)


116
# File 'lib/xero_kiwi/accounting/contact.rb', line 116

def archived? = contact_status == "ARCHIVED"

#customer?Boolean

Returns:

  • (Boolean)


112
# File 'lib/xero_kiwi/accounting/contact.rb', line 112

def customer? = is_customer == true

#hashObject



127
# File 'lib/xero_kiwi/accounting/contact.rb', line 127

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

#inspectObject



129
130
131
132
# File 'lib/xero_kiwi/accounting/contact.rb', line 129

def inspect
  "#<#{self.class} contact_id=#{contact_id.inspect} " \
    "name=#{name.inspect} contact_status=#{contact_status.inspect}>"
end

#reference?Boolean

Returns:

  • (Boolean)


108
# File 'lib/xero_kiwi/accounting/contact.rb', line 108

def reference? = @is_reference

#supplier?Boolean

Returns:

  • (Boolean)


110
# File 'lib/xero_kiwi/accounting/contact.rb', line 110

def supplier? = is_supplier == true

#to_hObject



118
119
120
# File 'lib/xero_kiwi/accounting/contact.rb', line 118

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