85 lines
2.7 KiB
PHP
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\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'));
|
|
}
|
|
}
|