1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Http/Controllers/ReportController.php

177 lines
5.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-17 02:30:56 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
2017-11-23 09:10:14 +01:00
use App\Jobs\ExportReportResults;
2017-11-23 11:23:19 +01:00
use App\Jobs\RunReport;
2017-01-30 20:40:43 +01:00
use App\Models\Account;
2017-11-21 22:51:59 +01:00
use App\Models\ScheduledReport;
2015-03-17 02:30:56 +01:00
use Auth;
use Input;
2017-01-30 20:40:43 +01:00
use Utils;
2015-04-01 21:57:02 +02:00
use View;
2017-11-23 09:10:14 +01:00
use Carbon;
2017-11-23 12:15:50 +01:00
use Validator;
2015-04-01 21:57:02 +02:00
/**
2017-01-30 20:40:43 +01:00
* Class ReportController.
*/
2015-04-01 21:57:02 +02:00
class ReportController extends BaseController
2015-03-16 22:45:25 +01:00
{
/**
* @return \Illuminate\Contracts\View\View
*/
2015-03-16 22:45:25 +01:00
public function d3()
{
$message = '';
2015-11-10 23:07:05 +01:00
$fileName = storage_path().'/dataviz_sample.txt';
2015-03-16 22:45:25 +01:00
if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) {
2015-08-11 16:38:36 +02:00
$account = Account::where('id', '=', Auth::user()->account->id)
->with(['clients.invoices.invoice_items', 'clients.contacts', 'clients.currency'])
2015-08-11 16:38:36 +02:00
->first();
2015-03-16 22:45:25 +01:00
$account = $account->hideFieldsForViz();
2017-04-30 11:22:31 +02:00
$clients = $account->clients;
2015-05-19 21:14:00 +02:00
} elseif (file_exists($fileName)) {
$clients = file_get_contents($fileName);
2015-03-16 22:45:25 +01:00
$message = trans('texts.sample_data');
} else {
$clients = '[]';
}
$data = [
'clients' => $clients,
'message' => $message,
];
return View::make('reports.d3', $data);
}
/**
* @return \Illuminate\Contracts\View\View
*/
2015-04-30 19:54:19 +02:00
public function showReports()
2015-03-16 22:45:25 +01:00
{
if (! Auth::user()->hasPermission('view_all')) {
return redirect('/');
}
2015-04-30 19:54:19 +02:00
$action = Input::get('action');
$format = Input::get('format');
2015-04-30 19:54:19 +02:00
2016-10-13 11:46:29 +02:00
if (Input::get('report_type')) {
2015-04-30 19:54:19 +02:00
$reportType = Input::get('report_type');
2016-02-24 21:58:42 +01:00
$dateField = Input::get('date_field');
2016-09-18 16:06:56 +02:00
$startDate = date_create(Input::get('start_date'));
$endDate = date_create(Input::get('end_date'));
2015-03-16 22:45:25 +01:00
} else {
2015-11-10 23:07:05 +01:00
$reportType = ENTITY_INVOICE;
2016-02-24 21:58:42 +01:00
$dateField = FILTER_INVOICE_DATE;
2015-03-16 22:45:25 +01:00
$startDate = Utils::today(false)->modify('-3 month');
$endDate = Utils::today(false);
}
2015-04-30 19:54:19 +02:00
$reportTypes = [
2017-03-23 16:37:22 +01:00
'activity',
'aging',
2017-01-22 11:09:29 +01:00
'client',
2018-01-24 19:26:26 +01:00
'credit',
'document',
'expense',
2017-01-22 11:09:29 +01:00
'invoice',
'payment',
'product',
'profit_and_loss',
2017-01-22 11:09:29 +01:00
'task',
'tax_rate',
2017-03-29 20:17:05 +02:00
'quote',
2015-04-30 19:54:19 +02:00
];
2015-03-16 22:45:25 +01:00
$params = [
2016-09-18 16:06:56 +02:00
'startDate' => $startDate->format('Y-m-d'),
'endDate' => $endDate->format('Y-m-d'),
2017-01-22 11:09:29 +01:00
'reportTypes' => array_combine($reportTypes, Utils::trans($reportTypes)),
2015-04-30 19:54:19 +02:00
'reportType' => $reportType,
2015-07-07 22:08:16 +02:00
'title' => trans('texts.charts_and_reports'),
2016-09-18 16:06:56 +02:00
'account' => Auth::user()->account,
2015-03-16 22:45:25 +01:00
];
if (Auth::user()->account->hasFeature(FEATURE_REPORTS)) {
2016-09-11 16:30:23 +02:00
$isExport = $action == 'export';
2017-11-23 11:23:19 +01:00
$config = [
2017-01-22 11:09:29 +01:00
'date_field' => $dateField,
'status_ids' => request()->status_ids,
2017-01-22 11:09:29 +01:00
'group_dates_by' => request()->group_dates_by,
'document_filter' => request()->document_filter,
2017-11-19 10:55:01 +01:00
'currency_type' => request()->currency_type,
'export_format' => $format,
2017-11-23 11:23:19 +01:00
'start_date' => $params['startDate'],
'end_date' => $params['endDate'],
2017-01-22 11:09:29 +01:00
];
2017-11-23 11:23:19 +01:00
$report = dispatch(new RunReport(auth()->user(), $reportType, $config, $isExport));
$params = array_merge($params, $report->exportParams);
2017-11-21 22:51:59 +01:00
switch ($action) {
case 'export':
2017-11-23 09:10:14 +01:00
return dispatch(new ExportReportResults(auth()->user(), $format, $reportType, $params))->export($format);
2017-11-21 22:51:59 +01:00
break;
case 'schedule':
2017-11-23 11:23:19 +01:00
self::schedule($params, $config);
2017-11-23 12:50:11 +01:00
return redirect('/reports');
2017-11-21 22:51:59 +01:00
break;
case 'cancel_schedule':
self::cancelSchdule();
2017-11-23 12:50:11 +01:00
return redirect('/reports');
2017-11-21 22:51:59 +01:00
break;
2015-11-10 23:07:05 +01:00
}
2015-11-15 10:50:21 +01:00
} else {
$params['columns'] = [];
$params['displayData'] = [];
2016-02-27 22:00:27 +01:00
$params['reportTotals'] = [];
2017-01-22 11:09:29 +01:00
$params['report'] = false;
2015-11-10 23:07:05 +01:00
}
2017-11-21 22:51:59 +01:00
$params['scheduledReports'] = ScheduledReport::scope()->whereUserId(auth()->user()->id)->get();
return View::make('reports.report_builder', $params);
}
private function schedule($params, $options)
{
2017-11-23 12:15:50 +01:00
$validator = Validator::make(request()->all(), [
'frequency' => 'required|in:daily,weekly,biweekly,monthly',
'send_date' => 'required',
]);
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);
unset($options['start_date']);
unset($options['end_date']);
unset($options['group_dates_by']);
$schedule = ScheduledReport::createNew();
$schedule->config = json_encode($options);
$schedule->frequency = request('frequency');
$schedule->send_date = Utils::toSqlDate(request('send_date'));
$schedule->save();
2017-11-23 12:50:11 +01:00
session()->flash('message', trans('texts.created_scheduled_report'));
2017-11-23 12:15:50 +01:00
}
2017-11-21 22:51:59 +01:00
}
private function cancelSchdule()
{
ScheduledReport::scope()
->whereUserId(auth()->user()->id)
->wherePublicId(request('scheduled_report_id'))
->delete();
2017-10-22 10:27:42 +02:00
2017-11-23 12:50:11 +01:00
session()->flash('message', trans('texts.deleted_scheduled_report'));
2015-03-16 22:45:25 +01:00
}
}