tambah laporan keseluruhan peti besi

This commit is contained in:
BTMS2
2025-05-04 11:23:57 +08:00
parent e250f66797
commit f70c2ef4ef
6 changed files with 274 additions and 0 deletions
@@ -0,0 +1,72 @@
<?php
namespace App\Exports\LaporanKeseluruhan;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class PetibesiAllExportExcel implements FromCollection, WithHeadings, WithMapping, WithEvents
{
protected $petibesiData;
protected $total = 0;
protected $counter = 1;
public function __construct($petibesiData)
{
$this->petibesiData = $petibesiData;
}
public function collection()
{
return collect($this->petibesiData);
}
public function headings(): array
{
return [
'No.',
'Kod Cawangan',
'Cawangan',
'Nilai (RM)',
];
}
public function map($row): array
{
$nilai = is_numeric($row['bakieod'] ?? null) ? (float) $row['bakieod'] : 0;
$this->total += $nilai;
return [
$this->counter++,
$row['kodcaw'] ?? '',
$row['details_caw'] ?? '',
number_format($nilai, 2),
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
// 1. Tajuk laporan pada baris 1
$event->sheet->mergeCells('A1:D1');
$event->sheet->setCellValue('A1', 'LAPORAN PETI BESI');
$event->sheet->getStyle('A1')->getFont()->setSize(14)->setBold(true);
$event->sheet->getStyle('A1')->getAlignment()->setHorizontal('center');
// 2. Jumlah keseluruhan di bawah data
$lastRow = count($this->petibesiData) + 3; // +3 sebab ada tajuk + heading
$event->sheet->setCellValue("A{$lastRow}", 'Jumlah Keseluruhan');
$event->sheet->mergeCells("A{$lastRow}:C{$lastRow}");
$event->sheet->setCellValue("D{$lastRow}", number_format($this->total, 2));
$event->sheet->getStyle("A{$lastRow}:D{$lastRow}")->getFont()->setBold(true);
},
];
}
}
@@ -0,0 +1,84 @@
<?php
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Http;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Auth;
use App\Exports\LaporanKeseluruhan\PetibesiAllExportExcel;
class PetibesiController extends Controller
{
public function index(Request $request)
{
$current = new Carbon();
$request->hostname = config('api.url');
// dd($request->hostname );
$kod_cawangan = $request->kod_cawangan;
$action = $request->input('action', 'search');
// dd($action);
$getReportDate = function ($date_string) use ($current) {
if ($date_string == null) {
return $current->toDateString();
}
return $date_string;
};
$request->hostname = config('api.url');
$petibesi = Http::get($request->hostname . '/api/reports/petibesi', [
'endDate' => $getReportDate($request->endDate),
])->json();
$endDate = $getReportDate($request->endDate);
if ($action === 'excel') {
$file_title = sprintf("LaporanPetibesiCawangan.xlsx", $kod_cawangan, $request->endDate);
return Excel::download(new PetibesiAllExportExcel($petibesi), $file_title);
}
$petibesi = $petibesi ?? [];
return view('reports.petibesi.index', compact('petibesi', 'endDate'));
}
public function show(Request $request)
{
$current = Carbon::now();
$action = $request->input('action', 'search');
$getReportDate = function ($date_string) use ($current) {
return $date_string ? $date_string : $current->toDateString();
};
$hostname = config('api.url');
$response = Http::get("$hostname/api/reports/petibesi", [
'endDate' => $getReportDate($request->endDate),
]);
$petibesi = $response->json() ?? []; // Ensure it's an array
// Debugging: Check the response before using it
if (empty($petibesi)) {
return back()->with('error', 'Failed to fetch data from API.');
}
$endDate = $getReportDate($request->endDate);
if ($action === 'excel') {
// Ensure $kod_cawangan is defined (Modify as per your data structure)
$kod_cawangan = $request->input('kod_cawangan', 'DEFAULT');
$file_title = sprintf("LaporanPetibesi(%s).xlsx", $kod_cawangan, $endDate);
return Excel::download(new PetibesiAllExportExcel($petibesi), $file_title);
}
return view('reports.petibesi.index', compact('petibesi', 'endDate'));
}
}