131 lines
5.2 KiB
PHP
131 lines
5.2 KiB
PHP
<?php
|
|
require_once '../config.php';
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
switch ($method) {
|
|
case 'GET':
|
|
// Get schedules for a specific month and year
|
|
$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
|
|
$month = isset($_GET['month']) ? (int)$_GET['month'] : date('n');
|
|
|
|
try {
|
|
$stmt = $pdo->prepare("SELECT staff_id, day, shift_type FROM schedules WHERE year = :year AND month = :month");
|
|
$stmt->execute([':year' => $year, ':month' => $month]);
|
|
$schedules = $stmt->fetchAll();
|
|
|
|
// Format data for frontend: { staffId: { day: 'shift_type' } }
|
|
$formattedData = [];
|
|
foreach ($schedules as $row) {
|
|
$staffId = $row['staff_id'];
|
|
$day = $row['day'];
|
|
$shiftType = $row['shift_type'];
|
|
|
|
if (!isset($formattedData[$staffId])) {
|
|
$formattedData[$staffId] = [];
|
|
}
|
|
$formattedData[$staffId][$day] = $shiftType;
|
|
}
|
|
|
|
echo json_encode(['status' => 'success', 'data' => $formattedData]);
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
break;
|
|
|
|
case 'POST':
|
|
// Save or update a shift
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
// Check if this is a bulk operation
|
|
if (isset($input['bulk']) && is_array($input['schedules'])) {
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
// Optional: clear existing month data if requested
|
|
if (isset($input['clear_month']) && $input['clear_month']) {
|
|
$stmt = $pdo->prepare("DELETE FROM schedules WHERE year = :year AND month = :month");
|
|
$stmt->execute([
|
|
':year' => $input['year'],
|
|
':month' => $input['month']
|
|
]);
|
|
}
|
|
|
|
$insertStmt = $pdo->prepare("INSERT INTO schedules (staff_id, year, month, day, shift_type) VALUES (:staff_id, :year, :month, :day, :shift_type) ON DUPLICATE KEY UPDATE shift_type = :shift_type");
|
|
|
|
foreach ($input['schedules'] as $schedule) {
|
|
if ($schedule['shift_type'] !== '') {
|
|
$insertStmt->execute([
|
|
':staff_id' => $schedule['staff_id'],
|
|
':year' => $input['year'],
|
|
':month' => $input['month'],
|
|
':day' => $schedule['day'],
|
|
':shift_type' => $schedule['shift_type']
|
|
]);
|
|
}
|
|
}
|
|
|
|
$pdo->commit();
|
|
echo json_encode(['status' => 'success', 'message' => 'Bulk schedules saved']);
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
if (!isset($input['staff_id']) || !isset($input['year']) || !isset($input['month']) || !isset($input['day'])) {
|
|
http_response_code(400);
|
|
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
|
|
exit;
|
|
}
|
|
|
|
$staffId = $input['staff_id'];
|
|
$year = $input['year'];
|
|
$month = $input['month'];
|
|
$day = $input['day'];
|
|
$shiftType = isset($input['shift_type']) ? $input['shift_type'] : '';
|
|
|
|
try {
|
|
if ($shiftType === '') {
|
|
// If shift_type is empty, delete the record
|
|
$stmt = $pdo->prepare("DELETE FROM schedules WHERE staff_id = :staff_id AND year = :year AND month = :month AND day = :day");
|
|
$stmt->execute([
|
|
':staff_id' => $staffId,
|
|
':year' => $year,
|
|
':month' => $month,
|
|
':day' => $day
|
|
]);
|
|
} else {
|
|
// Insert or update (upsert)
|
|
$stmt = $pdo->prepare("
|
|
INSERT INTO schedules (staff_id, year, month, day, shift_type)
|
|
VALUES (:staff_id, :year, :month, :day, :shift_type)
|
|
ON DUPLICATE KEY UPDATE shift_type = :shift_type_update
|
|
");
|
|
$stmt->execute([
|
|
':staff_id' => $staffId,
|
|
':year' => $year,
|
|
':month' => $month,
|
|
':day' => $day,
|
|
':shift_type' => $shiftType,
|
|
':shift_type_update' => $shiftType
|
|
]);
|
|
}
|
|
|
|
echo json_encode(['status' => 'success', 'message' => 'Schedule updated successfully']);
|
|
} catch (PDOException $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
http_response_code(405);
|
|
echo json_encode(['status' => 'error', 'message' => 'Method not allowed']);
|
|
break;
|
|
}
|
|
?>
|