634 lines
31 KiB
PHP
634 lines
31 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/includes/media_helper.php';
|
|
|
|
$tenant_id = 1;
|
|
|
|
// Handle Bulk Labour
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['bulk_labour'])) {
|
|
$project_id = (int)($_POST['bulk_project_id'] ?? 0);
|
|
$employee_id = (int)($_POST['bulk_employee_id'] ?? 0);
|
|
$labour_type_id = (int)($_POST['bulk_labour_type_id'] ?? 0);
|
|
$evidence_type_id = (int)($_POST['bulk_evidence_type_id'] ?? 0);
|
|
$notes = $_POST['bulk_notes'] ?? '';
|
|
|
|
$days = $_POST['bulk_days'] ?? []; // Array of date => hours
|
|
|
|
if ($project_id && $employee_id && !empty($days)) {
|
|
$db = db();
|
|
foreach ($days as $date => $hours) {
|
|
$hours = (float)$hours;
|
|
if ($hours <= 0) continue;
|
|
|
|
$stmt = $db->prepare("INSERT INTO labour_entries (tenant_id, project_id, employee_id, entry_date, hours, labour_type_id, evidence_type_id, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$tenant_id, $project_id, $employee_id, $date, $hours, $labour_type_id, $evidence_type_id, $notes]);
|
|
}
|
|
|
|
$stmt = $db->prepare("INSERT INTO activity_log (tenant_id, action, details) VALUES (?, ?, ?)");
|
|
$stmt->execute([$tenant_id, 'Bulk Labour Added', "Logged hours for employee ID $employee_id via bulk entry"]);
|
|
|
|
header("Location: labour.php?success=1");
|
|
exit;
|
|
}
|
|
}
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_labour'])) {
|
|
$project_id = (int)($_POST['project_id'] ?? 0);
|
|
$employee_ids = isset($_POST['employee_ids']) ? array_map('intval', $_POST['employee_ids']) : [];
|
|
if (empty($employee_ids) && isset($_POST['employee_id'])) {
|
|
$employee_ids = [(int)$_POST['employee_id']];
|
|
}
|
|
|
|
$entry_date = $_POST['entry_date'] ?? date('Y-m-d');
|
|
$hours = (float)($_POST['hours'] ?? 0);
|
|
$labour_type_id = (int)($_POST['labour_type_id'] ?? 0);
|
|
$evidence_type_id = (int)($_POST['evidence_type_id'] ?? 0);
|
|
$notes = $_POST['notes'] ?? '';
|
|
|
|
if ($project_id && !empty($employee_ids) && $hours > 0) {
|
|
$db = db();
|
|
foreach ($employee_ids as $employee_id) {
|
|
$stmt = $db->prepare("INSERT INTO labour_entries (tenant_id, project_id, employee_id, entry_date, hours, labour_type_id, evidence_type_id, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$tenant_id, $project_id, $employee_id, $entry_date, $hours, $labour_type_id, $evidence_type_id, $notes]);
|
|
$labour_entry_id = (int)$db->lastInsertId();
|
|
|
|
// Handle File Uploads (only for the first one or all? Usually all if it's a team entry)
|
|
if (!empty($_FILES['attachments']['name'][0])) {
|
|
foreach ($_FILES['attachments']['tmp_name'] as $key => $tmp_name) {
|
|
if ($_FILES['attachments']['error'][$key] === UPLOAD_ERR_OK) {
|
|
$file_name = $_FILES['attachments']['name'][$key];
|
|
$file_size = $_FILES['attachments']['size'][$key];
|
|
$mime_type = $_FILES['attachments']['type'][$key];
|
|
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
|
|
$new_file_name = uniqid() . '.' . $file_ext;
|
|
$file_path = 'uploads/' . $new_file_name;
|
|
|
|
if (!is_dir('uploads')) mkdir('uploads', 0775, true);
|
|
if (move_uploaded_file($tmp_name, $file_path)) {
|
|
$thumbnail_path = null;
|
|
if (isImage($mime_type)) {
|
|
$thumb_name = 'thumb_' . $new_file_name;
|
|
$thumb_path = 'uploads/' . $thumb_name;
|
|
if (createThumbnail($file_path, $thumb_path)) {
|
|
$thumbnail_path = $thumb_path;
|
|
}
|
|
}
|
|
|
|
$stmt = $db->prepare("INSERT INTO attachments (tenant_id, entity_type, entity_id, file_name, file_path, thumbnail_path, file_size, mime_type, uploaded_by) VALUES (?, 'labour_entry', ?, ?, ?, ?, ?, ?, ?)");
|
|
$stmt->execute([$tenant_id, $labour_entry_id, $file_name, $file_path, $thumbnail_path, $file_size, $mime_type, $currentUserName]);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$stmt = $db->prepare("INSERT INTO activity_log (tenant_id, action, details) VALUES (?, ?, ?)");
|
|
$stmt->execute([$tenant_id, 'Labour Added', "Logged $hours hours for " . count($employee_ids) . " employee(s)"]);
|
|
|
|
header("Location: labour.php?success=1");
|
|
exit;
|
|
}
|
|
}
|
|
|
|
// Fetch Data
|
|
$filter_project = (int)($_GET['project_id'] ?? 0);
|
|
$filter_employee = (int)($_GET['employee_id'] ?? 0);
|
|
$filter_type = (int)($_GET['labour_type_id'] ?? 0);
|
|
$filter_evidence = (int)($_GET['evidence_type_id'] ?? 0);
|
|
$filter_start = $_GET['start_date'] ?? '';
|
|
$filter_end = $_GET['end_date'] ?? '';
|
|
|
|
$where = ["le.tenant_id = ?"];
|
|
$params = [$tenant_id];
|
|
|
|
if ($filter_project) {
|
|
$where[] = "le.project_id = ?";
|
|
$params[] = $filter_project;
|
|
}
|
|
if ($filter_employee) {
|
|
$where[] = "le.employee_id = ?";
|
|
$params[] = $filter_employee;
|
|
}
|
|
if ($filter_type) {
|
|
$where[] = "le.labour_type_id = ?";
|
|
$params[] = $filter_type;
|
|
}
|
|
if ($filter_evidence) {
|
|
$where[] = "le.evidence_type_id = ?";
|
|
$params[] = $filter_evidence;
|
|
}
|
|
if ($filter_start) {
|
|
$where[] = "le.entry_date >= ?";
|
|
$params[] = $filter_start;
|
|
}
|
|
if ($filter_end) {
|
|
$where[] = "le.entry_date <= ?";
|
|
$params[] = $filter_end;
|
|
}
|
|
|
|
$where_clause = implode(" AND ", $where);
|
|
|
|
$labourEntries = db()->prepare("
|
|
SELECT le.*, p.name as project_name, e.name as employee_name, lt.name as labour_type, et.name as evidence_type
|
|
FROM labour_entries le
|
|
JOIN projects p ON le.project_id = p.id
|
|
JOIN employees e ON le.employee_id = e.id
|
|
LEFT JOIN labour_types lt ON le.labour_type_id = lt.id
|
|
LEFT JOIN evidence_types et ON le.evidence_type_id = et.id
|
|
WHERE $where_clause
|
|
ORDER BY le.entry_date DESC, le.created_at DESC
|
|
");
|
|
$labourEntries->execute($params);
|
|
$labourList = $labourEntries->fetchAll();
|
|
|
|
$projects = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? AND is_archived = 0 ORDER BY name");
|
|
$projects->execute([$tenant_id]);
|
|
$projectList = $projects->fetchAll();
|
|
|
|
$employees = db()->prepare("SELECT id, first_name, last_name FROM employees WHERE tenant_id = ? ORDER BY first_name, last_name");
|
|
$employees->execute([$tenant_id]);
|
|
$employeeList = $employees->fetchAll();
|
|
|
|
$labourTypes = db()->prepare("SELECT * FROM labour_types WHERE tenant_id = ? ORDER BY name");
|
|
$labourTypes->execute([$tenant_id]);
|
|
$labourTypeList = $labourTypes->fetchAll();
|
|
|
|
$evidenceTypes = db()->prepare("SELECT * FROM evidence_types WHERE tenant_id = ? ORDER BY name");
|
|
$evidenceTypes->execute([$tenant_id]);
|
|
$evidenceTypeList = $evidenceTypes->fetchAll();
|
|
|
|
// Find current employee (mocked as user_id 1)
|
|
$currentUser = db()->prepare("SELECT name FROM users WHERE id = 1");
|
|
$currentUser->execute();
|
|
$currentUserName = $currentUser->fetchColumn() ?: 'System';
|
|
|
|
$currentEmployeeStmt = db()->prepare("SELECT id FROM employees WHERE user_id = 1 AND tenant_id = ?");
|
|
$currentEmployeeStmt->execute([$tenant_id]);
|
|
$currentEmployeeId = $currentEmployeeStmt->fetchColumn() ?: null;
|
|
|
|
$teams = db()->prepare("SELECT * FROM teams WHERE tenant_id = ? ORDER BY name");
|
|
$teams->execute([$tenant_id]);
|
|
$teamList = $teams->fetchAll();
|
|
|
|
$teamMembers = db()->prepare("SELECT * FROM employee_teams WHERE tenant_id = ?");
|
|
$teamMembers->execute([$tenant_id]);
|
|
$teamMemberList = $teamMembers->fetchAll();
|
|
// Group by team
|
|
$teamMembersGrouped = [];
|
|
foreach ($teamMemberList as $tm) {
|
|
$teamMembersGrouped[$tm['team_id']][] = $tm['employee_id'];
|
|
}
|
|
|
|
$pageTitle = "SR&ED Manager - Labour Tracking";
|
|
include __DIR__ . '/includes/header.php';
|
|
?>
|
|
|
|
<div class="container-fluid py-4">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2 class="fw-bold mb-0">Labour Tracking</h2>
|
|
<div>
|
|
<a href="api/export_labour.php?<?= http_build_query($_GET) ?>" class="btn btn-primary me-2"><i class="bi bi-file-earmark-excel me-1"></i> Export to Excel</a>
|
|
<button class="btn btn-primary me-2" data-bs-toggle="modal" data-bs-target="#bulkLabourModal"><i class="bi bi-calendar3 me-1"></i> Bulk Add</button>
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addLabourModal">+ Add Labour Entry</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card border-0 shadow-sm mb-4">
|
|
<div class="card-body">
|
|
<form method="GET" class="row g-2 align-items-end">
|
|
<div class="col-md-2">
|
|
<label class="form-label small fw-bold">Project</label>
|
|
<select name="project_id" class="form-select form-select-sm">
|
|
<option value="">All Projects</option>
|
|
<?php
|
|
$allProjects = db()->prepare("SELECT id, name FROM projects WHERE tenant_id = ? ORDER BY name");
|
|
$allProjects->execute([$tenant_id]);
|
|
foreach ($allProjects->fetchAll() as $p): ?>
|
|
<option value="<?= $p['id'] ?>" <?= $filter_project == $p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label small fw-bold">Employee</label>
|
|
<select name="employee_id" class="form-select form-select-sm">
|
|
<option value="">All Employees</option>
|
|
<?php foreach ($employeeList as $e): ?>
|
|
<option value="<?= $e['id'] ?>" <?= $filter_employee == $e['id'] ? 'selected' : '' ?>><?= htmlspecialchars($e['first_name'] . ' ' . $e['last_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label small fw-bold">Labour Type</label>
|
|
<select name="labour_type_id" class="form-select form-select-sm">
|
|
<option value="">All Types</option>
|
|
<?php foreach ($labourTypeList as $lt): ?>
|
|
<option value="<?= $lt['id'] ?>" <?= $filter_type == $lt['id'] ? 'selected' : '' ?>><?= htmlspecialchars($lt['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2">
|
|
<label class="form-label small fw-bold">Evidence</label>
|
|
<select name="evidence_type_id" class="form-select form-select-sm">
|
|
<option value="">All Evidence</option>
|
|
<?php foreach ($evidenceTypeList as $et): ?>
|
|
<option value="<?= $et['id'] ?>" <?= $filter_evidence == $et['id'] ? 'selected' : '' ?>><?= htmlspecialchars($et['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-1">
|
|
<label class="form-label small fw-bold">From</label>
|
|
<input type="date" name="start_date" class="form-control form-control-sm" value="<?= htmlspecialchars($filter_start) ?>">
|
|
</div>
|
|
<div class="col-md-1">
|
|
<label class="form-label small fw-bold">To</label>
|
|
<input type="date" name="end_date" class="form-control form-control-sm" value="<?= htmlspecialchars($filter_end) ?>">
|
|
</div>
|
|
<div class="col-md-2">
|
|
<div class="d-flex gap-1">
|
|
<button type="submit" class="btn btn-sm btn-primary flex-grow-1">Filter</button>
|
|
<a href="labour.php" class="btn btn-sm btn-secondary">Reset</a>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if (isset($_GET['success'])): ?>
|
|
<div class="alert alert-success alert-dismissible fade show border-0 shadow-sm mb-4" role="alert">
|
|
Labour entry successfully saved.
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="table-responsive">
|
|
<table class="table align-middle mb-0">
|
|
<thead class="bg-light">
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Employee</th>
|
|
<th>Project</th>
|
|
<th>Hours</th>
|
|
<th>Type / Evidence</th>
|
|
<th>Notes</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($labourList)): ?>
|
|
<tr><td colspan="7" class="text-center py-5 text-muted">No labour entries found.</td></tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($labourList as $l): ?>
|
|
<tr>
|
|
<td class="text-muted"><?= $l['entry_date'] ?></td>
|
|
<td><strong><?= htmlspecialchars($l['employee_name']) ?></strong></td>
|
|
<td><?= htmlspecialchars($l['project_name']) ?></td>
|
|
<td><span class="badge bg-light text-primary border"><?= number_format((float)$l['hours'], 2) ?> h</span></td>
|
|
<td>
|
|
<div class="small fw-bold"><?= htmlspecialchars($l['labour_type'] ?? 'N/A') ?></div>
|
|
<div class="extra-small text-muted"><?= htmlspecialchars($l['evidence_type'] ?? 'N/A') ?></div>
|
|
</td>
|
|
<td class="small text-muted"><?= htmlspecialchars($l['notes'] ?? '') ?></td>
|
|
<td class="text-end">
|
|
<button class="btn btn-sm btn-secondary">Details</button>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal -->
|
|
<div class="modal fade" id="addLabourModal" tabindex="-1">
|
|
<div class="modal-dialog modal-lg modal-dialog-centered">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title fw-bold">Add Labour Entry</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<div class="modal-body">
|
|
<div class="row">
|
|
<div class="col-12 mb-4">
|
|
<label class="form-label small fw-bold d-block">Entry Mode</label>
|
|
<div class="btn-group w-100" role="group">
|
|
<input type="radio" class="btn-check" name="entry_mode" id="modeIndividual" value="individual" checked autocomplete="off">
|
|
<label class="btn btn-outline-primary" for="modeIndividual">Individual</label>
|
|
|
|
<input type="radio" class="btn-check" name="entry_mode" id="modeTeam" value="team" autocomplete="off">
|
|
<label class="btn btn-outline-primary" for="modeTeam">Team</label>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-12 mb-3" id="individualSelect">
|
|
<label class="form-label small fw-bold">Employee</label>
|
|
<select name="employee_id" class="form-select">
|
|
<option value="">Select Employee...</option>
|
|
<?php foreach ($employeeList as $e): ?>
|
|
<option value="<?= $e['id'] ?>" <?= $e['id'] == $currentEmployeeId ? 'selected' : '' ?>><?= htmlspecialchars($e['first_name'] . ' ' . $e['last_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
|
|
<div id="teamSelect" style="display: none;" class="col-12">
|
|
<div class="row">
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label small fw-bold">Select Team</label>
|
|
<select id="teamIdSelect" class="form-select mb-3">
|
|
<option value="">Select Team...</option>
|
|
<?php foreach ($teamList as $t): ?>
|
|
<option value="<?= $t['id'] ?>"><?= htmlspecialchars($t['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label small fw-bold d-block">Team Members</label>
|
|
<div id="teamMembersList" class="p-3 border rounded bg-light" style="max-height: 200px; overflow-y: auto;">
|
|
<p class="text-muted small mb-0">Select a team to see members</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-12 mb-3">
|
|
<label class="form-label small fw-bold">Project</label>
|
|
<select name="project_id" class="form-select" required>
|
|
<option value="">Select Project...</option>
|
|
<?php foreach ($projectList as $p): ?>
|
|
<option value="<?= $p['id'] ?>"><?= htmlspecialchars($p['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label small fw-bold">Date</label>
|
|
<input type="date" name="entry_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label small fw-bold">Hours</label>
|
|
<input type="number" name="hours" class="form-control" step="0.25" min="0" required>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label small fw-bold">Type of Labour</label>
|
|
<select name="labour_type_id" class="form-select">
|
|
<?php foreach ($labourTypeList as $lt): ?>
|
|
<option value="<?= $lt['id'] ?>"><?= htmlspecialchars($lt['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label small fw-bold">Objective Evidence</label>
|
|
<select name="evidence_type_id" class="form-select">
|
|
<?php foreach ($evidenceTypeList as $et): ?>
|
|
<option value="<?= $et['id'] ?>"><?= htmlspecialchars($et['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="col-12 mb-3">
|
|
<label class="form-label small fw-bold">Attachments</label>
|
|
<input type="file" name="attachments[]" class="form-control" multiple>
|
|
</div>
|
|
<div class="col-12">
|
|
<label class="form-label small fw-bold">Notes</label>
|
|
<textarea name="notes" class="form-control" rows="2"></textarea>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer border-0">
|
|
<button type="submit" name="add_labour" class="btn btn-primary px-4">Save Entry</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Bulk Entry Modal -->
|
|
<div class="modal fade" id="bulkLabourModal" tabindex="-1">
|
|
<div class="modal-dialog modal-xl modal-dialog-centered">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title fw-bold">Bulk Labour Entry</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<form method="POST">
|
|
<div class="modal-body">
|
|
<div class="row g-4">
|
|
<div class="col-md-8">
|
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
|
<h6 class="fw-bold mb-0">Weekly Timesheet</h6>
|
|
<div class="d-flex gap-2">
|
|
<input type="week" id="bulkWeek" class="form-control form-control-sm" value="<?= date('Y-\WW') ?>">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered text-center">
|
|
<thead class="bg-light">
|
|
<tr id="bulkHeader">
|
|
<!-- To be populated by JS -->
|
|
</tr>
|
|
</thead>
|
|
<tbody id="bulkBody">
|
|
<!-- To be populated by JS -->
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div id="existingHoursInfo" class="small text-muted mt-2"></div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card bg-light border-0">
|
|
<div class="card-body">
|
|
<h6 class="fw-bold mb-3">Labour Details</h6>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Employee</label>
|
|
<select name="bulk_employee_id" id="bulkEmployee" class="form-select form-select-sm" required>
|
|
<option value="">Select Employee...</option>
|
|
<?php foreach ($employeeList as $e): ?>
|
|
<option value="<?= $e['id'] ?>" <?= $e['id'] == $currentEmployeeId ? 'selected' : '' ?>><?= htmlspecialchars($e['first_name'] . ' ' . $e['last_name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Project</label>
|
|
<select name="bulk_project_id" id="bulkProject" class="form-select form-select-sm" required>
|
|
<?php foreach ($projectList as $p): ?>
|
|
<option value="<?= $p['id'] ?>"><?= htmlspecialchars($p['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Labour Type</label>
|
|
<select name="bulk_labour_type_id" class="form-select form-select-sm">
|
|
<?php foreach ($labourTypeList as $lt): ?>
|
|
<option value="<?= $lt['id'] ?>"><?= htmlspecialchars($lt['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label small fw-bold">Evidence</label>
|
|
<select name="bulk_evidence_type_id" class="form-select form-select-sm">
|
|
<?php foreach ($evidenceTypeList as $et): ?>
|
|
<option value="<?= $et['id'] ?>"><?= htmlspecialchars($et['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<div class="mb-0">
|
|
<label class="form-label small fw-bold">Notes</label>
|
|
<textarea name="bulk_notes" class="form-control form-control-sm" rows="2"></textarea>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer border-0">
|
|
<button type="submit" name="bulk_labour" class="btn btn-primary px-4">Save All Entries</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
// Convert PHP arrays to JS for dynamic UI
|
|
$employeesJson = json_encode($employeeList);
|
|
$teamMembersJson = json_encode($teamMembersGrouped);
|
|
?>
|
|
<script>
|
|
const employees = <?= $employeesJson ?>;
|
|
const teamMembersGrouped = <?= $teamMembersJson ?>;
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
const modeIndividual = document.getElementById('modeIndividual');
|
|
const modeTeam = document.getElementById('modeTeam');
|
|
const individualSelect = document.getElementById('individualSelect');
|
|
const teamSelect = document.getElementById('teamSelect');
|
|
const teamIdSelect = document.getElementById('teamIdSelect');
|
|
const teamMembersList = document.getElementById('teamMembersList');
|
|
|
|
function toggleMode() {
|
|
if (modeIndividual.checked) {
|
|
individualSelect.style.display = 'block';
|
|
teamSelect.style.display = 'none';
|
|
individualSelect.querySelector('select').required = true;
|
|
} else {
|
|
individualSelect.style.display = 'none';
|
|
teamSelect.style.display = 'block';
|
|
individualSelect.querySelector('select').required = false;
|
|
}
|
|
}
|
|
|
|
modeIndividual.addEventListener('change', toggleMode);
|
|
modeTeam.addEventListener('change', toggleMode);
|
|
|
|
teamIdSelect.addEventListener('change', function() {
|
|
const teamId = this.value;
|
|
teamMembersList.innerHTML = '';
|
|
|
|
if (teamId && teamMembersGrouped[teamId]) {
|
|
const members = teamMembersGrouped[teamId];
|
|
members.forEach(empId => {
|
|
const emp = employees.find(e => e.id == empId);
|
|
if (emp) {
|
|
const div = document.createElement('div');
|
|
div.className = 'form-check';
|
|
div.innerHTML = `
|
|
<input class="form-check-input" type="checkbox" name="employee_ids[]" value="${emp.id}" id="emp_${emp.id}" checked>
|
|
<label class="form-check-label" for="emp_${emp.id}">
|
|
${emp.first_name} ${emp.last_name}
|
|
</label>
|
|
`;
|
|
teamMembersList.appendChild(div);
|
|
}
|
|
});
|
|
} else {
|
|
teamMembersList.innerHTML = '<p class="text-muted small mb-0">Select a team to see members</p>';
|
|
}
|
|
});
|
|
|
|
// Bulk Entry Logic
|
|
const bulkWeek = document.getElementById('bulkWeek');
|
|
const bulkHeader = document.getElementById('bulkHeader');
|
|
const bulkBody = document.getElementById('bulkBody');
|
|
const bulkEmployee = document.getElementById('bulkEmployee');
|
|
const existingHoursInfo = document.getElementById('existingHoursInfo');
|
|
|
|
function updateBulkView() {
|
|
const weekStr = bulkWeek.value;
|
|
if (!weekStr) return;
|
|
|
|
const [year, week] = weekStr.split('-W').map(Number);
|
|
const startDate = getStartDateOfWeek(week, year);
|
|
|
|
bulkHeader.innerHTML = '';
|
|
bulkBody.innerHTML = '<tr>';
|
|
|
|
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
const dates = [];
|
|
|
|
for (let i = 0; i < 7; i++) {
|
|
const d = new Date(startDate);
|
|
d.setDate(startDate.getDate() + i);
|
|
const dateStr = d.toISOString().split('T')[0];
|
|
dates.push(dateStr);
|
|
|
|
const th = document.createElement('th');
|
|
th.innerHTML = `${days[i]}<br><small class="fw-normal text-muted">${d.getMonth() + 1}/${d.getDate()}</small>`;
|
|
bulkHeader.appendChild(th);
|
|
|
|
const td = document.createElement('td');
|
|
td.innerHTML = `
|
|
<input type="number" name="bulk_days[${dateStr}]" class="form-control text-center bulk-hour-input" step="0.5" min="0" placeholder="0">
|
|
<div class="extra-small text-muted mt-1 existing-hours" data-date="${dateStr}"></div>
|
|
`;
|
|
bulkBody.appendChild(td);
|
|
}
|
|
bulkBody.innerHTML += '</tr>';
|
|
|
|
fetchExistingHours(dates[0], dates[6]);
|
|
}
|
|
|
|
function getStartDateOfWeek(w, y) {
|
|
const d = new Date(y, 0, 1 + (w - 1) * 7);
|
|
const dow = d.getDay();
|
|
const ISOweekStart = d;
|
|
if (dow <= 4)
|
|
ISOweekStart.setDate(d.getDate() - d.getDay());
|
|
else
|
|
ISOweekStart.setDate(d.getDate() + 8 - d.getDay());
|
|
return ISOweekStart;
|
|
}
|
|
|
|
function fetchExistingHours(start, end) {
|
|
const empId = bulkEmployee.value;
|
|
if (!empId) return;
|
|
|
|
fetch(`api/get_labour_stats.php?employee_id=${empId}&start_date=${start}&end_date=${end}`)
|
|
.then(res => res.json())
|
|
.then(data => {
|
|
document.querySelectorAll('.existing-hours').forEach(el => {
|
|
const date = el.dataset.date;
|
|
const record = data.find(d => d.entry_date === date);
|
|
if (record) {
|
|
el.innerHTML = `Total: ${record.total_hours}h`;
|
|
el.classList.add('text-primary', 'fw-bold');
|
|
} else {
|
|
el.innerHTML = 'Total: 0h';
|
|
el.classList.remove('text-primary', 'fw-bold');
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
bulkWeek.addEventListener('change', updateBulkView);
|
|
bulkEmployee.addEventListener('change', updateBulkView);
|
|
|
|
// Initial update
|
|
updateBulkView();
|
|
});
|
|
</script>
|
|
|
|
<?php include __DIR__ . '/includes/footer.php'; ?>
|