2023-01-08 06:15:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2023-01-08 06:15:33 +01:00
|
|
|
*
|
|
|
|
* @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-02-17 22:36:51 +01:00
|
|
|
use App\DataMapper\Schedule\EmailStatement;
|
2023-01-13 23:46:17 +01:00
|
|
|
use App\Models\Client;
|
2023-02-16 22:59:19 +01:00
|
|
|
use App\Models\RecurringInvoice;
|
2023-02-17 22:36:51 +01:00
|
|
|
use App\Models\Scheduler;
|
2023-02-16 22:59:19 +01:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2023-02-17 22:36:51 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
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-02-16 02:36:09 +01:00
|
|
|
public function __construct(public Scheduler $scheduler)
|
|
|
|
{
|
|
|
|
}
|
2023-01-08 06:15:33 +01:00
|
|
|
|
2023-01-13 23:46:17 +01:00
|
|
|
/**
|
|
|
|
* Called from the TaskScheduler Cron
|
2023-02-16 02:36:09 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2023-01-13 23:46:17 +01:00
|
|
|
*/
|
|
|
|
public function runTask(): void
|
2023-01-08 06:15:33 +01:00
|
|
|
{
|
2023-01-13 23:46:17 +01:00
|
|
|
$this->{$this->scheduler->template}();
|
|
|
|
}
|
|
|
|
|
2023-02-17 06:02:46 +01:00
|
|
|
private function email_statement()
|
2023-02-16 02:36:09 +01:00
|
|
|
{
|
2023-01-13 23:46:17 +01:00
|
|
|
$query = Client::query()
|
2023-01-17 08:25:43 +01:00
|
|
|
->where('company_id', $this->scheduler->company_id)
|
2023-02-16 02:36:09 +01:00
|
|
|
->where('is_deleted', 0);
|
2023-01-13 23:46:17 +01:00
|
|
|
|
|
|
|
//Email only the selected clients
|
2023-02-16 02:36:09 +01:00
|
|
|
if (count($this->scheduler->parameters['clients']) >= 1) {
|
2023-01-18 00:34:06 +01:00
|
|
|
$query->whereIn('id', $this->transformKeys($this->scheduler->parameters['clients']));
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2023-01-14 08:47:14 +01:00
|
|
|
|
2023-01-13 23:46:17 +01:00
|
|
|
$query->cursor()
|
2023-02-16 02:36:09 +01:00
|
|
|
->each(function ($_client) {
|
|
|
|
$this->client = $_client;
|
2023-01-13 23:46:17 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
//work out the date range
|
|
|
|
$statement_properties = $this->calculateStatementProperties();
|
2023-01-17 10:48:10 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$_client->service()->statement($statement_properties, true);
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2023-01-17 10:48:10 +01:00
|
|
|
/**
|
|
|
|
* Hydrates the array needed to generate the statement
|
2023-02-16 02:36:09 +01:00
|
|
|
*
|
2023-01-17 10:48:10 +01:00
|
|
|
* @return array The statement options array
|
|
|
|
*/
|
|
|
|
private function calculateStatementProperties(): array
|
2023-01-14 08:47:14 +01:00
|
|
|
{
|
|
|
|
$start_end = $this->calculateStartAndEndDates();
|
|
|
|
|
|
|
|
return [
|
2023-02-16 02:36:09 +01:00
|
|
|
'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
|
|
|
];
|
|
|
|
}
|
2023-02-16 22:59:19 +01:00
|
|
|
|
2023-01-17 10:48:10 +01:00
|
|
|
/**
|
|
|
|
* Start and end date of the statement
|
2023-02-16 02:36:09 +01:00
|
|
|
*
|
2023-01-17 10:48:10 +01:00
|
|
|
* @return array [$start_date, $end_date];
|
|
|
|
*/
|
|
|
|
private function calculateStartAndEndDates(): array
|
2023-01-14 08:47:14 +01:00
|
|
|
{
|
|
|
|
return match ($this->scheduler->parameters['date_range']) {
|
2023-02-17 22:06:53 +01:00
|
|
|
EmailStatement::LAST7 => [now()->startOfDay()->subDays(7)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
|
|
|
|
EmailStatement::LAST30 => [now()->startOfDay()->subDays(30)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
|
|
|
|
EmailStatement::LAST365 => [now()->startOfDay()->subDays(365)->format('Y-m-d'), now()->startOfDay()->format('Y-m-d')],
|
|
|
|
EmailStatement::THIS_MONTH => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
|
|
|
|
EmailStatement::LAST_MONTH => [now()->startOfDay()->subMonthNoOverflow()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->subMonthNoOverflow()->lastOfMonth()->format('Y-m-d')],
|
|
|
|
EmailStatement::THIS_QUARTER => [now()->startOfDay()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->lastOfQuarter()->format('Y-m-d')],
|
|
|
|
EmailStatement::LAST_QUARTER => [now()->startOfDay()->subQuarterNoOverflow()->firstOfQuarter()->format('Y-m-d'), now()->startOfDay()->subQuarterNoOverflow()->lastOfQuarter()->format('Y-m-d')],
|
|
|
|
EmailStatement::THIS_YEAR => [now()->startOfDay()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->lastOfYear()->format('Y-m-d')],
|
|
|
|
EmailStatement::LAST_YEAR => [now()->startOfDay()->subYearNoOverflow()->firstOfYear()->format('Y-m-d'), now()->startOfDay()->subYearNoOverflow()->lastOfYear()->format('Y-m-d')],
|
|
|
|
EmailStatement::CUSTOM_RANGE => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']],
|
2023-02-16 02:36:09 +01:00
|
|
|
default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
|
2023-01-14 08:47:14 +01:00
|
|
|
};
|
2023-01-13 23:46:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 10:48:10 +01:00
|
|
|
/**
|
|
|
|
* Sets the next run date of the scheduled task
|
2023-02-16 02:36:09 +01:00
|
|
|
*
|
2023-01-17 10:48:10 +01:00
|
|
|
*/
|
|
|
|
private function calculateNextRun()
|
2023-01-17 09:42:34 +01:00
|
|
|
{
|
|
|
|
if (! $this->scheduler->next_run) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset = $this->scheduler->company->timezone_offset();
|
|
|
|
|
|
|
|
switch ($this->scheduler->frequency_id) {
|
|
|
|
case RecurringInvoice::FREQUENCY_DAILY:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addDay();
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_WEEKLY:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addWeek();
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_WEEKS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addWeeks(2);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_WEEKS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addWeeks(4);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_MONTHLY:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addMonthNoOverflow();
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_MONTHS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(2);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_MONTHS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(3);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_FOUR_MONTHS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(4);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_SIX_MONTHS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addMonthsNoOverflow(6);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_ANNUALLY:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addYear();
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_TWO_YEARS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addYears(2);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
case RecurringInvoice::FREQUENCY_THREE_YEARS:
|
2023-01-18 00:39:01 +01:00
|
|
|
$next_run = now()->startOfDay()->addYears(3);
|
2023-01-17 09:42:34 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$next_run = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$this->scheduler->next_run_client = $next_run ?: null;
|
2023-01-17 09:42:34 +01:00
|
|
|
$this->scheduler->next_run = $next_run ? $next_run->copy()->addSeconds($offset) : null;
|
|
|
|
$this->scheduler->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
//handle when the scheduler has been paused.
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|