Class: InvoiceFundCheckerWorker

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

Instance Method Summary collapse

Instance Method Details

#performObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/workers/invoice_fund_checker_worker.rb', line 7

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

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