where('status_id', Quote::STATUS_SENT) ->where('is_deleted', false) ->whereNull('deleted_at') ->whereNotNull('due_date') ->whereHas('client', function ($query) { $query->where('is_deleted', 0) ->where('deleted_at', null); }) ->whereHas('company', function ($query) { $query->where('is_disabled', 0); }) // ->where('due_date', '<='. now()->toDateTimeString()) ->whereBetween('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('status_id', Quote::STATUS_SENT) ->where('is_deleted', false) ->whereNull('deleted_at') ->whereNotNull('due_date') ->whereHas('client', function ($query) { $query->where('is_deleted', 0) ->where('deleted_at', null); }) ->whereHas('company', function ($query) { $query->where('is_disabled', 0); }) // ->where('due_date', '<='. now()->toDateTimeString()) ->whereBetween('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->mailable = new NinjaMailer((new QuoteExpiredObject($quote, $quote->company))->build()); $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; } /* 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); } } } }