Class: InvoiceFundCheckerWorker

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

Overview

Cron worker that emails accounting a digest of due-on-delivery
Invoices with authorised-but-not-captured Bread / CC / PayPal
Payments — the first half of the auto-capture safety net (paired
with InvoiceFundAutoCaptureWorker which actually captures).

Instance Method Summary collapse

Instance Method Details

#performvoid

This method returns an undefined value.

Builds the unpaid-due-Bread/CC/PayPal scope and sends the digest
via FinancialsMailer. No-op when the scope is empty.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/workers/invoice_fund_checker_worker.rb', line 15

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)")

    FinancialsMailer.unpaid_invoices(@invoices).deliver_now if @invoices.present?
end