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

83 lines
2.3 KiB
PHP
Raw Normal View History

2019-05-23 07:08:31 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @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;
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;
use App\Utils\Traits\GeneratesCounter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
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;
use Illuminate\Foundation\Bus\Dispatchable;
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;
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);
$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();
// Queue: Emails for invoice
// foreach invoice->invitations
2019-05-23 07:08:31 +02:00
// Fire Payment if auto-bill is enabled
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
// Clean up recurring invoice object
2019-05-23 07:08:31 +02: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');
if ($this->recurring_invoice->remaining_cycles != 0) {
2019-05-23 07:08:31 +02:00
$this->recurring_invoice->next_send_date = $this->recurring_invoice->nextSendDate();
} else {
2019-05-23 07:08:31 +02:00
$this->recurring_invoice->setCompleted();
}
2019-05-23 07:08:31 +02:00
$this->recurring_invoice->save();
2019-05-23 07:08:31 +02:00
}
}