83 lines
2.3 KiB
PHP
83 lines
2.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Medicine;
|
||
|
|
use App\Models\Food;
|
||
|
|
use App\Models\Supply;
|
||
|
|
use App\Traits\LogsActivity; // Tambahkan Trait Log
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\View\View;
|
||
|
|
use Illuminate\Http\RedirectResponse;
|
||
|
|
use Exception;
|
||
|
|
|
||
|
|
class InventoryController extends Controller
|
||
|
|
{
|
||
|
|
use LogsActivity;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Tampilkan Inventori Obat.
|
||
|
|
*/
|
||
|
|
public function medicines(): View
|
||
|
|
{
|
||
|
|
// Model Medicine sudah mendukung casting expired_date & qty float
|
||
|
|
$medicines = Medicine::latest()->paginate(10);
|
||
|
|
return view('pages.inventory.medicines.index', compact('medicines'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Tampilkan Inventori Makanan.
|
||
|
|
*/
|
||
|
|
public function foods(): View
|
||
|
|
{
|
||
|
|
$foods = Food::latest()->paginate(10);
|
||
|
|
return view('pages.inventory.foods.index', compact('foods'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Tampilkan Inventori Perlengkapan.
|
||
|
|
*/
|
||
|
|
public function supplies(): View
|
||
|
|
{
|
||
|
|
$supplies = Supply::latest()->paginate(10);
|
||
|
|
return view('pages.inventory.supplies.index', compact('supplies'));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update stok secara cepat (Fast Update).
|
||
|
|
* Mendukung desimal (ml/butir) dan Audit Trail.
|
||
|
|
*/
|
||
|
|
public function updateStock(Request $request, string $type, int $id): RedirectResponse
|
||
|
|
{
|
||
|
|
// Pastikan validasi tetap mendukung desimal untuk obat cair
|
||
|
|
$request->validate([
|
||
|
|
'qty' => 'required|numeric|min:0'
|
||
|
|
]);
|
||
|
|
|
||
|
|
try {
|
||
|
|
$item = match($type) {
|
||
|
|
'medicine' => \App\Models\Medicine::findOrFail($id),
|
||
|
|
'food' => \App\Models\Food::findOrFail($id),
|
||
|
|
'supply' => \App\Models\Supply::findOrFail($id),
|
||
|
|
default => abort(404),
|
||
|
|
};
|
||
|
|
|
||
|
|
$oldQty = $item->qty;
|
||
|
|
$item->update(['qty' => $request->qty]);
|
||
|
|
|
||
|
|
// FIX: Evaluasi nama barang di luar string agar tidak menyebabkan ParseError
|
||
|
|
$itemName = $item->nama_obat ?? $item->nama_makanan ?? $item->nama_barang ?? 'Item';
|
||
|
|
|
||
|
|
$this->logAction(
|
||
|
|
'STOCK_ADJUSTMENT',
|
||
|
|
$item,
|
||
|
|
"Penyesuaian stok " . ucfirst($type) . ": {$itemName} (Dari {$oldQty} ke {$request->qty})"
|
||
|
|
);
|
||
|
|
|
||
|
|
return back()->with('success', 'Stok ' . ucfirst($type) . ' berhasil diperbarui.');
|
||
|
|
|
||
|
|
} catch (Exception $e) {
|
||
|
|
return back()->with('error', 'Gagal memperbarui stok: ' . $e->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|