auto entry attendance table

parent 14c715a4
......@@ -102,9 +102,9 @@
<label>Day Type <span style="color:#e53935;"></span></label>
<select id="ocd_day_type" name="ocd_day_type">
<option value="">-- Select --</option>
<option value="Hari Bekerja">Working Day</option>
<option value="Hari Rehat">Rest Day</option>
<option value="Cuti Umum">Public Holiday</option>
<option value="1">Weekdays</option>
<option value="2">Weekends</option>
<option value="3">Public Holiday</option>
</select>
<div class="field-hint"><i class="magic icon"></i>Auto-detect when date is selected.</div>
</div>
......@@ -220,8 +220,7 @@ function bindEvents() {
$('#ocd_date_input').on('change', function () {
var v = $(this).val();
if (!v) return;
var dow = new Date(v).getDay();
$('#ocd_day_type').val((dow === 0 || dow === 6) ? 'Hari Rehat' : 'Hari Bekerja');
$('#ocd_day_type').val('');
fetchAttendance(v);
});
......@@ -240,9 +239,14 @@ function fetchAttendance(dateVal) {
method: 'GET',
data: { date: dateVal },
success: function (res) {
if (res.success && res.attendance && res.attendance.time_in) {
var start = (res.attendance.time_in || '').substring(0, 5);
var end = (res.attendance.time_out || '').substring(0, 5);
if (res.success && res.attendance) {
var attendance = res.attendance;
var start = (attendance.time_in || '').substring(0, 5);
var flexiEnd = (attendance.time_out_flexi || '').substring(0, 5);
var finalOut = (attendance.time_out || '').substring(0, 5);
var mappedDayType = mapAttendanceDayTypeCode(attendance.fk_lkp_day_type, attendance.day_type);
$('#ocd_day_type').val(mappedDayType || '');
$('#ocd_office_start')
.val(start)
......@@ -251,13 +255,19 @@ function fetchAttendance(dateVal) {
.removeAttr('placeholder');
$('#ocd_office_end')
.val(end)
.val(flexiEnd)
.prop('readonly', true)
.addClass('auto-field');
// If time_out is missing in attendance record, fallback to +9 hours.
if (!end && start) {
$('#ocd_overtime_from').val(flexiEnd);
$('#ocd_overtime_to').val(finalOut);
updateTotalHours();
// If time_out_flexi is missing in attendance record, fallback office end to +9 hours.
if (!flexiEnd && start) {
updateOfficeEndFromStart();
$('#ocd_overtime_from').val($('#ocd_office_end').val());
updateTotalHours();
}
} else {
enableManualOfficeHours();
......@@ -269,6 +279,17 @@ function fetchAttendance(dateVal) {
});
}
function mapAttendanceDayTypeCode(dayTypeId, fallbackLabel) {
var id = parseInt(dayTypeId, 10);
if (id === 1 || id === 2 || id === 3) return String(id);
if (fallbackLabel === 'Hari Bekerja') return '1';
if (fallbackLabel === 'Hari Rehat') return '2';
if (fallbackLabel === 'Cuti Umum') return '3';
return '';
}
function enableManualOfficeHours() {
$('#ocd_office_start')
.prop('readonly', false)
......@@ -280,6 +301,10 @@ function enableManualOfficeHours() {
.prop('readonly', true)
.addClass('auto-field')
.val('');
$('#ocd_overtime_from').val('');
$('#ocd_overtime_to').val('');
updateTotalHours();
}
function updateOfficeEndFromStart() {
......@@ -292,7 +317,13 @@ function updateOfficeEndFromStart() {
var p = startVal.split(':');
var d = new Date(2000, 0, 1, +p[0], +p[1]);
d.setHours(d.getHours() + 9);
$('#ocd_office_end').val(pad(d.getHours()) + ':' + pad(d.getMinutes()));
var computedEnd = pad(d.getHours()) + ':' + pad(d.getMinutes());
$('#ocd_office_end').val(computedEnd);
if (!$('#ocd_overtime_from').val()) {
$('#ocd_overtime_from').val(computedEnd);
updateTotalHours();
}
}
function updateTotalHours() {
......
......@@ -32,6 +32,26 @@ public function getOtStatuses(): \Illuminate\Support\Collection
->keyBy('ref_id');
}
private function mapAttendanceDayType(?int $attendanceDayType): ?string
{
return match ($attendanceDayType) {
1 => 'Hari Bekerja',
2 => 'Hari Rehat',
3 => 'Cuti Umum',
default => null,
};
}
private function mapDayTypeCodeToLabel($dayTypeCode): ?string
{
return match ((string) $dayTypeCode) {
'1' => 'Hari Bekerja',
'2' => 'Hari Rehat',
'3' => 'Cuti Umum',
default => null,
};
}
public function getAttendanceByDate(string $date): ?array
{
$staffInfoId = $this->currentStaffInfoId();
......@@ -49,9 +69,16 @@ public function getAttendanceByDate(string $date): ?array
return null;
}
$dayType = $this->mapAttendanceDayType(
isset($attendance->fk_lkp_day_type) ? (int) $attendance->fk_lkp_day_type : null
);
return [
'time_in' => $attendance->time_in,
'time_out_flexi' => $attendance->time_out_flexi,
'time_out' => $attendance->time_out,
'fk_lkp_day_type' => isset($attendance->fk_lkp_day_type) ? (int) $attendance->fk_lkp_day_type : null,
'day_type' => $dayType,
];
}
......@@ -150,9 +177,18 @@ public function addOvertimeClaimDetail(int $id, $request): OvertimeClaimDetail
? $request->ocd_office_start . ' – ' . ($request->ocd_office_end ?? '?')
: null;
$attendanceDayType = DB::table('attendance')
->where('fk_staff_info', $this->currentStaffInfoId())
->whereDate('date', $request->ocd_date)
->value('fk_lkp_day_type');
$resolvedDayType = $this->mapAttendanceDayType(
$attendanceDayType !== null ? (int) $attendanceDayType : null
) ?? $this->mapDayTypeCodeToLabel($request->ocd_day_type) ?? $request->ocd_day_type;
$payload = [
'ocd_date' => $request->ocd_date,
'ocd_day_type' => $request->ocd_day_type,
'ocd_day_type' => $resolvedDayType,
'ocd_office_hour' => $officeHour,
'ocd_overtime_from' => $request->ocd_overtime_from,
'ocd_overtime_to' => $request->ocd_overtime_to,
......
......@@ -177,12 +177,33 @@ public function getProjectStaffOptions(int $projectId)
->join('staff_info as si', 'si.staff_id', '=', 'pt.pt_staff_id')
->where("pt.{$projectColumn}", $projectId)
->whereNull('si.deleted_at')
->select('si.id', 'si.staff_id', 'si.fullname', 'si.user_name', 'pt.pt_team')
->select('si.id', 'si.staff_id', 'si.fullname', 'si.user_name', 'pt.pt_team')
->distinct()
->orderBy('si.fullname')
->get();
}
private function resolveProjectStaffIdByName(int $projectId, string $staffName): ?int
{
$selectedName = trim($staffName);
if ($selectedName === '') {
return null;
}
$staff = $this->getProjectStaffOptions($projectId)
->first(function ($staff) use ($selectedName) {
$name = trim((string) ($staff->fullname ?: ($staff->pt_team ?: $staff->user_name)));
return $name === $selectedName;
});
if (!$staff || empty($staff->staff_id) || !is_numeric((string) $staff->staff_id)) {
return null;
}
return (int) $staff->staff_id;
}
public function isStaffNameInRequestProject(int $requestFormId, string $staffName): bool
{
$record = $this->findRequestForm($requestFormId);
......@@ -371,6 +392,11 @@ public function addRequestFormDetail(int $mainId, $request): OvertimeRequestDeta
{
$record = $this->findRequestForm($mainId);
$this->authorizePmRequestAccess($record, [0, 8, 10]);
$staffUserId = $this->resolveProjectStaffIdByName((int) $record->orm_project, (string) $request->ord_name);
if (!$staffUserId) {
throw new \InvalidArgumentException('Selected project staff cannot be resolved to a staff ID.');
}
$budgeted = (float) ($request->ord_budgeted ?? 0);
$utilised = (float) ($request->ord_utilised_to_date ?? 0);
......@@ -378,7 +404,7 @@ public function addRequestFormDetail(int $mainId, $request): OvertimeRequestDeta
$detail = OvertimeRequestDetail::create([
'fk_overtime_request_main' => $mainId,
'ord_staff_id' => Auth::id(),
'ord_staff_id' => $staffUserId,
'ord_name' => $request->ord_name,
'ord_month_of_overtime' => $request->ord_month_of_overtime,
'ord_budgeted' => $budgeted,
......@@ -400,12 +426,18 @@ public function updateRequestFormDetail(int $detailId, $request): OvertimeReques
$detail = OvertimeRequestDetail::findOrFail($detailId);
$record = $this->findRequestForm((int) $detail->fk_overtime_request_main);
$this->authorizePmRequestAccess($record, [0, 8, 10]);
$staffUserId = $this->resolveProjectStaffIdByName((int) $record->orm_project, (string) $request->ord_name);
if (!$staffUserId) {
throw new \InvalidArgumentException('Selected project staff cannot be resolved to a staff ID.');
}
$budgeted = (float) ($request->ord_budgeted ?? 0);
$utilised = (float) ($request->ord_utilised_to_date ?? 0);
$balance = $budgeted - $utilised;
$detail->update([
'ord_staff_id' => Auth::id(),
'ord_staff_id' => $staffUserId,
'ord_name' => $request->ord_name,
'ord_month_of_overtime' => $request->ord_month_of_overtime,
'ord_budgeted' => $budgeted,
......
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