Files
admin-erahn/app/Http/Controllers/Reports/AgingController.php
T
2026-05-28 15:56:36 +08:00

158 lines
5.1 KiB
PHP

<?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\AgingTebusAllExportExcel;
class AgingController 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');
$aging = Http::get($request->hostname . '/api/reports/aging', [
'endDate' => $getReportDate($request->endDate),
])->json();
$endDate = $getReportDate($request->endDate);
if ($action === 'excel') {
$file_title = sprintf("LaporanAging(%s).xlsx", $kod_cawangan, $request->endDate);
return Excel::download(new AgingAllExportExcel($aging), $file_title);
}
$aging = $aging ?? [];
return view('reports.aging.index', compact('aging', '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/aging", [
'endDate' => $getReportDate($request->endDate),
]);
$aging = $response->json() ?? []; // Ensure it's an array
// Debugging: Check the response before using it
if (empty($aging)) {
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("LaporanAging(%s).xlsx", $kod_cawangan, $endDate);
return Excel::download(new AgingAllExportExcel($aging), $file_title);
}
return view('reports.aging.index', compact('aging', 'endDate'));
}
public function agingtebus(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');
$startDate = $getReportDate($request->startDate);
$endDate = $getReportDate($request->endDate);
$aging = Http::get($request->hostname . '/api/reports/agingtebus', [
'startDate' => $startDate,
'endDate' => $endDate,
])->json();
$aging = $aging ?? [];
if ($action === 'excel') {
$file_title = sprintf("LaporanAging(%s).xlsx", $kod_cawangan, $startDate, $endDate);
return Excel::download(new AgingTebusAllExportExcel($aging,$startDate,$endDate), $file_title);
}
return view('reports.agingtebus.index', compact('aging', 'startDate', 'endDate'));
}
public function showagingtebus(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');
$startDate = $getReportDate($request->startDate);
$endDate = $getReportDate($request->endDate);
$response = Http::get("$hostname/api/reports/agingtebus", [
'startDate' => $startDate,
'endDate' => $endDate,
]);
$aging = $response->json() ?? []; // Ensure it's an array
// Debugging: Check the response before using it
if (empty($aging)) {
return back()->with('error', 'Failed to fetch data from API.');
}
if ($action === 'excel') {
// Ensure $kod_cawangan is defined (Modify as per your data structure)
$kod_cawangan = $request->input('kod_cawangan', 'DEFAULT');
$file_title = sprintf("LaporanAgingTebus(%s).xlsx", $kod_cawangan, $startDate, $endDate);
return Excel::download(new AgingTebusAllExportExcel($aging, $startDate, $endDate), $file_title);
}
return view('reports.agingtebus.index', compact('aging', 'startDate', 'endDate'));
}
}