Working on scheduled reports

This commit is contained in:
Hillel Coren 2017-11-21 23:51:59 +02:00
parent 3115656cea
commit 7336cd4234
6 changed files with 143 additions and 21 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Account; use App\Models\Account;
use App\Models\ScheduledReport;
use Auth; use Auth;
use Input; use Input;
use Str; use Str;
@ -109,8 +110,16 @@ class ReportController extends BaseController
} }
$params['report'] = $report; $params['report'] = $report;
$params = array_merge($params, $report->results()); $params = array_merge($params, $report->results());
if ($isExport) { switch ($action) {
case 'export':
return self::export($format, $reportType, $params); return self::export($format, $reportType, $params);
break;
case 'schedule':
self::schedule($params, $options);
break;
case 'cancel_schedule':
self::cancelSchdule();
break;
} }
} else { } else {
$params['columns'] = []; $params['columns'] = [];
@ -119,7 +128,27 @@ class ReportController extends BaseController
$params['report'] = false; $params['report'] = false;
} }
return View::make('reports.chart_builder', $params); $params['scheduledReports'] = ScheduledReport::scope()->whereUserId(auth()->user()->id)->get();
return View::make('reports.report_builder', $params);
}
private function schedule($params, $options)
{
$options['report_type'] = $params['reportType'];
$schedule = ScheduledReport::createNew();
$schedule->config = json_encode($options);
$schedule->frequency = request('frequency');
$schedule->save();
}
private function cancelSchdule()
{
ScheduledReport::scope()
->whereUserId(auth()->user()->id)
->wherePublicId(request('scheduled_report_id'))
->delete();
} }
/** /**

View File

@ -30,7 +30,7 @@ class DuplicateSubmissionCheck
$lastPage = session(SESSION_LAST_REQUEST_PAGE); $lastPage = session(SESSION_LAST_REQUEST_PAGE);
$lastTime = session(SESSION_LAST_REQUEST_TIME); $lastTime = session(SESSION_LAST_REQUEST_TIME);
if ($lastPage == $path && (microtime(true) - $lastTime <= 2)) { if ($lastPage == $path && (microtime(true) - $lastTime <= 1)) {
return redirect('/')->with('warning', trans('texts.duplicate_post')); return redirect('/')->with('warning', trans('texts.duplicate_post'));
} }

View File

@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Scheduled Report
*/
class ScheduledReport extends EntityModel
{
use SoftDeletes;
/**
* @var array
*/
protected $fillable = [
'frequency',
'config',
];
}

View File

@ -44,6 +44,25 @@ class AddSubdomainToLookups extends Migration
Schema::table('account_gateways', function ($table) { Schema::table('account_gateways', function ($table) {
$table->boolean('show_shipping_address')->default(false)->nullable(); $table->boolean('show_shipping_address')->default(false)->nullable();
}); });
Schema::dropIfExists('scheduled_reports');
Schema::create('scheduled_reports', function ($table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('account_id')->index();
$table->timestamps();
$table->softDeletes();
$table->text('config');
$table->enum('frequency', ['daily', 'weekly', 'monthly']);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
$table->unsignedInteger('public_id')->nullable();
$table->unique(['account_id', 'public_id']);
});
} }
/** /**
@ -77,5 +96,7 @@ class AddSubdomainToLookups extends Migration
Schema::table('account_gateways', function ($table) { Schema::table('account_gateways', function ($table) {
$table->dropColumn('show_shipping_address'); $table->dropColumn('show_shipping_address');
}); });
Schema::dropIfExists('scheduled_reports');
} }
} }

View File

@ -1378,6 +1378,7 @@ $LANG = array(
// Frequencies // Frequencies
'freq_inactive' => 'Inactive', 'freq_inactive' => 'Inactive',
'freq_daily' => 'Daily',
'freq_weekly' => 'Weekly', 'freq_weekly' => 'Weekly',
'freq_two_weeks' => 'Two weeks', 'freq_two_weeks' => 'Two weeks',
'freq_four_weeks' => 'Four weeks', 'freq_four_weeks' => 'Four weeks',
@ -2544,7 +2545,8 @@ $LANG = array(
'show_shipping_address_help' => 'Require client to provide their shipping address', 'show_shipping_address_help' => 'Require client to provide their shipping address',
'ship_to_billing_address' => 'Ship to billing address', 'ship_to_billing_address' => 'Ship to billing address',
'delivery_note' => 'Delivery Note', 'delivery_note' => 'Delivery Note',
'show_tasks_in_portal' => 'Show tasks in the client portal' 'show_tasks_in_portal' => 'Show tasks in the client portal',
'cancel_schedule' => 'Cancel Schedule',
); );

View File

@ -86,7 +86,8 @@
<div style="display:none"> <div style="display:none">
{!! Former::text('action') !!} {!! Former::text('action') !!}
{!! Former::text('format') !!} {!! Former::text('frequency') !!}
{!! Former::text('scheduled_report_id') !!}
</div> </div>
{!! Former::populateField('start_date', $startDate) !!} {!! Former::populateField('start_date', $startDate) !!}
@ -177,15 +178,31 @@
@endif @endif
<center class="buttons"> <center class="buttons form-inline">
{!! DropdownButton::primary(trans('texts.export')) <span class="well" style="padding-right:8px; padding-left:14px;">
->large() {!! Former::select('format')
->withAttributes(array('id' => 'export-button')) ->addOption('CSV', 'csv')
->addOption('XLSX', 'xlsx')
->addOption('PDF', 'pdf')
->raw() !!} &nbsp;
{!! Button::normal(trans('texts.export'))
->withAttributes(['onclick' => 'onExportClick()'])
->appendIcon(Icon::create('download-alt')) !!}
{!! Button::normal(trans('texts.cancel_schedule'))
->withAttributes(['id' => 'cancelSchduleButton', 'onclick' => 'onCancelScheduleClick()', 'style' => 'display:none'])
->appendIcon(Icon::create('remove')) !!}
{!! DropdownButton::primary(trans('texts.schedule'))
->withAttributes(['id'=>'scheduleDropDown'])
->withContents([ ->withContents([
['url' => 'javascript:onExportClick("csv")', 'label' => 'CSV'], ['url' => 'javascript:onScheduleClick("daily")', 'label' => trans('texts.freq_daily')],
['url' => 'javascript:onExportClick("xlsx")', 'label' => 'XLSX'], ['url' => 'javascript:onScheduleClick("weekly")', 'label' => trans('texts.freq_weekly')],
['url' => 'javascript:onExportClick("pdf")', 'label' => 'PDF'], ['url' => 'javascript:onScheduleClick("monthly")', 'label' => trans('texts.freq_monthly')],
]) !!} ]) !!}
</span> &nbsp;&nbsp;
{!! Button::success(trans('texts.run')) {!! Button::success(trans('texts.run'))
->withAttributes(array('id' => 'submitButton')) ->withAttributes(array('id' => 'submitButton'))
->submit() ->submit()
@ -277,9 +294,33 @@
<script type="text/javascript"> <script type="text/javascript">
function onExportClick(format) { var scheduledReports = {!! $scheduledReports !!};
var scheduledReportMap = {};
for (var i=0; i<scheduledReports.length; i++) {
var schedule = scheduledReports[i];
var config = JSON.parse(schedule.config);
scheduledReportMap[config.report_type] = schedule.public_id;
}
function onExportClick() {
$('#action').val('export'); $('#action').val('export');
$('#format').val(format); $('#submitButton').click();
$('#action').val('');
}
function onScheduleClick(frequency) {
$('#action').val('schedule');
$('#frequency').val(frequency);
$('#submitButton').click();
$('#action').val('');
}
function onCancelScheduleClick() {
var reportType = $('#report_type').val();
$('#action').val('cancel_schedule');
$('#frequency').val(frequency);
$('#scheduled_report_id').val(scheduledReportMap[reportType]);
$('#submitButton').click(); $('#submitButton').click();
$('#action').val(''); $('#action').val('');
} }
@ -293,17 +334,22 @@
} }
function setDocumentZipShown() { function setDocumentZipShown() {
var $ul = $('#export-button').next();
var val = $('#report_type').val(); var val = $('#report_type').val();
var showOption = ['invoice', 'quote', 'expense', 'document'].indexOf(val) >= 0; var showOption = ['invoice', 'quote', 'expense', 'document'].indexOf(val) >= 0;
var numOptions = $ul.children().length; var numOptions = $('#format option').size();
if (showOption && numOptions == 3) { if (showOption && numOptions == 3) {
$ul.append('<li><a href="javascript:onExportClick(\'zip\')">ZIP - {{ trans('texts.documents') }}</a></li>'); $("#format").append(new Option("ZIP - {{ trans('texts.documents') }}", 'zip'));
} else if (! showOption && numOptions == 4) { } else if (! showOption && numOptions == 4) {
$ul.find('li:last-child').remove(); $("#format option:last").remove();
} }
} }
function setScheduleButton() {
var reportType = $('#report_type').val();
$('#scheduleDropDown').toggle(! scheduledReportMap[reportType]);
$('#cancelSchduleButton').toggle(!! scheduledReportMap[reportType]);
}
var sumColumns = []; var sumColumns = [];
@foreach ($columns as $column) @foreach ($columns as $column)
sumColumns.push("{{ in_array($column, ['amount', 'paid', 'balance', 'cost', 'duration']) ? trans("texts.{$column}") : false }}"); sumColumns.push("{{ in_array($column, ['amount', 'paid', 'balance', 'cost', 'duration']) ? trans("texts.{$column}") : false }}");
@ -328,6 +374,7 @@
var val = $('#report_type').val(); var val = $('#report_type').val();
setFiltersShown(); setFiltersShown();
setDocumentZipShown(); setDocumentZipShown();
setScheduleButton();
if (isStorageSupported()) { if (isStorageSupported()) {
localStorage.setItem('last:report_type', val); localStorage.setItem('last:report_type', val);
} }
@ -401,6 +448,7 @@
setFiltersShown(); setFiltersShown();
setDocumentZipShown(); setDocumentZipShown();
setScheduleButton();
}); });
}) })