2022-11-15 11:09:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-11-15 11:09:05 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Quote;
|
|
|
|
|
2022-11-15 11:25:34 +01:00
|
|
|
use App\Jobs\Mail\NinjaMailer;
|
|
|
|
use App\Jobs\Mail\NinjaMailerJob;
|
|
|
|
use App\Jobs\Mail\NinjaMailerObject;
|
2022-11-15 11:09:05 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2022-11-15 11:25:34 +01:00
|
|
|
use App\Mail\Admin\QuoteExpiredObject;
|
2022-11-15 11:09:05 +01:00
|
|
|
use App\Models\Quote;
|
2022-11-15 11:25:34 +01:00
|
|
|
use App\Utils\Traits\Notifications\UserNotifies;
|
2022-11-15 11:09:05 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
class QuoteCheckExpired implements ShouldQueue
|
|
|
|
{
|
2022-11-15 11:25:34 +01:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UserNotifies;
|
2022-11-15 11:09:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*/
|
2023-02-16 02:36:09 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
2022-11-15 11:09:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2023-02-16 02:36:09 +01:00
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
2022-11-26 04:38:09 +01:00
|
|
|
Quote::query()
|
|
|
|
->where('status_id', Quote::STATUS_SENT)
|
|
|
|
->where('is_deleted', false)
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->whereNotNull('due_date')
|
|
|
|
->whereHas('client', function ($query) {
|
2023-02-16 02:36:09 +01:00
|
|
|
$query->where('is_deleted', 0)
|
|
|
|
->where('deleted_at', null);
|
|
|
|
})
|
2022-11-26 04:38:09 +01:00
|
|
|
->whereHas('company', function ($query) {
|
|
|
|
$query->where('is_disabled', 0);
|
|
|
|
})
|
|
|
|
// ->where('due_date', '<='. now()->toDateTimeString())
|
|
|
|
->whereBetween('due_date', [now()->subDay()->startOfDay(), now()->startOfDay()->subSecond()])
|
|
|
|
->cursor()
|
2023-02-16 02:36:09 +01:00
|
|
|
->each(function ($quote) {
|
|
|
|
$this->queueExpiredQuoteNotification($quote);
|
2022-11-26 04:38:09 +01:00
|
|
|
});
|
2023-02-16 02:36:09 +01:00
|
|
|
} else {
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
2022-11-26 04:38:09 +01:00
|
|
|
MultiDB::setDB($db);
|
2022-11-15 11:09:05 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
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);
|
|
|
|
});
|
2022-11-26 04:38:09 +01:00
|
|
|
}
|
2022-11-15 11:09:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkForExpiredQuotes()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:25:34 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-06-07 09:15:04 +02:00
|
|
|
|
|
|
|
$nmo->mailable = new NinjaMailer((new QuoteExpiredObject($quote, $quote->company, $company_user->portalType()))->build());
|
2022-11-15 11:25:34 +01:00
|
|
|
|
|
|
|
/* Returns an array of notification methods */
|
2023-01-24 22:26:32 +01:00
|
|
|
$methods = $this->findUserNotificationTypes($quote->invitations()->first(), $company_user, 'quote', ['all_notifications', 'quote_expired', 'quote_expired_all', 'quote_expired_user']);
|
2022-11-15 11:25:34 +01:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 11:09:05 +01:00
|
|
|
}
|