Class: Customer::SyncAddressWithTaxjar

Inherits:
BaseService show all
Defined in:
app/services/customer/sync_address_with_taxjar.rb

Overview

Service object: sync address with taxjar.

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #process, #tagged_logger

Constructor Details

#initialize(customer, address, options = {}) ⇒ SyncAddressWithTaxjar

Returns a new instance of SyncAddressWithTaxjar.

Parameters:

  • customer (Customer)

    the customer whose address is synced

  • address (Address)

    the address to sync

  • options (Hash) (defaults to: {})

    sync options

Options Hash (options):

  • effective_date (Date)

    exemption effective date (defaults to today)

  • logger (Logger)

    logger to use (defaults to Rails.logger)



16
17
18
19
20
21
22
# File 'app/services/customer/sync_address_with_taxjar.rb', line 16

def initialize(customer, address, options = {})
  @customer = customer
  @address = address
  @options = options
  @logger = options[:logger] || Rails.logger
  @client = Api::Taxjar.client
end

Instance Method Details

#createObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/customer/sync_address_with_taxjar.rb', line 36

def create
  if existing_record?
    msg = "Record already exists on Taxjar for customer #{taxjar_id}"
    @logger.info msg
    Result.new(
      success: false,
      message: msg
    )
  else
    new_record = @client.create_customer(details)
    Result.new(
      success: true,
      exempt_regions: new_record.exempt_regions
    )
  end
end

#deleteObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/services/customer/sync_address_with_taxjar.rb', line 81

def delete
  @client.show_customer(taxjar_id)
  @client.delete_customer(taxjar_id)
  Result.new(
    success: true,
    message: "Record on Taxjar for customer #{taxjar_id} successfully deleted"
  )
rescue Api::Taxjar::Error::NotFound
  msg = "Record not found on Taxjar for customer #{taxjar_id}"
  @logger.info msg
  Result.new(
    success: false,
    message: msg
  )
end

#detailsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/services/customer/sync_address_with_taxjar.rb', line 97

def details
  {
    customer_id: taxjar_id,
    exemption_type: 'wholesale',
    name: "#{@address.company_name_override} #{@address.person_name_override} (#{@customer.name})",
    exempt_regions: [{ state: @address.state_code, country: @address.country.iso }],
    country: @customer.billing_address.country.iso,
    state: @customer.billing_address.state_code,
    zip: @customer.billing_address.zip,
    city: @customer.billing_address.city,
    street: @customer.billing_address.street1
  }
end

#effective_dateObject



24
25
26
# File 'app/services/customer/sync_address_with_taxjar.rb', line 24

def effective_date
  @options[:effective_date] || Date.current
end

#existing_record?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'app/services/customer/sync_address_with_taxjar.rb', line 57

def existing_record?
  # check it already exists
  existing_records = @client.list_customers
  existing_records.include?(taxjar_id)
end

#syncObject



28
29
30
31
32
33
34
# File 'app/services/customer/sync_address_with_taxjar.rb', line 28

def sync
  if existing_record?
    update
  else
    create
  end
end

#taxjar_idObject



53
54
55
# File 'app/services/customer/sync_address_with_taxjar.rb', line 53

def taxjar_id
  "C#{@customer.id}-A#{@address.id}"
end

#updateObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/services/customer/sync_address_with_taxjar.rb', line 63

def update
  @client.show_customer(taxjar_id)

  updated_record = @client.update_customer(details)

  Result.new(
    success: true,
    exempt_regions: updated_record.exempt_regions
  )
rescue Api::Taxjar::Error::NotFound
  msg = "Record not found on Taxjar for customer #{taxjar_id}"
  @logger.info msg
  Result.new(
    success: false,
    message: msg
  )
end