237 lines
9.2 KiB
PHP
237 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\User;
|
|
use App\Models\Sop;
|
|
use App\Models\ExamResult;
|
|
use Illuminate\View\View;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
/**
|
|
* 1. Fungsi Dashboard Utama Admin
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
$statistics = [
|
|
// PENTING: Menggunakan whereHas() untuk mengecek relasi Role di tabel pivot
|
|
'total_karyawan' => User::whereHas('roles', function ($query) {
|
|
$query->where('name', 'trainee'); // Atau 'karyawan', sesuaikan dengan nama role di database Anda
|
|
})->count(),
|
|
|
|
'total_trainer' => User::whereHas('roles', function ($query) {
|
|
// Menghitung admin dan trainer
|
|
$query->whereIn('name', ['trainer', 'admin']);
|
|
})->count(),
|
|
|
|
'total_sop' => Sop::where('status', 'Active')->count(),
|
|
|
|
'lulus_ujian' => ExamResult::where('is_passed', true)->count(),
|
|
];
|
|
|
|
// Pastikan path view ini benar-benar ada di resources/views/pages/admin/dashboard/dashboard.blade.php
|
|
return view('pages.admin.dashboard.dashboard', compact('statistics'));
|
|
}
|
|
|
|
/**
|
|
* 2. Fungsi untuk menu Departemen & Posisi (Master Data)
|
|
*/
|
|
public function masterIndex(): View
|
|
{
|
|
return view('pages.admin.master-data.index');
|
|
}
|
|
|
|
public function openSopFile($id)
|
|
{
|
|
// 1. Cari tahu versi terbaru dari dokumen ini di database
|
|
$sop = \Illuminate\Support\Facades\DB::connection('plantdoc')
|
|
->table('tblDocumentContent')
|
|
->where('document', $id)
|
|
->orderBy('version', 'desc')
|
|
->first();
|
|
|
|
// Jika file tidak ada di database, kembalikan ke halaman sebelumnya dengan pesan error
|
|
if (!$sop) {
|
|
return back()->with('error', 'File PDF untuk dokumen ini belum diunggah atau tidak ditemukan.');
|
|
}
|
|
|
|
$versi = $sop->version;
|
|
|
|
// 2. Susun URL persis seperti fitur "View Online" bawaan SeedDMS
|
|
$seeddmsUrl = "http://192.168.10.11/plantlib/plantlibrary/op/op.ViewOnline.php?documentid=" . $id . "&version=" . $versi;
|
|
|
|
// 3. Lontarkan pengguna ke URL tersebut
|
|
return redirect()->away($seeddmsUrl);
|
|
}
|
|
|
|
/**
|
|
* 4. Fungsi untuk menu Dokumen SOP
|
|
*/
|
|
public function sopIndex(\Illuminate\Http\Request $request)
|
|
{
|
|
$search = $request->input('search');
|
|
$department = $request->input('department');
|
|
$category = $request->input('category');
|
|
$version = $request->input('version');
|
|
|
|
// 1. Departemen
|
|
$departments = \Illuminate\Support\Facades\DB::connection('plantdoc')
|
|
->table('tblFolders')
|
|
->where('parent', 59)
|
|
->orderBy('name', 'asc')
|
|
->get();
|
|
|
|
// 2. Kategori
|
|
$categories = \Illuminate\Support\Facades\DB::connection('plantdoc')
|
|
->table('tblCategory')
|
|
->orderBy('name', 'asc')
|
|
->get();
|
|
|
|
// 3. Versi: Diambil dengan Logika Fallback (Cek Nomor -> Cek Judul)
|
|
$allDocs = \Illuminate\Support\Facades\DB::connection('plantdoc')
|
|
->table('tblDocuments')
|
|
->select('name', 'comment')
|
|
->get();
|
|
|
|
$versions = $allDocs->map(function ($doc) {
|
|
// Prioritas 1: 2 Karakter Terakhir dari Nomor SOP
|
|
$vName = substr(trim($doc->name), -2);
|
|
if (is_numeric($vName)) return $vName;
|
|
|
|
// Prioritas 2: 2 Karakter Terakhir dari Judul SOP
|
|
$vComment = substr(trim($doc->comment), -2);
|
|
if (is_numeric($vComment)) return $vComment;
|
|
|
|
return null; // Abaikan jika keduanya tidak memiliki akhiran angka
|
|
})->filter()->unique()->sort()->values();
|
|
|
|
// 4. Query Utama
|
|
$query = \Illuminate\Support\Facades\DB::connection('plantdoc')
|
|
->table('tblDocuments as d')
|
|
->leftJoin('tblFolders as f_doc', 'd.folder', '=', 'f_doc.id')
|
|
->leftJoin('tblFolders as f_parent', 'f_doc.parent', '=', 'f_parent.id')
|
|
->leftJoin('tblDocumentCategory as dc', 'd.id', '=', 'dc.documentID')
|
|
->leftJoin('tblCategory as cat', 'dc.categoryID', '=', 'cat.id')
|
|
->leftJoin('tblDocumentContent as c', 'd.id', '=', 'c.document')
|
|
->select(
|
|
'd.id',
|
|
'd.name as nomor_sop',
|
|
'd.comment as judul_sop',
|
|
'c.orgFileName as file_pdf',
|
|
'c.dir as direktori',
|
|
'd.date as tanggal_dibuat',
|
|
'cat.name as nama_kategori',
|
|
\Illuminate\Support\Facades\DB::raw("
|
|
CASE
|
|
WHEN f_doc.parent = 59 THEN f_doc.name
|
|
WHEN f_parent.parent = 59 THEN f_parent.name
|
|
ELSE f_doc.name
|
|
END as nama_departemen
|
|
")
|
|
);
|
|
|
|
// 5. Logika Filter
|
|
if ($search) {
|
|
$query->where(function($q) use ($search) {
|
|
$q->where('d.name', 'LIKE', "%{$search}%")
|
|
->orWhere('d.comment', 'LIKE', "%{$search}%");
|
|
});
|
|
}
|
|
if ($department) {
|
|
$query->where(function($q) use ($department) {
|
|
$q->where('f_doc.id', $department)
|
|
->orWhere('f_doc.parent', $department)
|
|
->orWhere('f_doc.folderList', 'LIKE', "%:{$department}:%");
|
|
});
|
|
}
|
|
if ($category) {
|
|
$query->where('dc.categoryID', $category);
|
|
}
|
|
|
|
// Filter Versi Cerdas SQL (Jika 2 digit nama bukan angka, cari di judul)
|
|
if ($version !== null && $version !== '') {
|
|
$query->where(function($q) use ($version) {
|
|
$q->whereRaw("RIGHT(TRIM(d.name), 2) = ?", [$version])
|
|
->orWhere(function($sub) use ($version) {
|
|
$sub->whereRaw("RIGHT(TRIM(d.name), 2) NOT REGEXP '^[0-9]+$'")
|
|
->whereRaw("RIGHT(TRIM(d.comment), 2) = ?", [$version]);
|
|
});
|
|
});
|
|
}
|
|
|
|
// 6. Logika Ekspor dengan Metadata
|
|
if ($request->has('export')) {
|
|
$dataExport = $query->orderBy('d.date', 'desc')->get();
|
|
|
|
$totalRecords = $dataExport->count();
|
|
$downloader = auth()->user()->first_name . ' ' . auth()->user()->last_name;
|
|
$downloadTime = now()->format('d M Y H:i:s');
|
|
|
|
if ($request->export == 'excel') {
|
|
return $this->exportCsv($dataExport, $totalRecords, $downloader, $downloadTime);
|
|
} elseif ($request->export == 'pdf') {
|
|
$pdf = \Barryvdh\DomPDF\Facade\Pdf::loadView('pages.admin.sops.pdf', compact('dataExport', 'totalRecords', 'downloader', 'downloadTime'))
|
|
->setPaper('a4', 'landscape');
|
|
return $pdf->download('Data_Dokumen_Tunggal_Pharma.pdf');
|
|
}
|
|
}
|
|
|
|
$sops = $query->orderBy('d.date', 'desc')->paginate(20);
|
|
|
|
return view('pages.admin.sops.index', compact('sops', 'departments', 'categories', 'versions'));
|
|
}
|
|
|
|
private function exportCsv($data, $totalRecords, $downloader, $downloadTime)
|
|
{
|
|
$fileName = 'Daftar_Dokumen_' . date('Ymd_His') . '.csv';
|
|
$headers = [
|
|
"Content-type" => "text/csv",
|
|
"Content-Disposition" => "attachment; filename=$fileName",
|
|
"Pragma" => "no-cache",
|
|
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
|
|
"Expires" => "0"
|
|
];
|
|
|
|
$callback = function() use($data, $totalRecords, $downloader, $downloadTime) {
|
|
$file = fopen('php://output', 'w');
|
|
|
|
fputcsv($file, ['DAFTAR MASTER DOKUMEN - TUNGGAL PHARMA']);
|
|
fputcsv($file, ['Diunduh Oleh:', $downloader]);
|
|
fputcsv($file, ['Waktu Unduh:', $downloadTime . ' WIB']);
|
|
fputcsv($file, ['Total Record:', $totalRecords . ' Dokumen']);
|
|
fputcsv($file, []);
|
|
|
|
fputcsv($file, ['No', 'Departemen/Folder', 'Kategori', 'Nomor Dokumen', 'Judul Dokumen', 'Versi', 'Tanggal Upload']);
|
|
|
|
$i = 1;
|
|
foreach ($data as $row) {
|
|
// Logika Ekstraksi Versi untuk Excel
|
|
$versi = substr(trim($row->nomor_sop), -2);
|
|
if (!is_numeric($versi)) {
|
|
$versi = substr(trim($row->judul_sop), -2);
|
|
if (!is_numeric($versi)) $versi = '-';
|
|
}
|
|
|
|
$tanggal = \Carbon\Carbon::createFromTimestamp($row->tanggal_dibuat)->format('d-m-Y');
|
|
|
|
fputcsv($file, [
|
|
$i++,
|
|
$row->nama_departemen ?? '-',
|
|
$row->nama_kategori ?? '-',
|
|
$row->nomor_sop,
|
|
$row->judul_sop,
|
|
"v." . $versi,
|
|
$tanggal
|
|
]);
|
|
}
|
|
fclose($file);
|
|
};
|
|
|
|
return response()->stream($callback, 200, $headers);
|
|
}
|
|
|
|
} |