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;
use App\Models\Account;
use App\Models\ScheduledReport;
use Auth;
use Input;
use Str;
@ -109,8 +110,16 @@ class ReportController extends BaseController
}
$params['report'] = $report;
$params = array_merge($params, $report->results());
if ($isExport) {
return self::export($format, $reportType, $params);
switch ($action) {
case 'export':
return self::export($format, $reportType, $params);
break;
case 'schedule':
self::schedule($params, $options);
break;
case 'cancel_schedule':
self::cancelSchdule();
break;
}
} else {
$params['columns'] = [];
@ -119,7 +128,27 @@ class ReportController extends BaseController
$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);
$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'));
}

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) {
$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) {
$table->dropColumn('show_shipping_address');
});
Schema::dropIfExists('scheduled_reports');
}
}

View File

@ -1378,6 +1378,7 @@ $LANG = array(
// Frequencies
'freq_inactive' => 'Inactive',
'freq_daily' => 'Daily',
'freq_weekly' => 'Weekly',
'freq_two_weeks' => 'Two weeks',
'freq_four_weeks' => 'Four weeks',
@ -2544,7 +2545,8 @@ $LANG = array(
'show_shipping_address_help' => 'Require client to provide their shipping address',
'ship_to_billing_address' => 'Ship to billing address',
'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

@ -85,8 +85,9 @@
{!! Former::open()->addClass('report-form')->rules(['start_date' => 'required', 'end_date' => 'required']) !!}
<div style="display:none">
{!! Former::text('action') !!}
{!! Former::text('format') !!}
{!! Former::text('action') !!}
{!! Former::text('frequency') !!}
{!! Former::text('scheduled_report_id') !!}
</div>
{!! Former::populateField('start_date', $startDate) !!}
@ -177,15 +178,31 @@
@endif
<center class="buttons">
{!! DropdownButton::primary(trans('texts.export'))
->large()
->withAttributes(array('id' => 'export-button'))
->withContents([
['url' => 'javascript:onExportClick("csv")', 'label' => 'CSV'],
['url' => 'javascript:onExportClick("xlsx")', 'label' => 'XLSX'],
['url' => 'javascript:onExportClick("pdf")', 'label' => 'PDF'],
<center class="buttons form-inline">
<span class="well" style="padding-right:8px; padding-left:14px;">
{!! Former::select('format')
->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([
['url' => 'javascript:onScheduleClick("daily")', 'label' => trans('texts.freq_daily')],
['url' => 'javascript:onScheduleClick("weekly")', 'label' => trans('texts.freq_weekly')],
['url' => 'javascript:onScheduleClick("monthly")', 'label' => trans('texts.freq_monthly')],
]) !!}
</span> &nbsp;&nbsp;
{!! Button::success(trans('texts.run'))
->withAttributes(array('id' => 'submitButton'))
->submit()
@ -277,13 +294,37 @@
<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');
$('#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();
$('#action').val('');
}
function setFiltersShown() {
var val = $('#report_type').val();
$('#dateField').toggle(val == '{{ ENTITY_TAX_RATE }}');
@ -293,17 +334,22 @@
}
function setDocumentZipShown() {
var $ul = $('#export-button').next();
var val = $('#report_type').val();
var showOption = ['invoice', 'quote', 'expense', 'document'].indexOf(val) >= 0;
var numOptions = $ul.children().length;
var numOptions = $('#format option').size();
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) {
$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 = [];
@foreach ($columns as $column)
sumColumns.push("{{ in_array($column, ['amount', 'paid', 'balance', 'cost', 'duration']) ? trans("texts.{$column}") : false }}");
@ -328,6 +374,7 @@
var val = $('#report_type').val();
setFiltersShown();
setDocumentZipShown();
setScheduleButton();
if (isStorageSupported()) {
localStorage.setItem('last:report_type', val);
}
@ -401,6 +448,7 @@
setFiltersShown();
setDocumentZipShown();
setScheduleButton();
});
})