<?php
namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Pengguna;
use App\Models\Role;
use App\Models\TableServiceSetting;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
class PenggunaController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
if (!Helper::hakAkses('pengaturan', 'View')) {
return abort(403, 'Unauthorized action.');
}
$datas = Pengguna::with('role')->paginate(10);
$data = [
"datas" => $datas,
];
return response()->view('pengguna.index', $data);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
if (!Helper::hakAkses('pengaturan', 'View')) {
return abort(403, 'Unauthorized action.');
}
$role = Role::all();
$data = [
"role" => $role
];
return response()->view("pengguna.create", $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
if (!Helper::hakAkses('pengaturan', 'View')) {
return abort(403, 'Unauthorized action.');
}
DB::beginTransaction();
try {
$request->validate([
"nama_pengguna" => "required",
"email" => "required",
"role_id" => "required",
"password" => "required",
"c_password" => "required|same:password",
]);
$pengguna = Pengguna::create([
"nama_pengguna" => $request->nama_pengguna,
"email" => $request->email,
"role_id" => $request->role_id,
'password' => Hash::make($request->password),
'no_telp' => $request->no_telp ?? null,
]);
$table = [
'pengguna_id' => $pengguna->id,
'no_service' => 1,
'tanggal_masuk' => 1,
'nama_pelanggan' => 1,
'status_perbaikan' => 1,
'status_pembayaran' => 1,
'status_transaksi' => 1,
'biaya' => 1,
'DP' => 1,
'merk_tipe_hp' => 1,
'kerusakan' => 1,
'status_garansi' => 1,
'nama_teknisi' => 1,
'nama_kasir' => 1,
'nama_sales' => 1,
'pesan_follow_up' => 1,
'action' => 1,
'tools' => 1,
];
TableServiceSetting::create($table);
Helper::addUserLog("Menambah data pengguna",$pengguna);
DB::commit();
return redirect()->route('pengguna.index')->with('success', 'Pengguna berhasil ditambah');
} catch (\Throwable $th) {
DB::rollback();
return redirect()->route('pengguna.create')->with('error', $th->getMessage());
}
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
if (!Helper::hakAkses('pengaturan', 'View')) {
return abort(403, 'Unauthorized action.');
}
$datas = Pengguna::find($id);
$role = Role::all();
$data = [
"datas" => $datas,
"role" => $role
];
return response()->view("pengguna.edit", $data);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
if (!Helper::hakAkses('pengaturan', 'View')) {
return abort(403, 'Unauthorized action.');
}
DB::beginTransaction();
try {
if (isset($request->password)) {
$request->validate([
"nama_pengguna" => "required",
"email" => "required",
"role_id" => "required",
"password" => "required",
"c_password" => "required|same:password",
]);
} else {
$request->validate([
"nama_pengguna" => "required",
"email" => "required",
"role_id" => "required",
]);
}
$pengguna = Pengguna::find($id);
$pengguna->update([
"nama_pengguna" => $request->nama_pengguna,
"email" => $request->email,
"role_id" => $request->role_id,
'no_telp' => $request->no_telp ?? null,
]);
if (isset($request->password)) {
$pengguna->update([
"password" => Hash::make($request->password),
]);
}
Helper::addUserLog("Mengubah data pengguna",$pengguna);
DB::commit();
return redirect()->route("pengguna.index")->with('success', 'data berhasil disimpan');
} catch (\Throwable $th) {
DB::rollback();
return redirect()->back()->with('error', $th->getMessage());
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
if (!Helper::hakAkses('pengaturan', 'View')) {
return abort(403, 'Unauthorized action.');
}
$data = Pengguna::findOrFail($id);
if($data->id == auth()->user()->id){
return redirect()->route('pengguna.index')->with('error', 'Tidak dapat menghapus akun yang sedang digunakan');
}
DB::beginTransaction();
try {
$data->delete();
Helper::addUserLog("Menghapus data pengguna",$data);
DB::commit();
return redirect()->route('pengguna.index')->with('success', 'Pengguna berhasil dihapus');
} catch (\Throwable $th) {
DB::rollback();
return redirect()->route('pengguna.index')->with('error', $th->getMessage());
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]