akaunting/app/Utilities/Reports.php

145 lines
3.2 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) {
2022-06-01 10:15:55 +03:00
if (! class_exists($class) || ($check_permission && static::cannotRead($class))) {
2019-11-16 10:21:14 +03:00
continue;
}
2020-02-06 17:08:20 +03:00
$classes[$class] = static::getDefaultName($class);
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-25 13:36:57 +03:00
public static function getClassInstance($model, $load_data = 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();
}
2022-06-01 10:15:55 +03:00
if ((! $model instanceof Report) || ! class_exists($model->class)) {
2020-01-04 13:42:58 +03:00
return false;
}
$class = $model->class;
2020-01-25 13:36:57 +03:00
return new $class($model, $load_data);
2019-11-16 10:21:14 +03:00
}
2020-01-04 17:49:11 +03:00
2020-06-13 00:13:39 +03:00
public static function canShow($class)
{
return (static::isModuleEnabled($class) && static::canRead($class));
}
2022-06-01 10:15:55 +03:00
public static function cannotShow($class)
{
return ! static::canShow($class);
}
2020-01-04 17:49:11 +03:00
public static function canRead($class)
{
return user()->can(static::getPermission($class));
}
2022-06-01 10:15:55 +03:00
public static function cannotRead($class)
{
return ! static::canRead($class);
}
2020-01-04 17:49:11 +03:00
public static function getPermission($class)
{
$arr = explode('\\', $class);
2020-01-04 17:49:11 +03:00
$prefix = 'read-';
// Add module
2020-06-13 00:13:39 +03:00
if ($alias = static::getModuleAlias($arr)) {
$prefix .= $alias . '-';
}
$prefix .= 'reports-';
$class_name = end($arr);
$permission = $prefix . Str::kebab($class_name);
2020-01-04 17:49:11 +03:00
2020-01-28 16:15:12 +03:00
return str_replace('--', '-', $permission);
2020-01-04 17:49:11 +03:00
}
2020-02-06 17:08:20 +03:00
public static function getDefaultName($class)
{
return (new $class())->getDefaultName();
}
2020-06-13 00:13:39 +03:00
public static function isModuleEnabled($class)
{
2022-06-01 10:15:55 +03:00
if (! $alias = static::getModuleAlias($class)) {
2020-06-13 00:13:39 +03:00
return true;
}
if (Module::alias($alias)->enabled()->first()) {
return true;
}
return false;
}
2022-06-01 10:15:55 +03:00
public static function isModuleDisabled($class)
{
return ! static::isModuleEnabled($class);
}
2020-06-13 00:13:39 +03:00
public static function isModule($class)
{
$arr = is_array($class) ? $class : explode('\\', $class);
return (strtolower($arr[0]) == 'modules');
}
2022-06-01 10:15:55 +03:00
public static function isNotModule($class)
{
return ! static::isModule($class);
}
2020-06-13 00:13:39 +03:00
public static function getModuleAlias($class)
{
2022-06-01 10:15:55 +03:00
if (static::isNotModule($class)) {
2020-06-13 00:13:39 +03:00
return false;
}
$arr = is_array($class) ? $class : explode('\\', $class);
return Str::kebab($arr[1]);
}
2019-11-16 10:21:14 +03:00
}