From ace4985d245843887727efbb8ca2a6bd12c3ee13 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 23 Nov 2017 12:23:19 +0200 Subject: [PATCH] Working on scheduled reports --- app/Console/Commands/SendReminders.php | 46 ++-------- app/Constants.php | 5 ++ app/Http/Controllers/ReportController.php | 25 +++--- app/Jobs/RunReport.php | 86 +++++++++++++++++++ app/Models/ScheduledReport.php | 20 +++++ .../views/reports/report_builder.blade.php | 8 +- 6 files changed, 136 insertions(+), 54 deletions(-) create mode 100644 app/Jobs/RunReport.php diff --git a/app/Console/Commands/SendReminders.php b/app/Console/Commands/SendReminders.php index 1ed44ce989e6..e0f9cf0ead10 100644 --- a/app/Console/Commands/SendReminders.php +++ b/app/Console/Commands/SendReminders.php @@ -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(); } } diff --git a/app/Constants.php b/app/Constants.php index 698470c051bf..36c480065839 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -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'); diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 9528aae9a62a..fcf719f06c39 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -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); diff --git a/app/Jobs/RunReport.php b/app/Jobs/RunReport.php new file mode 100644 index 000000000000..1efb5d994161 --- /dev/null +++ b/app/Jobs/RunReport.php @@ -0,0 +1,86 @@ +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; + } +} diff --git a/app/Models/ScheduledReport.php b/app/Models/ScheduledReport.php index 852ac72b4f12..1425457d5e22 100644 --- a/app/Models/ScheduledReport.php +++ b/app/Models/ScheduledReport.php @@ -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(); + } } diff --git a/resources/views/reports/report_builder.blade.php b/resources/views/reports/report_builder.blade.php index 93cd4350f107..1bef745cc412 100644 --- a/resources/views/reports/report_builder.blade.php +++ b/resources/views/reports/report_builder.blade.php @@ -319,10 +319,10 @@ {!! 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') !!}   {!! Former::text('send_date')