create function excel and their dependency for damage

This commit is contained in:
2025-09-20 11:37:04 +03:30
parent 546be58764
commit 1b7e26517a
5 changed files with 187 additions and 14 deletions

View File

@@ -0,0 +1,67 @@
<?php
namespace App\Exports\V3\Damage;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Collection;
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\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
class DamageCartableReport implements FromView, WithEvents, ShouldAutoSize, WithDrawings
{
public function __construct(private $data){}
public function view(): View
{
return view('v3.Reports.Damage.DamageCartableReport', [
'data' => $this->data
]);
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getDelegate()->setRightToLeft(true);
$event->sheet->getDelegate()->getStyle('A:U')->getFont()->setName('Arial');
$event->sheet->getDelegate()->getStyle('A:U')
->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER);
$event->sheet->getDelegate()->getStyle('A:U')
->getAlignment()
->setVertical(Alignment::VERTICAL_CENTER);
}
];
}
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('f1');
return [$drawing, $drawing2];
}
}

View File

@@ -2,14 +2,19 @@
namespace App\Http\Controllers\V3;
use App\Exports\V3\Damage\DamageCartableReport;
use App\Facades\DataTable\DataTableFacade;
use App\Http\Controllers\Controller;
use App\Http\Requests\V3\Damage\StoreRequest;
use App\Http\Requests\V3\Damage\UpdateRequest;
use App\Http\Traits\ApiResponse;
use App\Models\Damage;
use App\Services\Cartables\Damage\DamageService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Excel;
use PhpOffice\PhpSpreadsheet\Exception;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class DamageManagementController extends Controller
{
@@ -17,20 +22,9 @@ class DamageManagementController extends Controller
/**
* Display a listing of the resource.
*/
public function index(Request $request): JsonResponse
public function index(Request $request ,DamageService $damageService): JsonResponse
{
$allowedFilters = ['*'];
$allowedSortings = ['*'];
$query = Damage::query();
$data = DataTableFacade::run(
$query,
$request,
allowedFilters: $allowedFilters,
allowedSortings: $allowedSortings);
$data = $damageService->dataTable($request);
return response()->json($data);
}
@@ -71,7 +65,6 @@ class DamageManagementController extends Controller
*/
public function update(UpdateRequest $request, Damage $damage): JsonResponse
{
$damage->update([
'title' => $request->title,
@@ -92,4 +85,15 @@ class DamageManagementController extends Controller
return $this->successResponse();
}
/**
* @throws Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function excel(Request $request, DamageService $damageService): BinaryFileResponse
{
$name = 'آیتم خسارات وارده بر ابنیه فنی' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
$data = $damageService->datatable($request);
return Excel::download(new DamageCartableReport($data['data']), $name);
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Services\Cartables\Damage;
use App\Exceptions\ProhibitedAction;
use App\Facades\DataTable\DataTableFacade;
use App\Models\Damage;
use App\Models\SafetyAndPrivacy;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class DamageService
{
public function dataTable(Request $request)
{
return DataTableFacade::run(
Damage::query(),
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedSelects: ['title', 'unit', 'base_price', 'status', 'update_time']
);
}
}

View File

@@ -0,0 +1,77 @@
<!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;">
<table>
<thead>
<tr>
<th colspan="6"
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="6"
style="background-color: #DAEEF3;text-align: center;border-right:1px solid black;font-weight:bolder;">
</th>
</tr>
<tr>
<th colspan="6"
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>
<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['title'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['unit'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['base_price'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['status'] ?? '-' }}</td>
<td style="border: 1px solid black;text-align: center;">{{ $item['update_time'] ?? '-' }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>

View File

@@ -273,6 +273,7 @@ Route::prefix('damages')
Route::get('/{damage}', 'show')->name('show');
Route::post('/{damage}', 'update')->name('update');
Route::post('/activate/{damage}', 'activate')->name('activate');
Route::get('/excel', 'excel')->name('excel');
});
Route::prefix('receipts')