akaunting/app/Utilities/Reports.php

84 lines
1.9 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Utilities;
2020-01-04 13:42:58 +03:00
use App\Models\Common\Report;
2019-11-16 10:21:14 +03:00
use App\Models\Module\Module;
2020-01-04 17:49:11 +03:00
use Illuminate\Support\Str;
2019-11-16 10:21:14 +03:00
class Reports
{
2020-01-04 21:53:10 +03:00
public static function getClasses($check_permission = true)
2019-11-16 10:21:14 +03:00
{
$classes = [];
2020-01-04 13:42:58 +03:00
$list = [
2019-11-16 10:21:14 +03:00
'App\Reports\IncomeSummary',
'App\Reports\ExpenseSummary',
'App\Reports\IncomeExpenseSummary',
'App\Reports\TaxSummary',
'App\Reports\ProfitLoss',
];
2020-01-04 13:42:58 +03:00
Module::enabled()->each(function ($module) use (&$list) {
2019-11-16 10:21:14 +03:00
$m = module($module->alias);
if (!$m || empty($m->get('reports'))) {
2020-01-04 13:42:58 +03:00
return;
2019-11-16 10:21:14 +03:00
}
2020-01-04 13:42:58 +03:00
$list = array_merge($list, (array) $m->get('reports'));
});
2019-11-16 10:21:14 +03:00
foreach ($list as $class) {
2020-01-04 21:53:10 +03:00
if (!class_exists($class) || ($check_permission && !static::canRead($class))) {
2019-11-16 10:21:14 +03:00
continue;
}
2020-01-04 13:42:58 +03:00
$classes[$class] = (new $class())->getDefaultName();
2019-11-16 10:21:14 +03:00
}
2020-01-04 13:42:58 +03:00
return $classes;
2019-11-16 10:21:14 +03:00
}
2020-01-04 13:42:58 +03:00
public static function getClassInstance($model, $get_totals = true)
2019-11-16 10:21:14 +03:00
{
2020-01-04 13:42:58 +03:00
if (is_string($model)) {
$model = Report::where('class', $model)->first();
}
if ((!$model instanceof Report) || !class_exists($model->class)) {
return false;
}
$class = $model->class;
return new $class($model, $get_totals);
2019-11-16 10:21:14 +03:00
}
2020-01-04 17:49:11 +03:00
public static function canRead($class)
{
return user()->can(static::getPermission($class));
}
public static function getPermission($class)
{
$arr = explode('\\', $class);
2020-01-04 17:49:11 +03:00
$prefix = 'read-';
// Add module
if (strtolower($arr[0]) == 'modules') {
$prefix .= Str::kebab($arr[1]) . '-';
}
$prefix .= 'reports-';
$class_name = end($arr);
$permission = $prefix . Str::kebab($class_name);
2020-01-04 17:49:11 +03:00
return $permission;
}
2019-11-16 10:21:14 +03:00
}