2020-07-22 05:03:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-07-22 05:03:33 +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)
|
2020-07-22 05:03:33 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Recurring;
|
|
|
|
|
2021-03-01 00:40:18 +01:00
|
|
|
use App\Jobs\Util\UnlinkFile;
|
2020-07-22 05:03:33 +02:00
|
|
|
use App\Models\RecurringInvoice;
|
2021-01-15 03:02:55 +01:00
|
|
|
use App\Services\Recurring\GetInvoicePdf;
|
2020-09-24 13:03:59 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2020-07-22 05:03:33 +02:00
|
|
|
|
|
|
|
class RecurringService
|
|
|
|
{
|
|
|
|
protected $recurring_entity;
|
|
|
|
|
|
|
|
public function __construct($recurring_entity)
|
|
|
|
{
|
|
|
|
$this->recurring_entity = $recurring_entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set schedules - update next_send_dates
|
2020-09-24 02:57:44 +02:00
|
|
|
|
|
|
|
/**
|
2020-11-25 15:19:52 +01:00
|
|
|
* Stops a recurring invoice
|
|
|
|
*
|
2020-09-24 02:57:44 +02:00
|
|
|
* @return $this RecurringService object
|
|
|
|
*/
|
|
|
|
public function stop()
|
|
|
|
{
|
2021-02-01 21:14:38 +01:00
|
|
|
if($this->recurring_entity->status_id < RecurringInvoice::STATUS_PAUSED)
|
|
|
|
$this->recurring_entity->status_id = RecurringInvoice::STATUS_PAUSED;
|
2020-09-24 02:57:44 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-10-04 23:55:52 +02:00
|
|
|
public function createInvitations()
|
|
|
|
{
|
|
|
|
$this->recurring_entity = (new CreateRecurringInvitations($this->recurring_entity))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:57:44 +02:00
|
|
|
public function start()
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
//make sure next_send_date is either now or in the future else return.
|
|
|
|
// if(Carbon::parse($this->recurring_entity->next_send_date)->lt(now()))
|
|
|
|
// return $this;
|
2020-10-07 02:20:28 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if ($this->recurring_entity->remaining_cycles == 0) {
|
2020-10-07 02:20:28 +02:00
|
|
|
return $this;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-24 02:57:44 +02:00
|
|
|
|
2020-10-09 03:59:59 +02:00
|
|
|
$this->createInvitations()->setStatus(RecurringInvoice::STATUS_ACTIVE);
|
2020-09-24 02:57:44 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return $this;
|
2020-09-24 02:57:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 03:59:59 +02:00
|
|
|
public function setStatus($status)
|
|
|
|
{
|
|
|
|
$this->recurring_entity->status_id = $status;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-10-05 23:46:47 +02:00
|
|
|
/**
|
|
|
|
* Applies the invoice number.
|
|
|
|
* @return $this InvoiceService object
|
|
|
|
*/
|
|
|
|
public function applyNumber()
|
|
|
|
{
|
|
|
|
$this->recurring_entity = (new ApplyNumber($this->recurring_entity->client, $this->recurring_entity))->run();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-01-15 03:02:55 +01:00
|
|
|
public function getInvoicePdf($contact = null)
|
|
|
|
{
|
|
|
|
return (new GetInvoicePdf($this->recurring_entity, $contact))->run();
|
|
|
|
}
|
|
|
|
|
2021-03-01 00:40:18 +01:00
|
|
|
public function deletePdf()
|
|
|
|
{
|
2021-03-17 12:29:20 +01:00
|
|
|
UnlinkFile::dispatchNow(config('filesystems.default'), $this->recurring_entity->client->recurring_invoice_filepath() . $this->recurring_entity->numberFormatter().'.pdf');
|
2021-03-01 00:40:18 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:57:44 +02:00
|
|
|
public function save()
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
$this->recurring_entity->save();
|
2020-09-24 02:57:44 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return $this->recurring_entity;
|
2020-09-24 02:57:44 +02:00
|
|
|
}
|
2020-07-22 05:03:33 +02:00
|
|
|
}
|