Модуль отчетов
This commit is contained in:
62
app/Services/Reports/Export/ReportExcelExport.php
Normal file
62
app/Services/Reports/Export/ReportExcelExport.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user