Class: QuickbooksInvoiceHeaderResolverJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/quickbooks_invoice_header_resolver_job.rb

Overview

QuickbooksInvoiceHeaderResolverJob is responsible for processing QuickBooks invoice data, including both the invoice header and its line items. It orchestrates the entire invoice integration process by:

  1. Resolving the invoice header using QuickbooksInvoiceHeaderResolver

  2. Processing each line item using QuickbooksInvoiceDetailResolver

  3. Updating the SPS Ready status and calling the appropriate stored procedure to complete the integration process

This job is typically triggered when new invoice data is received from QuickBooks, either through a webhook or a scheduled poll.

Examples:

QuickbooksInvoiceHeaderResolverJob.perform_later(
  company_id: 123,
  customer_id: 456,
  invoice: qb_invoice_data
)

Instance Method Summary collapse

Instance Method Details

#perform(company_id:, customer_id:, invoice:) ⇒ void

This method returns an undefined value.

Processes a QuickBooks invoice and its line items

Parameters:

  • company_id (Integer)

    ID of the company in the application

  • customer_id (Integer)

    ID of the customer in the application

  • invoice (Hash)

    Complete invoice data from QuickBooks API



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
82
83
84
85
86
87
88
89
90
91
92
# File 'app/jobs/quickbooks_invoice_header_resolver_job.rb', line 29

def perform(company_id:, customer_id:, invoice:)
  invoice = invoice.with_indifferent_access

  # Step 1: Resolve the parent invoice
  result = QuickbooksInvoiceHeaderResolver.new(
    company_id: company_id,
    customer_id: customer_id,
    invoice: invoice
  ).resolve

  quickbooks_invoice_header_id = result[:quickbooks_invoice_header].id

  Sentry.logger.info('Successfully resolved invoice header', {
                       invoice_header_id: quickbooks_invoice_header_id,
                       qb_invoice_id: result[:quickbooks_invoice_header].quickbooks_invoice_id,
                       invoice_no: result[:quickbooks_invoice_header].invoice_no,
                       txn_date: result[:quickbooks_invoice_header].txn_date,
                       company_id: company_id,
                       customer_id: customer_id,
                       job: 'QuickbooksInvoiceHeaderResolverJob',
                       task: 'resolve_header'
                     })

  # Step 2: Resolve each invoice line
  detail_results = []
  Array(invoice['Line']).each_with_index do |line, index|
    detail_result = QuickbooksInvoiceDetailResolver.new(
      company_id: company_id,
      customer_id: customer_id,
      quickbooks_invoice_header_id: quickbooks_invoice_header_id,
      invoice_detail: line
    ).resolve

    detail_results << detail_result if detail_result
  rescue StandardError => e
    Sentry.logger.error('Error processing line', {
                          invoice_header_id: quickbooks_invoice_header_id,
                          qb_invoice_id: invoice['Id'],
                          invoice_no: invoice['DocNumber'],
                          line_number: index + 1,
                          error_message: e.message,
                          job: 'QuickbooksInvoiceHeaderResolverJob',
                          task: 'resolve_line',
                          error: true
                        })
    raise e
  end

  Sentry.logger.info('Completed processing all invoice lines', {
                       invoice_header_id: quickbooks_invoice_header_id,
                       qb_invoice_id: invoice['Id'],
                       invoice_no: invoice['DocNumber'],
                       lines_count: Array(invoice['Line']).size,
                       company_id: company_id,
                       customer_id: customer_id,
                       job: 'QuickbooksInvoiceHeaderResolverJob',
                       task: 'processing_complete'
                     })

  # Step 3: Update SPS Ready status to 'invoice sent' by POST request and call stored procedure
  return unless detail_results.any? { |r| r && %w[create update integrate].include?(r[:action]) }

  update_invoice_status_and_call_procedure(quickbooks_invoice_header_id)
end