akaunting/app/Utilities/Reports.php

105 lines
2.4 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
{
public static function getClasses()
{
$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 17:49:11 +03:00
if (!class_exists($class) || !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
}
public static function getGroups()
{
return [
'category' => trans_choice('general.categories', 1),
];
}
public static function getPeriods()
{
return [
'monthly' => trans('general.monthly'),
'quarterly' => trans('general.quarterly'),
'yearly' => trans('general.yearly'),
];
}
public static function getBasises()
{
return [
'accrual' => trans('general.accrual'),
'cash' => trans('general.cash'),
];
}
public static function getCharts()
{
return [
'0' => trans('general.disabled'),
'line' => trans('reports.charts.line'),
];
}
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)
{
$class_name = (new \ReflectionClass($class))->getShortName();
$permission = 'read-reports-' . Str::kebab($class_name);
return $permission;
}
2019-11-16 10:21:14 +03:00
}