Class: QuickbooksInvoiceHeaderResolver

Inherits:
Object
  • Object
show all
Includes:
AddressHelper, QuickbooksCustomerHelper
Defined in:
app/resolver/quickbooks_invoice_header_resolver.rb

Overview

QuickbooksInvoiceHeaderResolver is responsible for processing QuickBooks invoice data and integrating it with the application’s internal invoice data model. This resolver handles the creation, updating, and integration of QuickBooks invoices with corresponding invoice header records in the application.

The resolver handles three primary scenarios:

  1. Finding and updating existing integrated invoice headers

  2. Finding matching non-integrated invoice headers and integrating them

  3. Creating new invoice headers when no match is found

It also manages the creation of distribution center customers when necessary based on shipping address information in the invoice.

Examples:

resolver = QuickbooksInvoiceHeaderResolver.new(
  company_id: 123,
  customer_id: 456,
  invoice: qb_invoice_data
)
result = resolver.resolve
# => { action: 'create', quickbooks_invoice_header: #<QuickbooksInvoiceHeader id: 789, ...> }

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, #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:, customer_id:, invoice:) ⇒ QuickbooksInvoiceHeaderResolver

Initializes a new QuickbooksInvoiceHeaderResolver

Parameters:

  • company_id (Integer)

    ID of the company in the application

  • customer_id (Integer)

    ID of the customer in the application

  • invoice (Hash)

    Invoice data from QuickBooks API



39
40
41
42
43
44
# File 'app/resolver/quickbooks_invoice_header_resolver.rb', line 39

def initialize(company_id:, customer_id:, invoice:)
  @company_id = company_id
  @customer_id = customer_id
  @invoice = invoice.with_indifferent_access
  @customer_service = CustomerService.new(USER_NAME)
end

Instance Attribute Details

#company_idObject (readonly)

Returns the value of attribute company_id.



31
32
33
# File 'app/resolver/quickbooks_invoice_header_resolver.rb', line 31

def company_id
  @company_id
end

#customer_idObject (readonly)

Returns the value of attribute customer_id.



31
32
33
# File 'app/resolver/quickbooks_invoice_header_resolver.rb', line 31

def customer_id
  @customer_id
end

#invoiceObject (readonly)

Returns the value of attribute invoice.



31
32
33
# File 'app/resolver/quickbooks_invoice_header_resolver.rb', line 31

def invoice
  @invoice
end

Instance Method Details

#resolveHash

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

Returns:

  • (Hash)

    A result hash containing the action performed and the invoice header

Raises:

  • (StandardError)

    If an error occurs during processing



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/resolver/quickbooks_invoice_header_resolver.rb', line 51

def resolve
  ActiveRecord::Base.transaction do
    if find_integrated_invoice_header
      update_invoice_header
      check_and_create_dc_customer
      { action: 'update', quickbooks_invoice_header: @invoice_header }
    elsif find_invoice_header_to_integrate
      integrate_invoice_header
      update_invoice_header
      check_and_create_dc_customer
      { action: 'integrate', quickbooks_invoice_header: @invoice_header }
    elsif create_invoice_header
      integrate_invoice_header
      check_and_create_dc_customer
      { action: 'create', quickbooks_invoice_header: @invoice_header }
    end
  end
rescue StandardError => e
  Rails.logger.error "QuickBooks Invoice Header resolve failed: #{e.message}"
  Rails.logger.error e.backtrace.join("\n")
  raise e
end