crate excel for volation mission

This commit is contained in:
2025-12-31 20:07:07 +03:30
parent cd17555680
commit 79a5d56315
5 changed files with 366 additions and 16 deletions

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Exports\V3\Mission\Violation;
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 CountryReport implements FromView, WithEvents, ShouldAutoSize, WithDrawings
{
public function __construct(private array $data){}
public function view(): View
{
$grid = [];
$national = null;
foreach ($this->data as $r) {
$pid = (int) $r->province_id;
$name = $r->province_name;
if ($pid === -1) {
$national = [
'province_name' => $name,
'no_mission_count' => (int) ($r->no_mission_count ?? 0),
'out_of_area_count' => (int) ($r->out_of_area_count ?? 0),
];
continue;
}
if (!isset($grid[$pid])) {
$grid[$pid] = [
'province_name' => $name,
];
}
$grid[$pid]['no_mission_count'] = (int) ($r->no_mission_count ?? 0);
$grid[$pid]['out_of_area_count'] = (int) ($r->out_of_area_count ?? 0);
}
$exportRows = [];
if ($national) $exportRows[] = $national;
foreach ($grid as $row) {
$exportRows[] = $row;
}
return view('v3.Reports.Mission.Violation.CountryActivityReport', [
'rows' => $exportRows,
]);
}
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('E1');
return [$drawing, $drawing2];
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Exports\V3\Mission\Violation;
use App\Models\EdarateShahri;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
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\Exception;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class ProvinceReport implements FromView, WithEvents, ShouldAutoSize, WithDrawings
{
public function __construct(private array $data){}
public function view(): View
{
$grid = [];
$province = null;
foreach ($this->data as $r) {
$pid = (int) $r->province_id;
$name = $r->city_name;
if ($pid === -1) {
$province = [
'city_name' => $name,
'no_mission_count' => (int) ($r->no_mission_count ?? 0),
'out_of_area_count' => (int) ($r->out_of_area_count ?? 0),
];
continue;
}
if (!isset($grid[$pid])) {
$grid[$pid] = [
'city_name' => $name,
];
}
$grid[$pid]['no_mission_count'] = (int) ($r->no_mission_count ?? 0);
$grid[$pid]['out_of_area_count'] = (int) ($r->out_of_area_count ?? 0);
}
$exportRows = [];
if ($province) $exportRows[] = $province;
foreach ($grid as $row) {
$exportRows[] = $row;
}
return view('v3.Reports.Mission.Violation.ProvinceActivityReport', [
'rows' => $exportRows,
]);
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getDelegate()->setRightToLeft(true);
},
];
}
/**
* @throws Exception
*/
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('E1');
return [$drawing, $drawing2];
}
}

View File

@@ -2,6 +2,8 @@
namespace App\Http\Controllers\V3\Dashboard\Mission;
use App\Exports\V3\Mission\Violation\CountryReport;
use App\Exports\V3\Mission\Violation\ProvinceReport;
use App\Http\Controllers\Controller;
use App\Http\Traits\ApiResponse;
use App\Models\City;
@@ -9,6 +11,9 @@ use App\Models\Province;
use App\Services\Cartables\Mission\ReportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use PhpOffice\PhpSpreadsheet\Exception;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class ReportController extends Controller
{
@@ -29,20 +34,28 @@ class ReportController extends Controller
'city' => City::query()->where('province_id', $request->province_id)->get(['id', 'name_fa']),
]);
}
//
// public function countryExcelActivity(Request $request, OperatorService $operatorService): BinaryFileResponse
// {
// $data = $operatorService->countryActivity($request);
// $name = 'گزارش تخلفات ماموریت ها '.verta()->now()->format('Y-m-d H-i').'.xlsx';
//
// return Excel::download(new ExcelReport($data), $name);
// }
//
// public function provinceExcelActivity(Request $request, OperatorService $operatorService): BinaryFileResponse
// {
// $data = $operatorService->provinceActivity($request);
// $name = 'گزارش تخلفات ماموریت ها '.verta()->now()->format('Y-m-d H-i').'.xlsx';
//
// return Excel::download(new ProvinceExcelReport($data, $request), $name);
// }
/**
* @throws Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function countryExcelActivity(Request $request, ReportService $reportService): BinaryFileResponse
{
$data = $reportService->countryActivity($request);
$name = 'گزارش تخلفات ماموریت ها '.verta()->now()->format('Y-m-d H-i').'.xlsx';
return Excel::download(new CountryReport($data), $name);
}
/**
* @throws Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function provinceExcelActivity(Request $request, ReportService $reportService): BinaryFileResponse
{
$data = $reportService->provinceActivity($request);
$name = 'گزارش تخلفات ماموریت ها '.verta()->now()->format('Y-m-d H-i').'.xlsx';
return Excel::download(new ProvinceReport($data), $name);
}
}

View File

@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> گزارش کل کشور </title>
<style>
table,
th,
td {
border: 1px solid black;
}
</style>
</head>
<body dir="rtl" style="text-align: center;">
@php
// show a dot if key missing or value is 0/empty
function cell($row, $key) {
return array_key_exists($key, $row) ? ($row[$key] ?: '0') : '0';
}
@endphp
<table>
<thead>
<tr>
<th colspan="4"
style="background-color: #DAEEF3;text-align: center;border-right: 1px solid black;font-weight:bolder;">
تاریخ دریافت گزارش: {{ verta()->now()->format('Y/m/d H:i') }}
</th>
</tr>
<tr>
<th colspan="4"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;">
</th>
</tr>
<tr>
<th colspan="4"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13px;">
گزارش نخلف های صورت گرفته
</th>
</tr>
<tr>
<th rowspan="1"
style="text-align: center;border: 1px solid black;background-color: #EBF1DE;font-weight: bold;">
کد یکتا
</th>
<th rowspan="1"
style="text-align: center;border: 1px solid black;background-color: #EBF1DE;font-weight: bold;">
خروج بدون ماموریت </th>
<th rowspan="1"
style="text-align: center;border: 1px solid black;background-color: #EBF1DE;font-weight: bold;">
خارج از محدوده
</th>
</tr>
</thead>
<tbody>
@foreach ($data as $item)
<tr>
<td style="border: 1px solid black;text-align: center;">{{ $item['id'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['no_mission_count'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['out_of_area_count'] ?? '-' }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>

View File

@@ -0,0 +1,75 @@
<!DOCTYPE html>
<html lang="fa">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> گزارش ادارات شهرستان </title>
<style>
table,
th,
td {
border: 1px solid black;
}
</style>
</head>
<body dir="rtl" style="text-align: center;">
@php
// show a dot if key missing or value is 0/empty
function cell($row, $key) {
return array_key_exists($key, $row) ? ($row[$key] ?: '0') : '0';
}
@endphp
<table>
<thead>
<tr>
<th colspan="4"
style="background-color: #DAEEF3;text-align: center;border-right: 1px solid black;font-weight:bolder;">
تاریخ دریافت گزارش: {{ verta()->now()->format('Y/m/d H:i') }}
</th>
</tr>
<tr>
<th colspan="4"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;">
</th>
</tr>
<tr>
<th colspan="4"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;font-size: 13px;">
گزارش نخلف های صورت گرفته
</th>
</tr>
<tr>
<th rowspan="1"
style="text-align: center;border: 1px solid black;background-color: #EBF1DE;font-weight: bold;">
کد یکتا
</th>
<th rowspan="1"
style="text-align: center;border: 1px solid black;background-color: #EBF1DE;font-weight: bold;">
خروج بدون ماموریت </th>
<th rowspan="1"
style="text-align: center;border: 1px solid black;background-color: #EBF1DE;font-weight: bold;">
خارج از محدوده
</th>
</tr>
</thead>
<tbody>
@foreach ($data as $item)
<tr>
<td style="border: 1px solid black;text-align: center;">{{ $item['id'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['no_mission_count'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['out_of_area_count'] ?? '-' }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>