287 lines
7.1 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Http\Controllers\Common;
use App\Abstracts\Http\Controller;
use App\Http\Requests\Common\Report as Request;
use App\Jobs\Common\CreateReport;
use App\Jobs\Common\DeleteReport;
use App\Jobs\Common\UpdateReport;
use App\Models\Common\Report;
use App\Utilities\Reports as Utility;
2020-01-25 13:36:57 +03:00
use Illuminate\Support\Facades\Cache;
2019-11-16 10:21:14 +03:00
class Reports extends Controller
{
2021-02-12 13:28:03 +03:00
/**
* Instantiate a new controller instance.
*/
public function __construct()
{
// Add CRUD permission check
$this->middleware('permission:create-common-reports')->only('create', 'store', 'duplicate', 'import');
$this->middleware('permission:read-common-reports')->only('index', 'show', 'export');
$this->middleware('permission:update-common-reports')->only('edit', 'update', 'enable', 'disable');
$this->middleware('permission:delete-common-reports')->only('destroy');
}
2019-11-16 10:21:14 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2020-01-18 13:11:19 +03:00
$totals = $icons = $categories = [];
2019-11-16 10:21:14 +03:00
2020-01-26 02:21:34 +03:00
$reports = Report::orderBy('name')->get();
2019-11-16 10:21:14 +03:00
2020-01-04 13:42:58 +03:00
foreach ($reports as $report) {
2020-06-13 00:13:39 +03:00
if (!Utility::canShow($report->class)) {
2019-11-16 10:21:14 +03:00
continue;
}
2020-01-25 13:36:57 +03:00
$class = Utility::getClassInstance($report, false);
2020-01-28 16:13:00 +03:00
if (empty($class)) {
continue;
}
2020-01-25 13:36:57 +03:00
$ttl = 3600 * 6; // 6 hours
$totals[$report->id] = Cache::remember('reports.totals.' . $report->id, $ttl, function () use ($class) {
2020-01-29 01:06:12 +03:00
return $class->getGrandTotal();
2020-01-25 13:36:57 +03:00
});
2020-01-04 17:49:11 +03:00
2020-01-18 13:11:19 +03:00
$icons[$report->id] = $class->getIcon();
2020-01-04 13:42:58 +03:00
$categories[$class->getCategory()][] = $report;
2019-11-16 10:21:14 +03:00
}
2020-11-06 00:43:46 +03:00
return $this->response('common.reports.index', compact('categories', 'totals', 'icons'));
2019-11-16 10:21:14 +03:00
}
/**
* Show the form for viewing the specified resource.
*
* @param Report $report
* @return Response
*/
public function show(Report $report)
{
2020-06-13 00:13:39 +03:00
if (!Utility::canShow($report->class)) {
2019-11-16 10:21:14 +03:00
abort(403);
}
2020-01-25 13:36:57 +03:00
$class = Utility::getClassInstance($report);
// Update cache
2020-01-29 01:06:12 +03:00
Cache::put('reports.totals.' . $report->id, $class->getGrandTotal());
2020-01-25 13:36:57 +03:00
return $class->show();
2019-11-16 10:21:14 +03:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$classes = Utility::getClasses();
2020-01-16 15:39:37 +03:00
return view('common.reports.create', compact('classes'));
2019-11-16 10:21:14 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return Response
*/
public function store(Request $request)
{
$response = $this->ajaxDispatch(new CreateReport($request));
if ($response['success']) {
$response['redirect'] = route('reports.index');
$message = trans('messages.success.added', ['type' => trans_choice('general.reports', 1)]);
flash($message)->success();
} else {
$response['redirect'] = route('reports.create');
$message = $response['message'];
flash($message)->error()->important();
2019-11-16 10:21:14 +03:00
}
return response()->json($response);
}
2020-01-26 02:21:34 +03:00
/**
* Duplicate the specified resource.
*
* @param Report $report
*
* @return Response
*/
public function duplicate(Report $report)
{
$clone = $report->duplicate();
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.reports', 1)]);
flash($message)->success();
return redirect()->route('reports.edit', $clone->id);
}
2019-11-16 10:21:14 +03:00
/**
* Show the form for editing the specified resource.
*
* @param Report $report
*
* @return Response
*/
public function edit(Report $report)
{
$classes = Utility::getClasses();
2020-01-25 13:36:57 +03:00
$class = Utility::getClassInstance($report, false);
2019-11-16 10:21:14 +03:00
2020-01-16 15:39:37 +03:00
return view('common.reports.edit', compact('report', 'classes', 'class'));
2019-11-16 10:21:14 +03:00
}
/**
* Update the specified resource in storage.
*
* @param Report $report
* @param $request
* @return Response
*/
public function update(Report $report, Request $request)
{
$response = $this->ajaxDispatch(new UpdateReport($report, $request));
if ($response['success']) {
$response['redirect'] = route('reports.index');
$message = trans('messages.success.updated', ['type' => $report->name]);
flash($message)->success();
} else {
$response['redirect'] = route('reports.edit', $report->id);
$message = $response['message'];
flash($message)->error()->important();
2019-11-16 10:21:14 +03:00
}
return response()->json($response);
}
/**
* Remove the specified resource from storage.
*
* @param Report $report
*
* @return Response
*/
public function destroy(Report $report)
{
$response = $this->ajaxDispatch(new DeleteReport($report));
$response['redirect'] = route('reports.index');
if ($response['success']) {
$message = trans('messages.success.deleted', ['type' => $report->name]);
flash($message)->success();
} else {
$message = $response['message'];
flash($message)->error()->important();
2019-11-16 10:21:14 +03:00
}
return response()->json($response);
}
/**
* Print the report.
*
* @param Report $report
* @return Response
*/
public function print(Report $report)
{
2020-06-13 00:13:39 +03:00
if (!Utility::canShow($report->class)) {
2019-11-16 10:21:14 +03:00
abort(403);
}
2020-01-04 17:49:11 +03:00
return Utility::getClassInstance($report)->print();
2019-11-16 10:21:14 +03:00
}
/**
* Export the report.
*
* @param Report $report
* @return Response
*/
public function export(Report $report)
{
2020-06-13 00:13:39 +03:00
if (!Utility::canShow($report->class)) {
2019-11-16 10:21:14 +03:00
abort(403);
}
2020-01-04 17:49:11 +03:00
return Utility::getClassInstance($report)->export();
2019-11-16 10:21:14 +03:00
}
/**
2020-01-16 15:39:37 +03:00
* Get fields of the specified resource.
2019-11-16 10:21:14 +03:00
*
* @return Response
*/
2020-01-16 15:39:37 +03:00
public function fields()
2019-11-16 10:21:14 +03:00
{
$class = request('class');
if (!class_exists($class)) {
return response()->json([
'success' => false,
'error' => true,
2020-01-16 15:39:37 +03:00
'message' => 'Class does not exist',
'html' => '',
2019-11-16 10:21:14 +03:00
]);
}
2020-01-16 15:39:37 +03:00
$fields = (new $class())->getFields();
$html = view('partials.reports.fields', compact('fields'))->render();
2019-11-16 10:21:14 +03:00
return response()->json([
'success' => true,
'error' => false,
'message' => '',
2020-01-16 15:39:37 +03:00
'html' => $html,
2019-11-16 10:21:14 +03:00
]);
}
2020-01-25 13:36:57 +03:00
/**
* Clear the cache of the resource.
*
* @return Response
*/
public function clear()
{
Report::all()->each(function ($report) {
2020-06-13 00:13:39 +03:00
if (!Utility::canShow($report->class)) {
2020-01-25 13:36:57 +03:00
return;
}
2020-01-29 01:06:12 +03:00
Cache::put('reports.totals.' . $report->id, Utility::getClassInstance($report)->getGrandTotal());
2020-01-25 13:36:57 +03:00
});
return redirect()->back();
}
2019-11-16 10:21:14 +03:00
}