invoiceninja/app/Services/Scheduler/SchedulerService.php

129 lines
4.6 KiB
PHP
Raw Normal View History

2023-01-08 16:15:33 +11: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 12:43:38 +11:00
namespace App\Services\Scheduler;
2023-01-08 16:15:33 +11:00
2023-01-15 13:28:46 +11:00
use App\DataMapper\EmailTemplateDefaults;
2023-01-14 22:00:22 +11:00
use App\Mail\Client\ClientStatement;
2023-01-14 09:46:17 +11:00
use App\Models\Client;
2023-01-13 12:43:38 +11:00
use App\Models\Scheduler;
2023-01-15 13:28:46 +11:00
use App\Services\Email\EmailMailable;
use App\Services\Email\EmailObject;
use App\Services\Email\EmailService;
2023-01-14 22:00:22 +11:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesDates;
2023-01-14 09:46:17 +11:00
use App\Utils\Traits\MakesHash;
2023-01-14 22:00:22 +11:00
use Illuminate\Mail\Mailables\Address;
2023-01-14 09:46:17 +11:00
use Illuminate\Support\Str;
2023-01-13 12:43:38 +11:00
2023-01-13 22:24:23 +11:00
class SchedulerService
2023-01-08 16:15:33 +11:00
{
2023-01-14 09:46:17 +11:00
use MakesHash;
2023-01-14 22:00:22 +11:00
use MakesDates;
2023-01-14 09:46:17 +11:00
private string $method;
2023-01-08 16:15:33 +11:00
2023-01-14 22:00:22 +11:00
private Client $client;
2023-01-08 16:15:33 +11:00
public function __construct(public Scheduler $scheduler) {}
2023-01-14 09:46:17 +11:00
/**
* Called from the TaskScheduler Cron
*
* @return void
*/
public function runTask(): void
2023-01-08 16:15:33 +11:00
{
2023-01-14 09:46:17 +11:00
$this->{$this->scheduler->template}();
}
private function client_statement()
{
$query = Client::query()
->where('company_id', $this->scheduler->company_id);
//Email only the selected clients
if(count($this->scheduler->parameters['clients']) >= 1)
$query->where('id', $this->transformKeys($this->scheduler->parameters['clients']));
2023-01-14 18:47:14 +11:00
2023-01-14 09:46:17 +11:00
$query->cursor()
2023-01-14 22:00:22 +11:00
->each(function ($_client){
$this->client = $_client;
$statement_properties = $this->calculateStatementProperties();
2023-01-14 09:46:17 +11:00
//work out the date range
2023-01-14 22:00:22 +11:00
$pdf = $_client->service()->statement($statement_properties);
2023-01-15 13:28:46 +11:00
$email_service = new EmailService($this->buildMailableData($pdf), $_client->company);
$email_service->send();
2023-01-14 22:00:22 +11:00
2023-01-15 13:28:46 +11:00
//calculate next run dates;
2023-01-14 09:46:17 +11:00
});
2023-01-14 18:47:14 +11:00
}
private function calculateStatementProperties()
{
$start_end = $this->calculateStartAndEndDates();
2023-01-14 22:00:22 +11:00
$this->client_start_date = $this->translateDate($start_end[0], $this->client->date_format(), $this->client->locale());
$this->client_end_date = $this->translateDate($start_end[1], $this->client->date_format(), $this->client->locale());
2023-01-14 18:47:14 +11:00
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 22:00:22 +11:00
'status' => $this->scheduler->parameters['status']
2023-01-14 18:47:14 +11: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 22:00:22 +11:00
'this_year' => [now()->firstOfYear()->format('Y-m-d'), now()->lastOfYear()->format('Y-m-d')],
2023-01-14 18:47:14 +11: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 22:00:22 +11: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 13:28:46 +11:00
default => [now()->firstOfMonth()->format('Y-m-d'), now()->lastOfMonth()->format('Y-m-d')],
2023-01-14 18:47:14 +11:00
};
2023-01-14 09:46:17 +11:00
}
2023-01-14 22:00:22 +11:00
private function buildMailableData($pdf)
{
2023-01-15 13:28:46 +11:00
$email_object = new EmailObject;
$email_object->to = [new Address($this->client->present()->email(), $this->client->present()->name())];
2023-01-15 14:44:23 +11:00
$email_object->attachments = [['file' => base64_encode($pdf), 'name' => ctrans('texts.statement') . ".pdf"]];
$email_object->settings = $this->client->getMergedSettings();
$email_object->company = $this->client->company;
$email_object->client = $this->client;
$email_object->email_template_subject = 'email_subject_statement';
$email_object->email_template_body = 'email_template_statement';
$email_object->variables = [
'$client' => $this->client->present()->name(),
'$start_date' => $this->client_start_date,
'$end_date' => $this->client_end_date,
];
2023-01-14 22:00:22 +11:00
2023-01-15 13:28:46 +11:00
return $email_object;
2023-01-14 22:00:22 +11:00
}
2023-01-08 16:15:33 +11:00
}