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
*
2023-01-28 23:21:40 +01:00
* @ copyright Copyright ( c ) 2023. Invoice Ninja LLC ( https :// invoiceninja . com )
2020-07-22 05:03:33 +02:00
*
2021-06-16 08:58:16 +02:00
* @ license https :// www . elastic . co / licensing / elastic - license
2020-07-22 05:03:33 +02:00
*/
namespace App\Services\Recurring ;
2023-06-30 07:43:28 +02:00
use App\Utils\Ninja ;
2021-03-01 00:40:18 +01:00
use App\Jobs\Util\UnlinkFile ;
2023-04-26 13:18:01 +02:00
use App\Models\RecurringQuote ;
use Illuminate\Support\Carbon ;
2023-03-15 06:17:24 +01:00
use App\Models\RecurringExpense ;
use App\Models\RecurringInvoice ;
2023-06-30 07:43:28 +02:00
use Illuminate\Support\Facades\Storage ;
2023-04-26 13:18:01 +02:00
use App\Jobs\RecurringInvoice\SendRecurring ;
2020-07-22 05:03:33 +02:00
class RecurringService
{
2023-04-26 13:18:01 +02:00
public function __construct ( public RecurringInvoice | RecurringExpense | RecurringQuote $recurring_entity )
2020-07-22 05:03:33 +02:00
{
}
//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 ()
{
2023-02-16 02:36:09 +01:00
if ( $this -> recurring_entity -> status_id < RecurringInvoice :: STATUS_PAUSED ) {
2021-02-01 21:14:38 +01:00
$this -> recurring_entity -> status_id = RecurringInvoice :: STATUS_PAUSED ;
2023-02-16 02:36:09 +01:00
}
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 ()
{
2023-03-05 22:03:48 +01:00
if ( $this -> recurring_entity -> remaining_cycles == 0 || $this -> recurring_entity -> is_deleted ) {
2020-10-07 02:20:28 +02:00
return $this ;
2020-11-25 15:19:52 +01:00
}
2023-03-05 22:03:48 +01:00
2023-03-18 08:24:56 +01:00
if ( $this -> recurring_entity -> trashed ()) {
2023-03-05 22:03:48 +01:00
$this -> recurring_entity -> restore ();
2023-03-18 08:24:56 +01:00
}
2020-09-24 02:57:44 +02:00
2021-08-06 13:55:54 +02:00
$this -> setStatus ( RecurringInvoice :: STATUS_ACTIVE );
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 ()
{
2023-02-16 02:36:09 +01:00
$this -> recurring_entity -> invitations -> each ( function ( $invitation ) {
2023-06-30 07:43:28 +02:00
// (new UnlinkFile(config('filesystems.default'), $this->recurring_entity->client->recurring_invoice_filepath($invitation) . $this->recurring_entity->numberFormatter().'.pdf'))->handle();
//30-06-2023
try {
Storage :: disk ( config ( 'filesystems.default' )) -> delete ( $this -> recurring_entity -> client -> recurring_invoice_filepath ( $invitation ) . $this -> recurring_entity -> numberFormatter () . '.pdf' );
// if (Storage::disk(config('filesystems.default'))->exists($this->invoice->client->invoice_filepath($invitation).$this->invoice->numberFormatter().'.pdf')) {
// }
// if (Ninja::isHosted() && Storage::disk('public')->exists($this->invoice->client->invoice_filepath($invitation).$this->invoice->numberFormatter().'.pdf')) {
Storage :: disk ( 'public' ) -> delete ( $this -> recurring_entity -> client -> recurring_invoice_filepath ( $invitation ) . $this -> recurring_entity -> numberFormatter () . '.pdf' );
if ( Ninja :: isHosted ()) {
}
} catch ( \Exception $e ) {
nlog ( $e -> getMessage ());
}
2021-06-12 13:50:01 +02:00
});
2021-03-01 00:40:18 +01:00
return $this ;
}
2021-10-13 07:41:11 +02:00
public function triggeredActions ( $request )
{
if ( $request -> has ( 'start' ) && $request -> input ( 'start' ) == 'true' ) {
$this -> start ();
}
if ( $request -> has ( 'stop' ) && $request -> input ( 'stop' ) == 'true' ) {
$this -> stop ();
}
2022-07-11 02:33:41 +02:00
if ( $request -> has ( 'send_now' ) && $request -> input ( 'send_now' ) == 'true' && $this -> recurring_entity -> invoices () -> count () == 0 ) {
$this -> sendNow ();
2022-09-16 04:49:09 +02:00
return $this ;
2022-07-11 02:33:41 +02:00
}
2023-02-16 02:36:09 +01:00
if ( isset ( $this -> recurring_entity -> client )) {
2022-06-02 05:49:29 +02:00
$offset = $this -> recurring_entity -> client -> timezone_offset ();
$this -> recurring_entity -> next_send_date = Carbon :: parse ( $this -> recurring_entity -> next_send_date_client ) -> startOfDay () -> addSeconds ( $offset );
}
2021-10-13 07:41:11 +02:00
return $this ;
}
2022-07-11 02:33:41 +02:00
public function sendNow ()
{
2023-02-16 02:36:09 +01:00
if ( $this -> recurring_entity instanceof RecurringInvoice && $this -> recurring_entity -> status_id == RecurringInvoice :: STATUS_DRAFT ) {
2022-07-12 12:47:17 +02:00
$this -> start () -> save ();
2023-02-16 02:36:09 +01:00
( new SendRecurring ( $this -> recurring_entity , $this -> recurring_entity -> company -> db )) -> handle ();
2022-07-12 12:47:17 +02:00
}
2022-07-11 02:33:41 +02:00
2022-09-16 04:49:09 +02:00
$this -> recurring_entity = $this -> recurring_entity -> fresh ();
return $this ;
2022-07-11 02:33:41 +02:00
}
2021-09-25 01:26:27 +02:00
public function fillDefaults ()
{
return $this ;
}
2023-03-15 06:17:24 +01:00
2023-03-15 07:01:55 +01:00
public function increasePrice ( float $percentage )
2023-03-15 06:17:24 +01:00
{
2023-03-15 07:01:55 +01:00
( new IncreasePrice ( $this -> recurring_entity , $percentage )) -> run ();
2023-03-15 06:17:24 +01:00
2023-03-15 07:01:55 +01:00
return $this ;
2023-03-15 06:17:24 +01:00
}
2023-03-15 07:01:55 +01:00
public function updatePrice ()
2023-03-15 06:17:24 +01:00
{
2023-03-15 07:01:55 +01:00
( new UpdatePrice ( $this -> recurring_entity )) -> run ();
2023-03-15 06:17:24 +01:00
2023-03-15 07:01:55 +01:00
return $this ;
2023-03-15 06:17:24 +01:00
}
2021-09-25 01:26:27 +02:00
2020-09-24 02:57:44 +02:00
public function save ()
{
2021-10-10 11:56:05 +02:00
$this -> recurring_entity -> saveQuietly ();
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
}