From 6dc5e9927a4aedcf83757b4594c4c0634951e6a1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 1 Oct 2024 18:25:46 +1000 Subject: [PATCH] Small refactor to auto bill query --- app/Jobs/Cron/AutoBillCron.php | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/app/Jobs/Cron/AutoBillCron.php b/app/Jobs/Cron/AutoBillCron.php index 7a24b1c75a8b..0c22d7b670e2 100644 --- a/app/Jobs/Cron/AutoBillCron.php +++ b/app/Jobs/Cron/AutoBillCron.php @@ -49,17 +49,18 @@ class AutoBillCron Auth::logout(); if (! config('ninja.db.multi_db_enabled')) { + $auto_bill_partial_invoices = Invoice::query() - ->whereDate('partial_due_date', '<=', now()) - ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) - ->where('auto_bill_enabled', true) - ->where('auto_bill_tries', '<', 3) - ->where('balance', '>', 0) - ->where('is_deleted', false) - ->whereHas('company', function ($query) { - $query->where('is_disabled', 0); - }) - ->orderBy('id', 'DESC'); + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('balance', '>', 0) + ->whereDate('partial_due_date', '<=', now()) + ->where('auto_bill_enabled', true) + ->where('auto_bill_tries', '<', 3) + ->whereHas('company', function ($query) { + $query->where('is_disabled', 0); + }) + ->where('is_deleted', false) + ->orderBy('id', 'DESC'); nlog($auto_bill_partial_invoices->count().' partial invoices to auto bill'); @@ -72,18 +73,18 @@ class AutoBillCron }); $auto_bill_invoices = Invoice::query() - ->whereDate('due_date', '<=', now()) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('balance', '>', 0) + ->whereDate('due_date', '<=', now()) ->where('auto_bill_enabled', true) ->where('auto_bill_tries', '<', 3) - ->where('balance', '>', 0) - ->where('is_deleted', false) ->whereHas('company', function ($query) { $query->where('is_disabled', 0); }) ->whereHas('client', function ($query) { - $query->has('gateway_tokens', '>=', 1); + $query->has('gateway_tokens', '>', 0); }) + ->where('is_deleted', false) ->orderBy('id', 'DESC'); nlog($auto_bill_invoices->count().' full invoices to auto bill'); @@ -121,7 +122,6 @@ class AutoBillCron foreach ($invoices as $invoice) { AutoBill::dispatch($invoice->id, $db); } - sleep(1); }); @@ -146,7 +146,6 @@ class AutoBillCron foreach ($invoices as $invoice) { AutoBill::dispatch($invoice->id, $db); } - sleep(1); }); }