Class: QuickbooksCustomerResolver

Inherits:
Object
  • Object
show all
Includes:
QuickbooksCustomerHelper
Defined in:
app/resolver/quickbooks_customer_resolver.rb

Overview

QuickbooksCustomerResolver handles the creation, updating, and integration of QuickBooks customers with the application’s customer data model. It processes customer data from QuickBooks API and performs the necessary operations to maintain data consistency between systems.

This resolver is responsible for:

  • Finding existing integrated customers by external reference code

  • Finding existing customers by code and name for integration

  • Creating new customers when no match is found

  • Updating existing customers with the latest data from QuickBooks

  • Creating and managing customer hierarchy (head office, distribution center)

  • Adding integration references to track the connection between systems

Examples:

resolver = QuickbooksCustomerResolver.new(
  company_id: 123,
  tenant_id: 456,
  suffix_code: 'QB',
  default_province_id: 'ON',
  quickbooks_customer: qb_customer_data
)
result = resolver.resolve
# => { action: 'create', customer: #<Customer id: 789, ...> }

Defined Under Namespace

Classes: Actions

Constant Summary collapse

USER_NAME =
'SYSTEM'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QuickbooksCustomerHelper

#check_and_create_member_customer, #check_and_set_customer_hierarchy, #external_reference_code, #extract_shipping_address_from_quickbooks_customer, #find_currency_id, #find_payment_term_id, #find_type, #format_known_as, #formatted_code

Methods included from CustomerHierarchyManagement

#assign_customer_hierarchy, #billing_shipping_addresses_match?, #create_dc_customer_for_shipping_address, #create_member_customer_for_shipping_address, #determine_hierarchy_attributes, #find_customer_with_same_address, #shipping_address_valid?

Methods included from AddressHelper

#canadian_postal_code?, #clean_city, #normalize_postal_code, #parse_address_line, #parse_address_parts, #us_zip_code?, #valid_city?, #valid_state?

Constructor Details

#initialize(company_id:, tenant_id:, suffix_code:, default_province_id:, quickbooks_customer:) ⇒ QuickbooksCustomerResolver

Initializes a new QuickbooksCustomerResolver

Parameters:

  • company_id (Integer)

    ID of the company in the application

  • tenant_id (Integer)

    ID of the tenant in the application

  • suffix_code (String)

    Suffix code to use for customer codes (e.g., ‘QB’)

  • default_province_id (String)

    Default province ID to use if none is provided in customer data

  • quickbooks_customer (Hash)

    Customer data from QuickBooks API



48
49
50
51
52
53
54
55
# File 'app/resolver/quickbooks_customer_resolver.rb', line 48

def initialize(company_id:, tenant_id:, suffix_code:, default_province_id:, quickbooks_customer:)
  @company_id = company_id
  @tenant_id = tenant_id
  @suffix_code = suffix_code
  @default_province_id = default_province_id
  @quickbooks_customer = quickbooks_customer.with_indifferent_access
  @customer_service = CustomerService.new(USER_NAME)
end

Instance Attribute Details

#company_idObject (readonly)

Returns the value of attribute company_id.



38
39
40
# File 'app/resolver/quickbooks_customer_resolver.rb', line 38

def company_id
  @company_id
end

#default_province_idObject (readonly)

Returns the value of attribute default_province_id.



38
39
40
# File 'app/resolver/quickbooks_customer_resolver.rb', line 38

def default_province_id
  @default_province_id
end

#quickbooks_customerObject (readonly)

Returns the value of attribute quickbooks_customer.



38
39
40
# File 'app/resolver/quickbooks_customer_resolver.rb', line 38

def quickbooks_customer
  @quickbooks_customer
end

#suffix_codeObject (readonly)

Returns the value of attribute suffix_code.



38
39
40
# File 'app/resolver/quickbooks_customer_resolver.rb', line 38

def suffix_code
  @suffix_code
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



38
39
40
# File 'app/resolver/quickbooks_customer_resolver.rb', line 38

def tenant_id
  @tenant_id
end

Instance Method Details

#resolveHash

Processes the QuickBooks customer data and performs the necessary actions to integrate it with the application’s customer data model

Returns:

  • (Hash)

    A result hash containing the action performed and the customer

Raises:

  • (StandardError)

    If an error occurs during processing



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/resolver/quickbooks_customer_resolver.rb', line 62

def resolve
  ActiveRecord::Base.transaction do
    if find_integrated_customer
      update_customer
      check_and_create_dc_customer_if_needed
      { action: Actions::UPDATE, customer: @customer }
    elsif find_customer_to_integrate
      integrate_customer
      update_customer
      check_and_create_dc_customer_if_needed
      { action: Actions::INTEGRATE, customer: @customer }
    elsif create_customer
      integrate_customer
      check_and_create_dc_customer_if_needed
      { action: Actions::CREATE, customer: @customer }
    end
  end
rescue StandardError => e
  Rails.logger.error "QuickBooks Customer resolve failed: #{e.message}"
  raise e
end