<?php include __DIR__ . '/../includes/header.php'; ensure_can_manage_payroll();
if (is_post()) {
  $stmt = db()->prepare("INSERT INTO salary_advances (employee_id, advance_date, amount, notes, status, created_at) VALUES (?, ?, ?, ?, ?, ?)");
  $stmt->execute([(int)$_POST['employee_id'], $_POST['advance_date'], (float)$_POST['amount'], trim($_POST['notes'] ?? ''), trim($_POST['status'] ?? 'Activo'), now()]);
  audit_log('create', 'salary_advances', db()->lastInsertId(), 'Alta adelanto salarial');
  set_flash('success', 'Adelanto registrado. Se aplicará al generar o cerrar la nómina del mes correspondiente.');
  redirect('advances/index.php');
}
$title = 'Nuevo adelanto'; $row = [];
include __DIR__ . '/form.php';
include __DIR__ . '/../includes/footer.php';
