2023-03-18 09:06:32 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Scheduler;
|
|
|
|
|
2023-06-16 07:23:59 +02:00
|
|
|
use App\DataMapper\Schedule\EmailStatement;
|
2023-03-18 09:06:32 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Scheduler;
|
2023-04-13 05:31:19 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2023-06-16 07:23:59 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-06-15 19:46:11 +02:00
|
|
|
use Carbon\Carbon;
|
2023-03-18 09:06:32 +01:00
|
|
|
|
|
|
|
class EmailStatementService
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2023-04-13 05:31:19 +02:00
|
|
|
use MakesDates;
|
2023-03-18 09:06:32 +01:00
|
|
|
|
|
|
|
private Client $client;
|
|
|
|
|
|
|
|
public function __construct(public Scheduler $scheduler)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$query = Client::query()
|
|
|
|
->where('company_id', $this->scheduler->company_id)
|
2023-06-02 03:54:40 +02:00
|
|
|
->where('is_deleted', 0);
|
2023-03-18 09:06:32 +01:00
|
|
|
|
|
|
|
//Email only the selected clients
|
|
|
|
if (count($this->scheduler->parameters['clients']) >= 1) {
|
2023-06-02 03:53:47 +02:00
|
|
|
$query->whereIn('id', $this->transformKeys($this->scheduler->parameters['clients']));
|
2023-06-16 07:23:59 +02:00
|
|
|
} else {
|
2023-06-02 03:54:40 +02:00
|
|
|
$query->where('balance', '>', 0);
|
2023-03-18 09:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$query->cursor()
|
|
|
|
->each(function ($_client) {
|
|
|
|
$this->client = $_client;
|
|
|
|
|
|
|
|
//work out the date range
|
2023-06-15 19:46:11 +02:00
|
|
|
$statement_properties = $this->calculateStatementProperties($_client);
|
2023-03-18 09:06:32 +01:00
|
|
|
|
|
|
|
$_client->service()->statement($statement_properties, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
//calculate next run dates;
|
|
|
|
$this->scheduler->calculateNextRun();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hydrates the array needed to generate the statement
|
|
|
|
*
|
|
|
|
* @return array The statement options array
|
|
|
|
*/
|
2023-06-15 19:46:11 +02:00
|
|
|
private function calculateStatementProperties(Client $client): array
|
2023-03-18 09:06:32 +01:00
|
|
|
{
|
2023-06-15 19:46:11 +02:00
|
|
|
$start_end = $this->calculateStartAndEndDates($client);
|
2023-03-18 09:06:32 +01:00
|
|
|
|
|
|
|
return [
|
2023-06-15 19:46:11 +02:00
|
|
|
'start_date' => $start_end[0],
|
|
|
|
'end_date' => $start_end[1],
|
2023-04-26 04:13:02 +02:00
|
|
|
'show_payments_table' => $this->scheduler->parameters['show_payments_table'] ?? true,
|
|
|
|
'show_aging_table' => $this->scheduler->parameters['show_aging_table'] ?? true,
|
|
|
|
'show_credits_table' => $this->scheduler->parameters['show_credits_table'] ?? true,
|
2023-06-15 19:46:11 +02:00
|
|
|
'only_clients_with_invoices' => $this->scheduler->parameters['only_clients_with_invoices'] ?? false,
|
2023-03-18 09:06:32 +01:00
|
|
|
'status' => $this->scheduler->parameters['status']
|
|
|
|
];
|
|
|
|
}
|
2023-06-13 21:23:54 +02:00
|
|
|
|
2023-03-18 09:06:32 +01:00
|
|
|
/**
|
|
|
|
* Start and end date of the statement
|
|
|
|
*
|
|
|
|
* @return array [$start_date, $end_date];
|
|
|
|
*/
|
2023-06-15 19:46:11 +02:00
|
|
|
private function calculateStartAndEndDates(Client $client): array
|
2023-03-18 09:06:32 +01:00
|
|
|
{
|
|
|
|
return match ($this->scheduler->parameters['date_range']) {
|
|
|
|
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')],
|
2023-06-15 19:46:11 +02:00
|
|
|
EmailStatement::ALL_TIME => [
|
2023-06-16 06:44:43 +02:00
|
|
|
$client->invoices()->selectRaw('MIN(invoices.date) as start_date')->pluck('start_date')->first()
|
2023-06-15 19:46:11 +02:00
|
|
|
?: Carbon::now()->format('Y-m-d'),
|
|
|
|
Carbon::now()->format('Y-m-d')
|
|
|
|
],
|
2023-03-18 09:06:32 +01:00
|
|
|
EmailStatement::CUSTOM_RANGE => [$this->scheduler->parameters['start_date'], $this->scheduler->parameters['end_date']],
|
|
|
|
default => [now()->startOfDay()->firstOfMonth()->format('Y-m-d'), now()->startOfDay()->lastOfMonth()->format('Y-m-d')],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|