2019-05-23 07:08:31 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-23 07:08:31 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-23 07:08:31 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-23 07:08:31 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\RecurringInvoice;
|
|
|
|
|
2020-10-07 05:00:32 +02:00
|
|
|
use App\DataMapper\Analytics\SendRecurringFailure;
|
2020-09-08 12:34:14 +02:00
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
2021-10-18 04:31:21 +02:00
|
|
|
use App\Factory\InvoiceInvitationFactory;
|
2019-05-23 07:08:31 +02:00
|
|
|
use App\Factory\RecurringInvoiceToInvoiceFactory;
|
2022-08-01 04:50:55 +02:00
|
|
|
use App\Jobs\Cron\AutoBill;
|
2020-10-28 07:58:15 +01:00
|
|
|
use App\Jobs\Entity\EmailEntity;
|
2019-05-23 08:15:06 +02:00
|
|
|
use App\Models\Invoice;
|
2019-05-23 07:08:31 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2020-09-24 13:03:59 +02:00
|
|
|
use App\Utils\Ninja;
|
2019-05-27 12:48:52 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2021-10-18 04:31:21 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-09-26 04:13:03 +02:00
|
|
|
use App\Utils\Traits\MakesInvoiceValues;
|
|
|
|
use Carbon\Carbon;
|
2020-02-24 11:15:30 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
2020-09-06 11:38:10 +02:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2020-02-24 11:15:30 +01:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-10-07 05:00:32 +02:00
|
|
|
use Turbo124\Beacon\Facades\LightLogs;
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2020-02-24 11:15:30 +01:00
|
|
|
class SendRecurring implements ShouldQueue
|
2019-05-23 07:08:31 +02:00
|
|
|
{
|
2020-02-24 11:15:30 +01:00
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
2019-05-27 12:48:52 +02:00
|
|
|
use GeneratesCounter;
|
2021-10-18 04:31:21 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
2019-05-23 07:08:31 +02:00
|
|
|
public $recurring_invoice;
|
|
|
|
|
2019-05-24 00:37:47 +02:00
|
|
|
protected $db;
|
|
|
|
|
2021-03-30 03:28:49 +02:00
|
|
|
public $tries = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-23 07:08:31 +02:00
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param RecurringInvoice $recurring_invoice
|
|
|
|
* @param string $db
|
2019-05-23 07:08:31 +02:00
|
|
|
*/
|
2019-05-24 00:37:47 +02:00
|
|
|
public function __construct(RecurringInvoice $recurring_invoice, string $db = 'db-ninja-01')
|
2019-05-23 07:08:31 +02:00
|
|
|
{
|
|
|
|
$this->recurring_invoice = $recurring_invoice;
|
2019-05-24 00:37:47 +02:00
|
|
|
$this->db = $db;
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle() : void
|
|
|
|
{
|
2021-08-06 13:55:54 +02:00
|
|
|
|
2019-05-23 07:08:31 +02:00
|
|
|
// Generate Standard Invoice
|
2020-02-26 04:26:07 +01:00
|
|
|
$invoice = RecurringInvoiceToInvoiceFactory::create($this->recurring_invoice, $this->recurring_invoice->client);
|
2020-10-07 05:00:32 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->recurring_invoice->auto_bill === 'always') {
|
2021-10-02 03:10:12 +02:00
|
|
|
$invoice->auto_bill_enabled = true;
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif ($this->recurring_invoice->auto_bill === 'optout' || $this->recurring_invoice->auto_bill === 'optin') {
|
|
|
|
} elseif ($this->recurring_invoice->auto_bill === 'off') {
|
2021-10-02 03:10:12 +02:00
|
|
|
$invoice->auto_bill_enabled = false;
|
2022-02-01 10:04:03 +01:00
|
|
|
}
|
2021-10-02 03:10:12 +02:00
|
|
|
|
2022-09-08 03:30:40 +02:00
|
|
|
$invoice->date = date('Y-m-d');
|
2022-09-08 04:15:25 +02:00
|
|
|
|
|
|
|
nlog("Recurring Invoice Date Set on Invoice = {$invoice->date} - ". now()->format('Y-m-d'));
|
|
|
|
|
2022-09-08 03:30:40 +02:00
|
|
|
$invoice->due_date = $this->recurring_invoice->calculateDueDate(date('Y-m-d'));
|
2021-08-24 12:14:59 +02:00
|
|
|
$invoice->recurring_id = $this->recurring_invoice->id;
|
2021-10-25 12:39:02 +02:00
|
|
|
$invoice->saveQuietly();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($invoice->client->getSetting('auto_email_invoice')) {
|
2021-08-05 01:06:13 +02:00
|
|
|
$invoice = $invoice->service()
|
|
|
|
->markSent()
|
|
|
|
->applyNumber()
|
|
|
|
->fillDefaults()
|
2022-06-08 12:40:26 +02:00
|
|
|
->adjustInventory()
|
2021-08-05 01:06:13 +02:00
|
|
|
->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-08-05 01:06:13 +02:00
|
|
|
$invoice = $invoice->service()
|
|
|
|
->fillDefaults()
|
2022-06-21 11:57:17 +02:00
|
|
|
->save();
|
2021-08-05 01:06:13 +02:00
|
|
|
}
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2021-12-14 21:05:12 +01:00
|
|
|
$invoice = $this->createRecurringInvitations($invoice);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-01-09 03:53:27 +01:00
|
|
|
/* 09-01-2022 ensure we create the PDFs at this point in time! */
|
2022-01-10 02:47:16 +01:00
|
|
|
$invoice->service()->touchPdf(true);
|
2021-12-14 21:05:12 +01:00
|
|
|
|
2020-09-08 12:34:14 +02:00
|
|
|
/* Set next date here to prevent a recurring loop forming */
|
2021-06-11 00:20:46 +02:00
|
|
|
$this->recurring_invoice->next_send_date = $this->recurring_invoice->nextSendDate();
|
2022-06-02 05:49:29 +02:00
|
|
|
$this->recurring_invoice->next_send_date_client = $this->recurring_invoice->nextSendDateClient();
|
2020-09-08 12:34:14 +02:00
|
|
|
$this->recurring_invoice->remaining_cycles = $this->recurring_invoice->remainingCycles();
|
2021-06-11 00:20:46 +02:00
|
|
|
$this->recurring_invoice->last_sent_date = now();
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2020-09-08 12:34:14 +02:00
|
|
|
/* Set completed if we don't have any more cycles remaining*/
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->recurring_invoice->remaining_cycles == 0) {
|
2019-05-23 07:08:31 +02:00
|
|
|
$this->recurring_invoice->setCompleted();
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->recurring_invoice->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-11-09 08:54:56 +01:00
|
|
|
event('eloquent.created: App\Models\Invoice', $invoice);
|
2021-10-20 09:07:33 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($invoice->client->getSetting('auto_email_invoice')) {
|
|
|
|
//Admin notification for recurring invoice sent.
|
|
|
|
if ($invoice->invitations->count() >= 1) {
|
2021-12-14 21:05:12 +01:00
|
|
|
$invoice->entityEmailEvent($invoice->invitations->first(), 'invoice', 'email_template_invoice');
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-12-14 21:05:12 +01:00
|
|
|
$invoice->invitations->each(function ($invitation) use ($invoice) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($invitation->contact && ! $invitation->contact->trashed() && strlen($invitation->contact->email) >= 1 && $invoice->client->getSetting('auto_email_invoice')) {
|
|
|
|
try {
|
2022-10-28 03:10:20 +02:00
|
|
|
EmailEntity::dispatch($invitation, $invoice->company)->delay(rand(1,2));
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
2021-12-14 21:05:12 +01:00
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
2021-04-30 01:01:56 +02:00
|
|
|
|
2021-12-14 21:05:12 +01:00
|
|
|
nlog("Firing email for invoice {$invoice->number}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-12-15 13:17:50 +01:00
|
|
|
//auto bill, BUT NOT DRAFTS!!
|
|
|
|
if ($invoice->auto_bill_enabled && $invoice->client->getSetting('auto_bill_date') == 'on_send_date' && $invoice->client->getSetting('auto_email_invoice')) {
|
2021-03-30 03:28:49 +02:00
|
|
|
nlog("attempting to autobill {$invoice->number}");
|
2022-10-28 03:10:20 +02:00
|
|
|
AutoBill::dispatch($invoice->id, $this->db)->delay(rand(1,2));
|
2022-08-01 04:50:55 +02:00
|
|
|
|
2022-12-15 13:17:50 +01:00
|
|
|
} elseif ($invoice->auto_bill_enabled && $invoice->client->getSetting('auto_bill_date') == 'on_due_date' && $invoice->client->getSetting('auto_email_invoice')) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($invoice->due_date && Carbon::parse($invoice->due_date)->startOfDay()->lte(now()->startOfDay())) {
|
2021-09-02 08:17:46 +02:00
|
|
|
nlog("attempting to autobill {$invoice->number}");
|
2022-10-28 03:10:20 +02:00
|
|
|
AutoBill::dispatch($invoice->id, $this->db)->delay(rand(1,2));
|
2021-09-02 08:17:46 +02:00
|
|
|
}
|
2021-03-30 03:28:49 +02:00
|
|
|
}
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
2020-09-08 12:34:14 +02:00
|
|
|
|
2021-10-18 04:31:21 +02:00
|
|
|
/**
|
|
|
|
* Only create the invitations that are defined on the recurring invoice.
|
2022-06-21 11:57:17 +02:00
|
|
|
* @param Invoice $invoice
|
2021-10-18 04:31:21 +02:00
|
|
|
* @return Invoice $invoice
|
|
|
|
*/
|
|
|
|
private function createRecurringInvitations($invoice) :Invoice
|
|
|
|
{
|
2022-12-05 03:27:33 +01:00
|
|
|
|
|
|
|
if($this->recurring_invoice->invitations->count() == 0) {
|
|
|
|
$this->recurring_invoice = $this->recurring_invoice->service()->createInvitations()->save();
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->recurring_invoice->invitations->each(function ($recurring_invitation) use ($invoice) {
|
2021-10-18 04:31:21 +02:00
|
|
|
$ii = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
|
2021-11-06 01:46:12 +01:00
|
|
|
$ii->key = $this->createDbHash($invoice->company->db);
|
2021-10-18 04:31:21 +02:00
|
|
|
$ii->invoice_id = $invoice->id;
|
|
|
|
$ii->client_contact_id = $recurring_invitation->client_contact_id;
|
|
|
|
$ii->save();
|
|
|
|
});
|
|
|
|
|
|
|
|
return $invoice->fresh();
|
|
|
|
}
|
|
|
|
|
2020-10-07 05:00:32 +02:00
|
|
|
public function failed($exception = null)
|
|
|
|
{
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog('the job failed');
|
2020-10-07 05:00:32 +02:00
|
|
|
|
|
|
|
$job_failure = new SendRecurringFailure();
|
|
|
|
$job_failure->string_metric5 = get_class($this);
|
|
|
|
$job_failure->string_metric6 = $exception->getMessage();
|
|
|
|
|
|
|
|
LightLogs::create($job_failure)
|
2022-10-28 05:37:01 +02:00
|
|
|
->send();
|
2020-10-07 05:00:32 +02:00
|
|
|
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog(print_r($exception->getMessage(), 1));
|
2020-10-07 05:00:32 +02:00
|
|
|
}
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
2022-08-01 04:50:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* 1/8/2022
|
|
|
|
*
|
|
|
|
* Improvements here include moving the emailentity and autobilling into the queue.
|
|
|
|
*
|
|
|
|
* Further improvements could using the CompanyRecurringCron.php stub which divides
|
|
|
|
* the recurring invoices into companies and spins them off into their own queue to
|
|
|
|
* improve parallel processing.
|
|
|
|
*
|
|
|
|
* Need to be careful we do not overload redis and OOM.
|
|
|
|
*/
|