From 429982cb38f32bd0277e2f66c382937d8d197042 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 16 Jan 2018 14:57:37 +0200 Subject: [PATCH] Handle archived user in cron --- app/Console/Commands/SendRecurringInvoices.php | 4 ++-- app/Models/Invoice.php | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/SendRecurringInvoices.php b/app/Console/Commands/SendRecurringInvoices.php index 86586efcc43e..1e3d11fe7bf2 100644 --- a/app/Console/Commands/SendRecurringInvoices.php +++ b/app/Console/Commands/SendRecurringInvoices.php @@ -111,7 +111,7 @@ class SendRecurringInvoices extends Command $account = $recurInvoice->account; $account->loadLocalizationSettings($recurInvoice->client); - Auth::loginUsingId($recurInvoice->user_id); + Auth::loginUsingId($recurInvoice->activeUser()->id); try { $invoice = $this->invoiceRepo->createRecurringInvoice($recurInvoice); @@ -157,7 +157,7 @@ class SendRecurringInvoices extends Command continue; } - Auth::loginUsingId($invoice->user_id); + Auth::loginUsingId($recurInvoice->activeUser()->id); $this->paymentService->autoBillInvoice($invoice); Auth::logout(); } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index adb984a7173c..84799bb0c64f 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -690,6 +690,15 @@ class Invoice extends EntityModel implements BalanceAffecting } } + public function activeUser() + { + if (! $this->user->trashed()) { + return $this->user; + } + + return $this->account->users->first(); + } + /** * @return mixed */