« Back to History
leave_apply.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php // Path: /erp/api/attendance/leave_apply.php // Purpose: Employee applies for leave. Manager will approve via admin UI (not included here). require_once __DIR__ . '/../../core/db.php'; require_once __DIR__ . '/../../modules/auth/auth.php'; require_once __DIR__ . '/../../config.php'; header('Content-Type: application/json'); try { $db = getDB(); if (!isset($company_id)) { http_response_code(401); echo json_encode(['error'=>'unauth']); exit; } $input = json_decode(file_get_contents('php://input'), true); $employee_id = intval($input['employee_id'] ?? ($current_user['id'] ?? 0)); $leave_type = trim($input['leave_type'] ?? ''); $start_date = $input['start_date'] ?? null; $end_date = $input['end_date'] ?? null; $reason = $input['reason'] ?? null; if (!$employee_id || !$leave_type || !$start_date || !$end_date) { http_response_code(400); echo json_encode(['error'=>'missing fields']); exit; } // compute days inclusive $dt1 = new DateTime($start_date); $dt2 = new DateTime($end_date); $interval = $dt1->diff($dt2); $days = $interval->days + 1; $stmt = $db->prepare("INSERT INTO fc_leave (company_id, employee_id, leave_type, start_date, end_date, days, reason, status, applied_at) VALUES (:cid, :eid, :lt, :sd, :ed, :days, :reason, 'pending', NOW())"); $stmt->execute([ ':cid'=>$company_id, ':eid'=>$employee_id, ':lt'=>$leave_type, ':sd'=>$start_date, ':ed'=>$end_date, ':days'=>$days, ':reason'=>$reason ]); echo json_encode(['ok'=>1, 'leave_id'=>$db->lastInsertId()]); } catch (Exception $ex) { http_response_code(500); error_log("leave_apply error: ".$ex->getMessage()); echo json_encode(['error'=>'internal','msg'=>$ex->getMessage()]); }