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) ->where('is_deleted', false) ->with('company'); nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill"); $auto_bill_partial_invoices->cursor()->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) ->where('is_deleted', false) ->with('company'); nlog($auto_bill_invoices->count(). " full invoices to auto bill"); $auto_bill_invoices->cursor()->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) ->where('is_deleted', false) ->with('company'); nlog($auto_bill_partial_invoices->count(). " partial invoices to auto bill db = {$db}"); $auto_bill_partial_invoices->cursor()->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) ->where('is_deleted', false) ->with('company'); nlog($auto_bill_invoices->count(). " full invoices to auto bill db = {$db}"); $auto_bill_invoices->cursor()->each(function ($invoice){ $this->runAutoBiller($invoice); }); } } } private function runAutoBiller(Invoice $invoice) { info("Firing autobill for {$invoice->company_id} - {$invoice->number}"); try{ $invoice->service()->autoBill()->save(); } catch(\Exception $e) { nlog("Failed to capture payment for {$invoice->company_id} - {$invoice->number} ->" . $e->getMessage()); } } }