perbaikan upadte assign team activity jika berhalangan by region dan team
This commit is contained in:
@@ -14,6 +14,8 @@ use Modules\Lpj\Models\PenilaianTeam;
|
|||||||
use Modules\Lpj\Models\StatusPermohonan;
|
use Modules\Lpj\Models\StatusPermohonan;
|
||||||
use Modules\Lpj\Exports\PermohonanExport;
|
use Modules\Lpj\Exports\PermohonanExport;
|
||||||
use Maatwebsite\Excel\Facades\Excel;
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
|
use Modules\Lpj\Models\Teams;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class ActivityController extends Controller
|
class ActivityController extends Controller
|
||||||
{
|
{
|
||||||
@@ -46,6 +48,7 @@ class ActivityController extends Controller
|
|||||||
$teamId = $userTeam?->teams_id;
|
$teamId = $userTeam?->teams_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
$teamsActivity = TeamsUsers::with(['user', 'team', 'team.regions', 'user.roles'])
|
$teamsActivity = TeamsUsers::with(['user', 'team', 'team.regions', 'user.roles'])
|
||||||
->whereHas('team', function ($q) use ($regionId, $teamId) {
|
->whereHas('team', function ($q) use ($regionId, $teamId) {
|
||||||
$q->when($regionId, fn ($q) => $q->where('regions_id', $regionId))
|
$q->when($regionId, fn ($q) => $q->where('regions_id', $regionId))
|
||||||
@@ -55,7 +58,15 @@ class ActivityController extends Controller
|
|||||||
->whereHas('user.roles', fn ($q) => $q->whereIn('name', ['surveyor', 'surveyor-penilai']))
|
->whereHas('user.roles', fn ($q) => $q->whereIn('name', ['surveyor', 'surveyor-penilai']))
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
return view('lpj::activity.progres_activity.index', compact('teamsActivity'));
|
$teamId = is_array($teamId) ? $teamId : [$teamId];
|
||||||
|
|
||||||
|
$teamPenilai = Teams::with(['regions', 'teamsUsers', 'teamsUsers.user'])->whereNotIn(
|
||||||
|
'id',
|
||||||
|
$teamId
|
||||||
|
)->get();
|
||||||
|
|
||||||
|
|
||||||
|
return view('lpj::activity.progres_activity.index', compact('teamsActivity', 'teamPenilai'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -63,23 +74,31 @@ class ActivityController extends Controller
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
// Validasi input
|
// Validasi input
|
||||||
$request->validate([
|
$validatedData = $request->validate([
|
||||||
'id' => 'required|integer', // penilaian_id
|
'id' => 'required|integer', // penilaian_id
|
||||||
'team_member_id' => 'required|integer',
|
'team_member_id' => 'nullable|integer',
|
||||||
|
'region_id' => 'nullable|integer',
|
||||||
'permohonan_id' => 'required|integer',
|
'permohonan_id' => 'required|integer',
|
||||||
'user_id' => 'required|integer',
|
'user_id' => 'required|integer',
|
||||||
|
'team_id' => 'required|integer',
|
||||||
|
], [
|
||||||
|
'team_member_id.required_without' => 'Field team_member_id atau region_id harus diisi.',
|
||||||
|
'region_id.required_without' => 'Field region_id atau team_member_id harus diisi.',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$penilaianId = $request->input('id');
|
|
||||||
$teamMemberId = $request->input('team_member_id');
|
$penilaianId = $validatedData['id'];
|
||||||
$permohonanId = $request->input('permohonan_id');
|
$teamMemberId = $validatedData['team_member_id'] ?? null;
|
||||||
$user_id = $request->input('user_id');
|
$regionId = $validatedData['region_id'] ?? null;
|
||||||
|
$permohonanId = $validatedData['permohonan_id'];
|
||||||
|
$userId = $validatedData['user_id'];
|
||||||
|
$teamId = $validatedData['team_id'];
|
||||||
|
|
||||||
// Cek apakah permohonan ada
|
// Cek apakah permohonan ada
|
||||||
$permohonan = Permohonan::findOrFail($permohonanId);
|
$permohonan = Permohonan::findOrFail($permohonanId);
|
||||||
|
|
||||||
// Validasi status permohonan
|
// Validasi status permohonan
|
||||||
if ($permohonan->status != 'assign') {
|
if ($permohonan->status !== 'assign') {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => 'Tidak bisa ganti tim, permohonan ini sudah mengatur jadwal.',
|
'message' => 'Tidak bisa ganti tim, permohonan ini sudah mengatur jadwal.',
|
||||||
@@ -88,7 +107,7 @@ class ActivityController extends Controller
|
|||||||
|
|
||||||
// Ambil tim berdasarkan penilaian_id dan user_id
|
// Ambil tim berdasarkan penilaian_id dan user_id
|
||||||
$teams = PenilaianTeam::where('penilaian_id', $penilaianId)
|
$teams = PenilaianTeam::where('penilaian_id', $penilaianId)
|
||||||
->where('user_id', $user_id)
|
->where('user_id', $userId)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
// Cek apakah tim ditemukan
|
// Cek apakah tim ditemukan
|
||||||
@@ -99,39 +118,66 @@ class ActivityController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flag untuk mendeteksi perubahan
|
// Mulai transaksi
|
||||||
|
DB::beginTransaction();
|
||||||
$updated = false;
|
$updated = false;
|
||||||
|
|
||||||
// Proses update berdasarkan role
|
// Proses update berdasarkan region_id
|
||||||
foreach ($teams as $team) {
|
if ($regionId) {
|
||||||
if ($team->role == 'surveyor') {
|
foreach ($teams as $team) {
|
||||||
// Jika role-nya surveyor, update user_id menjadi teamMemberId
|
if ($team->role === 'surveyor') {
|
||||||
$team->user_id = $teamMemberId;
|
$team->update([
|
||||||
$team->save();
|
'team_id' => $regionId,
|
||||||
$updated = true;
|
'user_id' => null]);
|
||||||
} elseif ($team->role == 'penilai') {
|
$permohonan->update([
|
||||||
// Jika role-nya penilai, update user_id menjadi teamMemberId
|
'status' => 'registered']);
|
||||||
$team->user_id = $teamMemberId;
|
$updated = true;
|
||||||
$team->save();
|
}
|
||||||
$updated = true;
|
|
||||||
|
if ($team->role === 'penilai') {
|
||||||
|
$team->update([
|
||||||
|
'team_id' => $regionId,
|
||||||
|
'user_id' => null,
|
||||||
|
]);
|
||||||
|
$permohonan->update([
|
||||||
|
'status' => 'registered',
|
||||||
|
'region_id' => $regionId,
|
||||||
|
]);
|
||||||
|
$updated = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Proses update berdasarkan team_member_id
|
||||||
|
if ($teamMemberId) {
|
||||||
|
foreach ($teams as $team) {
|
||||||
|
if ($team->role === 'surveyor' || $team->role === 'penilai') {
|
||||||
|
$team->update(['user_id' => $teamMemberId]);
|
||||||
|
$updated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($updated) {
|
if ($updated) {
|
||||||
|
DB::commit();
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'success',
|
'status' => 'success',
|
||||||
'message' => 'Data tim berhasil diperbarui.',
|
'message' => 'Data tim berhasil diperbarui.',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DB::rollBack();
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => 'Tidak ada perubahan yang dilakukan.',
|
'message' => 'Tidak ada perubahan yang dilakukan.',
|
||||||
]);
|
]);
|
||||||
} catch (\Throwable $th) {
|
} catch (\Throwable $th) {
|
||||||
|
// Rollback transaksi jika terjadi error
|
||||||
|
DB::rollBack();
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'error',
|
'status' => 'error',
|
||||||
'message' => $th->getMessage(),
|
'message' => 'Terjadi kesalahan: ' . $th->getMessage(),
|
||||||
], 500);
|
], 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,6 +185,7 @@ class ActivityController extends Controller
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function senior()
|
public function senior()
|
||||||
{
|
{
|
||||||
return view('lpj::activity.senior_officer.index');
|
return view('lpj::activity.senior_officer.index');
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ class PenilaianController extends Controller
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
DB::commit();
|
DB::commit();
|
||||||
return response()->json(['success' => true, 'message' => 'Data berhasil disimpan'], 200);
|
return response()->json(['success' => true, 'message' => 'Data berhasil di-assign.'], 200);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
DB::rollBack();
|
DB::rollBack();
|
||||||
|
|
||||||
@@ -194,7 +194,7 @@ class PenilaianController extends Controller
|
|||||||
'status' => 'assign',
|
'status' => 'assign',
|
||||||
]);
|
]);
|
||||||
DB::commit();
|
DB::commit();
|
||||||
return response()->json(['success' => true, 'message' => 'Data berhasil disimpan'], 200);
|
return response()->json(['success' => true, 'message' => 'Data berhasil di-assign.'], 200);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
DB::rollBack();
|
DB::rollBack();
|
||||||
return response()->json(['success' => false, 'error' => $e->getMessage()]);
|
return response()->json(['success' => false, 'error' => $e->getMessage()]);
|
||||||
|
|||||||
@@ -249,7 +249,7 @@
|
|||||||
|
|
||||||
paparan: {
|
paparan: {
|
||||||
title: 'Paparan',
|
title: 'Paparan',
|
||||||
render: (item, data) => {
|
render: (item, data) => {
|
||||||
if (!data.due_date_sla) {
|
if (!data.due_date_sla) {
|
||||||
return `<span class="badge badge-sm badge-danger uppercase flex justify-center">Sudah melewati batas</span>`;
|
return `<span class="badge badge-sm badge-danger uppercase flex justify-center">Sudah melewati batas</span>`;
|
||||||
}
|
}
|
||||||
@@ -279,7 +279,7 @@
|
|||||||
actions: {
|
actions: {
|
||||||
title: 'Action',
|
title: 'Action',
|
||||||
render: (item, data) => `
|
render: (item, data) => `
|
||||||
<a class="btn btn-sm btn-icon btn-clear btn-primary" onclick="updateTeam(${data.user_penilai[0].penilaian_id}, ${data.permohonan.id}, ${data.user_penilai[0].user_id})" title="Ganti Team">
|
<a class="btn btn-sm btn-icon btn-clear btn-primary" onclick="updateTeam(${data.user_penilai[0].penilaian_id}, ${data.permohonan.id}, ${data.user_penilai[0].user_id}, ${data.user_penilai[0].team_id})" title="Ganti Team">
|
||||||
<i class="ki-filled ki-user-edit"></i>
|
<i class="ki-filled ki-user-edit"></i>
|
||||||
</a>`,
|
</a>`,
|
||||||
},
|
},
|
||||||
@@ -311,68 +311,110 @@
|
|||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
function updateTeam(penilaaniId, permohonanId, userId) {
|
function updateTeam(penilaaniId, permohonanId, userId, teamId) {
|
||||||
|
const selectCategory = () => {
|
||||||
Swal.fire({
|
Swal.fire({
|
||||||
title: 'Pilih Tim',
|
title: 'Pilih Kategori',
|
||||||
input: 'select',
|
input: 'select',
|
||||||
inputOptions: {
|
inputOptions: {
|
||||||
@foreach ($teamsActivity as $item)
|
team: 'Pilih dari Team',
|
||||||
{{ $item->user->id }}: '{{ $item->user->name }}',
|
region: 'Pilih dari Wilayah'
|
||||||
@endforeach
|
|
||||||
},
|
},
|
||||||
inputPlaceholder: 'Pilih anggota tim',
|
inputPlaceholder: 'Pilih kategori Pengganti',
|
||||||
icon: 'warning',
|
icon: 'warning',
|
||||||
showCancelButton: true,
|
showCancelButton: true,
|
||||||
confirmButtonColor: '#3085d6',
|
confirmButtonColor: '#3085d6',
|
||||||
cancelButtonColor: '#d33',
|
cancelButtonColor: '#d33',
|
||||||
confirmButtonText: 'Ya',
|
confirmButtonText: 'Lanjut',
|
||||||
cancelButtonText: 'Batal',
|
cancelButtonText: 'Batal',
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
if (result.isConfirmed) {
|
if (result.isConfirmed) {
|
||||||
if (!result.value) {
|
const selectedCategory = result.value;
|
||||||
// Tampilkan pesan error jika tidak ada pilihan
|
let subOptions = {};
|
||||||
Swal.fire('Error!', 'Anda harus memilih anggota tim sebelum melanjutkan.', 'error');
|
|
||||||
return;
|
if (selectedCategory === 'team') {
|
||||||
|
subOptions = {
|
||||||
|
@foreach ($teamsActivity as $item)
|
||||||
|
{{ $item->user->id }}: '{{ $item->user->name }}',
|
||||||
|
@endforeach
|
||||||
|
};
|
||||||
|
} else if (selectedCategory === 'region') {
|
||||||
|
subOptions = {
|
||||||
|
@foreach ($teamPenilai as $item)
|
||||||
|
{{ $item->regions->id }}: '{{ $item->regions->name }}',
|
||||||
|
@endforeach
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define variables
|
Swal.fire({
|
||||||
let token = "{{ csrf_token() }}";
|
title: 'Pilih ' + (selectedCategory === 'team' ? 'Anggota Tim' : 'Wilayah'),
|
||||||
let useURL = "{{ URL::to('/activity/update-team') }}";
|
input: 'select',
|
||||||
|
inputOptions: subOptions,
|
||||||
// Prepare input data
|
inputPlaceholder: 'Pilih salah satu ' + (selectedCategory === 'team' ? 'Anggota Tim' : 'Wilayah'),
|
||||||
let input_data = {
|
icon: 'info',
|
||||||
_token: token,
|
showCancelButton: true,
|
||||||
id: penilaaniId,
|
confirmButtonColor: '#3085d6',
|
||||||
team_member_id: result.value,
|
cancelButtonColor: '#d33',
|
||||||
permohonan_id: permohonanId,
|
confirmButtonText: 'Ya',
|
||||||
user_id: userId,
|
cancelButtonText: 'Kembali',
|
||||||
};
|
}).then((subResult) => {
|
||||||
|
if (subResult.dismiss === Swal.DismissReason.cancel) {
|
||||||
// AJAX request
|
// Kembali ke tahap pemilihan kategori
|
||||||
$.ajax({
|
selectCategory();
|
||||||
url: useURL,
|
} else if (subResult.isConfirmed) {
|
||||||
type: "PUT",
|
if (!subResult.value) {
|
||||||
cache: false,
|
Swal.fire('Error!', 'Anda harus memilih opsi sebelum melanjutkan.', 'error');
|
||||||
data: input_data,
|
return;
|
||||||
dataType: "json",
|
|
||||||
success: function(response) {
|
|
||||||
if (response.status === 'success') {
|
|
||||||
Swal.fire('Sukses!', response.message, 'success').then(() => {
|
|
||||||
location.reload(true);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
Swal.fire('Error!', response.message, 'error');
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
error: function(xhr) {
|
// Define variables
|
||||||
console.error(xhr);
|
let token = "{{ csrf_token() }}";
|
||||||
Swal.fire('Error!', 'Terjadi kesalahan saat memproses permintaan.',
|
let useURL = "{{ URL::to('/activity/update-team') }}";
|
||||||
'error');
|
|
||||||
},
|
// Tentukan tipe parameter berdasarkan kategori
|
||||||
|
const paramKey = selectedCategory === 'team' ? 'team_member_id' : 'region_id';
|
||||||
|
|
||||||
|
// Prepare input data
|
||||||
|
let input_data = {
|
||||||
|
_token: token,
|
||||||
|
id: penilaaniId,
|
||||||
|
[paramKey]: subResult.value,
|
||||||
|
permohonan_id: permohonanId,
|
||||||
|
user_id: userId,
|
||||||
|
team_id: teamId,
|
||||||
|
};
|
||||||
|
|
||||||
|
// AJAX request
|
||||||
|
$.ajax({
|
||||||
|
url: useURL,
|
||||||
|
type: "PUT",
|
||||||
|
cache: false,
|
||||||
|
data: input_data,
|
||||||
|
dataType: "json",
|
||||||
|
success: function(response) {
|
||||||
|
if (response.status === 'success') {
|
||||||
|
Swal.fire('Sukses!', response.message, 'success').then(() => {
|
||||||
|
location.reload(true);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Swal.fire('Error!', response.message, 'error');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function(xhr) {
|
||||||
|
console.error(xhr);
|
||||||
|
Swal.fire('Error!', 'Terjadi kesalahan saat memproses permintaan.', 'error');
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
</script>
|
|
||||||
|
// Mulai dari pemilihan kategori
|
||||||
|
selectCategory();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
@endpush
|
@endpush
|
||||||
|
|||||||
Reference in New Issue
Block a user