where('quotes.status_id', Quote::STATUS_SENT) ->where('quotes.is_deleted', false) ->whereNull('quotes.deleted_at') ->whereNotNull('quotes.due_date') ->whereHas('client', function ($query) { $query->where('is_deleted', 0) ->where('deleted_at', null); }) ->whereHas('company', function ($query) { $query->where('is_disabled', 0); }) ->whereBetween('quotes.due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()]) ->cursor() ->each(function ($quote) { $this->queueExpiredQuoteNotification($quote); }); } else { foreach (MultiDB::$dbs as $db) { MultiDB::setDB($db); Quote::query() ->where('quotes.status_id', Quote::STATUS_SENT) ->where('quotes.is_deleted', false) ->whereNull('quotes.deleted_at') ->whereNotNull('quotes.due_date') ->whereHas('client', function ($query) { $query->where('is_deleted', 0) ->where('deleted_at', null); }) ->whereHas('company', function ($query) { $query->where('is_disabled', 0); }) ->whereBetween('quotes.due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()]) ->cursor() ->each(function ($quote) { $this->queueExpiredQuoteNotification($quote); }); } } } private function checkForExpiredQuotes() { } private function queueExpiredQuoteNotification(Quote $quote) { $nmo = new NinjaMailerObject(); $nmo->company = $quote->company; $nmo->settings = $quote->company->settings; /* We loop through each user and determine whether they need to be notified */ foreach ($quote->company->company_users as $company_user) { /* The User */ $user = $company_user->user; if (! $user) { continue; } $nmo->mailable = new NinjaMailer((new QuoteExpiredObject($quote, $quote->company, $company_user->portalType()))->build()); /* Returns an array of notification methods */ $methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_expired', 'quote_expired_all', 'quote_expired_user']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); $nmo->to_user = $user; NinjaMailerJob::dispatch($nmo); } } } }