1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Jobs/RecurringInvoice/SendRecurring.php

201 lines
7.6 KiB
PHP
Raw Normal View History

2019-05-23 07:08:31 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-23 07:08:31 +02:00
*
* @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)
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;
2023-10-26 04:57:44 +02:00
use App\DataMapper\Analytics\SendRecurringFailure;
use App\Events\Invoice\InvoiceWasCreated;
use App\Factory\InvoiceInvitationFactory;
use App\Factory\RecurringInvoiceToInvoiceFactory;
use App\Jobs\Cron\AutoBill;
2020-10-28 07:58:15 +01:00
use App\Jobs\Entity\EmailEntity;
2023-10-26 04:57:44 +02:00
use App\Models\Invoice;
2019-05-23 07:08:31 +02:00
use App\Models\RecurringInvoice;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\GeneratesCounter;
2023-10-26 04:57:44 +02:00
use App\Utils\Traits\MakesHash;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-10-26 04:57:44 +02:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Turbo124\Beacon\Facades\LightLogs;
2019-05-23 07:08:31 +02:00
class SendRecurring implements ShouldQueue
2019-05-23 07:08:31 +02:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use GeneratesCounter;
use MakesHash;
2021-03-30 03:28:49 +02:00
public $tries = 1;
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
*/
2023-05-10 01:06:34 +02:00
public function __construct(public RecurringInvoice $recurring_invoice, public 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
{
// Generate Standard Invoice
$invoice = RecurringInvoiceToInvoiceFactory::create($this->recurring_invoice, $this->recurring_invoice->client);
2020-10-07 05:00:32 +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'));
$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();
if ($invoice->client->getSetting('auto_email_invoice')) {
$invoice = $invoice->service()
->markSent()
->applyNumber()
->fillDefaults()
2022-06-08 12:40:26 +02:00
->adjustInventory()
->save();
} else {
$invoice = $invoice->service()
->fillDefaults()
->save();
}
2019-05-23 07:08:31 +02:00
//12-01-2023 i moved this block after fillDefaults to handle if standard invoice auto bill config has been enabled, recurring invoice should override.
if ($this->recurring_invoice->auto_bill == 'always') {
$invoice->auto_bill_enabled = true;
$invoice->saveQuietly();
} elseif ($this->recurring_invoice->auto_bill == 'optout' || $this->recurring_invoice->auto_bill == 'optin') {
} elseif ($this->recurring_invoice->auto_bill == 'off') {
$invoice->auto_bill_enabled = false;
$invoice->saveQuietly();
}
$invoice = $this->createRecurringInvitations($invoice);
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();
$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
}
$this->recurring_invoice->save();
event('eloquent.created: App\Models\Invoice', $invoice);
event(new InvoiceWasCreated($invoice, $invoice->company, Ninja::eventVars()));
//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')) {
nlog("attempting to autobill {$invoice->number}");
AutoBill::dispatch($invoice->id, $this->db, true)->delay(rand(1, 2));
2023-08-04 08:40:44 +02:00
//04-08-2023 edge case to support where online payment notifications are not enabled
2023-10-26 04:57:44 +02:00
if(!$invoice->client->getSetting('client_online_payment_notification')) {
$this->sendRecurringEmails($invoice);
}
2023-10-26 04:57:44 +02:00
} elseif ($invoice->auto_bill_enabled && $invoice->client->getSetting('auto_bill_date') == 'on_due_date' && $invoice->client->getSetting('auto_email_invoice') && ($invoice->due_date && Carbon::parse($invoice->due_date)->startOfDay()->lte(now()->startOfDay()))) {
nlog("attempting to autobill {$invoice->number}");
AutoBill::dispatch($invoice->id, $this->db, true)->delay(rand(1, 2));
2023-08-04 08:40:44 +02:00
//04-08-2023 edge case to support where online payment notifications are not enabled
if(!$invoice->client->getSetting('client_online_payment_notification')) {
$this->sendRecurringEmails($invoice);
}
2023-10-26 04:57:44 +02:00
} elseif ($invoice->client->getSetting('auto_email_invoice')) {
$this->sendRecurringEmails($invoice);
}
}
2023-08-04 08:40:44 +02:00
/**
* Sends the recurring invoice emails to
* the designated contacts
2023-10-26 04:57:44 +02:00
*
2023-08-04 08:40:44 +02:00
* @param Invoice $invoice
* @return void
*/
private function sendRecurringEmails(Invoice $invoice): void
{
//Admin notification for recurring invoice sent.
if ($invoice->invitations->count() >= 1) {
$invoice->entityEmailEvent($invoice->invitations->first(), 'invoice', 'email_template_invoice');
}
2021-04-30 01:01:56 +02:00
$invoice->invitations->each(function ($invitation) use ($invoice) {
if ($invitation->contact && ! $invitation->contact->trashed() && strlen($invitation->contact->email) >= 1 && $invoice->client->getSetting('auto_email_invoice')) {
try {
EmailEntity::dispatch($invitation, $invoice->company)->delay(rand(1, 2));
} catch (\Exception $e) {
nlog($e->getMessage());
}
nlog("Firing email for invoice {$invoice->number}");
}
});
2019-05-23 07:08:31 +02:00
}
2020-09-08 12:34:14 +02:00
/**
* Only create the invitations that are defined on the recurring invoice.
* @param Invoice $invoice
* @return Invoice $invoice
*/
private function createRecurringInvitations($invoice) :Invoice
{
2023-02-16 02:36:09 +01:00
if ($this->recurring_invoice->invitations->count() == 0) {
2023-08-06 04:20:18 +02:00
$this->recurring_invoice->service()->createInvitations()->save();
$this->recurring_invoice = $this->recurring_invoice->fresh();
}
$this->recurring_invoice->invitations->each(function ($recurring_invitation) use ($invoice) {
$ii = InvoiceInvitationFactory::create($invoice->company_id, $invoice->user_id);
2021-11-06 01:46:12 +01:00
$ii->key = $this->createDbHash($invoice->company->db);
$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)
->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
}
2023-10-26 04:57:44 +02:00
}