Files
admin-erahn/app/Http/Controllers/Reports/OutstandingController.php
T
2025-04-21 14:53:34 +08:00

85 lines
2.7 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\OutstandingAllExportExcel;
class OutstandingController 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');
$outstanding = Http::get($request->hostname . '/api/reports/outstanding', [
'endDate' => $getReportDate($request->endDate),
])->json();
$endDate = $getReportDate($request->endDate);
if ($action === 'excel') {
$file_title = sprintf("LaporanOutstanding(%s).xlsx", $kod_cawangan, $request->endDate);
return Excel::download(new OutstandingAllExportExcel($outstanding), $file_title);
}
$outstanding = $outstanding ?? [];
return view('reports.outstanding.index', compact('outstanding', '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/outstanding", [
'endDate' => $getReportDate($request->endDate),
]);
$outstanding = $response->json() ?? []; // Ensure it's an array
// Debugging: Check the response before using it
if (empty($outstanding)) {
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("LaporanOutstanding(%s).xlsx", $kod_cawangan, $endDate);
return Excel::download(new OutstandingAllExportExcel($outstanding), $file_title);
}
return view('reports.outstanding.index', compact('outstanding', 'endDate'));
}
}