Модуль отчетов

This commit is contained in:
brusnitsyn
2026-06-21 23:40:55 +09:00
parent f163b95663
commit bd2cc24b98
27 changed files with 2781 additions and 3 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Services\Reports\Export;
use App\Exports\Sheets\ArraySheetExport;
use App\Services\Reports\ReportPayload;
use App\Services\Reports\ReportSection;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class ReportExcelExport implements WithMultipleSheets
{
public function __construct(private readonly ReportPayload $payload) {}
public function sheets(): array
{
$sheets = [
new ArraySheetExport('Сводка', $this->metaRows()),
];
foreach ($this->payload->sections as $section) {
$sheets[] = new ArraySheetExport($this->sheetTitle($section->title), $this->sectionRows($section));
}
return $sheets;
}
private function metaRows(): array
{
$rows = [['Показатель', 'Значение']];
foreach ($this->payload->meta as $label => $value) {
$rows[] = [$label, $value];
}
return $rows;
}
private function sectionRows(ReportSection $section): array
{
$rows = [array_values($section->columns)];
if (empty($section->rows)) {
$rows[] = ['Нет данных за выбранный период'];
}
foreach ($section->rows as $row) {
$line = [];
foreach (array_keys($section->columns) as $key) {
$line[] = $row[$key] ?? '';
}
$rows[] = $line;
}
return $rows;
}
private function sheetTitle(string $title): string
{
// Excel ограничивает название листа 31 символом и запрещает символы \/?*[]:
return mb_substr(preg_replace('/[\\\\\/\?\*\[\]:]/', ' ', $title), 0, 31);
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Services\Reports\Export;
use App\Services\Reports\ReportPayload;
use Barryvdh\DomPDF\Facade\Pdf;
use Barryvdh\DomPDF\PDF as PdfDocument;
class ReportPdfExport
{
public static function render(ReportPayload $payload): PdfDocument
{
return Pdf::loadView('reports.pdf', ['payload' => $payload])
->setPaper('a4', 'portrait');
}
}