ot request edit row

parent 6c2eb663
......@@ -16,6 +16,8 @@
.rf-tbl tfoot .lbl{text-align:right;letter-spacing:.04em;font-size:.82rem}
.btn-del{border:1px solid #e53935;background:transparent;color:#e53935;border-radius:4px;padding:2px 9px;font-size:.78rem;cursor:pointer;line-height:1.6}
.btn-del:hover{background:#e53935;color:#fff}
.btn-edit{border:1px solid #2185d0;background:transparent;color:#2185d0;border-radius:4px;padding:2px 9px;font-size:.78rem;cursor:pointer;line-height:1.6;margin-right:4px}
.btn-edit:hover{background:#2185d0;color:#fff}
.auto-field,input[readonly]{background:#f4f9ff!important;color:#555!important;cursor:default!important}
.badge-auto{display:inline-block;font-size:.68rem;font-weight:600;background:#e3f0ff;color:#1565c0;border-radius:3px;padding:1px 5px;margin-left:.25rem}
.rf-footer,.button-row{display:flex;justify-content:flex-end;gap:.55rem;padding-top:.9rem;border-top:1px solid #e5e8ee}
......@@ -23,6 +25,7 @@
.query-history{width:100%;border-collapse:collapse;font-size:.85rem}
.query-history th,.query-history td{border:1px solid #cfd8e3;padding:8px 10px;vertical-align:top}
.query-history th{background:#f5f7fa;text-align:left}
.row-editing{outline:2px solid #2185d0;outline-offset:-2px}
</style>
<div class="ui segment" style="padding-bottom:2rem;">
......@@ -129,6 +132,8 @@
</div>
<div class="button-row">
<button type="button" class="ui teal button" id="btn-add-row"><i class="plus icon"></i>Add to Table</button>
<button type="button" class="ui blue button" id="btn-update-row" style="display:none;"><i class="edit icon"></i>Update Row</button>
<button type="button" class="ui basic button" id="btn-cancel-edit" style="display:none;"><i class="times icon"></i>Cancel Edit</button>
<button type="button" class="ui grey button" id="btn-reset"><i class="redo icon"></i>Clear</button>
</div>
</div>
......@@ -183,6 +188,7 @@
<script>
var mainId = {{ $data->id }};
var rows = {};
var editingRowId = null;
$(document).ready(function () {
if ($.fn.dropdown) $('.ui.dropdown').dropdown({ fullTextSearch: true });
......@@ -197,6 +203,8 @@ function bindEvents() {
$('#ord_balance').val((b - u).toFixed(2));
});
$('#btn-add-row').on('click', addRow);
$('#btn-update-row').on('click', updateRow);
$('#btn-cancel-edit').on('click', cancelEditMode);
$('#btn-reset').on('click', resetRequestForm);
$('#btn-save-draft').on('click', saveRequestDetails);
$('#btn-submit').on('click', submitRequestForm);
......@@ -207,6 +215,15 @@ function esc(s) { return s === null || s === undefined ? '' : String(s).replace(
function showSuccess(msg) { $('#success-text').text(msg); $('#success-msg').show(); setTimeout(function(){ $('#success-msg').hide(); }, 3500); }
function showError(msg) { $('#error-text').text(msg); $('#error-msg').show(); setTimeout(function(){ $('#error-msg').hide(); }, 6000); }
function fmtMonth(val) { if (!val) return '-'; var d = new Date(val); var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; return months[d.getMonth()] + ' ' + d.getFullYear(); }
function toMonthYearParts(val) {
if (!val) return { month: '', year: '' };
var d = new Date(val);
if (isNaN(d.getTime())) return { month: '', year: '' };
return {
month: String(d.getMonth() + 1).padStart(2, '0'),
year: String(d.getFullYear())
};
}
function loadRows() {
$.ajax({
......@@ -244,10 +261,16 @@ function renderTable() {
html += '<td>' + parseFloat(d.ord_planned_ot_hour || 0).toFixed(2) + '</td>';
html += '<td>' + parseFloat(d.ord_balance || 0).toFixed(2) + '</td>';
html += '<td class="td-left">' + esc(d.ord_reason) + '</td>';
html += '<td><button class="btn-del" onclick="removeRow(' + id + ')"><i class="trash icon"></i></button></td>';
html += '<td>';
html += '<button class="btn-edit" onclick="editRow(' + id + ')"><i class="edit icon"></i></button>';
html += '<button class="btn-del" onclick="removeRow(' + id + ')"><i class="trash icon"></i></button>';
html += '</td>';
html += '</tr>';
});
tbody.html(html);
if (editingRowId) {
$('#rf-tbody tr[data-id="' + editingRowId + '"]').addClass('row-editing');
}
$('#grand-total').text(total.toFixed(2));
}
......@@ -266,6 +289,8 @@ function addRow() {
if (!planned) { showError('Please enter planned OT hours.'); return; }
if (!reason) { showError('Please enter a reason.'); return; }
cancelEditMode();
$.ajax({
url: "{{ route('overtime::overtime.addovertimerequestdetail', $data->id) }}",
method: 'POST',
......@@ -295,6 +320,85 @@ function addRow() {
});
}
function editRow(id) {
var d = rows[id];
if (!d) { showError('Unable to edit this entry.'); return; }
editingRowId = String(id);
var parts = toMonthYearParts(d.ord_month_of_overtime);
$('#ord_name').val(d.ord_name || '');
$('#ord_month').val(parts.month);
$('#ord_year').val(parts.year);
if ($.fn.dropdown) {
$('#ord_name').dropdown('set selected', d.ord_name || '');
$('#ord_month').dropdown('set selected', parts.month);
$('#ord_year').dropdown('set selected', parts.year);
}
$('#ord_budgeted').val(parseFloat(d.ord_budgeted || 0).toFixed(2));
$('#ord_utilised_to_date').val(parseFloat(d.ord_utilised_to_date || 0).toFixed(2));
$('#ord_planned_ot_hour').val(parseFloat(d.ord_planned_ot_hour || 0).toFixed(2));
$('#ord_balance').val(parseFloat(d.ord_balance || 0).toFixed(2));
$('#ord_reason').val(d.ord_reason || '');
$('#btn-add-row').hide();
$('#btn-update-row').show();
$('#btn-cancel-edit').show();
$('#rf-tbody tr').removeClass('row-editing');
$('#rf-tbody tr[data-id="' + editingRowId + '"]').addClass('row-editing');
showSuccess('Editing selected row.');
}
function updateRow() {
if (!editingRowId) {
showError('No row selected for editing.');
return;
}
var name = ($('#ord_name').val() || '').trim();
var monthNo = $('#ord_month').val();
var year = $('#ord_year').val();
var month = year && monthNo ? year + '-' + monthNo : '';
var budgeted = $('#ord_budgeted').val();
var utilised = $('#ord_utilised_to_date').val();
var planned = $('#ord_planned_ot_hour').val();
var reason = $('#ord_reason').val().trim();
if (!name) { showError('Please enter a name.'); return; }
if (!month) { showError('Please select OT month.'); return; }
if (!planned) { showError('Please enter planned OT hours.'); return; }
if (!reason) { showError('Please enter a reason.'); return; }
$.ajax({
url: "{{ route('overtime::overtime.updateovertimerequestdetail', ':id') }}".replace(':id', editingRowId),
method: 'POST',
data: {
_token: csrfToken(),
ord_name: name,
ord_month_of_overtime: month + '-01',
ord_budgeted: budgeted || 0,
ord_utilised_to_date: utilised || 0,
ord_planned_ot_hour: planned,
ord_reason: reason,
ord_total_hours: planned,
},
success: function (res) {
if (!res.success) { showError(res.message || 'Error while updating.'); return; }
rows[res.detail.id] = res.detail;
renderTable();
cancelEditMode();
showSuccess(res.message || 'Entry updated.');
},
error: function (xhr) {
var msg = 'Error while updating entry.';
if (xhr.responseJSON && xhr.responseJSON.message) msg = xhr.responseJSON.message;
if (xhr.responseJSON && xhr.responseJSON.errors) msg = Object.values(xhr.responseJSON.errors).flat().join(' ');
showError(msg);
},
});
}
function removeRow(id) {
if (!confirm('Are you sure you want to remove this entry from the table?')) return;
$.ajax({
......@@ -304,6 +408,9 @@ function removeRow(id) {
success: function (res) {
if (!res.success) { showError(res.message || 'Unable to remove the entry.'); return; }
delete rows[id];
if (editingRowId === String(id)) {
cancelEditMode();
}
renderTable();
showSuccess(res.message || 'Entry removed.');
},
......@@ -311,6 +418,15 @@ function removeRow(id) {
});
}
function cancelEditMode() {
editingRowId = null;
$('#btn-add-row').show();
$('#btn-update-row').hide();
$('#btn-cancel-edit').hide();
$('#rf-tbody tr').removeClass('row-editing');
resetRequestForm();
}
function resetRequestForm() {
$('#ord_name, #ord_month, #ord_year').val('');
if ($.fn.dropdown) $('#ord_name, #ord_month, #ord_year').dropdown('clear');
......
......@@ -83,7 +83,7 @@ private function authorizeHodRequestAccess(OvertimeRequestMain $record, ?array $
}
// =========================================================================
// LIST (all status=2 — no role scoping for now)
// LIST (status 2 = Permohonan, status 3 = Permohonan Semula)
// =========================================================================
public function getReviewList($request)
......@@ -94,7 +94,7 @@ public function getReviewList($request)
->from('overtime_request_main as orm')
->leftJoin('project_team as pt', 'pt.id', '=', 'orm.orm_requested_by_id')
->leftJoin('staff_info as si', 'si.staff_id', '=', 'pt.pt_staff_id')
->where('orm.orm_status', 2)
->whereIn('orm.orm_status', [2, 3])
->whereNull('orm.deleted_at')
->where(function ($q) {
$q->where('si.penyokong', $this->currentHodUserId())
......@@ -153,9 +153,9 @@ public function findQueryHistory(int $id)
public function approveRequestForm(int $id): OvertimeRequestMain
{
$record = OvertimeRequestMain::findOrFail($id);
$this->authorizeHodRequestAccess($record, [2]);
$this->authorizeHodRequestAccess($record, [2, 3]);
if ($record->orm_status !== 2) {
if (!in_array((int) $record->orm_status, [2, 3], true)) {
throw new \Exception('Borang ini tidak lagi menunggu semakan HOD.');
}
......@@ -177,9 +177,9 @@ public function approveRequestForm(int $id): OvertimeRequestMain
public function kueriRequestForm(int $id, string $remark): OvertimeRequestMain
{
$record = OvertimeRequestMain::findOrFail($id);
$this->authorizeHodRequestAccess($record, [2]);
$this->authorizeHodRequestAccess($record, [2, 3]);
if ($record->orm_status !== 2) {
if (!in_array((int) $record->orm_status, [2, 3], true)) {
throw new \Exception('Borang ini tidak lagi menunggu semakan HOD.');
}
......@@ -213,7 +213,7 @@ public function kueriRequestForm(int $id, string $remark): OvertimeRequestMain
public function rejectRequestForm(int $id): OvertimeRequestMain
{
$record = OvertimeRequestMain::findOrFail($id);
$this->authorizeHodRequestAccess($record, [2]);
$this->authorizeHodRequestAccess($record, [2, 3]);
$record->update([
'orm_status' => 12,
......
......@@ -504,6 +504,7 @@ private function logAuditTrail(string $taskCode, int $requestId, ?int $detailId
public function submitRequestForm(int $id): OvertimeRequestMain
{
$record = $this->findRequestForm($id);
$currentStatus = (int) $record->orm_status;
$this->authorizePmRequestAccess($record, [1, 8, 10]);
$detailCount = OvertimeRequestDetail::where('fk_overtime_request_main', $id)->count();
......@@ -511,14 +512,23 @@ public function submitRequestForm(int $id): OvertimeRequestMain
throw new \Exception('Sila tambah sekurang-kurangnya satu entri sebelum menghantar.');
}
$isResubmission = in_array($currentStatus, [8, 10], true);
$record->update([
'orm_status' => 2, // New request (also used for resubmission)
'orm_status' => $isResubmission ? 3 : 2,
'orm_requested_date' => now(),
'updated_by' => Auth::id(),
]);
// Audit trail: submission
$this->logAuditTrail('SUBMIT', $record->id, null, 'Request form submitted for HOD review.');
$this->logAuditTrail(
'SUBMIT',
$record->id,
null,
$isResubmission
? 'Request form resubmitted after query review.'
: 'Request form submitted for HOD review.'
);
return $record;
}
......
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