format('Y-m-d h:i:s')); if (! config('ninja.db.multi_db_enabled')) { $auto_bill_partial_invoices = Invoice::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'); nlog($auto_bill_partial_invoices->count().' partial invoices to auto bill'); $auto_bill_partial_invoices->chunk(400, function ($invoices) { foreach ($invoices as $invoice) { AutoBill::dispatch($invoice->id, false); } sleep(2); }); $auto_bill_invoices = Invoice::whereDate('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'); nlog($auto_bill_invoices->count().' full invoices to auto bill'); $auto_bill_invoices->chunk(400, function ($invoices) { foreach ($invoices as $invoice) { AutoBill::dispatch($invoice->id, false); } sleep(2); }); } else { //multiDB environment, need to foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); $auto_bill_partial_invoices = Invoice::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'); nlog($auto_bill_partial_invoices->count()." partial invoices to auto bill db = {$db}"); $auto_bill_partial_invoices->chunk(400, function ($invoices) use ($db) { foreach ($invoices as $invoice) { AutoBill::dispatch($invoice->id, $db); } sleep(2); }); $auto_bill_invoices = Invoice::whereDate('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'); nlog($auto_bill_invoices->count()." full invoices to auto bill db = {$db}"); $auto_bill_invoices->chunk(400, function ($invoices) use ($db) { foreach ($invoices as $invoice) { AutoBill::dispatch($invoice->id, $db); } sleep(2); }); } nlog('Auto Bill - fine'); } } }