2017-11-23 11:23:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App;
|
|
|
|
use Str;
|
|
|
|
use Utils;
|
|
|
|
use Carbon;
|
|
|
|
use App\Jobs\Job;
|
|
|
|
|
|
|
|
class RunReport extends Job
|
|
|
|
{
|
|
|
|
public function __construct($user, $reportType, $config, $isExport = false)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
$this->reportType = $reportType;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->isExport = $isExport;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2018-06-07 12:08:34 +02:00
|
|
|
if (! $this->user->hasPermission('view_reports')) {
|
2017-11-23 11:23:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$reportType = $this->reportType;
|
|
|
|
$config = $this->config;
|
2018-04-23 11:17:16 +02:00
|
|
|
$config['subgroup'] = ! empty($config['subgroup']) ? $config['subgroup'] : false; // don't yet support charts in export
|
2018-03-21 15:40:39 +01:00
|
|
|
|
2017-11-23 11:23:19 +01:00
|
|
|
$isExport = $this->isExport;
|
|
|
|
$reportClass = '\\App\\Ninja\\Reports\\' . Str::studly($reportType) . 'Report';
|
|
|
|
|
|
|
|
if (! empty($config['range'])) {
|
|
|
|
switch ($config['range']) {
|
|
|
|
case 'this_month':
|
|
|
|
$startDate = Carbon::now()->firstOfMonth()->toDateString();
|
|
|
|
$endDate = Carbon::now()->lastOfMonth()->toDateString();
|
|
|
|
break;
|
|
|
|
case 'last_month':
|
|
|
|
$startDate = Carbon::now()->subMonth()->firstOfMonth()->toDateString();
|
|
|
|
$endDate = Carbon::now()->subMonth()->lastOfMonth()->toDateString();
|
|
|
|
break;
|
|
|
|
case 'this_year':
|
|
|
|
$startDate = Carbon::now()->firstOfYear()->toDateString();
|
|
|
|
$endDate = Carbon::now()->lastOfYear()->toDateString();
|
|
|
|
break;
|
|
|
|
case 'last_year':
|
|
|
|
$startDate = Carbon::now()->subYear()->firstOfYear()->toDateString();
|
|
|
|
$endDate = Carbon::now()->subYear()->lastOfYear()->toDateString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} elseif (! empty($config['start_date_offset'])) {
|
|
|
|
$startDate = Carbon::now()->subDays($config['start_date_offset'])->toDateString();
|
|
|
|
$endDate = Carbon::now()->subDays($config['end_date_offset'])->toDateString();
|
|
|
|
} else {
|
|
|
|
$startDate = $config['start_date'];
|
|
|
|
$endDate = $config['end_date'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$report = new $reportClass($startDate, $endDate, $isExport, $config);
|
|
|
|
$report->run();
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'startDate' => $startDate,
|
|
|
|
'endDate' => $endDate,
|
|
|
|
'report' => $report,
|
|
|
|
];
|
2017-11-23 12:42:58 +01:00
|
|
|
|
2017-11-23 11:23:19 +01:00
|
|
|
$report->exportParams = array_merge($params, $report->results());
|
|
|
|
|
|
|
|
return $report;
|
|
|
|
}
|
|
|
|
}
|