Product Report Scheduler

This commit is contained in:
David Bomba 2023-04-13 13:31:19 +10:00
parent 0a49bd4afa
commit 3a778525c3
5 changed files with 170 additions and 3 deletions

View File

@ -0,0 +1,70 @@
<?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\DataMapper\Schedule;
class EmailProductSalesReport
{
/**
* Defines the template name
*
* @var string
*/
public string $template = 'email_product_sales_report';
/**
* An array of clients hashed_ids
*
* Leave blank if this action should apply to all clients
*
* @var array
*/
public array $clients = [];
/**
* The consts to be used to define the date_range variable of the statement
*/
public const LAST7 = "last7_days";
public const LAST30 = "last30_days";
public const LAST365 = "last365_days";
public const THIS_MONTH = "this_month";
public const LAST_MONTH = "last_month";
public const THIS_QUARTER = "this_quarter";
public const LAST_QUARTER = "last_quarter";
public const THIS_YEAR = "this_year";
public const LAST_YEAR = "last_year";
public const CUSTOM_RANGE = "custom";
/**
* The date range the statement should include
*
* @var string
*/
public string $date_range = 'this_month';
/**
* If a custom range is select for the date range then
* the start_date should be supplied in Y-m-d format
*
* @var string
*/
public string $start_date = '';
/**
* If a custom range is select for the date range then
* the end_date should be supplied in Y-m-d format
*
* @var string
*/
public string $end_date = '';
}

View File

@ -0,0 +1,67 @@
<?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;
use App\DataMapper\Schedule\EmailStatement;
use App\Models\Client;
use App\Models\Scheduler;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
class EmailProductSalesReport
{
use MakesHash;
use MakesDates;
private Client $client;
private bool $multiple_clients = false;
public function __construct(public Scheduler $scheduler)
{
}
public function run()
{
$query = Client::query()
->where('company_id', $this->scheduler->company_id)
->where('is_deleted', 0);
//Email only the selected clients
$start_end_dates = $this->calculateStartAndEndDates();
if (count($this->scheduler->parameters['clients']) >= 1) {
$query->whereIn('id', $this->transformKeys($this->scheduler->parameters['clients']));
}
$data = [
'start_date' => $start_end_dates[0],
'end_date' => $start_end_dates[1],
'date_range' => 'custom',
'client_id' =>
];
$export = new ProductSalesExport($this->scheduler->company, $data);
//calculate next run dates;
$this->scheduler->calculateNextRun();
}
}

View File

@ -15,10 +15,12 @@ use App\Models\Client;
use App\Models\Scheduler;
use App\Utils\Traits\MakesHash;
use App\DataMapper\Schedule\EmailStatement;
use App\Utils\Traits\MakesDates;
class EmailStatementService
{
use MakesHash;
use MakesDates;
private Client $client;

View File

@ -12,8 +12,9 @@
namespace App\Services\Scheduler;
use App\Models\Scheduler;
use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\MakesDates;
use App\Services\Scheduler\EmailProductSalesReport;
class SchedulerService
{
@ -43,12 +44,15 @@ class SchedulerService
(new EmailRecord($this->scheduler))->run();
}
private function email_statement()
{
(new EmailStatementService($this->scheduler))->run();
}
private function email_product_sales_report()
{
(new EmailProductSalesReport($this->scheduler))->run();
}
/**
* Sets the next run date of the scheduled task

View File

@ -11,9 +11,10 @@
namespace App\Utils\Traits;
use Carbon\Carbon;
use DateTime;
use DateTimeZone;
use Carbon\Carbon;
use App\DataMapper\Schedule\EmailStatement;
/**
* Class MakesDates.
@ -112,4 +113,27 @@ trait MakesDates
return 'Invalid date!';
}
}
/**
* Start and end date of the statement
*
* @return array [$start_date, $end_date];
*/
public function calculateStartAndEndDates(): array
{
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')],
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')],
};
}
}