102 lines
4.1 KiB
PHP
102 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ObatMasuk;
|
|
use App\Models\ObatKeluar;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Exports\ObatMasukExport;
|
|
use App\Exports\ObatKeluarExport;
|
|
|
|
class LaporanController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$jenis = $request->get('jenis', 'masuk');
|
|
$tanggalMulai = $request->get('tanggal_mulai', now()->startOfMonth()->format('Y-m-d'));
|
|
$tanggalAkhir = $request->get('tanggal_akhir', now()->endOfMonth()->format('Y-m-d'));
|
|
|
|
if ($jenis === 'masuk') {
|
|
$data = ObatMasuk::with(['kategori', 'satuan', 'supplier'])
|
|
->whereBetween('tanggal_penerimaan', [$tanggalMulai, $tanggalAkhir])
|
|
->orderBy('tanggal_penerimaan', 'desc')
|
|
->paginate(15);
|
|
} else {
|
|
$data = ObatKeluar::whereNotIn('status', ['proses', 'dibatalkan'])
|
|
->whereBetween('tanggal_pengeluaran', [$tanggalMulai, $tanggalAkhir])
|
|
->orderBy('tanggal_pengeluaran', 'desc')
|
|
->paginate(15);
|
|
}
|
|
|
|
return view('laporan.index', compact('data', 'jenis', 'tanggalMulai', 'tanggalAkhir'));
|
|
}
|
|
|
|
/**
|
|
* Helper to get aggregated (grouped by nama_obat) data for exports.
|
|
*/
|
|
private function getAggregatedData(string $jenis, string $tanggalMulai, string $tanggalAkhir)
|
|
{
|
|
if ($jenis === 'masuk') {
|
|
return DB::table('obat_masuks')
|
|
->join('satuans', 'obat_masuks.satuan_id', '=', 'satuans.id')
|
|
->whereBetween('tanggal_penerimaan', [$tanggalMulai, $tanggalAkhir])
|
|
->select(
|
|
'obat_masuks.nama_obat',
|
|
'satuans.nama as satuan',
|
|
DB::raw('SUM(obat_masuks.stok) as total_jumlah')
|
|
)
|
|
->groupBy('obat_masuks.nama_obat', 'satuans.nama')
|
|
->orderBy('obat_masuks.nama_obat')
|
|
->get();
|
|
} else {
|
|
return DB::table('obat_keluars')
|
|
->join('obat_masuks', 'obat_keluars.obat_masuk_id', '=', 'obat_masuks.id')
|
|
->leftJoin('satuans', 'obat_masuks.satuan_id', '=', 'satuans.id')
|
|
->whereNotIn('obat_keluars.status', ['proses', 'dibatalkan'])
|
|
->whereBetween('obat_keluars.tanggal_pengeluaran', [$tanggalMulai, $tanggalAkhir])
|
|
->select(
|
|
'obat_keluars.nama_obat',
|
|
'satuans.nama as satuan',
|
|
DB::raw('SUM(obat_keluars.jumlah) as total_jumlah')
|
|
)
|
|
->groupBy('obat_keluars.nama_obat', 'satuans.nama')
|
|
->orderBy('obat_keluars.nama_obat')
|
|
->get();
|
|
}
|
|
}
|
|
|
|
public function exportPdf(Request $request)
|
|
{
|
|
$jenis = $request->get('jenis', 'masuk');
|
|
$tanggalMulai = $request->get('tanggal_mulai', now()->startOfMonth()->format('Y-m-d'));
|
|
$tanggalAkhir = $request->get('tanggal_akhir', now()->endOfMonth()->format('Y-m-d'));
|
|
|
|
$title = $jenis === 'masuk' ? 'Laporan Obat Masuk' : 'Laporan Obat Keluar';
|
|
$data = $this->getAggregatedData($jenis, $tanggalMulai, $tanggalAkhir);
|
|
|
|
$pdf = Pdf::loadView('laporan.pdf', compact('data', 'jenis', 'title', 'tanggalMulai', 'tanggalAkhir'));
|
|
$filename = "laporan-obat-{$jenis}-" . date('Y-m-d') . ".pdf";
|
|
|
|
return $pdf->download($filename);
|
|
}
|
|
|
|
public function exportExcel(Request $request)
|
|
{
|
|
$jenis = $request->get('jenis', 'masuk');
|
|
$tanggalMulai = $request->get('tanggal_mulai', now()->startOfMonth()->format('Y-m-d'));
|
|
$tanggalAkhir = $request->get('tanggal_akhir', now()->endOfMonth()->format('Y-m-d'));
|
|
|
|
$filename = "laporan-obat-{$jenis}-" . date('Y-m-d') . ".xlsx";
|
|
|
|
if ($jenis === 'masuk') {
|
|
return Excel::download(new ObatMasukExport($tanggalMulai, $tanggalAkhir), $filename);
|
|
} else {
|
|
return Excel::download(new ObatKeluarExport($tanggalMulai, $tanggalAkhir), $filename);
|
|
}
|
|
}
|
|
}
|
|
|