db) { MultiDB::setDb($this->db); } $invoice = false; try { nlog("autobill {$this->invoice_id}"); $invoice = Invoice::withTrashed()->find($this->invoice_id); $invoice->service()->autoBill(); } catch (\Exception $e) { nlog("Failed to capture payment for {$this->invoice_id} ->".$e->getMessage()); if($this->send_email_on_failure && $invoice) { $invoice->invitations->each(function ($invitation) use ($invoice) { if ($invitation->contact && ! $invitation->contact->trashed() && strlen($invitation->contact->email) >= 1 && $invoice->client->getSetting('auto_email_invoice')) { try { EmailEntity::dispatch($invitation, $invoice->company)->delay(rand(1, 2)); $invoice->entityEmailEvent($invitation, 'invoice', 'email_template_invoice'); } catch (\Exception $e) { nlog($e->getMessage()); } nlog("Firing email for invoice {$invoice->number}"); } }); } } } }