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
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2020-04-15 02:30:52 +02:00
|
|
|
|
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
|
|
|
use App\Helpers\Email\InvoiceEmail;
|
|
|
|
use App\Jobs\Invoice\EmailInvoice;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Utils\ClientPortal\CustomMessage\invitation;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
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;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class ReminderJob implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
|
|
|
|
//always make sure you have set the company as this command is being
|
|
|
|
//run from the console so we have no awareness of the DB.
|
|
|
|
|
|
|
|
if (! config('ninja.db.multi_db_enabled')) {
|
|
|
|
$this->processReminders();
|
|
|
|
} else {
|
|
|
|
//multiDB environment, need to
|
|
|
|
foreach (MultiDB::$dbs as $db) {
|
|
|
|
MultiDB::setDB($db);
|
|
|
|
|
|
|
|
$this->processReminders($db);
|
|
|
|
}
|
|
|
|
}
|
2020-09-08 13:01:55 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function processReminders($db = null)
|
|
|
|
{
|
|
|
|
|
2020-09-08 12:34:14 +02:00
|
|
|
Invoice::where('next_send_date', Carbon::today()->format('Y-m-d'))->with('invitations')->cursor()->each(function ($invoice) {
|
2020-09-08 13:01:55 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
if ($invoice->isPayable()) {
|
2020-09-08 12:34:14 +02:00
|
|
|
|
2020-10-28 00:02:32 +01:00
|
|
|
$reminder_template = $invoice->calculateTemplate('invoice');
|
2020-09-08 13:01:55 +02:00
|
|
|
$invoice->service()->touchReminder($this->reminder_template)->save();
|
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$invoice->invitations->each(function ($invitation) use ($invoice) {
|
|
|
|
$email_builder = (new InvoiceEmail())->build($invitation);
|
|
|
|
|
|
|
|
EmailInvoice::dispatch($email_builder, $invitation, $invoice->company);
|
|
|
|
|
|
|
|
info("Firing email for invoice {$invoice->number}");
|
|
|
|
});
|
|
|
|
|
2020-09-08 12:34:14 +02:00
|
|
|
if ($invoice->invitations->count() > 0)
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars()));
|
2020-09-08 12:34:14 +02:00
|
|
|
|
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
} else {
|
2020-09-08 12:34:14 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
$invoice->next_send_date = null;
|
|
|
|
$invoice->save();
|
2020-09-08 12:34:14 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
}
|
2020-09-08 12:34:14 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
});
|
2020-09-08 12:34:14 +02:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
}
|
|
|
|
}
|