Menggabungkan beberapa baris kode untuk menyederhanakan dan meningkatkan efisiensi pengambilan data di fungsi progres_activity. Inisialisasi variabel disingkat dan penggunaan metode chaining diterapkan untuk query Eloquent. Selain itu, penyaringan peran juga disederhanakan dengan menggunakan fungsi lambda.
299 lines
9.2 KiB
PHP
299 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\Lpj\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Exception;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Modules\Lpj\Models\Permohonan;
|
|
use Modules\Lpj\Models\Penilaian;
|
|
use Modules\Lpj\Models\TeamsUsers;
|
|
use Modules\Lpj\Models\PenilaianTeam;
|
|
use Modules\Lpj\Models\StatusPermohonan;
|
|
use Modules\Lpj\Exports\PermohonanExport;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class ActivityController extends Controller
|
|
{
|
|
public $user;
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$status_permohonan = StatusPermohonan::all();
|
|
return view('lpj::activity.index', compact('status_permohonan'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
|
|
|
|
public function progres_activity()
|
|
{
|
|
// Ambil user yang sedang login dengan roles
|
|
$user = auth()->user()->load('roles');
|
|
|
|
// Inisialisasi regionId dan teamId
|
|
$regionId = $teamId = null;
|
|
|
|
if ($user->roles->pluck('name')->contains('senior-officer')) {
|
|
$userTeam = TeamsUsers::with('team')->firstWhere('user_id', $user->id);
|
|
$regionId = $userTeam?->team->regions_id;
|
|
$teamId = $userTeam?->teams_id;
|
|
}
|
|
|
|
$teamsActivity = TeamsUsers::with(['user', 'team', 'team.regions', 'user.roles'])
|
|
->whereHas('team', function ($q) use ($regionId, $teamId) {
|
|
$q->when($regionId, fn($q) => $q->where('regions_id', $regionId))
|
|
->when($teamId, fn($q) => $q->where('id', $teamId));
|
|
})
|
|
->where('user_id', '!=', $user->id)
|
|
->whereHas('user.roles', fn($q) => $q->whereIn('name', ['surveyor', 'surveyor-penilai']))
|
|
->get();
|
|
|
|
return view('lpj::activity.progres_activity.index', compact('teamsActivity'));
|
|
}
|
|
|
|
|
|
function updateTeamAssingment(Request $request) {
|
|
|
|
try {
|
|
$id = $request->input('id');
|
|
$user = PenilaianTeam::where('penilaian_id', $id)->get();
|
|
if ($user) {
|
|
foreach ($user as $item) {
|
|
if($item->role == 'surveyor') {
|
|
$item->update(['user_id' => $request->surveyor_id]);
|
|
}
|
|
}
|
|
return redirect()->route('activity.progres.index')->with('success', 'Surveyor berhasil diganti');
|
|
}
|
|
|
|
} catch (\Throwable $th) {
|
|
return redirect()->route('activity.progres.index')->with('success', $th->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function senior()
|
|
{
|
|
return view('lpj::activity.senior_officer.index');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request): RedirectResponse
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the specified resource.
|
|
*/
|
|
public function show($id)
|
|
{
|
|
|
|
$status_permohonan = StatusPermohonan::orderBy('id')->get()->reverse();
|
|
|
|
$permohonan = Permohonan::with(
|
|
[
|
|
'user',
|
|
'debiture.province',
|
|
'debiture.city',
|
|
'debiture.district',
|
|
'debiture.village',
|
|
'branch',
|
|
'tujuanPenilaian',
|
|
'penilaian'
|
|
],
|
|
)->findOrFail($id);
|
|
|
|
return view('lpj::activity.activitydetail', compact('id', 'status_permohonan', 'permohonan'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
return view('lpj::edit');
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/public function dataForDatatables(Request $request)
|
|
{
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
// Check permissions
|
|
if (is_null($this->user) || !$this->user->can('debitur.view')) {
|
|
// abort(403, 'Sorry! You are not allowed to view users.');
|
|
}
|
|
|
|
// Retrieve data from the database
|
|
$query = Permohonan::query();
|
|
|
|
// Apply search filter if provided
|
|
if ($request->has('search') && !empty($request->get('search'))) {
|
|
$search = $request->get('search');
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('nomor_registrasi', 'LIKE', '%' . $search . '%')
|
|
->orWhere('tanggal_permohonan', 'LIKE', '%' . $search . '%')
|
|
->orWhereRelation('user', 'name', 'LIKE', '%' . $search . '%')
|
|
->orWhereRelation('debiture', 'name', 'LIKE', '%' . $search . '%')
|
|
->orWhereRelation('tujuanPenilaian', 'name', 'LIKE', '%' . $search . '%')
|
|
->orWhereRelation('branch', 'name', 'LIKE', '%' . $search . '%');
|
|
|
|
// Split search term by comma to allow multiple statuses
|
|
$statusKeywords = explode(',', $search);
|
|
foreach ($statusKeywords as $keyword) {
|
|
$q->orWhere('status', 'LIKE', '%' . trim($keyword) . '%');
|
|
}
|
|
});
|
|
}
|
|
|
|
// Default sorting if no sort provided
|
|
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
|
$order = $request->get('sortOrder');
|
|
$column = $request->get('sortField');
|
|
$query->orderBy($column, $order);
|
|
} else {
|
|
$query->orderBy('nomor_registrasi', 'asc');
|
|
}
|
|
|
|
// Get total count of records before pagination
|
|
$totalRecords = $query->count();
|
|
|
|
// Pagination
|
|
if ($request->has('page') && $request->has('size')) {
|
|
$page = (int) $request->get('page', 1);
|
|
$size = (int) $request->get('size', 10);
|
|
$offset = ($page - 1) * $size;
|
|
$query->skip($offset)->take($size);
|
|
}
|
|
|
|
// Get filtered count
|
|
$filteredRecords = $query->count();
|
|
|
|
// Get data
|
|
|
|
$data = null;
|
|
$userRole = $user->roles[0]->name ?? null;
|
|
|
|
if (in_array($userRole, ['surveyor', 'surveyor-penilai'])) {
|
|
$data = $query->with(['user', 'debiture', 'branch', 'tujuanPenilaian', 'penilaian',])
|
|
->whereHas('penilaian.userPenilai', function ($q) use ($user) {
|
|
$q->where('user_id', $user->id);
|
|
})
|
|
->get();
|
|
} else {
|
|
$data = $query->with(['user', 'debiture', 'branch', 'tujuanPenilaian'])
|
|
->get();
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate total pages
|
|
$pageCount = ceil($totalRecords / $request->get('size', 10));
|
|
|
|
return response()->json([
|
|
'draw' => $request->get('draw'),
|
|
'recordsTotal' => $totalRecords,
|
|
'recordsFiltered' => $filteredRecords,
|
|
'pageCount' => $pageCount,
|
|
'page' => $request->get('page', 1),
|
|
'totalCount' => $totalRecords,
|
|
'data' => $data,
|
|
]);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Download the specified resource from storage.
|
|
*/
|
|
public function download($id)
|
|
{
|
|
$document = Permohonan::find($id);
|
|
return response()->download(storage_path('app/public/' . $document->dokumen));
|
|
}
|
|
|
|
public function export()
|
|
{
|
|
return Excel::download(new PermohonanExport(), 'activity.xlsx');
|
|
}
|
|
|
|
|
|
|
|
|
|
public function dataTablesForActivity(Request $request, $id)
|
|
{
|
|
|
|
if (is_null($this->user) || !$this->user->can('debitur.view')) {
|
|
// abort(403, 'Sorry! You are not allowed to view users.');
|
|
}
|
|
// Query Penilaian dengan relasi yang diperlukan
|
|
$query = Penilaian::with([
|
|
'permohonan',
|
|
'permohonan.debiture',
|
|
'permohonan.tujuanPenilaian',
|
|
'permohonan.debiture.documents.jenisJaminan',
|
|
'userPenilai'
|
|
])
|
|
->whereHas('userPenilai', function ($q) use ($id) {
|
|
$q->where('user_id', $id);
|
|
});
|
|
|
|
|
|
|
|
// Filter pencarian
|
|
if ($request->has('search') && !empty($request->get('search'))) {
|
|
$search = $request->get('search');
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('nomor_registrasi', 'LIKE', "%$search%")
|
|
->orWhere('status', 'LIKE', "%$search%");
|
|
});
|
|
}
|
|
|
|
// Sorting
|
|
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
|
$order = $request->get('sortOrder');
|
|
$column = $request->get('sortField');
|
|
$query->orderBy($column, $order);
|
|
}
|
|
|
|
// Hitung total records
|
|
$totalRecords = $query->count();
|
|
|
|
// Pagination
|
|
$size = $request->get('size', 10);
|
|
$page = $request->get('page', 1);
|
|
$offset = ($page - 1) * $size;
|
|
|
|
// Ambil data dengan pagination
|
|
$data = $query->skip($offset)->take($size)->get();
|
|
$filteredRecords = $data->count();
|
|
$pageCount = ceil($totalRecords / $size);
|
|
|
|
// Return data dalam bentuk JSON
|
|
return response()->json([
|
|
'draw' => $request->get('draw'),
|
|
'recordsTotal' => $totalRecords,
|
|
'recordsFiltered' => $filteredRecords,
|
|
'pageCount' => $pageCount,
|
|
'page' => $page,
|
|
'totalCount' => $totalRecords,
|
|
'data' => $data
|
|
]);
|
|
}
|
|
|
|
}
|