Class: QuickbooksInvoiceDetailResolver

Inherits:
Object
  • Object
show all
Defined in:
app/resolver/quickbooks_invoice_detail_resolver.rb

Overview

QuickbooksInvoiceDetailResolver handles the processing and integration of invoice line items from QuickBooks with the application’s internal invoice detail data model. This resolver processes individual invoice detail lines from the QuickBooks API and performs the necessary operations to maintain data consistency between systems.

The resolver manages three primary scenarios:

  1. Finding and updating existing integrated invoice details

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

  3. Creating new invoice details when no match is found

It also handles special cases such as skipping subtotal lines and lines with missing IDs.

Examples:

resolver = QuickbooksInvoiceDetailResolver.new(
  company_id: 123,
  customer_id: 456,
  quickbooks_invoice_header_id: 789,
  invoice_detail: qb_line_item_data
)
result = resolver.resolve
# => { action: 'create', invoice_detail: #<QuickbooksInvoiceDetail id: 101, ...> }

Constant Summary collapse

USER_NAME =
'SYSTEM'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(company_id:, customer_id:, quickbooks_invoice_header_id:, invoice_detail:) ⇒ QuickbooksInvoiceDetailResolver

Initializes a new QuickbooksInvoiceDetailResolver

Parameters:

  • company_id (Integer)

    ID of the company in the application

  • customer_id (Integer)

    ID of the customer in the application

  • quickbooks_invoice_header_id (Integer)

    ID of the parent invoice header

  • invoice_detail (Hash)

    Invoice line item data from QuickBooks API



37
38
39
40
41
42
# File 'app/resolver/quickbooks_invoice_detail_resolver.rb', line 37

def initialize(company_id:, customer_id:, quickbooks_invoice_header_id:, invoice_detail:)
  @company_id = company_id
  @customer_id = customer_id
  @quickbooks_invoice_header_id = quickbooks_invoice_header_id
  @invoice = invoice_detail.with_indifferent_access
end

Instance Attribute Details

#company_idObject (readonly)

Returns the value of attribute company_id.



28
29
30
# File 'app/resolver/quickbooks_invoice_detail_resolver.rb', line 28

def company_id
  @company_id
end

#customer_idObject (readonly)

Returns the value of attribute customer_id.



28
29
30
# File 'app/resolver/quickbooks_invoice_detail_resolver.rb', line 28

def customer_id
  @customer_id
end

#invoice_detailObject (readonly)

Returns the value of attribute invoice_detail.



28
29
30
# File 'app/resolver/quickbooks_invoice_detail_resolver.rb', line 28

def invoice_detail
  @invoice_detail
end

#quickbooks_invoice_header_idObject (readonly)

Returns the value of attribute quickbooks_invoice_header_id.



28
29
30
# File 'app/resolver/quickbooks_invoice_detail_resolver.rb', line 28

def quickbooks_invoice_header_id
  @quickbooks_invoice_header_id
end

Instance Method Details

#resolveHash?

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

Returns:

  • (Hash, nil)

    A result hash containing the action performed and the invoice detail, or nil if the line should be skipped

Raises:

  • (StandardError)

    If an error occurs during processing



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/resolver/quickbooks_invoice_detail_resolver.rb', line 50

def resolve
  # Skip processing subtotal line types
  if @invoice['DetailType'] == 'SubTotalLineDetail'
    return { action: 'skip', invoice_detail: nil }
  end

  # Skip if the line ID is missing
  if @invoice['Id'].blank?
    Rails.logger.warn("Skipping invoice detail with missing line ID for header #{quickbooks_invoice_header_id}")
    return { action: 'skip', invoice_detail: nil }
  end

  begin
    ActiveRecord::Base.transaction do
      if find_integrated_invoice_detail
        update_invoice_detail
        { action: 'update', invoice_detail: @invoice_detail }
      elsif find_invoice_detail_to_integrate
        integrate_invoice_detail
        update_invoice_detail
        { action: 'integrate', invoice_detail: @invoice_detail }
      elsif create_invoice_detail
        integrate_invoice_detail
        { action: 'create', invoice_detail: @invoice_detail }
      end
    end
  rescue StandardError => e
    Rails.logger.error "QuickBooks Invoice Detail resolve failed: #{e.message}"
    Rails.logger.error e.backtrace.join("\n")
    raise e
  end
end