2023-01-08 06:15:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
2023-01-13 02:43:38 +01:00
|
|
|
namespace App\Services\Scheduler;
|
2023-01-08 06:15:33 +01:00
|
|
|
|
2023-01-13 23:46:17 +01:00
|
|
|
use App\Models\Client;
|
2023-01-17 09:42:34 +01:00
|
|
|
use App\Models\RecurringInvoice;
|
2023-01-13 02:43:38 +01:00
|
|
|
use App\Models\Scheduler;
|
2023-01-14 12:00:22 +01:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2023-01-13 23:46:17 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-01-17 09:42:34 +01:00
|
|
|
use Carbon\Carbon;
|
2023-01-13 02:43:38 +01:00
|
|
|
|
2023-01-13 12:24:23 +01:00
|
|
|
class SchedulerService
|
2023-01-08 06:15:33 +01:00
|
|
|
{
|
2023-01-13 23:46:17 +01:00
|
|
|
use MakesHash;
|
2023-01-14 12:00:22 +01:00
|
|
|
use MakesDates;
|
2023-01-13 23:46:17 +01:00
|
|
|
|
|
|
|
private string $method;
|
2023-01-08 06:15:33 +01:00
|
|
|
|
2023-01-14 12:00:22 +01:00
|
|
|
private Client $client;
|
|
|
|
|
2023-01-08 06:15:33 +01:00
|
|
|
public function __construct(public Scheduler $scheduler) {}
|
|
|
|
|
2023-01-13 23:46:17 +01:00
|
|
|
/**
|
|
|
|
* Called from the TaskScheduler Cron
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function runTask(): void
|
2023-01-08 06:15:33 +01:00
|
|
|
{
|
2023-01-13 23:46:17 +01:00
|
|
|
$this->{$this->scheduler->template}();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function client_statement()
|
|
|
|
{
|
|
|
|
$query = Client::query()
|
2023-01-17 08:25:43 +01:00
|
|
|
->where('company_id', $this->scheduler->company_id)
|
|
|
|
->where('is_deleted',0);
|
2023-01-13 23:46:17 +01:00
|
|
|
|
|
|
|
//Email only the selected clients
|
|
|
|
if(count($this->scheduler->parameters['clients']) >= 1)
|
|
|
|
$query->where('id', $this->transformKeys($this->scheduler->parameters['clients']));
|
2023-01-14 08:47:14 +01:00
|
|
|
|
2023-01-13 23:46:17 +01:00
|
|
|
$query->cursor()
|
2023-01-14 12:00:22 +01:00
|
|
|
->each(function ($_client){
|
|
|
|
|
|
|
|
$this->client = $_client;
|
|
|
|
$statement_properties = $this->calculateStatementProperties();
|
2023-01-13 23:46:17 +01:00
|
|
|
|
|
|
|
//work out the date range
|
2023-01-17 01:00:12 +01:00
|
|
|
$pdf = $_client->service()->statement($statement_properties,true);
|
2023-01-14 12:00:22 +01:00
|
|
|
|
2023-01-13 23:46:17 +01:00
|
|
|
});
|
2023-01-14 08:47:14 +01:00
|
|
|
|
2023-01-17 09:42:34 +01:00
|
|
|
//calculate next run dates;
|
|
|
|
$this->calculateNextRun();
|
|
|
|
|
2023-01-14 08:47:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function calculateStatementProperties()
|
|
|
|
{
|
|
|
|
$start_end = $this->calculateStartAndEndDates();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'start_date' =>$start_end[0],
|
|
|
|
'end_date' =>$start_end[1],
|
|
|
|
'show_payments_table' => $this->scheduler->parameters['show_payments_table'],
|
|
|
|
'show_aging_table' => $this->scheduler->parameters['show_aging_table'],
|
2023-01-14 12:00:22 +01:00
|
|
|
'status' => $this->scheduler->parameters['status']
|
2023-01-14 08:47:14 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function calculateStartAndEndDates()
|
|
|
|
{
|
|
|
|
return match ($this->scheduler->parameters['date_range']) {
|
|
|
|
'this_month' => [now()->firstOfMonth()->format('Y-m-d'), now()->lastOfMonth()->format('Y-m-d')],
|
|
|
|
'this_quarter' => [now()->firstOfQuarter()->format('Y-m-d'), now()->lastOfQuarter()->format('Y-m-d')],
|
2023-01-14 12:00:22 +01:00
|
|
|
'this_year' => [now()->firstOfYear()->format('Y-m-d'), now()->lastOfYear()->format('Y-m-d')],
|
2023-01-14 08:47:14 +01:00
|
|
|
'previous_month' => [now()->subMonth()->firstOfMonth()->format('Y-m-d'), now()->subMonth()->lastOfMonth()->format('Y-m-d')],
|
|
|
|
'previous_quarter' => [now()->subQuarter()->firstOfQuarter()->format('Y-m-d'), now()->subQuarter()->lastOfQuarter()->format('Y-m-d')],
|
2023-01-14 12:00:22 +01:00
|
|
|
'previous_year' => [now()->subYear()->firstOfYear()->format('Y-m-d'), now()->subYear()->lastOfYear()->format('Y-m-d')],
|
|
|
|
'custom_range' => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']],
|
2023-01-15 03:28:46 +01:00
|
|
|
default => [now()->firstOfMonth()->format('Y-m-d'), now()->lastOfMonth()->format('Y-m-d')],
|
2023-01-14 08:47:14 +01:00
|
|
|
};
|
2023-01-13 23:46:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-14 12:00:22 +01:00
|
|
|
|
2023-01-17 09:42:34 +01:00
|
|
|
public function calculateNextRun() :?Carbon
|
|
|
|
{
|
|
|
|
if (! $this->scheduler->next_run) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = $this->scheduler->company->timezone_offset();
|
|
|
|
|
|
|
|
switch ($this->scheduler->frequency_id) {
|
|
|
|
case RecurringInvoice::FREQUENCY_DAILY:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addDay();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_WEEKLY:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addWeek();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addWeeks(2);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addWeeks(4);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_MONTHLY:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthNoOverflow();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(2);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(3);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(4);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addMonthsNoOverflow(6);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addYear();
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addYears(2);
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
|
|
|
$next_run = Carbon::parse($this->scheduler->next_run)->startOfDay()->addYears(3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$next_run = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$this->scheduler->next_run_client = $next_run ?: null;
|
|
|
|
$this->scheduler->next_run = $next_run ? $next_run->copy()->addSeconds($offset) : null;
|
|
|
|
$this->scheduler->save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//handle when the scheduler has been paused.
|
2023-01-14 12:00:22 +01:00
|
|
|
|
|
|
|
|
2023-01-08 06:15:33 +01:00
|
|
|
}
|