1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 12:12:48 +01:00

Working on scheduled reports

This commit is contained in:
Hillel Coren 2017-11-23 12:23:19 +02:00
parent 0b99e44907
commit ace4985d24
6 changed files with 136 additions and 54 deletions

View File

@ -13,6 +13,7 @@ use App\Models\ScheduledReport;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use App\Jobs\ExportReportResults;
use App\Jobs\RunReport;
/**
* Class SendReminders.
@ -138,52 +139,21 @@ class SendReminders extends Command
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');
foreach ($scheduledReports as $scheduledReport) {
$config = json_decode($scheduledReport->config);
$reportType = $config->report_type;
$reportClass = '\\App\\Ninja\\Reports\\' . Str::studly($reportType) . 'Report';
$config = (array) json_decode($scheduledReport->config);
$reportType = $config['report_type'];
if ($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;
}
} 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));
$report = dispatch(new RunReport($scheduledReport->user, $reportType, $config, true));
$file = dispatch(new ExportReportResults($scheduledReport->user, $config['export_format'], $reportType, $report->exportParams));
if ($file) {
$this->userMailer->sendScheduledReport($scheduledReport, $file);
}
$scheduledReport->updateSendDate();
}
}

View File

@ -228,6 +228,11 @@ if (! defined('APP_NAME')) {
define('FREQUENCY_SIX_MONTHS', 8);
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_CURRENCY', 'currency');
define('SESSION_CURRENCY_DECORATOR', 'currency_decorator');

View File

@ -3,11 +3,11 @@
namespace App\Http\Controllers;
use App\Jobs\ExportReportResults;
use App\Jobs\RunReport;
use App\Models\Account;
use App\Models\ScheduledReport;
use Auth;
use Input;
use Str;
use Utils;
use View;
use Carbon;
@ -96,27 +96,24 @@ class ReportController extends BaseController
if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) {
$isExport = $action == 'export';
$reportClass = '\\App\\Ninja\\Reports\\' . Str::studly($reportType) . 'Report';
$options = [
$config = [
'date_field' => $dateField,
'invoice_status' => request()->invoice_status,
'group_dates_by' => request()->group_dates_by,
'document_filter' => request()->document_filter,
'currency_type' => request()->currency_type,
'export_format' => $format,
'start_date' => $params['startDate'],
'end_date' => $params['endDate'],
];
$report = new $reportClass($startDate, $endDate, $isExport, $options);
if (Input::get('report_type')) {
$report->run();
}
$params['report'] = $report;
$params = array_merge($params, $report->results());
$report = dispatch(new RunReport(auth()->user(), $reportType, $config, $isExport));
$params = array_merge($params, $report->exportParams);
switch ($action) {
case 'export':
return dispatch(new ExportReportResults(auth()->user(), $format, $reportType, $params))->export($format);
break;
case 'schedule':
self::schedule($params, $options);
self::schedule($params, $config);
break;
case 'cancel_schedule':
self::cancelSchdule();
@ -138,8 +135,12 @@ class ReportController extends BaseController
{
$options['report_type'] = $params['reportType'];
$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['end_date'] = $options['range'] ? '' : Carbon::parse($params['endDate'])->diffInDays(null, false);
$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_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->config = json_encode($options);

86
app/Jobs/RunReport.php Normal file
View 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;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
@ -36,4 +37,23 @@ class ScheduledReport extends EntityModel
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();
}
}

View File

@ -319,10 +319,10 @@
</center>
{!! Former::select('frequency')
->addOption(trans('texts.freq_daily'), 'daily')
->addOption(trans('texts.freq_weekly'), 'weekly')
->addOption(trans('texts.freq_biweekly'), 'biweekly')
->addOption(trans('texts.freq_monthly'), 'monthly')
->addOption(trans('texts.freq_daily'), REPORT_FREQUENCY_DAILY)
->addOption(trans('texts.freq_weekly'), REPORT_FREQUENCY_WEEKLY)
->addOption(trans('texts.freq_biweekly'), REPORT_FREQUENCY_BIWEEKLY)
->addOption(trans('texts.freq_monthly'), REPORT_FREQUENCY_MONTHLY)
->value('weekly') !!} &nbsp;
{!! Former::text('send_date')