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('balance', '>', 0) ->with('company') ->cursor(); $auto_bill_partial_invoices->each(function ($invoice){ $this->runAutoBiller($invoice); }); $auto_bill_invoices = Invoice::whereDate('due_date', '<=', now()) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->where('auto_bill_enabled', true) ->where('balance', '>', 0) ->with('company') ->cursor(); $auto_bill_invoices->each(function ($invoice){ $this->runAutoBiller($invoice); }); } 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('balance', '>', 0) ->with('company') ->cursor(); $auto_bill_partial_invoices->each(function ($invoice){ $this->runAutoBiller($invoice); }); $auto_bill_invoices = Invoice::whereDate('due_date', '<=', now()) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->where('auto_bill_enabled', true) ->where('balance', '>', 0) ->with('company') ->cursor(); $auto_bill_invoices->each(function ($invoice){ $this->runAutoBiller($invoice); }); } } } private function runAutoBiller(Invoice $invoice) { $invoice->service()->autoBill()->save(); } }