Files
backend/app/Exports/V3/SafetyAndPrivacy/Country/ConstructionReport.php

99 lines
3.0 KiB
PHP

<?php
namespace App\Exports\V3\SafetyAndPrivacy\Country;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
class ConstructionReport implements FromView, WithEvents, ShouldAutoSize, WithDrawings
{
public function __construct(private array $data){}
public function view(): View
{
$axis = [
1 => 'آزادراه',
2 => 'بزرگراه',
3 => 'اصلی',
4 => 'فرعی',
5 => 'روستایی'
];
$grid = [];
$national = null;
foreach ($this->data as $r) {
$pid = $r->province_id;
$name = $r->province_name;
$axisKey = $axis[$r->axis_type_id] ?? "axis_{$r->axis_type_id}";
if ($pid === -1) {
$national ??= ['province_name' => $name, 'total' => 0];
$national["{$axisKey}_1"] = $r->s1;
$national["{$axisKey}_2"] = $r->s2;
$national["{$axisKey}_3"] = $r->s3;
$national['total'] = $r->total_sum;
continue;
}
if (!isset($grid[$pid])) {
$grid[$pid] = ['province_name' => $name, 'total' => 0];
}
$grid[$pid]["{$axisKey}_1"] = $r->s1;
$grid[$pid]["{$axisKey}_2"] = $r->s2;
$grid[$pid]["{$axisKey}_3"] = $r->s3;
$grid[$pid]['total'] = $r->total_sum;
}
$exportRows = [];
if ($national) $exportRows[] = $national;
foreach ($grid as $row) {
$exportRows[] = $row;
}
return view('v3.Reports.SafetyAndPrivacy.Country.ConstructionActivityReport', [
'rows' => $exportRows,
'axis' => $axis,
'axisOrder' => array_keys($axis),
]);
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getDelegate()->setRightToLeft(true);
},
];
}
public function drawings(): array
{
$drawing = new Drawing();
$drawing->setName('Logo');
$drawing->setDescription('This is my logo');
$drawing->setPath(public_path('/dist/logo.png'));
$drawing->setWidth(50);
$drawing->setHeight(50);
$drawing->setOffsetX(5);
$drawing->setOffsetY(5);
$drawing->setCoordinates('A1');
$drawing2 = new Drawing();
$drawing2->setName('Logo');
$drawing2->setDescription('This is my logo');
$drawing2->setPath(public_path('/dist/141icon.png'));
$drawing2->setWidth(50);
$drawing2->setHeight(50);
$drawing2->setOffsetX(5);
$drawing2->setOffsetY(5);
$drawing2->setCoordinates('Q1');
return [$drawing, $drawing2];
}
}