Class: InvoiceFundAutoCaptureWorker

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

Instance Method Summary collapse

Instance Method Details

#performObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/workers/invoice_fund_auto_capture_worker.rb', line 8

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