download pdf features for staff and hr

parent c56cda95
<title>Maklumat Tuntutan (HR)</title>
@extends('ui::layouts.app')
@section('content')
<style>
body { background-color: #f0f2f5; }
.ui.container { max-width: 70% !important; margin-top: 2rem; }
.ui.segment {
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
border-radius: 10px;
background-color: #fff;
padding: 2rem;
}
table.ui.definition.table td:first-child {
width: 40%;
font-weight: 600;
background-color: #f9fafb;
}
.totals-row td {
font-weight: 700;
background: #f7f7f8;
}
.button-bar { margin-top: 2rem; display: flex; justify-content: space-between; }
</style>
<div class="ui container">
<h2 class="ui header">Maklumat Tuntutan (HR)</h2>
<div class="ui segment">
<table class="ui definition table">
<tr><td>Tarikh Tuntutan</td><td>{{ \Carbon\Carbon::parse($claim->claim_date)->format('d/m/Y') }}</td></tr>
<tr><td>Jenis Kenderaan</td><td>{{ $claim->jenisKenderaan->keterangan ?? $claim->jenisKenderaan->name ?? '-' }}</td></tr>
<tr><td>Nama Projek</td><td>{{ $claim->project->p_project_description ?? 'Tanpa Projek' }}</td></tr>
<tr><td>Butiran</td><td>{{ $claim->description ?? '-' }}</td></tr>
<tr><td>Jarak Dari</td><td>{{ $claim->distance_from ?? '-' }}</td></tr>
<tr><td>Jarak Ke</td><td>{{ $claim->distance_to ?? '-' }}</td></tr>
<tr><td>Jumlah Perbatuan</td><td>{{ number_format($claim->total_mileage, 2) }} km</td></tr>
<tr><td>Toll</td><td>RM {{ number_format($claim->toll_amount ?? 0, 2) }}</td></tr>
<tr><td>Lain-lain</td><td>RM {{ number_format($claim->others_amount ?? 0, 2) }}</td></tr>
<tr><td>Butiran Lain-lain</td><td>{{ $claim->others_description ?? '-' }}</td></tr>
<tr><td>Status</td><td><strong>{{ $claim->status }}</strong></td></tr>
<tr class="totals-row"><td>Jumlah Akhir</td><td><strong>RM {{ number_format($claim->total_claim_amount, 2) }}</strong></td></tr>
</table>
</div>
<div class="button-bar">
<form action="{{ url()->previous() }}" method="GET" style="display:inline;">
<button type="submit" class="ui grey button">
<i class="arrow left icon"></i> Kembali
</button>
</form>
@if(!in_array($claim->status, ['Ditolak', 'Diluluskan']))
<form action="{{ route('mileage::HR.reject', $claim->id) }}" method="POST" style="display:inline;">
@csrf
<button type="submit" class="ui red button" onclick="return confirm('Tolak tuntutan ini?')">
<i class="times icon"></i> Tolak
</button>
</form>
@endif
</div>
</div>
@endsection
......@@ -455,8 +455,14 @@
<a href="{{ route('mileage::mileage.listmileage') }}" class="ui right floated button">
<i class="arrow left icon"></i> Kembali ke Senarai
</a>
@if(in_array($claim->status, [11, 12]))
<a href="{{ route('mileage::mileage.claim.downloadPdf', $claim->id) }}" class="ui right floated teal button" target="_blank">
<i class="file pdf icon"></i> Muat Turun PDF
</a>
@endif
@endif
<br>
<br>
......
......@@ -87,11 +87,8 @@ function () {
// HR
Route::prefix('HR')->name('HR')->group(function () {
Route::get('/dashboard', [HRController::class, 'index'])->name('.index');
Route::get('/show/{userId}', [HRController::class, 'show'])->name('.show');
Route::get('/view/claim/{id}', [HRController::class, 'viewEach'])->name('.viewEach');
Route::post('/{id}/reject', [HRController::class, 'reject'])->name('.reject');
Route::get('/export', [HRController::class, 'export'])->name('.export');
Route::get('/', [HRController::class, 'index'])->name('.index');
Route::get('/{id}', [HRController::class, 'show'])->name('.show');
});
Route::get('index', ['uses' => 'MileageController@index', 'as' => 'mileage.index']);
......@@ -114,6 +111,8 @@ function () {
// Tetapan
Route::get('tetapan', ['uses' => 'TetapanController@index', 'as' => 'mileage.tetapan']);
Route::get('claim/{id}/pdf', ['uses' => 'MileageController@downloadPdf', 'as' => 'mileage.claim.downloadPdf']);
}
);
......
......@@ -2,128 +2,57 @@
namespace Portal\Mileage\Http\Controllers;
use Portal\Mileage\Model\Users;
use Portal\Mileage\Model\Project;
use Portal\Mileage\Model\JenisKenderaan;
use Portal\Mileage\Model\Claim;
use Portal\Mileage\Model\ProjectTeam;
use Portal\Mileage\Model\MileageClaimMain;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Portal\Mileage\Exports\ClaimsExport;
use DB;
class HRController extends Controller
{
public function index(Request $request)
{
$month = $request->input('month', Carbon::now()->format('Y-m'));
$parsedMonth = Carbon::parse($month);
// Group claims by staff for the selected month
$claims = Claim::with('user')
->whereMonth('claim_date', $parsedMonth->month)
->whereYear('claim_date', $parsedMonth->year)
->get()
->groupBy('user_id');
$users = $claims->map(function ($claimsForUser) {
$user = $claimsForUser->first()->user;
return [
'id' => $user->id,
'name' => $user->name ?? '-',
'total_claims' => $claimsForUser->count(),
'total_amount' => $claimsForUser->sum('total_claim_amount'),
];
$search = $request->get('search');
$tahun = $request->get('tahun');
$bulan = $request->get('bulan');
$status = $request->get('status');
$list = MileageClaimMain::with(['lkpStatus', 'staffInfo'])
->whereIn('status', [11, 12])
->when($search, function ($q) use ($search) {
$q->where(function ($q2) use ($search) {
$q2->where('mm_apply_no', 'like', '%' . $search . '%')
->orWhere('mm_staff_name', 'like', '%' . $search . '%');
});
return view('mileage::hr.index', compact('users', 'month'));
}
/**
* Show individual claim details
*/
public function show($userId, Request $request)
{
$month = $request->input('month', Carbon::now()->format('Y-m'));
$parsedMonth = Carbon::parse($month);
$user = Users::findOrFail($userId);
$claims = Claim::where('user_id', $userId)
->whereMonth('claim_date', $parsedMonth->month)
->whereYear('claim_date', $parsedMonth->year)
->orderBy('claim_date', 'asc')
->get();
return view('mileage::hr.show', compact('user', 'claims', 'month'));
}
/**
* Show a single claim (HR detailed view)
*/
public function viewEach($id)
{
$claim = Claim::with(['user', 'project', 'jenisKenderaan'])->findOrFail($id);
return view('mileage::HR.view', compact('claim'));
})
->when($tahun, fn($q) => $q->where('mm_claim_year', $tahun))
->when($bulan, fn($q) => $q->where('mm_claim_month', $bulan))
->when($status, fn($q) => $q->where('status', $status))
->orderBy('created_at', 'desc')
->paginate(15);
return view('mileage::HR.index', compact('list'));
}
/**
* Reject claim (HR decision)
*/
public function reject($id)
public function show($id)
{
$hr = Auth::user();
$claim = Claim::findOrFail($id);
$claim->status = 'Ditolak';
$claim->rejected_by = $hr->id; // record who rejected it
$claim->save();
return back()->with('error', 'Tuntutan telah ditolak oleh HR.');
$claim = MileageClaimMain::with([
'details.jenisKenderaan.rates',
'details.project',
'details.pmReviews',
'lkpStatus',
'staffInfo',
'auditQueries',
'attachments',
'faCategory',
'financeDetail',
'verifiedBy',
'approvedBy',
])->findOrFail($id);
if (!in_array($claim->status, [11, 12])) {
abort(403, 'Permohonan ini tidak tersedia untuk semakan HR.');
}
/**
* Export all claims to Excel
*/
public function export(Request $request)
{
$userId = $request->input('user_id');
$month = $request->input('month');
// parse month safely
$parsedMonth = $month ? Carbon::parse($month) : null;
// fetch user
$user = $userId ? Users::find($userId) : null;
// query claims by filter
$claims = Claim::with(['user', 'project'])
->when($userId, fn($q) => $q->where('user_id', $userId))
->when($parsedMonth, fn($q) =>
$q->whereMonth('claim_date', $parsedMonth->month)
->whereYear('claim_date', $parsedMonth->year)
)
->orderBy('claim_date', 'asc')
->get();
// if no data found, return with warning
if ($claims->isEmpty()) {
return back()->with('error', 'Tiada tuntutan dijumpai untuk penapis yang dipilih.');
}
// build filename dynamically
$staffName = $user ? str_replace(' ', '_', strtoupper($user->name)) : 'SEMUA_STAF';
$monthName = $parsedMonth ? $parsedMonth->format('m-Y') : Carbon::now()->format('m-Y');
$fileName = "Tuntutan_{$staffName}_{$monthName}.xlsx";
// generate Excel using filtered data
return Excel::download(new ClaimsExport($user, $claims), $fileName);
return view('mileage::HR.show', compact('claim'));
}
}
......@@ -4,10 +4,12 @@
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
use Auth;
use DB;
use Event;
use File;
......@@ -175,5 +177,39 @@ public function deleteClaim(Request $request, $id)
return redirect()->route('mileage::mileage.listmileage')->with('success', 'Draf permohonan berjaya dipadam.');
}
public function downloadPdf(Request $request, $id)
{
$claim = \Portal\Mileage\Model\MileageClaimMain::with([
'details.jenisKenderaan.rates',
'details.project',
'details.pmReviews',
'lkpStatus',
'staffInfo',
'auditQueries',
'attachments',
'faCategory',
'financeDetail',
])->findOrFail($id);
if (!in_array($claim->status, [11, 12])) {
abort(403, 'PDF tidak tersedia untuk status ini.');
}
$staff = \Portal\Mileage\Model\StaffInfo::where('fk_users', Auth::id())->first();
$isOwner = $staff && $claim->fk_lkp_staff_info == $staff->id;
$isHR = Gate::allows(\App\Enums\Permission::HR);
if (!$isOwner && !$isHR) {
abort(403, 'Anda tidak mempunyai akses untuk memuat turun dokumen ini.');
}
$html = view('mileage::mileage.pdf', compact('claim'))->render();
$pdf = \Barryvdh\DomPDF\Facade\Pdf::loadHTML($html)->setPaper('A4', 'portrait');
return $pdf->download('Tuntutan_Mileage_' . $claim->mm_apply_no . '.pdf');
}
}
......@@ -122,10 +122,10 @@ protected function menu()
->data('permission', Permission::BOD);
// $menu->add(__('HR Dashboard'), route('mileage::HR.index'))
// ->data('icon', 'id badge')
// ->active('HR/*')
// ->data('permission', Permission::HR);
$menu->add(__('HR Dashboard'), route('mileage::HR.index'))
->data('icon', 'id badge')
->active('HR/*')
->data('permission', Permission::HR);
$menu->add(__('Finance Dashboard'), route('mileage::finance.index'))
->data('icon', 'calculator')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment