initial commit
This commit is contained in:
@@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Storage;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use App\Imports\MedicineImport;
|
||||
use App\Exports\MedicineTemplateExport;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class MedicineController extends Controller
|
||||
{
|
||||
@@ -20,18 +21,74 @@ class MedicineController extends Controller
|
||||
* Tampilkan daftar obat-obatan (Index).
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$query = Medicine::query();
|
||||
{
|
||||
// Proteksi Akses
|
||||
Gate::authorize('check-access', ['medicines', 'read']);
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$query->where('nama_obat', 'like', '%' . $request->search . '%');
|
||||
$now = now();
|
||||
$query = Medicine::query();
|
||||
|
||||
// 1. FILTER KHUSUS (DARI DASHBOARD)
|
||||
if ($request->filled('filter')) {
|
||||
$filter = $request->get('filter');
|
||||
|
||||
if ($filter === 'expired') {
|
||||
// Menampilkan yang sudah lewat tanggal kadaluwarsa
|
||||
$query->whereNotNull('expired_at')->where('expired_at', '<', $now);
|
||||
} elseif ($filter === 'expiring_soon') {
|
||||
// Menampilkan yang akan kadaluwarsa dalam 7 hari kedepan
|
||||
$query->whereNotNull('expired_at')
|
||||
->whereBetween('expired_at', [$now, $now->copy()->addDays(7)]);
|
||||
} elseif ($filter === 'low_stock') {
|
||||
// Menampilkan stok kritis (<= 5)
|
||||
$query->where('qty', '<=', 5);
|
||||
}
|
||||
|
||||
$medicines = $query->latest()->paginate(10);
|
||||
|
||||
return view('pages.inventory.medicines.index', compact('medicines'));
|
||||
}
|
||||
|
||||
// 2. SEARCH GLOBAL
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->search;
|
||||
$query->where(function($q) use ($search) {
|
||||
$q->where('nama_obat', 'like', "%{$search}%")
|
||||
->orWhere('kategori', 'like', "%{$search}%")
|
||||
->orWhere('unit', 'like', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
// 3. FILTER KATEGORI (Opsional jika ada dropdown di UI)
|
||||
if ($request->filled('category')) {
|
||||
$query->where('kategori', $request->category);
|
||||
}
|
||||
|
||||
// 4. SORTING DINAMIS
|
||||
// Gunakan 'expired_at' sebagai default sorting jika dari Dashboard 'expired'
|
||||
$allowedSorts = ['nama_obat', 'kategori', 'qty', 'expired_at', 'created_at'];
|
||||
$sort = in_array($request->get('sort'), $allowedSorts) ? $request->get('sort') : 'nama_obat';
|
||||
$direction = strtolower($request->get('direction')) === 'desc' ? 'desc' : 'asc';
|
||||
|
||||
$medicines = $query->orderBy($sort, $direction)
|
||||
->paginate(15)
|
||||
->withQueryString();
|
||||
|
||||
// 5. STATISTIK BOX (Selalu hitung total keseluruhan, bukan hasil filter)
|
||||
$totalStock = Medicine::sum('qty');
|
||||
$lowStockCount = Medicine::where('qty', '<=', 5)->count();
|
||||
|
||||
// Menghitung barang yang sudah expired (merah)
|
||||
$expiredCount = Medicine::whereNotNull('expired_at')
|
||||
->where('expired_at', '<', $now)
|
||||
->count();
|
||||
|
||||
$categories = Medicine::distinct()->whereNotNull('kategori')->pluck('kategori');
|
||||
|
||||
return view('pages.inventory.medicines.index', compact(
|
||||
'medicines',
|
||||
'totalStock',
|
||||
'lowStockCount',
|
||||
'expiredCount', // Tambahan statistik baru
|
||||
'categories'
|
||||
));
|
||||
}
|
||||
/**
|
||||
* Tampilkan form tambah obat.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user