overtime claim - audit trail

parent 2b6dcc3b
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('lkp_overtime_task', function (Blueprint $table) {
$table->id();
$table->string('task_name');
$table->string('task_code')->unique();
$table->boolean('is_active')->default(true);
$table->integer('created_by')->nullable();
$table->integer('updated_by')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function down(): void
{
Schema::dropIfExists('lkp_overtime_task');
}
};
\ No newline at end of file
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('overtime_audit_trail', function (Blueprint $table) {
$table->id();
$table->integer('fk_overtime_claim_main')->nullable();
$table->integer('fk_overtime_claim_detail')->nullable();
$table->integer('fk_lkp_overtime_task')->nullable();
$table->integer('user')->nullable();
$table->text('description')->nullable();
$table->timestamps();
$table->index('fk_overtime_claim_main');
$table->index('fk_overtime_claim_detail');
$table->index('fk_lkp_overtime_task');
});
}
public function down(): void
{
Schema::dropIfExists('overtime_audit_trail');
}
};
\ No newline at end of file
@extends('ui::layouts.app')
@section('content')
<style>
.at-tbl td { vertical-align: middle; }
.at-desc { font-size: .85rem; color: #333; }
.at-date { font-size: .8rem; color: #888; white-space: nowrap; }
</style>
<div class="ui segment">
<a class="ui teal left ribbon label">Audit Trail Tuntutan Lebih Masa</a>
<div class="ui segment">
<div class="ui stackable two column grid" style="margin-bottom:1rem;">
<div class="column">
<h1 class="ui header">
<div class="content">Audit Trail</div>
</h1>
</div>
<div class="column" style="text-align:right; padding-top:1.2rem;">
<a href="{{ route('overtime::overtime.listovertime') }}" class="ui grey button">
<i class="arrow left icon"></i> Kembali
</a>
</div>
</div>
<div class="ui divider"></div>
<form method="GET" class="ui form" style="margin-bottom:1rem;">
<div class="fields" style="align-items:flex-end;">
<div class="field" style="flex:1;">
<input type="text" name="search" placeholder="Cari No. Tuntutan…" value="{{ request('search') }}">
</div>
<div class="field">
<button type="submit" class="ui teal button">
<i class="search icon"></i> Cari
</button>
</div>
</div>
</form>
<table class="ui celled striped table at-tbl">
<thead>
<tr>
<th style="width:50px;">No</th>
<th>No. Tuntutan</th>
<th>Tindakan</th>
<th>Penerangan</th>
<th>Tarikh</th>
</tr>
</thead>
<tbody>
@forelse($list as $i => $row)
<tr>
<td>{{ $list->firstItem() + $i }}</td>
<td>{{ $row->claim->ocm_apply_no ?? '-' }}</td>
<td>
<span class="ui label" style="font-size:.75rem;">
{{ $row->task->task_name ?? '-' }}
</span>
</td>
<td class="at-desc">{{ $row->description }}</td>
<td class="at-date">{{ $row->created_at?->format('d/m/Y H:i') ?? '-' }}</td>
</tr>
@empty
<tr>
<td colspan="5" style="text-align:center; color:#bbb; font-style:italic; padding:1.6rem;">
Tiada rekod audit trail dijumpai.
</td>
</tr>
@endforelse
</tbody>
</table>
<div style="margin-top:1rem;">
{{ $list->links() }}
</div>
</div>
</div>
@endsection
\ No newline at end of file
...@@ -61,15 +61,25 @@ ...@@ -61,15 +61,25 @@
return '-'; return '-';
}], }],
['header' => __('Status'), 'raw' => function ($row) { ['header' => __('Status'), 'raw' => function ($row) use ($statuses) {
$map = [ $colours = [
1 => ['colour' => 'grey', 'label' => 'Draf'], 1 => 'grey',
2 => ['colour' => 'yellow', 'label' => 'Pending'], 2 => 'blue',
3 => ['colour' => 'green', 'label' => 'Lulus'], 3 => 'blue',
4 => ['colour' => 'red', 'label' => 'Tolak'], 4 => 'red',
5 => 'yellow',
6 => 'orange',
7 => 'yellow',
8 => 'orange',
9 => 'yellow',
10 => 'orange',
11 => 'green',
12 => 'red',
]; ];
$status = $map[$row->ocm_status] ?? ['colour' => 'grey', 'label' => '-']; $statusRow = $statuses[$row->ocm_status] ?? null;
return "<span class='ui {$status['colour']} label'>{$status['label']}</span>"; $label = $statusRow ? $statusRow->description : '-';
$colour = $colours[$row->ocm_status] ?? 'grey';
return "<span class='ui {$colour} label'>{$label}</span>";
}], }],
['header' => __('Tindakan'), 'raw' => function ($row) { ['header' => __('Tindakan'), 'raw' => function ($row) {
......
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script> <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
#ocm_month option:disabled {
color: #bbb !important;
background: #f3f3f3 !important;
font-style: italic;
}
</style>
<div class="ui small modal" id="modalTuntutanBaharu"> <div class="ui small modal" id="modalTuntutanBaharu">
<div class="header"> <div class="header">
<i class="file alternate outline icon"></i> <i class="file alternate outline icon"></i>
...@@ -85,6 +93,7 @@ ...@@ -85,6 +93,7 @@
9: 'September', 10: 'Oktober', 11: 'November', 12: 'Disember', 9: 'September', 10: 'Oktober', 11: 'November', 12: 'Disember',
}; };
// ONE PERMOHONAN PER MONTH
function refreshMonthOptions() { function refreshMonthOptions() {
var year = $('#ocm_year').val(); var year = $('#ocm_year').val();
if (!year) return; if (!year) return;
...@@ -93,32 +102,46 @@ function refreshMonthOptions() { ...@@ -93,32 +102,46 @@ function refreshMonthOptions() {
url : EXISTING_MONTHS_URL, url : EXISTING_MONTHS_URL,
method : 'GET', method : 'GET',
success: function (res) { success: function (res) {
var occupied = res.success ? (res.existingMonths || []) : []; var occupied = (res.success && res.existingMonths) ? res.existingMonths : [];
var current = $('#ocm_month').val(); buildMonthOptions(year, occupied);
},
error: function (xhr) {
console.warn('Gagal memuat bulan sedia ada.', xhr.status, xhr.statusText);
buildMonthOptions(year, []);
}
});
}
function buildMonthOptions(year, occupied) {
var current = $('#ocm_month').val();
var html = '<option value="">-- Pilih Bulan --</option>'; var html = '<option value="">-- Pilih Bulan --</option>';
for (var m = 1; m <= 12; m++) { for (var m = 1; m <= 12; m++) {
var key = year + String(m).padStart(2, '0'); var key = String(year) + String(m).padStart(2, '0');
var isTaken = occupied.indexOf(key) !== -1; var isTaken = occupied.indexOf(key) !== -1;
var label = MONTH_NAMES[m] + (isTaken ? ' (Sudah ada)' : ''); var label = isTaken
// disabled so it can't be selected, but still visible in dropdown ? MONTH_NAMES[m] + ' (Telah ada permohonan)'
var attrs = isTaken ? ' disabled style="color:#bbb;"' : ''; : MONTH_NAMES[m];
var selected = (!isTaken && current == m) ? ' selected' : ''; var disabled = isTaken ? ' disabled' : '';
html += '<option value="' + m + '"' + attrs + selected + '>' + label + '</option>'; var selected = (!isTaken && String(current) === String(m)) ? ' selected' : '';
html += '<option value="' + m + '"' + disabled + selected + '>' + label + '</option>';
} }
$('#ocm_month').html(html); $('#ocm_month').html(html);
},
});
} }
$('#ocm_year').on('change', refreshMonthOptions); $('#ocm_year').on('change', refreshMonthOptions);
refreshMonthOptions();
// REFRESH LIST EVERY TIME MODAL OPENS
$('#modalTuntutanBaharu').on('show', function () { $('#modalTuntutanBaharu').on('show', function () {
$('#modal-success-msg, #modal-error-msg').hide(); $('#modal-success-msg, #modal-error-msg').hide();
$('#field-ocm_month, #field-ocm_year').removeClass('error'); $('#field-ocm_month, #field-ocm_year').removeClass('error');
refreshMonthOptions(); refreshMonthOptions();
}); });
// SUBMIT
$('#btn-modal-claim-submit').on('click', function () { $('#btn-modal-claim-submit').on('click', function () {
var btn = $(this); var btn = $(this);
var month = $('#ocm_month').val(); var month = $('#ocm_month').val();
...@@ -155,6 +178,7 @@ function refreshMonthOptions() { ...@@ -155,6 +178,7 @@ function refreshMonthOptions() {
} else { } else {
$('#modal-error-text').text(res.message || 'Ralat tidak diketahui.'); $('#modal-error-text').text(res.message || 'Ralat tidak diketahui.');
$('#modal-error-msg').show(); $('#modal-error-msg').show();
refreshMonthOptions();
} }
}, },
error: function (xhr) { error: function (xhr) {
...@@ -165,6 +189,10 @@ function refreshMonthOptions() { ...@@ -165,6 +189,10 @@ function refreshMonthOptions() {
msg = Object.values(errors).flat().join(' '); msg = Object.values(errors).flat().join(' ');
if (errors.ocm_month) $('#field-ocm_month').addClass('error'); if (errors.ocm_month) $('#field-ocm_month').addClass('error');
if (errors.ocm_year) $('#field-ocm_year').addClass('error'); if (errors.ocm_year) $('#field-ocm_year').addClass('error');
} else if (xhr.status === 422 && xhr.responseJSON && xhr.responseJSON.message) {
msg = xhr.responseJSON.message;
$('#field-ocm_month').addClass('error');
refreshMonthOptions();
} else if (xhr.responseJSON && xhr.responseJSON.message) { } else if (xhr.responseJSON && xhr.responseJSON.message) {
msg = xhr.responseJSON.message; msg = xhr.responseJSON.message;
} }
......
...@@ -52,13 +52,6 @@ ...@@ -52,13 +52,6 @@
<script> <script>
var VCM_MONTHS = ['Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sep','Okt','Nov','Dis']; var VCM_MONTHS = ['Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sep','Okt','Nov','Dis'];
var VCM_DAYS = ['Ahad','Isnin','Selasa','Rabu','Khamis','Jumaat','Sabtu']; var VCM_DAYS = ['Ahad','Isnin','Selasa','Rabu','Khamis','Jumaat','Sabtu'];
var VCM_STATUS = {
1: { colour: 'grey', label: 'Draf' },
2: { colour: 'yellow', label: 'Pending' },
3: { colour: 'green', label: 'Lulus' },
4: { colour: 'red', label: 'Tolak' },
};
var VCM_MONTH_MS = [ var VCM_MONTH_MS = [
'Januari','Februari','Mac','April','Mei','Jun', 'Januari','Februari','Mac','April','Mei','Jun',
'Julai','Ogos','September','Oktober','November','Disember' 'Julai','Ogos','September','Oktober','November','Disember'
...@@ -106,10 +99,9 @@ function viewTuntutan(id) { ...@@ -106,10 +99,9 @@ function viewTuntutan(id) {
} }
function vcmBuildHtml(main, details) { function vcmBuildHtml(main, details) {
var st = VCM_STATUS[main.ocm_status] || { colour: 'grey', label: '-' }; // Status label comes directly from the controller (lkp_status lookup)
var badge = '<span class="ui ' + st.colour + ' label" style="font-size:.78rem;">' + st.label + '</span>'; var badge = '<span class="ui label" style="font-size:.78rem;">' + vcmEsc(main.ocm_status_label || '-') + '</span>';
// Derive month/year from ocm_year/ocm_month on main record
var monthName = (main.ocm_month && main.ocm_year) var monthName = (main.ocm_month && main.ocm_year)
? VCM_MONTH_MS[main.ocm_month - 1] + ' ' + main.ocm_year ? VCM_MONTH_MS[main.ocm_month - 1] + ' ' + main.ocm_year
: '-'; : '-';
......
...@@ -89,24 +89,35 @@ ...@@ -89,24 +89,35 @@
<p id="error-text"></p> <p id="error-text"></p>
</div> </div>
{{-- ══ SUMMARY ════════════════════════════════════════════════════════ --}}
<div class="ot-summary">
@php @php
$statusMap = [ $s = isset($statuses[$data->ocm_status])
1 => ['colour' => 'grey', 'label' => 'Draf'], ? ['label' => $statuses[$data->ocm_status]->description]
2 => ['colour' => 'yellow', 'label' => 'Pending'], : ['label' => '-'];
3 => ['colour' => 'green', 'label' => 'Lulus'],
4 => ['colour' => 'red', 'label' => 'Tolak'], $colours = [
1=>'grey', 2=>'blue', 3=>'blue', 4=>'red',
5=>'yellow', 6=>'orange', 7=>'yellow', 8=>'orange',
9=>'yellow', 10=>'orange', 11=>'green', 12=>'red',
];
$sColour = $colours[$data->ocm_status] ?? 'grey';
$msMonths = [
1=>'Januari',2=>'Februari',3=>'Mac',4=>'April',5=>'Mei',6=>'Jun',
7=>'Julai',8=>'Ogos',9=>'September',10=>'Oktober',11=>'November',12=>'Disember',
]; ];
$s = $statusMap[$data->ocm_status] ?? ['colour' => 'grey', 'label' => '-']; $monthLabel = ($data->ocm_month && $data->ocm_year)
$firstDate = $details->first()?->ocd_date; ? ($msMonths[$data->ocm_month] ?? '-') . ' ' . $data->ocm_year
: '-';
@endphp @endphp
{{-- ══ SUMMARY ════════════════════════════════════════════════════════ --}}
<div class="ot-summary">
<div class="apply-no">{{ $data->ocm_apply_no }}</div> <div class="apply-no">{{ $data->ocm_apply_no }}</div>
<div class="meta-line"> <div class="meta-line">
Bulan: <strong>{{ $firstDate ? $firstDate->locale('ms_MY')->monthName . ' ' . $firstDate->year : '-' }}</strong> Bulan: <strong>{{ $monthLabel }}</strong>
</div> </div>
<div class="meta-line"> <div class="meta-line">
Status: <span class="ui {{ $s['colour'] }} label" style="font-size:.78rem;">{{ $s['label'] }}</span> Status: <span class="ui {{ $sColour }} label" style="font-size:.78rem;">{{ $s['label'] }}</span>
</div> </div>
</div> </div>
...@@ -116,7 +127,7 @@ ...@@ -116,7 +127,7 @@
<i class="table icon" style="color:#2185d0;"></i> <i class="table icon" style="color:#2185d0;"></i>
Jadual Tuntutan Lebih Masa Jadual Tuntutan Lebih Masa
</div> </div>
<div class="card-sub">{{ $firstDate ? $firstDate->locale('ms_MY')->monthName . ' ' . $firstDate->year : '-' }}</div> <div class="card-sub">{{ $monthLabel }}</div>
<div style="overflow-x:auto;"> <div style="overflow-x:auto;">
<table class="ot-tbl" id="ot-table"> <table class="ot-tbl" id="ot-table">
...@@ -170,10 +181,14 @@ ...@@ -170,10 +181,14 @@
<div class="two fields"> <div class="two fields">
<div class="required field" id="field-tarikh"> <div class="required field" id="field-tarikh">
<label>Tarikh <span style="color:#e53935;">*</span></label> <label>Tarikh <span style="color:#e53935;">*</span></label>
<input type="date" id="ocd_date_input" name="ocd_date_input"> <input type="date" id="ocd_date_input" name="ocd_date_input"
@if($data->ocm_month && $data->ocm_year)
min="{{ \Carbon\Carbon::createFromDate($data->ocm_year, $data->ocm_month, 1)->format('Y-m-d') }}"
max="{{ \Carbon\Carbon::createFromDate($data->ocm_year, $data->ocm_month, 1)->endOfMonth()->format('Y-m-d') }}"
@endif>
<div class="field-hint"> <div class="field-hint">
<i class="info circle icon"></i> <i class="info circle icon"></i>
Pilih tarikh lebih masa. Hanya tarikh dalam bulan {{ $monthLabel }} dibenarkan.
</div> </div>
</div> </div>
<div class="required field" id="field-jenis-hari"> <div class="required field" id="field-jenis-hari">
...@@ -276,6 +291,8 @@ ...@@ -276,6 +291,8 @@
<script> <script>
var mainId = {{ $data->id }}; var mainId = {{ $data->id }};
var otMonth = {{ $data->ocm_month ?? 'null' }};
var otYear = {{ $data->ocm_year ?? 'null' }};
var MS_MONTH = ['Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sep','Okt','Nov','Dis']; var MS_MONTH = ['Jan','Feb','Mac','Apr','Mei','Jun','Jul','Ogos','Sep','Okt','Nov','Dis'];
var DAY_NAMES = ['Ahad','Isnin','Selasa','Rabu','Khamis','Jumaat','Sabtu']; var DAY_NAMES = ['Ahad','Isnin','Selasa','Rabu','Khamis','Jumaat','Sabtu'];
...@@ -435,6 +452,17 @@ function addDay() { ...@@ -435,6 +452,17 @@ function addDay() {
if (projectId === '') projectName = '-'; if (projectId === '') projectName = '-';
if (!dateVal) { showError('Sila pilih tarikh.'); return; } if (!dateVal) { showError('Sila pilih tarikh.'); return; }
if (otMonth && otYear) {
var dParts = dateVal.split('-');
var dYear = parseInt(dParts[0], 10);
var dMonth = parseInt(dParts[1], 10);
if (dYear !== otYear || dMonth !== otMonth) {
showError('Tarikh mesti dalam bulan permohonan ini.');
return;
}
}
if (!dayType) { showError('Sila pilih jenis hari.'); return; } if (!dayType) { showError('Sila pilih jenis hari.'); return; }
if (!officeStart) { showError('Sila isi Office Hour — Mula.'); return; } if (!officeStart) { showError('Sila isi Office Hour — Mula.'); return; }
if (!overtimeFrom) { showError('Sila isi Overtime From.'); return; } if (!overtimeFrom) { showError('Sila isi Overtime From.'); return; }
......
...@@ -29,6 +29,7 @@ function () { ...@@ -29,6 +29,7 @@ function () {
Route::post('claim/{id}/submit', ['uses' => 'OvertimeClaimController@submitOvertimeClaim', 'as' => 'overtime.submitovertimeclaim']); Route::post('claim/{id}/submit', ['uses' => 'OvertimeClaimController@submitOvertimeClaim', 'as' => 'overtime.submitovertimeclaim']);
Route::post('claim/{id}/update', ['uses' => 'OvertimeClaimController@updateOvertimeClaim', 'as' => 'overtime.updateovertimeclaim']); Route::post('claim/{id}/update', ['uses' => 'OvertimeClaimController@updateOvertimeClaim', 'as' => 'overtime.updateovertimeclaim']);
Route::post('claim/{id}/delete', ['uses' => 'OvertimeClaimController@deleteOvertimeClaim', 'as' => 'overtime.deleteovertimeclaim']); Route::post('claim/{id}/delete', ['uses' => 'OvertimeClaimController@deleteOvertimeClaim', 'as' => 'overtime.deleteovertimeclaim']);
Route::get('claim/audit-trail', ['uses' => 'OvertimeClaimController@getAuditTrailList', 'as' => 'overtime.audittrail']);
// OVERTIME REQUEST // OVERTIME REQUEST
Route::get( 'request/create', ['uses' => 'OvertimeRequestController@createOvertimeRequest', 'as' => 'overtime.createovertimerequest']); Route::get( 'request/create', ['uses' => 'OvertimeRequestController@createOvertimeRequest', 'as' => 'overtime.createovertimerequest']);
......
...@@ -4,17 +4,44 @@ ...@@ -4,17 +4,44 @@
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Portal\Overtime\Model\OvertimeClaimDetail; use Portal\Overtime\Model\OvertimeClaimDetail;
use Portal\Overtime\Model\OvertimeClaimMain; use Portal\Overtime\Model\OvertimeClaimMain;
use Portal\Overtime\Model\OvertimeAuditTrail;
class OvertimeClaimRepo class OvertimeClaimRepo
{ {
// HELPERS
private function currentStaffInfoId(): ?int
{
return DB::table('staff_info')
->where('fk_users', Auth::id())
->value('id');
}
public function getOtStatuses(): \Illuminate\Support\Collection
{
return DB::table('lkp_status')
->where('module', 'ot')
->where('status', 1)
->orderBy('ref_id')
->get()
->keyBy('ref_id');
}
// LIST // LIST
public function overtimeClaimList($request) public function overtimeClaimList($request)
{ {
$search = $request->get('search'); $search = $request->get('search');
$staffInfoId = $this->currentStaffInfoId();
return OvertimeClaimMain::where('fk_lkp_staff_info', Auth::id()) return OvertimeClaimMain::where('fk_lkp_staff_info', $staffInfoId)
->where(function ($q) use ($search) { ->where(function ($q) use ($search) {
$q->where('ocm_apply_no', 'like', "%{$search}%") $q->where('ocm_apply_no', 'like', "%{$search}%")
->orWhere('ocm_staff_name', 'like', "%{$search}%"); ->orWhere('ocm_staff_name', 'like', "%{$search}%");
...@@ -23,22 +50,26 @@ public function overtimeClaimList($request) ...@@ -23,22 +50,26 @@ public function overtimeClaimList($request)
->paginate(10); ->paginate(10);
} }
// STORE // STORE
public function storeOvertimeClaim($request): OvertimeClaimMain public function storeOvertimeClaim($request): OvertimeClaimMain
{ {
$user = Auth::user(); $user = Auth::user();
$staffInfo = DB::table('staff_info')->where('fk_users', Auth::id())->first();
$record = OvertimeClaimMain::create([ $record = OvertimeClaimMain::create([
'fk_lkp_staff_info' => $user->id, 'fk_lkp_staff_info' => $staffInfo->id ?? null,
'ocm_staff_name' => $user->name ?? '', 'ocm_staff_name' => $staffInfo->fullname ?? ($user->name ?? ''),
'ocm_staff_employee_code' => $user->employee_code ?? '', 'ocm_staff_employee_code' => $staffInfo->staff_id ?? '',
'ocm_division' => $staffInfo->fk_lkp_department ?? null,
'ocm_month' => $request->ocm_month, 'ocm_month' => $request->ocm_month,
'ocm_year' => $request->ocm_year, 'ocm_year' => $request->ocm_year,
'ocm_status' => 1, // Draft 'ocm_status' => 1, // Draf
'updated_by' => $user->id, 'updated_by' => Auth::id(),
]); ]);
// Format: OTC-202606-0001
$record->ocm_apply_no = $record->ocm_apply_no =
'OTC-' . 'OTC-' .
$request->ocm_year . $request->ocm_year .
...@@ -48,10 +79,16 @@ public function storeOvertimeClaim($request): OvertimeClaimMain ...@@ -48,10 +79,16 @@ public function storeOvertimeClaim($request): OvertimeClaimMain
$record->save(); $record->save();
$this->getAuditTrail('CREATE', $record->id, null,
'Tuntutan baharu dicipta untuk bulan ' . $request->ocm_month . '/' . $request->ocm_year . '.');
return $record; return $record;
} }
// FIND // FIND
public function findOvertimeClaim(int $id): OvertimeClaimMain public function findOvertimeClaim(int $id): OvertimeClaimMain
{ {
return OvertimeClaimMain::findOrFail($id); return OvertimeClaimMain::findOrFail($id);
...@@ -64,10 +101,15 @@ public function findOvertimeClaimDetails(int $id) ...@@ -64,10 +101,15 @@ public function findOvertimeClaimDetails(int $id)
->get(); ->get();
} }
// EXISTING MONTHS // EXISTING MONTHS
public function getExistingMonthsForUser(): array public function getExistingMonthsForUser(): array
{ {
return OvertimeClaimMain::where('fk_lkp_staff_info', Auth::id()) $staffInfoId = $this->currentStaffInfoId();
return OvertimeClaimMain::where('fk_lkp_staff_info', $staffInfoId)
->whereNotNull('ocm_month') ->whereNotNull('ocm_month')
->whereNotNull('ocm_year') ->whereNotNull('ocm_year')
->get() ->get()
...@@ -75,10 +117,12 @@ public function getExistingMonthsForUser(): array ...@@ -75,10 +117,12 @@ public function getExistingMonthsForUser(): array
->toArray(); ->toArray();
} }
// ADD DETAIL // ADD DETAIL
public function addOvertimeClaimDetail(int $id, $request): OvertimeClaimDetail public function addOvertimeClaimDetail(int $id, $request): OvertimeClaimDetail
{ {
// Combine office start + end into single "HH:MM – HH:MM" string
$officeHour = $request->ocd_office_start $officeHour = $request->ocd_office_start
? $request->ocd_office_start . ' – ' . ($request->ocd_office_end ?? '?') ? $request->ocd_office_start . ' – ' . ($request->ocd_office_end ?? '?')
: null; : null;
...@@ -101,21 +145,44 @@ public function addOvertimeClaimDetail(int $id, $request): OvertimeClaimDetail ...@@ -101,21 +145,44 @@ public function addOvertimeClaimDetail(int $id, $request): OvertimeClaimDetail
if ($existing) { if ($existing) {
$existing->update($payload); $existing->update($payload);
return $existing->fresh(); $detail = $existing->fresh();
$this->getAuditTrail('UPDATE', $id, $detail->id,
'Entri tarikh ' . $request->ocd_date . ' dikemaskini.');
return $detail;
} }
return OvertimeClaimDetail::create(array_merge($payload, [ $detail = OvertimeClaimDetail::create(array_merge($payload, [
'fk_overtime_claim_main' => $id, 'fk_overtime_claim_main' => $id,
])); ]));
$this->getAuditTrail('CREATE', $id, $detail->id,
'Entri tarikh ' . $request->ocd_date . ' ditambah.');
return $detail;
} }
// REMOVE DETAIL // REMOVE DETAIL
public function removeOvertimeClaimDetail(int $id): void public function removeOvertimeClaimDetail(int $id): void
{ {
OvertimeClaimDetail::findOrFail($id)->delete(); $detail = OvertimeClaimDetail::findOrFail($id);
$mainId = $detail->fk_overtime_claim_main;
$date = $detail->ocd_date;
$detail->delete();
$this->getAuditTrail('DELETE', $mainId, $id,
'Entri tarikh ' . $date . ' dibuang.');
} }
// UPDATE MAIN // UPDATE MAIN
public function updateOvertimeClaim($request, int $id): OvertimeClaimMain public function updateOvertimeClaim($request, int $id): OvertimeClaimMain
{ {
$record = $this->findOvertimeClaim($id); $record = $this->findOvertimeClaim($id);
...@@ -130,10 +197,15 @@ public function updateOvertimeClaim($request, int $id): OvertimeClaimMain ...@@ -130,10 +197,15 @@ public function updateOvertimeClaim($request, int $id): OvertimeClaimMain
'updated_by' => Auth::id(), 'updated_by' => Auth::id(),
]); ]);
$this->getAuditTrail('UPDATE', $id, null, 'Tuntutan dikemaskini.');
return $record; return $record;
} }
// SUBMIT // SUBMIT
public function submitOvertimeClaim($request, int $id): OvertimeClaimMain public function submitOvertimeClaim($request, int $id): OvertimeClaimMain
{ {
$record = $this->findOvertimeClaim($id); $record = $this->findOvertimeClaim($id);
...@@ -144,18 +216,74 @@ public function submitOvertimeClaim($request, int $id): OvertimeClaimMain ...@@ -144,18 +216,74 @@ public function submitOvertimeClaim($request, int $id): OvertimeClaimMain
} }
$record->update([ $record->update([
'ocm_status' => 2, 'ocm_status' => 2, // Permohonan Baharu
'ocm_submit_date' => now(), 'ocm_submit_date' => now(),
'updated_by' => Auth::id(), 'updated_by' => Auth::id(),
]); ]);
$this->getAuditTrail('UPDATE', $id, null, 'Tuntutan dihantar untuk kelulusan.');
return $record; return $record;
} }
// DELETE // DELETE
public function deleteOvertimeClaim(int $id): void public function deleteOvertimeClaim(int $id): void
{ {
OvertimeClaimDetail::where('fk_overtime_claim_main', $id)->delete(); OvertimeClaimDetail::where('fk_overtime_claim_main', $id)->delete();
OvertimeClaimMain::findOrFail($id)->delete(); OvertimeClaimMain::findOrFail($id)->delete();
$this->getAuditTrail('DELETE', $id, null, 'Tuntutan dipadam.');
} }
// AUDIT TRAIL
public function getAuditTrail(string $taskCode, int $claimId, ?int $detailId = null, string $extra = ''): void
{
$taskId = DB::table('lkp_overtime_task')
->where('task_code', $taskCode)
->where('is_active', 1)
->value('id');
$taskName = DB::table('lkp_overtime_task')
->where('task_code', $taskCode)
->value('task_name') ?? $taskCode;
$userName = Auth::user()->name ?? 'Unknown';
$applyNo = OvertimeClaimMain::where('id', $claimId)->value('ocm_apply_no') ?? '-';
$description = "{$userName} melakukan tindakan {$taskName} pada permohonan {$applyNo}.";
if ($extra) {
$description .= ' ' . $extra;
}
OvertimeAuditTrail::create([
'fk_overtime_claim_main' => $claimId,
'fk_overtime_claim_detail' => $detailId,
'fk_lkp_overtime_task' => $taskId,
'user' => Auth::id(),
'description' => $description,
]);
}
// AUDIT TRAIL LIST (for viewing)
public function getAuditTrailList($request)
{
$search = $request->get('search');
return \Portal\Overtime\Model\OvertimeAuditTrail::with(['claim', 'task'])
->when($search, function ($q) use ($search) {
$q->whereHas('claim', function ($q2) use ($search) {
$q2->where('ocm_apply_no', 'like', "%{$search}%");
});
})
->orderBy('created_at', 'desc')
->paginate(15);
}
} }
\ No newline at end of file
...@@ -16,16 +16,23 @@ public function __construct( ...@@ -16,16 +16,23 @@ public function __construct(
protected OvertimeClaimRepo $repos protected OvertimeClaimRepo $repos
) {} ) {}
// LIST // LIST
public function getListOvertimeClaim(Request $request) public function getListOvertimeClaim(Request $request)
{ {
$list = $this->repos->overtimeClaimList($request); $list = $this->repos->overtimeClaimList($request);
$existingMonths = $this->repos->getExistingMonthsForUser(); $existingMonths = $this->repos->getExistingMonthsForUser();
$statuses = $this->repos->getOtStatuses();
return view('overtime::overtime.list', compact('list', 'existingMonths')); return view('overtime::overtime.list', compact('list', 'existingMonths', 'statuses'));
} }
// CREATE // CREATE
public function createOvertimeClaim() public function createOvertimeClaim()
{ {
$existingMonths = $this->repos->getExistingMonthsForUser(); $existingMonths = $this->repos->getExistingMonthsForUser();
...@@ -67,7 +74,10 @@ public function storeOvertimeClaim(Request $request): JsonResponse ...@@ -67,7 +74,10 @@ public function storeOvertimeClaim(Request $request): JsonResponse
}); });
} }
// EXISTING MONTHS // EXISTING MONTHS
public function getExistingMonths(): JsonResponse public function getExistingMonths(): JsonResponse
{ {
return response()->json([ return response()->json([
...@@ -76,12 +86,27 @@ public function getExistingMonths(): JsonResponse ...@@ -76,12 +86,27 @@ public function getExistingMonths(): JsonResponse
]); ]);
} }
// VIEW // VIEW
public function viewOvertimeClaim(Request $request, int $id): JsonResponse public function viewOvertimeClaim(Request $request, int $id): JsonResponse
{ {
$main = $this->repos->findOvertimeClaim($id); $main = $this->repos->findOvertimeClaim($id);
$details = $this->repos->findOvertimeClaimDetails($id); $details = $this->repos->findOvertimeClaimDetails($id);
$projectIds = $details->pluck('fk_lkp_project')->filter()->unique()->values();
$projectNames = $projectIds->isEmpty()
? collect()
: DB::table('lkp_project')
->whereIn('id', $projectIds)
->pluck('p_project_description', 'id');
$statusRow = DB::table('lkp_status')
->where('module', 'ot')
->where('ref_id', $main->ocm_status)
->first();
return response()->json([ return response()->json([
'success' => true, 'success' => true,
'data' => [ 'data' => [
...@@ -90,39 +115,48 @@ public function viewOvertimeClaim(Request $request, int $id): JsonResponse ...@@ -90,39 +115,48 @@ public function viewOvertimeClaim(Request $request, int $id): JsonResponse
'ocm_apply_no' => $main->ocm_apply_no, 'ocm_apply_no' => $main->ocm_apply_no,
'ocm_staff_name' => $main->ocm_staff_name, 'ocm_staff_name' => $main->ocm_staff_name,
'ocm_status' => $main->ocm_status, 'ocm_status' => $main->ocm_status,
// Derive month/year from first detail row 'ocm_status_label' => $statusRow->description ?? '-',
'ocm_month' => $details->first()?->ocd_date?->month, 'ocm_month' => $main->ocm_month,
'ocm_year' => $details->first()?->ocd_date?->year, 'ocm_year' => $main->ocm_year,
'ocm_submit_date' => $main->ocm_submit_date?->format('Y-m-d'), 'ocm_submit_date' => $main->ocm_submit_date?->format('Y-m-d'),
'ocm_approved_date'=> $main->ocm_approved_date?->format('Y-m-d'), 'ocm_approved_date' => $main->ocm_approved_date?->format('Y-m-d'),
], ],
'details' => $details->map(fn ($d) => [ 'details' => $details->map(fn ($d) => [
'id' => $d->id, 'id' => $d->id,
'ocd_date' => $d->ocd_date->format('Y-m-d'), // Carbon 'date' cast 'ocd_date' => $d->ocd_date->format('Y-m-d'),
'ocd_day_type' => $d->ocd_day_type, 'ocd_day_type' => $d->ocd_day_type,
'ocd_office_hour' => $d->ocd_office_hour, // stored as "HH:MM – HH:MM" 'ocd_office_hour' => $d->ocd_office_hour,
'ocd_overtime_from' => $d->ocd_overtime_from, // raw time string 'ocd_overtime_from' => $d->ocd_overtime_from,
'ocd_overtime_to' => $d->ocd_overtime_to, // raw time string 'ocd_overtime_to' => $d->ocd_overtime_to,
'ocd_effective_hours' => $d->ocd_effective_hours, 'ocd_effective_hours' => $d->ocd_effective_hours,
'ocd_tasks' => $d->ocd_tasks, 'ocd_tasks' => $d->ocd_tasks,
'fk_lkp_project' => $d->fk_lkp_project, 'fk_lkp_project' => $d->fk_lkp_project,
'project_name' => $d->project?->p_project_description ?? '', 'project_name' => $d->fk_lkp_project
? ($projectNames[$d->fk_lkp_project] ?? '')
: '',
]), ]),
], ],
]); ]);
} }
// EDIT // EDIT
public function editOvertimeClaim(Request $request, int $id) public function editOvertimeClaim(Request $request, int $id)
{ {
$data = $this->repos->findOvertimeClaim($id); $data = $this->repos->findOvertimeClaim($id);
$details = $this->repos->findOvertimeClaimDetails($id); $details = $this->repos->findOvertimeClaimDetails($id);
$projects = \DB::table('lkp_project')->orderBy('p_project_description')->get(); $projects = DB::table('lkp_project')->orderBy('p_project_description')->get();
$statuses = $this->repos->getOtStatuses();
return view('overtime::overtime.overtimeclaim.edit', compact('data', 'details', 'projects')); return view('overtime::overtime.overtimeclaim.edit', compact('data', 'details', 'projects', 'statuses'));
} }
// ADD DETAIL // ADD DETAIL
public function addOvertimeClaimDetail(Request $request, int $id): JsonResponse public function addOvertimeClaimDetail(Request $request, int $id): JsonResponse
{ {
$request->validate( $request->validate(
...@@ -149,6 +183,10 @@ public function addOvertimeClaimDetail(Request $request, int $id): JsonResponse ...@@ -149,6 +183,10 @@ public function addOvertimeClaimDetail(Request $request, int $id): JsonResponse
return $this->withTransaction(function () use ($request, $id) { return $this->withTransaction(function () use ($request, $id) {
$detail = $this->repos->addOvertimeClaimDetail($id, $request); $detail = $this->repos->addOvertimeClaimDetail($id, $request);
$projectName = $detail->fk_lkp_project
? DB::table('lkp_project')->where('id', $detail->fk_lkp_project)->value('p_project_description') ?? ''
: '';
return response()->json([ return response()->json([
'success' => true, 'success' => true,
'message' => 'Entri lebih masa berjaya ditambah.', 'message' => 'Entri lebih masa berjaya ditambah.',
...@@ -162,13 +200,16 @@ public function addOvertimeClaimDetail(Request $request, int $id): JsonResponse ...@@ -162,13 +200,16 @@ public function addOvertimeClaimDetail(Request $request, int $id): JsonResponse
'ocd_effective_hours' => $detail->ocd_effective_hours, 'ocd_effective_hours' => $detail->ocd_effective_hours,
'ocd_tasks' => $detail->ocd_tasks, 'ocd_tasks' => $detail->ocd_tasks,
'fk_lkp_project' => $detail->fk_lkp_project, 'fk_lkp_project' => $detail->fk_lkp_project,
'project_name' => $detail->project?->p_project_description ?? '', 'project_name' => $projectName,
], ],
]); ]);
}); });
} }
// REMOVE DETAIL // REMOVE DETAIL
public function removeOvertimeClaimDetail(Request $request, int $id): JsonResponse public function removeOvertimeClaimDetail(Request $request, int $id): JsonResponse
{ {
return $this->withTransaction(function () use ($id) { return $this->withTransaction(function () use ($id) {
...@@ -181,7 +222,10 @@ public function removeOvertimeClaimDetail(Request $request, int $id): JsonRespon ...@@ -181,7 +222,10 @@ public function removeOvertimeClaimDetail(Request $request, int $id): JsonRespon
}); });
} }
// SAVE DRAFT // SAVE DRAFT
public function saveOvertimeClaimDetails(Request $request, int $id): JsonResponse public function saveOvertimeClaimDetails(Request $request, int $id): JsonResponse
{ {
return $this->withTransaction(function () use ($request, $id) { return $this->withTransaction(function () use ($request, $id) {
...@@ -195,7 +239,10 @@ public function saveOvertimeClaimDetails(Request $request, int $id): JsonRespons ...@@ -195,7 +239,10 @@ public function saveOvertimeClaimDetails(Request $request, int $id): JsonRespons
}); });
} }
// SUBMIT // SUBMIT
public function submitOvertimeClaim(Request $request, int $id): JsonResponse public function submitOvertimeClaim(Request $request, int $id): JsonResponse
{ {
return $this->withTransaction(function () use ($request, $id) { return $this->withTransaction(function () use ($request, $id) {
...@@ -209,7 +256,10 @@ public function submitOvertimeClaim(Request $request, int $id): JsonResponse ...@@ -209,7 +256,10 @@ public function submitOvertimeClaim(Request $request, int $id): JsonResponse
}); });
} }
// UPDATE // UPDATE
public function updateOvertimeClaim(Request $request, int $id): JsonResponse public function updateOvertimeClaim(Request $request, int $id): JsonResponse
{ {
return $this->withTransaction(function () use ($request, $id) { return $this->withTransaction(function () use ($request, $id) {
...@@ -223,7 +273,10 @@ public function updateOvertimeClaim(Request $request, int $id): JsonResponse ...@@ -223,7 +273,10 @@ public function updateOvertimeClaim(Request $request, int $id): JsonResponse
}); });
} }
// DELETE // DELETE
public function deleteOvertimeClaim(Request $request, int $id): JsonResponse public function deleteOvertimeClaim(Request $request, int $id): JsonResponse
{ {
return $this->withTransaction(function () use ($id) { return $this->withTransaction(function () use ($id) {
...@@ -236,7 +289,10 @@ public function deleteOvertimeClaim(Request $request, int $id): JsonResponse ...@@ -236,7 +289,10 @@ public function deleteOvertimeClaim(Request $request, int $id): JsonResponse
}); });
} }
// PRIVATE HELPERS // PRIVATE HELPERS
private function withTransaction(callable $callback): JsonResponse private function withTransaction(callable $callback): JsonResponse
{ {
try { try {
...@@ -255,4 +311,15 @@ private function withTransaction(callable $callback): JsonResponse ...@@ -255,4 +311,15 @@ private function withTransaction(callable $callback): JsonResponse
], 500); ], 500);
} }
} }
// =========================================================================
// AUDIT TRAIL
// =========================================================================
public function getAuditTrailList(Request $request)
{
$list = $this->repos->getAuditTrailList($request);
return view('overtime::overtime.audit_trail', compact('list'));
}
} }
\ No newline at end of file
<?php
namespace Portal\Overtime\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class LkpOvertimeTask extends Model
{
use SoftDeletes;
protected $table = 'lkp_overtime_task';
protected $guarded = ['id'];
}
\ No newline at end of file
<?php
namespace Portal\Overtime\Model;
use Illuminate\Database\Eloquent\Model;
class OvertimeAuditTrail extends Model
{
protected $table = 'overtime_audit_trail';
protected $guarded = ['id'];
public function claim()
{
return $this->belongsTo(OvertimeClaimMain::class, 'fk_overtime_claim_main');
}
public function task()
{
return $this->belongsTo(LkpOvertimeTask::class, 'fk_lkp_overtime_task');
}
public function userInfo()
{
return $this->belongsTo(\App\Models\User::class, 'user');
}
}
\ No newline at end of file
...@@ -14,10 +14,9 @@ class OvertimeClaimDetail extends Model ...@@ -14,10 +14,9 @@ class OvertimeClaimDetail extends Model
protected $guarded = ['id']; protected $guarded = ['id'];
protected $casts = [ protected $casts = [
'ocd_date' => 'date', // plain date — avoids ISO timestamp in JSON 'ocd_date' => 'date',
'ocd_effective_hours' => 'decimal:2', 'ocd_effective_hours' => 'decimal:2',
'ocd_total_hours' => 'decimal:2', 'ocd_total_hours' => 'decimal:2',
'deleted_at' => 'datetime', 'deleted_at' => 'datetime',
]; ];
} }
\ No newline at end of file
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