Files
admin-erahn/app/Http/Controllers/AutoSerahanController.php
T
2025-07-09 16:06:24 +08:00

231 lines
8.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Events\AutoSerahan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Log;
use Carbon\Carbon;
class AutoSerahanController extends Controller
{
public function index(Request $request)
{
$response = Http::get(config('api.url') . '/api/autoserahan/list', [
'page' => request('page', 1) // Get current page from request
])->json();
$items = $response['data'] ?? []; // Data array
$total = $response['total'] ?? 0; // Total items
$perPage = $response['per_page'] ?? 10; // Items per page
$currentPage = $response['current_page'] ?? 1; // Current page
$items = array_map(function ($item) {
$time_created = Carbon::parse($item['created_at'])->setTimezone('Asia/Kuala_Lumpur');
$item['cawangan_name'] = $this->CawanganKod($item['kodcaw']);
$item['tarikh'] = $time_created->toDateString();
$item['masa'] = $time_created->format('h:i A');
return $item;
}, $items);
// Create Laravel paginator
$list = new LengthAwarePaginator($items, $total, $perPage, $currentPage, [
'path' => request()->url(), // Current URL
'query' => request()->query(), // Append other query params
]);
return view('autoserahan.index', compact('list'));
}
public function record(Request $request){
$response = Http::get(config('api.url') . '/api/autoserahan/allrecord', [
'page' => request('page', 1),
'kodcaw' => request('kodcaw', null),
'tarikh_mula' => request('tarikh_mula', null),
'tarikh_hingga' => request('tarikh_hingga', null),
'masa_mula' => request('masa_mula', null),
'masa_hingga' => request('masa_hingga', null),
'status' => request('status', null),
'transaksi' => request('transaksi', null),
])->json();
$items = $response['data'] ?? []; // Data array
$total = $response['total'] ?? 0; // Total items
$perPage = $response['per_page'] ?? 10; // Items per page
$currentPage = $response['current_page'] ?? 1; // Current page
$items = array_map(function ($item) {
$time_created = Carbon::parse($item['created_at'])->setTimezone('Asia/Kuala_Lumpur');
$item['cawangan_name'] = $this->CawanganKod($item['kodcaw']);
$item['tarikh'] = $time_created->toDateString();
$item['masa'] = $time_created->format('h:i A');
return $item;
}, $items);
$list = new LengthAwarePaginator($items, $total, $perPage, $currentPage, [
'path' => request()->url(), // Current URL
'query' => request()->query(), // Append other query params
]);
$filteredCawangan = $this->listkodcaw();
return view('autoserahan.rekod', compact('list','filteredCawangan'));
}
public function getList(Request $request){
// Fetch paginated data from API
$response = Http::get(config('api.url') . '/api/autoserahan/list', [
'page' => request('page', 1) // Get current page from request
])->json();
// Extract pagination details
$items = $response['data'] ?? []; // Data array
$total = $response['total'] ?? 0; // Total items
$perPage = $response['per_page'] ?? 10; // Items per page
$currentPage = $response['current_page'] ?? 1; // Current page
$items = array_map(function ($item) {
$time_created = Carbon::parse($item['created_at'])->setTimezone('Asia/Kuala_Lumpur');
$item['cawangan_name'] = $this->CawanganKod($item['kodcaw']);
$item['tarikh'] = $time_created->toDateString();
$item['masa'] = $time_created->format('h:i A');
return $item;
}, $items);
// Create Laravel paginator
$list = new LengthAwarePaginator($items, $total, $perPage, $currentPage, [
'path' => request()->url(), // Current URL
'query' => request()->query(), // Append other query params
]);
return response()->json($list);
}
public function approve(Request $request)
{
$response = Http::put(config('api.url') . '/api/autoserahan/approve', [
'id' => $request->id,
'kodcaw' => $request->kodcaw,
'teller' => $request->teller,
'amaun' => $request->amaun,
'sendApproval' => 'ACCEPTED',
'operasi_name' => auth()->user()->name,
])->json();
if($response['approve_status'] == 'duplicate'){
return redirect()->route('autoserahan')->with('error', 'Already Approved.');
}
if($response['approve_status'] == 'approved'){
$approvalserahan = Http::get(config('api.url_koop') . '/api/autoserahan/get', [
'id' => $request->id,
'kodcaw' => $request->kodcaw
])->json();
if($approvalserahan['action'] == 'create'){
$pendahuluan = Http::put(config('api.url_koop') . '/api/autoserahan/pendahuluan', [
'id' => $request->id,
'kodcaw' => $request->kodcaw,
'teller' => $request->teller,
'amaun' => $request->amaun,
'denopendser_id' => $approvalserahan['denopendser_id_v3']
])->json();
$serahan = Http::put(config('api.url_pkb') . '/api/autoserahan/serahan', [
'id' => $request->id,
'kodcaw' => $request->kodcaw,
'teller' => $request->teller,
'amaun' => $request->amaun,
'denopendser_id' => $approvalserahan['denopendser_id_v2']
])->json();
$call_set_approval_v3 = Http::withOptions(['verify' => config('app.api_verify')])
->put(config('api.url_koop') . '/api/autoserahan/setapproval', [
'id' => $request->id,
'jenis' => 'rewrite',
'gldetail_id' => $serahan['gldetail_id']
])->json();
}else{
$pendahuluan = Http::put(config('api.url_koop') . '/api/autoserahan/delete/pendahuluan', [
'array_approval' => $approvalserahan,
'kodcaw' => $request->kodcaw,
'teller' => $request->teller,
'amaun' => $request->amaun,
])->json();
$serahan = Http::put(config('api.url_pkb') . '/api/autoserahan/delete/serahan', [
'array_approval' => $approvalserahan,
'kodcaw' => $request->kodcaw,
'teller' => $request->teller,
'amaun' => $request->amaun,
])->json();
}
}
if($response['status'] == 'success'){
event(new AutoSerahan("Lulus",$request->id,$request->kodcaw,$request->teller));
return redirect()->route('autoserahan')->with('success', 'Serahan approved successfully');
}else{
event(new AutoSerahan("Error",$request->id,$request->kodcaw,$request->teller));
return redirect()->route('autoserahan')->with('error', 'Serahan approved failed');
}
}
public function reject(Request $request)
{
$response = Http::put(config('api.url') . '/api/autoserahan/approve', [
'id' => $request->id,
'kodcaw' => $request->kodcaw,
'teller' => $request->teller,
'amaun' => $request->amaun,
'sendApproval' => 'REJECTED',
'operasi_name' => auth()->user()->name,
])->json();
if($response['approve_status'] == 'duplicate'){
return redirect()->route('autoserahan')->with('error', 'Already Rejected.');
}
if($response['status'] == 'success'){
event(new AutoSerahan("Batal",$request->id,$request->kodcaw,$request->teller));
return redirect()->route('autoserahan')->with('success', 'Serahan rejected successfully');
}else{
event(new AutoSerahan("Error",$request->id,$request->kodcaw,$request->teller));
return redirect()->route('autoserahan')->with('error', 'Serahan rejected failed');
}
}
private function listkodcaw(){
$cawangan = Http::get(config('api.url_koop') . '/api/cawangan/')->json();
// Extract only kodcaw and details_caw
$filteredCawangan = array_map(function ($item) {
return [
'kodcaw' => $item['kodcaw'],
'details_caw' => $item['details_caw']
];
}, $cawangan);
return $filteredCawangan;
}
private function CawanganKod($kodcaw) {
$cawangan = Http::get(config('api.url_koop') . '/api/cawangan/' . $kodcaw)->json();
return $cawangan['details_caw'] ?? 'Undefined';
}
}