From b118052d7c0313fd67ddde196ffa66a7ec9fad1a Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 16 Jan 2018 14:58:44 +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 b608a092055d..65c87bb5dc94 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); @@ -150,7 +150,7 @@ class SendRecurringInvoices extends Command if ($invoice->getAutoBillEnabled() && $invoice->client->autoBillLater()) { $this->info('Processing Autobill-delayed Invoice: ' . $invoice->id); - Auth::loginUsingId($invoice->user_id); + Auth::loginUsingId($invoice->activeUser()->id); $this->paymentService->autoBillInvoice($invoice); Auth::logout(); } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index e478e3d50c76..15784bedf823 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 */