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
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @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;
|
|
|
|
|
|
|
|
use App\Factory\RecurringInvoiceToInvoiceFactory;
|
2019-05-23 08:15:06 +02:00
|
|
|
use App\Models\Invoice;
|
2019-05-23 07:08:31 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
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;
|
2019-05-23 07:08:31 +02:00
|
|
|
use Illuminate\Http\Request;
|
2020-02-24 11:15:30 +01:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2019-05-23 07:08:31 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
|
|
|
|
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;
|
|
|
|
|
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
|
2020-02-26 04:26:07 +01:00
|
|
|
$invoice = RecurringInvoiceToInvoiceFactory::create($this->recurring_invoice, $this->recurring_invoice->client);
|
2019-11-27 11:27:24 +01:00
|
|
|
$invoice->number = $this->getNextRecurringInvoiceNumber($this->recurring_invoice->client);
|
2019-05-23 08:15:06 +02:00
|
|
|
$invoice->status_id = Invoice::STATUS_SENT;
|
2019-05-23 07:08:31 +02:00
|
|
|
$invoice->save();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
// Queue: Emails for invoice
|
2019-05-31 03:10:05 +02:00
|
|
|
// foreach invoice->invitations
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-05-23 07:08:31 +02:00
|
|
|
// Fire Payment if auto-bill is enabled
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($this->recurring_invoice->settings->auto_bill) {
|
2019-05-23 08:15:06 +02:00
|
|
|
//PAYMENT ACTION HERE TODO
|
2019-05-23 07:08:31 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
// Clean up recurring invoice object
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$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');
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($this->recurring_invoice->remaining_cycles != 0) {
|
2020-04-15 02:30:52 +02:00
|
|
|
$this->recurring_invoice->next_send_date = $this->recurring_invoice->nextSendDate()->format('Y-m-d');
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-05-23 07:08:31 +02:00
|
|
|
$this->recurring_invoice->setCompleted();
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->recurring_invoice->save();
|
2019-05-23 07:08:31 +02:00
|
|
|
}
|
|
|
|
}
|