Модуль отчетов
This commit is contained in:
63
app/Services/Reports/TemplateReportDefinition.php
Normal file
63
app/Services/Reports/TemplateReportDefinition.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Reports;
|
||||
|
||||
use App\Models\Department;
|
||||
use App\Models\ReportTemplate;
|
||||
use App\Services\DateRange;
|
||||
use App\Services\Reports\Contracts\ReportDefinition;
|
||||
|
||||
/**
|
||||
* Адаптер, превращающий пользовательский шаблон (ReportTemplate), собранный
|
||||
* в конструкторе админ-панели, в обычный ReportDefinition — для движка
|
||||
* не важно, встроенный это отчёт или шаблон.
|
||||
*/
|
||||
class TemplateReportDefinition implements ReportDefinition
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ReportTemplate $template,
|
||||
private readonly ReportSourceRegistry $sources,
|
||||
) {}
|
||||
|
||||
public function code(): string
|
||||
{
|
||||
return 'template:'.$this->template->id;
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return $this->template->name;
|
||||
}
|
||||
|
||||
public function requiredPermissions(): array
|
||||
{
|
||||
return $this->template->required_permissions ?? [];
|
||||
}
|
||||
|
||||
public function build(Department $department, DateRange $dateRange): ReportPayload
|
||||
{
|
||||
$sections = [];
|
||||
|
||||
foreach ($this->template->sections ?? [] as $sectionConfig) {
|
||||
$source = $this->sources->get($sectionConfig['source']);
|
||||
|
||||
$sections[] = $source->toSection(
|
||||
$department,
|
||||
$dateRange,
|
||||
$sectionConfig['columns'] ?? null,
|
||||
$sectionConfig['filters'] ?? [],
|
||||
$sectionConfig['title'] ?? null,
|
||||
);
|
||||
}
|
||||
|
||||
return new ReportPayload(
|
||||
title: $this->template->name,
|
||||
meta: [
|
||||
'Отделение' => $department->name_full ?? $department->name_short,
|
||||
'Период' => $dateRange->start()->format('d.m.Y H:i').' — '.$dateRange->end()->format('d.m.Y H:i'),
|
||||
'Сформирован' => now('Asia/Yakutsk')->format('d.m.Y H:i'),
|
||||
],
|
||||
sections: $sections,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user