213 lines
5.4 KiB
PHP
213 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Tindakan;
|
|
use Carbon\Carbon;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
class TindakanController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
$data['title'] = 'TINDAKAN';
|
|
return view('tindakan.index', $data);
|
|
}
|
|
|
|
public function get_list_table(Request $request)
|
|
{
|
|
$tindakan = Tindakan::select([
|
|
'id',
|
|
'tindakan_name',
|
|
'tarif',
|
|
'created_at',
|
|
'updated_at',
|
|
]);
|
|
return DataTables::of($tindakan)
|
|
->editColumn('tarif', function ($row) {
|
|
return number_format($row->tarif, 0, ',', '.');
|
|
})
|
|
->editColumn('created_at', function ($row) {
|
|
return Carbon::parse($row->created_at)->format('d-m-Y H:i');
|
|
})
|
|
->editColumn('updated_at', function ($row) {
|
|
return Carbon::parse($row->updated_at)->format('d-m-Y H:i');
|
|
})
|
|
->addColumn('action', function ($row) {
|
|
return '<a href="' . url('/tindakan/edit/' . $row->id) . '" class="btn btn-sm btn-primary edit" data-id="' . $row->id . '">Edit</a>
|
|
<button class="btn btn-sm btn-danger" onclick="deleteData(' . $row->id . ')" data-id="' . $row->id . '">Hapus</button>';
|
|
})
|
|
->rawColumns(['action'])
|
|
->make(true);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
$data['title'] = 'TAMBAH PEGAWAI';
|
|
return view('tindakan.create', $data);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
//
|
|
$name = $request->name;
|
|
$tarif = $request->tarif;
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$insert = Tindakan::create([
|
|
'tindakan_name' => $name,
|
|
'tarif' => $tarif
|
|
]);
|
|
|
|
DB::commit();
|
|
$data_return = [
|
|
'status' => true,
|
|
'data' => null,
|
|
'msg' => null
|
|
];
|
|
return response()->json($data_return, 200);
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
// Handle the exception or log the error
|
|
dd($e);
|
|
$data_return = [
|
|
'status' => false,
|
|
'data' => null,
|
|
'msg' => 'something wrong!!'
|
|
];
|
|
return response()->json($data_return, 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
//
|
|
$data['title'] = 'EDIT TINDAKAN';
|
|
$data['tindakan'] = Tindakan::findorFail($id);
|
|
|
|
return view('tindakan.edit', $data);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request)
|
|
{
|
|
//
|
|
$id = $request->id;
|
|
$name = $request->name;
|
|
$tarif = $request->tarif;
|
|
|
|
$tindakan = Tindakan::findorFail($id);
|
|
|
|
if (!$tindakan) {
|
|
$data_return = [
|
|
'status' => false,
|
|
'data' => null,
|
|
'msg' => 'Data Not Found!'
|
|
];
|
|
return response()->json($data_return, 404);
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$tindakan->update([
|
|
'tindakan_name' => $name,
|
|
'tarif' => $tarif
|
|
]);
|
|
|
|
DB::commit();
|
|
|
|
$data_return = [
|
|
'status' => true,
|
|
'data' => null,
|
|
'msg' => null
|
|
];
|
|
return response()->json($data_return, 200);
|
|
} catch (Exception $e) {
|
|
//throw $th;
|
|
DB::rollBack();
|
|
$data_return = [
|
|
'status' => false,
|
|
'data' => null,
|
|
'msg' => 'something wrong!!',
|
|
'msg_for_dev' => $e->getMessage()
|
|
];
|
|
return response()->json($data_return, 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Request $request)
|
|
{
|
|
//
|
|
$id = $request->id;
|
|
|
|
$tindakan = Tindakan::findorFail($id);
|
|
|
|
if (!$tindakan) {
|
|
$data_return = [
|
|
'status' => false,
|
|
'data' => null,
|
|
'msg' => 'Data Not Found!'
|
|
];
|
|
return response()->json($data_return, 404);
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$tindakan->delete();
|
|
|
|
DB::commit();
|
|
|
|
$data_return = [
|
|
'status' => true,
|
|
'data' => null,
|
|
'msg' => null
|
|
];
|
|
return response()->json($data_return, 200);
|
|
} catch (Exception $e) {
|
|
//throw $th;
|
|
DB::rollBack();
|
|
$data_return = [
|
|
'status' => false,
|
|
'data' => null,
|
|
'msg' => 'something wrong!!',
|
|
'msg_for_dev' => $e->getMessage()
|
|
];
|
|
return response()->json($data_return, 500);
|
|
}
|
|
}
|
|
}
|