mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-08 19:34:39 -04:00
Working on scheduled reports
This commit is contained in:
parent
0b99e44907
commit
ace4985d24
@ -13,6 +13,7 @@ use App\Models\ScheduledReport;
|
|||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use App\Jobs\ExportReportResults;
|
use App\Jobs\ExportReportResults;
|
||||||
|
use App\Jobs\RunReport;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SendReminders.
|
* Class SendReminders.
|
||||||
@ -138,52 +139,21 @@ class SendReminders extends Command
|
|||||||
|
|
||||||
private function sendScheduledReports()
|
private function sendScheduledReports()
|
||||||
{
|
{
|
||||||
$scheduledReports = ScheduledReport::where('send_date', '=', date('Y-m-d'))->get();
|
$scheduledReports = ScheduledReport::where('send_date', '<=', date('Y-m-d'))->get();
|
||||||
$this->info(count($scheduledReports) . ' scheduled reports');
|
$this->info(count($scheduledReports) . ' scheduled reports');
|
||||||
|
|
||||||
foreach ($scheduledReports as $scheduledReport) {
|
foreach ($scheduledReports as $scheduledReport) {
|
||||||
$config = json_decode($scheduledReport->config);
|
$config = (array) json_decode($scheduledReport->config);
|
||||||
$reportType = $config->report_type;
|
$reportType = $config['report_type'];
|
||||||
$reportClass = '\\App\\Ninja\\Reports\\' . Str::studly($reportType) . 'Report';
|
|
||||||
|
|
||||||
if ($config->range) {
|
$report = dispatch(new RunReport($scheduledReport->user, $reportType, $config, true));
|
||||||
switch ($config->range) {
|
$file = dispatch(new ExportReportResults($scheduledReport->user, $config['export_format'], $reportType, $report->exportParams));
|
||||||
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;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$startDate = Carbon::now()->subDays($config->start_date)->toDateString();
|
|
||||||
$endDate = Carbon::now()->subDays($config->end_date)->toDateString();
|
|
||||||
}
|
|
||||||
|
|
||||||
$report = new $reportClass($startDate, $endDate, true, (array) $config);
|
|
||||||
$params = [
|
|
||||||
'startDate' => $startDate,
|
|
||||||
'endDate' => $endDate,
|
|
||||||
'report' => $report,
|
|
||||||
];
|
|
||||||
|
|
||||||
$report->run();
|
|
||||||
$params = array_merge($params, $report->results());
|
|
||||||
$file = dispatch(new ExportReportResults($scheduledReport->user, $config->export_format, $reportType, $params));
|
|
||||||
|
|
||||||
if ($file) {
|
if ($file) {
|
||||||
$this->userMailer->sendScheduledReport($scheduledReport, $file);
|
$this->userMailer->sendScheduledReport($scheduledReport, $file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$scheduledReport->updateSendDate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,6 +228,11 @@ if (! defined('APP_NAME')) {
|
|||||||
define('FREQUENCY_SIX_MONTHS', 8);
|
define('FREQUENCY_SIX_MONTHS', 8);
|
||||||
define('FREQUENCY_ANNUALLY', 9);
|
define('FREQUENCY_ANNUALLY', 9);
|
||||||
|
|
||||||
|
define('REPORT_FREQUENCY_DAILY', 'daily');
|
||||||
|
define('REPORT_FREQUENCY_WEEKLY', 'weekly');
|
||||||
|
define('REPORT_FREQUENCY_BIWEEKLY', 'biweekly');
|
||||||
|
define('REPORT_FREQUENCY_MONTHLY', 'monthly');
|
||||||
|
|
||||||
define('SESSION_TIMEZONE', 'timezone');
|
define('SESSION_TIMEZONE', 'timezone');
|
||||||
define('SESSION_CURRENCY', 'currency');
|
define('SESSION_CURRENCY', 'currency');
|
||||||
define('SESSION_CURRENCY_DECORATOR', 'currency_decorator');
|
define('SESSION_CURRENCY_DECORATOR', 'currency_decorator');
|
||||||
|
@ -3,11 +3,11 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Jobs\ExportReportResults;
|
use App\Jobs\ExportReportResults;
|
||||||
|
use App\Jobs\RunReport;
|
||||||
use App\Models\Account;
|
use App\Models\Account;
|
||||||
use App\Models\ScheduledReport;
|
use App\Models\ScheduledReport;
|
||||||
use Auth;
|
use Auth;
|
||||||
use Input;
|
use Input;
|
||||||
use Str;
|
|
||||||
use Utils;
|
use Utils;
|
||||||
use View;
|
use View;
|
||||||
use Carbon;
|
use Carbon;
|
||||||
@ -96,27 +96,24 @@ class ReportController extends BaseController
|
|||||||
|
|
||||||
if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) {
|
if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) {
|
||||||
$isExport = $action == 'export';
|
$isExport = $action == 'export';
|
||||||
$reportClass = '\\App\\Ninja\\Reports\\' . Str::studly($reportType) . 'Report';
|
$config = [
|
||||||
$options = [
|
|
||||||
'date_field' => $dateField,
|
'date_field' => $dateField,
|
||||||
'invoice_status' => request()->invoice_status,
|
'invoice_status' => request()->invoice_status,
|
||||||
'group_dates_by' => request()->group_dates_by,
|
'group_dates_by' => request()->group_dates_by,
|
||||||
'document_filter' => request()->document_filter,
|
'document_filter' => request()->document_filter,
|
||||||
'currency_type' => request()->currency_type,
|
'currency_type' => request()->currency_type,
|
||||||
'export_format' => $format,
|
'export_format' => $format,
|
||||||
|
'start_date' => $params['startDate'],
|
||||||
|
'end_date' => $params['endDate'],
|
||||||
];
|
];
|
||||||
$report = new $reportClass($startDate, $endDate, $isExport, $options);
|
$report = dispatch(new RunReport(auth()->user(), $reportType, $config, $isExport));
|
||||||
if (Input::get('report_type')) {
|
$params = array_merge($params, $report->exportParams);
|
||||||
$report->run();
|
|
||||||
}
|
|
||||||
$params['report'] = $report;
|
|
||||||
$params = array_merge($params, $report->results());
|
|
||||||
switch ($action) {
|
switch ($action) {
|
||||||
case 'export':
|
case 'export':
|
||||||
return dispatch(new ExportReportResults(auth()->user(), $format, $reportType, $params))->export($format);
|
return dispatch(new ExportReportResults(auth()->user(), $format, $reportType, $params))->export($format);
|
||||||
break;
|
break;
|
||||||
case 'schedule':
|
case 'schedule':
|
||||||
self::schedule($params, $options);
|
self::schedule($params, $config);
|
||||||
break;
|
break;
|
||||||
case 'cancel_schedule':
|
case 'cancel_schedule':
|
||||||
self::cancelSchdule();
|
self::cancelSchdule();
|
||||||
@ -138,8 +135,12 @@ class ReportController extends BaseController
|
|||||||
{
|
{
|
||||||
$options['report_type'] = $params['reportType'];
|
$options['report_type'] = $params['reportType'];
|
||||||
$options['range'] = request('range');
|
$options['range'] = request('range');
|
||||||
$options['start_date'] = $options['range'] ? '' : Carbon::parse($params['startDate'])->diffInDays(null, false); // null,false to get the relative/non-absolute diff
|
$options['start_date_offset'] = $options['range'] ? '' : Carbon::parse($params['startDate'])->diffInDays(null, false); // null,false to get the relative/non-absolute diff
|
||||||
$options['end_date'] = $options['range'] ? '' : Carbon::parse($params['endDate'])->diffInDays(null, false);
|
$options['end_date_offset'] = $options['range'] ? '' : Carbon::parse($params['endDate'])->diffInDays(null, false);
|
||||||
|
|
||||||
|
unset($options['start_date']);
|
||||||
|
unset($options['end_date']);
|
||||||
|
unset($options['group_dates_by']);
|
||||||
|
|
||||||
$schedule = ScheduledReport::createNew();
|
$schedule = ScheduledReport::createNew();
|
||||||
$schedule->config = json_encode($options);
|
$schedule->config = json_encode($options);
|
||||||
|
86
app/Jobs/RunReport.php
Normal file
86
app/Jobs/RunReport.php
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
<?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()
|
||||||
|
{
|
||||||
|
if (! $this->user->hasPermission('view_all')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$reportType = $this->reportType;
|
||||||
|
$config = $this->config;
|
||||||
|
$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'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// send email as user
|
||||||
|
if (App::runningInConsole() && $this->user) {
|
||||||
|
auth()->onceUsingId($this->user->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$report = new $reportClass($startDate, $endDate, $isExport, $config);
|
||||||
|
$report->run();
|
||||||
|
|
||||||
|
if (App::runningInConsole() && $this->user) {
|
||||||
|
auth()->logout();
|
||||||
|
}
|
||||||
|
|
||||||
|
$params = [
|
||||||
|
'startDate' => $startDate,
|
||||||
|
'endDate' => $endDate,
|
||||||
|
'report' => $report,
|
||||||
|
];
|
||||||
|
|
||||||
|
$report->exportParams = array_merge($params, $report->results());
|
||||||
|
|
||||||
|
return $report;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Carbon;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,4 +37,23 @@ class ScheduledReport extends EntityModel
|
|||||||
return $this->belongsTo('App\Models\User')->withTrashed();
|
return $this->belongsTo('App\Models\User')->withTrashed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updateSendDate()
|
||||||
|
{
|
||||||
|
switch ($this->frequency) {
|
||||||
|
case REPORT_FREQUENCY_DAILY;
|
||||||
|
$this->send_date = Carbon::now()->addDay()->toDateString();
|
||||||
|
break;
|
||||||
|
case REPORT_FREQUENCY_WEEKLY:
|
||||||
|
$this->send_date = Carbon::now()->addWeek()->toDateString();
|
||||||
|
break;
|
||||||
|
case REPORT_FREQUENCY_BIWEEKLY:
|
||||||
|
$this->send_date = Carbon::now()->addWeeks(2)->toDateString();
|
||||||
|
break;
|
||||||
|
case REPORT_FREQUENCY_MONTHLY:
|
||||||
|
$this->send_date = Carbon::now()->addMonth()->toDateString();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -319,10 +319,10 @@
|
|||||||
</center>
|
</center>
|
||||||
|
|
||||||
{!! Former::select('frequency')
|
{!! Former::select('frequency')
|
||||||
->addOption(trans('texts.freq_daily'), 'daily')
|
->addOption(trans('texts.freq_daily'), REPORT_FREQUENCY_DAILY)
|
||||||
->addOption(trans('texts.freq_weekly'), 'weekly')
|
->addOption(trans('texts.freq_weekly'), REPORT_FREQUENCY_WEEKLY)
|
||||||
->addOption(trans('texts.freq_biweekly'), 'biweekly')
|
->addOption(trans('texts.freq_biweekly'), REPORT_FREQUENCY_BIWEEKLY)
|
||||||
->addOption(trans('texts.freq_monthly'), 'monthly')
|
->addOption(trans('texts.freq_monthly'), REPORT_FREQUENCY_MONTHLY)
|
||||||
->value('weekly') !!}
|
->value('weekly') !!}
|
||||||
|
|
||||||
{!! Former::text('send_date')
|
{!! Former::text('send_date')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user