update
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BioCabinet extends Model
|
||||
{
|
||||
protected $table = 'bio_cabinets';
|
||||
protected $guarded = [];
|
||||
|
||||
public function racks()
|
||||
{
|
||||
return $this->hasMany(BioRack::class, 'cabinet_id');
|
||||
}
|
||||
|
||||
public function specimens()
|
||||
{
|
||||
return $this->hasMany(BioSpecimen::class, 'cabinet_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BioRack extends Model
|
||||
{
|
||||
protected $table = 'bio_racks';
|
||||
protected $guarded = [];
|
||||
|
||||
public function cabinet()
|
||||
{
|
||||
return $this->belongsTo(BioCabinet::class, 'cabinet_id');
|
||||
}
|
||||
|
||||
public function specimens()
|
||||
{
|
||||
return $this->hasMany(BioSpecimen::class, 'rack_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class BioSpecimen extends Model
|
||||
{
|
||||
protected $table = 'bio_specimens';
|
||||
protected $guarded = [];
|
||||
|
||||
protected $appends = ['storage_days'];
|
||||
|
||||
public function cabinet()
|
||||
{
|
||||
return $this->belongsTo(BioCabinet::class, 'cabinet_id');
|
||||
}
|
||||
|
||||
public function rack()
|
||||
{
|
||||
return $this->belongsTo(BioRack::class, 'rack_id');
|
||||
}
|
||||
|
||||
public function getStorageDaysAttribute()
|
||||
{
|
||||
if (!$this->stored_at) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return Carbon::parse($this->stored_at)->diffInDays(Carbon::now());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\BioCabinet;
|
||||
use App\BioRack;
|
||||
use App\BioSpecimen;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Session;
|
||||
use Validator;
|
||||
|
||||
class BiorepositoryController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
if (Session::get('previlage') == '') {
|
||||
return redirect('/login');
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data['cabinetOptions'] = BioCabinet::orderBy('name', 'ASC')->get();
|
||||
$data['rackOptions'] = BioRack::with('cabinet')->orderBy('name', 'ASC')->get();
|
||||
$data['totalCabinets'] = BioCabinet::count();
|
||||
$data['totalRacks'] = BioRack::count();
|
||||
$data['totalSpecimens'] = BioSpecimen::count();
|
||||
|
||||
$data['cabinets'] = BioCabinet::with([
|
||||
'racks' => function ($query) {
|
||||
$query->orderBy('level', 'ASC')->orderBy('name', 'ASC');
|
||||
},
|
||||
'racks.specimens' => function ($query) {
|
||||
$query->orderBy('stored_at', 'ASC');
|
||||
},
|
||||
])->orderBy('name', 'ASC')->get();
|
||||
|
||||
$data['oldestSpecimen'] = BioSpecimen::with(['cabinet', 'rack'])
|
||||
->whereNotNull('stored_at')
|
||||
->orderBy('stored_at', 'ASC')
|
||||
->first();
|
||||
|
||||
$data['oldestStorageDays'] = 0;
|
||||
if ($data['oldestSpecimen']) {
|
||||
$data['oldestStorageDays'] = Carbon::parse($data['oldestSpecimen']->stored_at)->diffInDays(Carbon::now());
|
||||
}
|
||||
|
||||
return view('admin.biorepository', $data);
|
||||
}
|
||||
|
||||
public function storeCabinet(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'code' => 'required|max:50|unique:bio_cabinets,code',
|
||||
'name' => 'required|max:150',
|
||||
'location' => 'nullable|max:200',
|
||||
'notes' => 'nullable',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return back()->withErrors($validator)->withInput();
|
||||
}
|
||||
|
||||
BioCabinet::create([
|
||||
'code' => $request->input('code'),
|
||||
'name' => $request->input('name'),
|
||||
'location' => $request->input('location'),
|
||||
'notes' => $request->input('notes'),
|
||||
]);
|
||||
|
||||
return redirect('/biorepository')->with('success', 'Lemari biorepository berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
public function storeRack(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'cabinet_id' => 'required|exists:bio_cabinets,id',
|
||||
'code' => 'required|max:50',
|
||||
'name' => 'required|max:150',
|
||||
'level' => 'required|integer|min:1',
|
||||
'capacity' => 'nullable|integer|min:0',
|
||||
'notes' => 'nullable',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return back()->withErrors($validator)->withInput();
|
||||
}
|
||||
|
||||
$exists = BioRack::where('cabinet_id', $request->input('cabinet_id'))
|
||||
->where('code', $request->input('code'))
|
||||
->exists();
|
||||
|
||||
if ($exists) {
|
||||
return back()->withErrors(['code' => 'Kode rack sudah dipakai pada lemari ini.'])->withInput();
|
||||
}
|
||||
|
||||
BioRack::create([
|
||||
'cabinet_id' => $request->input('cabinet_id'),
|
||||
'code' => $request->input('code'),
|
||||
'name' => $request->input('name'),
|
||||
'level' => $request->input('level'),
|
||||
'capacity' => $request->input('capacity') ?? 0,
|
||||
'notes' => $request->input('notes'),
|
||||
]);
|
||||
|
||||
return redirect('/biorepository')->with('success', 'Rack berhasil ditambahkan.');
|
||||
}
|
||||
|
||||
public function storeSpecimen(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'rack_id' => 'required|exists:bio_racks,id',
|
||||
'specimen_code' => 'required|max:100|unique:bio_specimens,specimen_code',
|
||||
'specimen_name' => 'required|max:200',
|
||||
'patient_name' => 'nullable|max:200',
|
||||
'collected_at' => 'nullable|date',
|
||||
'stored_at' => 'required|date',
|
||||
'volume' => 'nullable|max:50',
|
||||
'storage_condition' => 'nullable|max:100',
|
||||
'notes' => 'nullable',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return back()->withErrors($validator)->withInput();
|
||||
}
|
||||
|
||||
$rack = BioRack::find($request->input('rack_id'));
|
||||
|
||||
BioSpecimen::create([
|
||||
'cabinet_id' => $rack->cabinet_id,
|
||||
'rack_id' => $rack->id,
|
||||
'specimen_code' => $request->input('specimen_code'),
|
||||
'specimen_name' => $request->input('specimen_name'),
|
||||
'patient_name' => $request->input('patient_name'),
|
||||
'collected_at' => $request->input('collected_at'),
|
||||
'stored_at' => $request->input('stored_at'),
|
||||
'volume' => $request->input('volume'),
|
||||
'storage_condition' => $request->input('storage_condition'),
|
||||
'notes' => $request->input('notes'),
|
||||
]);
|
||||
|
||||
return redirect('/biorepository')->with('success', 'Spesimen berhasil ditambahkan.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user