auto entry attendance table

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