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

96 lines
2.9 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
*
* @copyright Copyright (c) 2020. 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-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-09-08 12:34:14 +02:00
use App\Helpers\Email\InvoiceEmail;
use App\Jobs\Invoice\EmailInvoice;
2019-05-23 08:15:06 +02:00
use App\Models\Invoice;
2019-05-23 07:08:31 +02:00
use App\Models\RecurringInvoice;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2019-05-23 07:08:31 +02:00
use Illuminate\Http\Request;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2019-05-23 07:08:31 +02:00
use Illuminate\Support\Carbon;
class SendRecurring implements ShouldQueue
2019-05-23 07:08:31 +02:00
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use GeneratesCounter;
2019-05-23 07:08:31 +02:00
public $recurring_invoice;
2019-05-24 00:37:47 +02:00
protected $db;
2019-05-23 07:08:31 +02:00
/**
* Create a new job instance.
*
* @return void
*/
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
$invoice = RecurringInvoiceToInvoiceFactory::create($this->recurring_invoice, $this->recurring_invoice->client);
2020-09-08 12:34:14 +02:00
$invoice = $invoice->service()
->markSent()
->applyRecurringNumber()
->createInvitations()
->save();
2019-05-23 07:08:31 +02:00
2020-09-08 12:34:14 +02:00
$invoice->invitations->each(function ($invitation) use ($invoice) {
2020-09-08 12:34:14 +02:00
$email_builder = (new InvoiceEmail())->build($invitation);
EmailInvoice::dispatch($email_builder, $invitation, $invoice->company);
2019-05-23 07:08:31 +02:00
2020-09-08 12:34:14 +02:00
info("Firing email for invoice {$invoice->number}");
2020-09-08 12:34:14 +02:00
});
/* Set next date here to prevent a recurring loop forming */
$this->recurring_invoice->next_send_date = $this->recurring_invoice->nextSendDate()->format('Y-m-d');
$this->recurring_invoice->remaining_cycles = $this->recurring_invoice->remainingCycles();
2019-05-23 07:08:31 +02:00
$this->recurring_invoice->last_sent_date = date('Y-m-d');
2020-09-08 12:34:14 +02:00
/* Set completed if we don't have any more cycles remaining*/
if ($this->recurring_invoice->remaining_cycles == 0)
2019-05-23 07:08:31 +02:00
$this->recurring_invoice->setCompleted();
$this->recurring_invoice->save();
2020-09-08 12:34:14 +02:00
if ($invoice->invitations->count() > 0)
event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars()));
// Fire Payment if auto-bill is enabled
if ($this->recurring_invoice->auto_bill)
$invoice->service()->autoBill()->save();
2019-05-23 07:08:31 +02:00
}
2020-09-08 12:34:14 +02:00
2019-05-23 07:08:31 +02:00
}