Class: InvoiceFundAutoCaptureWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/invoice_fund_auto_capture_worker.rb

Overview

Cron worker that runs capture_funds? on the same population
InvoiceFundCheckerWorker reports — Bread / CC / PayPal Invoices
whose orders are invoiced and not fully captured. Catches up
anything missed by the per-invoice capture flow on shipment.

Instance Method Summary collapse

Instance Method Details

#performvoid

This method returns an undefined value.

Iterates the unpaid-due scope and triggers
Invoice#capture_funds? on each match.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/workers/invoice_fund_auto_capture_worker.rb', line 16

def perform
  # Query to find potential invoices that have not been paid
  @invoices = Invoice.unpaid
                     .included_in_notifications
                     .joins(order: :payments)
                     .where(orders: { state: 'invoiced' })
                     .where(payments: { category: ['Bread', 'Credit Card', 'PayPal'] })
                     .where(invoices: { terms: 'Due' })
                     .joins("LEFT JOIN receipt_details on receipt_details.invoice_id = invoices.id and receipt_details.state != 'void'")
                     .where.not(payments: { state: 'captured' })
                     .group('invoices.id')
                     .having('invoices.total > coalesce(sum(receipt_details.amount + receipt_details.write_off + receipt_details.discount),0)')

  @invoices.each do |invoice|
    invoice.capture_funds?
    logger.info "invoicing capture finished for invoice id: #{invoice.id}"
  end
end