141 lines
4.0 KiB
PHP
141 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\JadwalKonsul;
|
|
use App\Models\JadwalKonsulDetail;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class JadwalKonsulController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$data = [
|
|
'title' => 'Master Jadwal Konsul'
|
|
];
|
|
return view('dashboard.jadwal_konsul.index', $data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
try {
|
|
DB::connection('dbOrderGizi')->beginTransaction();
|
|
$datas = request('data');
|
|
foreach ($datas as $data) {
|
|
$payload = [
|
|
'nama_dokter' => $data['nama_dokter']
|
|
];
|
|
$jk = JadwalKonsul::create($payload);
|
|
if(isset($data['tanggal'])){
|
|
$tanggal = $data['tanggal'];
|
|
foreach ($tanggal as $tgl) {
|
|
$payloadTgl = [
|
|
'tgl_harian' => $tgl,
|
|
'jadwal_konsul_id' => $jk->jadwal_konsul_id,
|
|
];
|
|
JadwalKonsulDetail::create($payloadTgl);
|
|
}
|
|
}
|
|
}
|
|
//code...
|
|
DB::connection('dbOrderGizi')->commit();
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Data berhasil ditambahkan!'
|
|
]);
|
|
} catch (\Throwable $th) {
|
|
DB::connection('dbOrderGizi')->rollBack();
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Data gagal ditambahkan!',
|
|
'errors' => $th->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(JadwalKonsul $jadwalKonsul)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(JadwalKonsul $jadwalKonsul)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
try {
|
|
DB::connection('dbOrderGizi')->beginTransaction();
|
|
JadwalKonsulDetail::where('jadwal_konsul_id', $id)->delete();
|
|
|
|
$jadwalKonsul = JadwalKonsul::where('jadwal_konsul_id', $id)->first();
|
|
$jadwalKonsul->update([
|
|
'nama_dokter' => request('nama_dokter')
|
|
]);
|
|
$tanggals = request('tanggal');
|
|
foreach ($tanggals as $tgl) {
|
|
$payload = [
|
|
'jadwal_konsul_id' => $id,
|
|
'tgl_harian' => $tgl
|
|
];
|
|
JadwalKonsulDetail::create($payload);
|
|
}
|
|
DB::connection('dbOrderGizi')->commit();
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Data berhasil diperbarui!'
|
|
]);
|
|
} catch (\Throwable $th) {
|
|
DB::connection('dbOrderGizi')->rollBack();
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Data gagal diperbarui!',
|
|
'errors' => $th->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
$jadwalKonsul = JadwalKonsul::where('jadwal_konsul_id', $id)->first();
|
|
$jadwalKonsul->update([
|
|
'statusenabled' => false
|
|
]);
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Data berhasil dihapus!'
|
|
]);
|
|
}
|
|
|
|
public function datatable(){
|
|
return JadwalKonsul::where('statusenabled', true)->with('tglAvailable')->get();
|
|
}
|
|
}
|