124 lines
4.3 KiB
PHP
124 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Reports;
|
|
|
|
use App\Exports\Ansuran\AnsuranExport;
|
|
use App\Http\Controllers\Controller;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\LaporanKeseluruhan\AnsuranAllExportExcel;
|
|
|
|
class AnsuranController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$current = new Carbon();
|
|
$request->hostname = config('api.url');
|
|
$kod_cawangan = $request->kod_cawangan;
|
|
$action = $request->input('action', 'search');
|
|
|
|
$getReportDate = function ($date_string) use ($current) {
|
|
if ($date_string == null) {
|
|
return $current->toDateString();
|
|
}
|
|
|
|
return $date_string;
|
|
};
|
|
|
|
$request->hostname = config('api.url');
|
|
|
|
$data = Http::get($request->hostname . '/api/reports/ansuran', [
|
|
'startDate' => $getReportDate($request->startDate),
|
|
'endDate' => $getReportDate($request->endDate),
|
|
])->json();
|
|
|
|
$summary_ansuran_by_cawangan = $data['summary_ansuran_by_cawangan'];
|
|
$summary_ansuran = $data['summary_ansuran'];
|
|
|
|
$startDate = $getReportDate($request->startDate);
|
|
$endDate = $getReportDate($request->endDate);
|
|
|
|
if ($action === 'excel') {
|
|
$file_title = sprintf("LaporanAnsuran %s (%s - %s).xlsx", $kod_cawangan, $request->startDate, $request->endDate);
|
|
return Excel::download(new AnsuranAllExportExcel($data), $file_title);
|
|
}
|
|
|
|
return view('reports.ansuran.index', compact('summary_ansuran_by_cawangan', 'summary_ansuran', 'startDate', 'endDate'));
|
|
}
|
|
|
|
public function show(Request $request)
|
|
{
|
|
$current = new Carbon();
|
|
$action = $request->input('action', 'search');
|
|
|
|
$getReportDate = function ($date_string) use ($current) {
|
|
if ($date_string == null) {
|
|
return $current->toDateString();
|
|
}
|
|
|
|
return $date_string;
|
|
};
|
|
|
|
$hostname = config('api.url');
|
|
|
|
$response = Http::get($hostname . '/api/reports/ansuran', [
|
|
'startDate' => $getReportDate($request->startDate),
|
|
'endDate' => $getReportDate($request->endDate),
|
|
]);
|
|
|
|
$ansuran = $response->json() ?? [];
|
|
if (empty($ansuran)) {
|
|
return back()->with('error', 'Failed to fetch data from API.');
|
|
}
|
|
|
|
$summary_ansuran_by_cawangan = $response['summary_ansuran_by_cawangan'];
|
|
$summary_ansuran = $response['summary_ansuran'];
|
|
|
|
$startDate = $getReportDate($request->startDate);
|
|
$endDate = $getReportDate($request->endDate);
|
|
// dd($ansuran);
|
|
|
|
if ($action === 'excel') {
|
|
|
|
$exportData = $ansuran['summary_ansuran_by_cawangan'];
|
|
// Ensure $kod_cawangan is defined (Modify as per your data structure)
|
|
$kod_cawangan = $request->input('kod_cawangan', 'DEFAULT');
|
|
|
|
$file_title = sprintf("LaporanAnsuran %s (%s - %s).xlsx", $kod_cawangan, $startDate, $endDate);
|
|
return Excel::download(new AnsuranAllExportExcel($exportData), $file_title);
|
|
}
|
|
return view('reports.ansuran.index', compact('summary_ansuran_by_cawangan', 'summary_ansuran', 'startDate', 'endDate'));
|
|
}
|
|
|
|
public function downloadAnsuranCawanganExcel(Request $request)
|
|
{
|
|
$api_url = $request->hostname;
|
|
$auth_user = Auth::user();
|
|
$current = Carbon::now();
|
|
$current = new Carbon();
|
|
$tarikh_cetak = $current->toDateString();
|
|
|
|
$getReportDate = function ($date_string) use ($current) {
|
|
if ($date_string == null) {
|
|
return $current->toDateString();
|
|
}
|
|
|
|
return $date_string;
|
|
};
|
|
|
|
$request->hostname = config('api.url');
|
|
$kod_cawangan = $request->kod_cawangan;
|
|
|
|
$data = Http::get($request->hostname . '/api/reports/ansuran/' . $kod_cawangan, [
|
|
'startDate' => $getReportDate($request->startDate),
|
|
'endDate' => $getReportDate($request->endDate),
|
|
])->json();
|
|
|
|
$file_title = sprintf("LaporanAnsuran %s (%s - %s).xlsx", $kod_cawangan, $request->startDate, $request->endDate);
|
|
return Excel::download(new AnsuranExport($data), $file_title);
|
|
}
|
|
}
|