2020-04-15 02:30:52 +02:00
|
|
|
<?php
|
2020-04-30 13:45:47 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-04-30 13:45:47 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-04-30 13:45:47 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-04-30 13:45:47 +02:00
|
|
|
*/
|
2020-04-15 02:30:52 +02:00
|
|
|
|
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
2021-05-19 02:15:29 +02:00
|
|
|
use App\Jobs\Entity\EmailEntity;
|
2020-04-15 02:30:52 +02:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Invoice;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2021-05-25 23:31:17 +02:00
|
|
|
use App\Utils\Traits\MakesReminders;
|
2020-04-15 02:30:52 +02:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
|
|
|
class ReminderJob implements ShouldQueue
|
|
|
|
{
|
2021-05-25 23:31:17 +02:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesReminders;
|
2020-04-15 02:30:52 +02:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
|
|
|
$this->processReminders();
|
|
|
|
} else {
|
|
|
|
//multiDB environment, need to
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
|
|
|
MultiDB::setDB($db);
|
2021-05-25 04:58:54 +02:00
|
|
|
$this->processReminders();
|
2020-04-15 02:30:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 04:58:54 +02:00
|
|
|
private function processReminders()
|
2020-04-15 02:30:52 +02:00
|
|
|
{
|
2021-06-21 07:57:14 +02:00
|
|
|
nlog("Sending invoice reminders " . now()->format('Y-m-d h:i:s'));
|
|
|
|
|
2021-06-22 10:46:46 +02:00
|
|
|
Invoice::where('next_send_date', '<=', now()->toDateTimeString())
|
2021-06-15 00:06:01 +02:00
|
|
|
->where('is_deleted', 0)
|
|
|
|
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
|
|
|
|
->where('balance', '>', 0)
|
|
|
|
->with('invitations')->cursor()->each(function ($invoice) {
|
2021-05-25 04:58:54 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
if ($invoice->isPayable()) {
|
2020-11-25 15:19:52 +01:00
|
|
|
$reminder_template = $invoice->calculateTemplate('invoice');
|
2021-01-18 11:59:24 +01:00
|
|
|
$invoice->service()->touchReminder($reminder_template)->save();
|
2020-09-08 13:01:55 +02:00
|
|
|
|
2020-11-08 11:53:47 +01:00
|
|
|
$invoice->invitations->each(function ($invitation) use ($invoice, $reminder_template) {
|
2021-06-13 06:19:40 +02:00
|
|
|
EmailEntity::dispatch($invitation, $invitation->company, $reminder_template);
|
2021-05-25 04:58:54 +02:00
|
|
|
nlog("Firing reminder email for invoice {$invoice->number}");
|
2020-04-15 02:30:52 +02:00
|
|
|
});
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($invoice->invitations->count() > 0) {
|
2021-01-13 08:20:46 +01:00
|
|
|
event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars(), $reminder_template));
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2021-05-25 23:31:17 +02:00
|
|
|
|
2021-05-26 02:35:39 +02:00
|
|
|
$invoice->service()->setReminder()->save();
|
2021-05-25 23:31:17 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
} else {
|
|
|
|
$invoice->next_send_date = null;
|
|
|
|
$invoice->save();
|
|
|
|
}
|
2021-05-25 04:58:54 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|