From 5a17953c76bc021104b03a83f18ab0475786e796 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 23 Nov 2017 13:15:50 +0200 Subject: [PATCH] Working on scheduled reports --- app/Console/Commands/SendReminders.php | 5 --- app/Http/Controllers/ReportController.php | 36 ++++++++++++------- app/Jobs/ExportReportResults.php | 34 +++++++++--------- app/Ninja/Reports/ActivityReport.php | 6 ++-- resources/lang/en/texts.php | 2 +- .../views/reports/report_builder.blade.php | 32 ++++++++++++----- 6 files changed, 69 insertions(+), 46 deletions(-) diff --git a/app/Console/Commands/SendReminders.php b/app/Console/Commands/SendReminders.php index e0f9cf0ead10..1b6189eedef5 100644 --- a/app/Console/Commands/SendReminders.php +++ b/app/Console/Commands/SendReminders.php @@ -76,11 +76,6 @@ class SendReminders extends Command $this->info('Done'); - if (\Utils::isNinjaDev()) { - $this->info('Stopping early on ninja dev'); - exit; - } - if ($errorEmail = env('ERROR_EMAIL')) { \Mail::raw('EOM', function ($message) use ($errorEmail, $database) { $message->to($errorEmail) diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index fcf719f06c39..ff6d4d36bcf1 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -11,6 +11,7 @@ use Input; use Utils; use View; use Carbon; +use Validator; /** * Class ReportController. @@ -133,22 +134,31 @@ class ReportController extends BaseController private function schedule($params, $options) { - $options['report_type'] = $params['reportType']; - $options['range'] = request('range'); - $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); + $validator = Validator::make(request()->all(), [ + 'frequency' => 'required|in:daily,weekly,biweekly,monthly', + 'send_date' => 'required', + ]); - unset($options['start_date']); - unset($options['end_date']); - unset($options['group_dates_by']); + if ($validator->fails()) { + session()->now('message', trans('texts.scheduled_report_error')); + } else { + $options['report_type'] = $params['reportType']; + $options['range'] = request('range'); + $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); - $schedule = ScheduledReport::createNew(); - $schedule->config = json_encode($options); - $schedule->frequency = request('frequency'); - $schedule->send_date = Utils::toSqlDate(request('send_date')); - $schedule->save(); + unset($options['start_date']); + unset($options['end_date']); + unset($options['group_dates_by']); - session()->now('message', trans('texts.created_scheduled_report')); + $schedule = ScheduledReport::createNew(); + $schedule->config = json_encode($options); + $schedule->frequency = request('frequency'); + $schedule->send_date = Utils::toSqlDate(request('send_date')); + $schedule->save(); + + session()->now('message', trans('texts.created_scheduled_report')); + } } private function cancelSchdule() diff --git a/app/Jobs/ExportReportResults.php b/app/Jobs/ExportReportResults.php index 17f002fa8d58..aed6a34e4a21 100644 --- a/app/Jobs/ExportReportResults.php +++ b/app/Jobs/ExportReportResults.php @@ -99,25 +99,27 @@ class ExportReportResults extends Job $sheet->setAutoSize(true); }); - $excel->sheet(trans("texts.totals"), function($sheet) use($report, $summary, $format) { - $sheet->setOrientation('landscape'); - $sheet->freezeFirstRow(); + if (count($summary)) { + $excel->sheet(trans("texts.totals"), function($sheet) use($report, $summary, $format) { + $sheet->setOrientation('landscape'); + $sheet->freezeFirstRow(); - if ($format == 'pdf') { - $sheet->setAllBorders('thin'); - } - $sheet->rows($summary); + if ($format == 'pdf') { + $sheet->setAllBorders('thin'); + } + $sheet->rows($summary); - // Styling header - $sheet->cells('A1:'.Utils::num2alpha(count($summary[0])-1).'1', function($cells) { - $cells->setBackground('#777777'); - $cells->setFontColor('#FFFFFF'); - $cells->setFontSize(13); - $cells->setFontFamily('Calibri'); - $cells->setFontWeight('bold'); + // Styling header + $sheet->cells('A1:'.Utils::num2alpha(count($summary[0])-1).'1', function($cells) { + $cells->setBackground('#777777'); + $cells->setFontColor('#FFFFFF'); + $cells->setFontSize(13); + $cells->setFontFamily('Calibri'); + $cells->setFontWeight('bold'); + }); + $sheet->setAutoSize(true); }); - $sheet->setAutoSize(true); - }); + } }); } diff --git a/app/Ninja/Reports/ActivityReport.php b/app/Ninja/Reports/ActivityReport.php index 3e843b25b3f9..2aaac5188af9 100644 --- a/app/Ninja/Reports/ActivityReport.php +++ b/app/Ninja/Reports/ActivityReport.php @@ -18,8 +18,8 @@ class ActivityReport extends AbstractReport { $account = Auth::user()->account; - $startDate = $this->startDate->format('Y-m-d'); - $endDate = $this->endDate->format('Y-m-d'); + $startDate = $this->startDate;; + $endDate = $this->endDate; $activities = Activity::scope() ->with('client.contacts', 'user', 'invoice', 'payment', 'credit', 'task', 'expense', 'account') @@ -32,7 +32,7 @@ class ActivityReport extends AbstractReport $activity->present()->createdAt, $client ? ($this->isExport ? $client->getDisplayName() : $client->present()->link) : '', $activity->present()->user, - $activity->getMessage(), + $this->isExport ? strip_tags($activity->getMessage()) : $activity->getMessage(), ]; } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 485cc4717785..be971dc7ee4a 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2553,7 +2553,7 @@ $LANG = array( 'created_scheduled_report' => 'Successfully scheduled report', 'deleted_scheduled_report' => 'Successfully canceled scheduled report', 'scheduled_report_attached' => 'Your scheduled :type report is attached.', - + 'scheduled_report_error' => 'Failed to create schedule report', ); return $LANG; diff --git a/resources/views/reports/report_builder.blade.php b/resources/views/reports/report_builder.blade.php index 1bef745cc412..b90bdacd338d 100644 --- a/resources/views/reports/report_builder.blade.php +++ b/resources/views/reports/report_builder.blade.php @@ -101,10 +101,11 @@ {!! Former::open()->addClass('report-form')->rules(['start_date' => 'required', 'end_date' => 'required']) !!} +
- {!! Former::text('action') !!} - {!! Former::text('range') !!} - {!! Former::text('scheduled_report_id') !!} + {!! Former::text('action')->forceValue('') !!} + {!! Former::text('range')->forceValue('') !!} + {!! Former::text('scheduled_report_id')->forceValue('') !!}
{!! Former::populateField('start_date', $startDate) !!} @@ -212,7 +213,7 @@ ->appendIcon(Icon::create('remove')) !!} {!! Button::primary(trans('texts.schedule')) - ->withAttributes(['id'=>'scheduleButton', 'onclick' => 'showScheduleModal()']) + ->withAttributes(['id'=>'scheduleButton', 'onclick' => 'showScheduleModal()', 'style' => 'display:none']) ->appendIcon(Icon::create('time')) !!}    @@ -433,6 +434,13 @@ } }); + $('#format').change(function() { + var val = $('#format').val(); + if (isStorageSupported() && val != 'zip') { + localStorage.setItem('last:report_format', val); + } + }); + $('#report_type').change(function() { var val = $('#report_type').val(); setFiltersShown(); @@ -498,6 +506,12 @@ widgets: ['zebra', 'uitheme'], }).show(); + setFiltersShown(); + setDocumentZipShown(); + setTimeout(function() { + setScheduleButton(); + }, 1); + if (isStorageSupported()) { var lastReportType = localStorage.getItem('last:report_type'); if (lastReportType) { @@ -507,11 +521,13 @@ if (lastDocumentFilter) { $('#document_filter').val(lastDocumentFilter); } + var lastFormat = localStorage.getItem('last:report_format'); + if (lastFormat) { + setTimeout(function() { + $('#format').val(lastFormat); + }, 1); + } } - - setFiltersShown(); - setDocumentZipShown(); - setScheduleButton(); }); })