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
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-23 07:08:31 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2019-05-23 07:08:31 +02:00
|
|
|
use App\Factory\RecurringInvoiceToInvoiceFactory;
|
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;
|
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;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
|
|
|
$invoice->date = now()->format('Y-m-d');
|
2021-01-08 02:44:31 +01:00
|
|
|
|
2020-09-08 12:34:14 +02:00
|
|
|
$invoice = $invoice->service()
|
|
|
|
->markSent()
|
2020-09-26 01:48:42 +02:00
|
|
|
->applyNumber()
|
2020-09-08 12:34:14 +02:00
|
|
|
->createInvitations()
|
2021-03-22 22:24:05 +01:00
|
|
|
->fillDefaults()
|
2020-09-08 12:34:14 +02:00
|
|
|
->save();
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog("updating recurring invoice dates");
|
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();
|
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
|
|
|
|
2020-12-29 22:10:03 +01:00
|
|
|
nlog("next send date = " . $this->recurring_invoice->next_send_date);
|
|
|
|
nlog("remaining cycles = " . $this->recurring_invoice->remaining_cycles);
|
|
|
|
nlog("last send date = " . $this->recurring_invoice->last_sent_date);
|
2020-09-26 01:48:42 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->recurring_invoice->save();
|
2021-01-13 08:20:46 +01:00
|
|
|
|
2021-03-30 03:28:49 +02:00
|
|
|
//Admin notification for recurring invoice sent.
|
|
|
|
if ($invoice->invitations->count() >= 1) {
|
|
|
|
$invoice->entityEmailEvent($invoice->invitations->first(), 'invoice', 'email_template_invoice');
|
|
|
|
}
|
|
|
|
|
2021-04-21 05:54:10 +02:00
|
|
|
nlog("Invoice {$invoice->number} created");
|
|
|
|
|
|
|
|
$invoice->invitations->each(function ($invitation) use ($invoice) {
|
|
|
|
if ($invitation->contact && strlen($invitation->contact->email) >=1) {
|
2021-04-30 01:01:56 +02:00
|
|
|
|
|
|
|
try{
|
2021-06-10 11:01:30 +02:00
|
|
|
EmailEntity::dispatch($invitation, $invoice->company)->delay(now()->addSeconds(60));
|
2021-04-30 01:01:56 +02:00
|
|
|
}
|
|
|
|
catch(\Exception $e) {
|
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2021-04-21 05:54:10 +02:00
|
|
|
nlog("Firing email for invoice {$invoice->number}");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-30 03:28:49 +02:00
|
|
|
if ($invoice->client->getSetting('auto_bill_date') == 'on_send_date' && $this->recurring_invoice->auto_bill_enabled) {
|
|
|
|
nlog("attempting to autobill {$invoice->number}");
|
|
|
|
$invoice->service()->autoBill()->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
2020-09-08 12:34:14 +02:00
|
|
|
|
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)
|
|
|
|
->batch();
|
|
|
|
|
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
|
|
|
}
|