<?php require_once __DIR__ . '/../includes/bootstrap.php'; require_login(); ensure_can_manage_payroll();
$id = (int)($_GET['id'] ?? 0);
$stmt = db()->prepare("SELECT * FROM payroll_periods WHERE id=?");
$stmt->execute([$id]);
$period = $stmt->fetch();
if (!$period) { exit('Período no encontrado'); }
$items = db()->prepare("SELECT pi.*, e.full_name, e.document_number FROM payroll_items pi JOIN employees e ON e.id = pi.employee_id WHERE pi.period_id=? ORDER BY e.full_name");
$items->execute([$id]);
$totalNet = 0;
?>
<!doctype html><html lang="es"><head><meta charset="utf-8"><title>Resumen de Nómina</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"><style>body{padding:30px;font-size:14px} .logo{height:70px;max-width:220px;object-fit:contain} @media print {.no-print{display:none}}</style></head><body>
<div class="d-flex justify-content-between align-items-center mb-4"><div><img class="logo" src="<?= e(public_logo_url()) ?>" alt="logo"></div><div class="text-end"><h3 class="mb-1">Resumen de Nómina</h3><div><?= e(company_name()) ?></div><div><?= e(month_name_es($period['month']) . ' ' . $period['year']) ?></div><div><?= e($period['start_date']) ?> al <?= e($period['end_date']) ?></div></div></div>
<div class="mb-3"><strong>RUC:</strong> <?= e(setting('company_ruc')) ?> · <strong>Tel:</strong> <?= e(setting('company_phone')) ?> · <strong>Email:</strong> <?= e(setting('company_email')) ?></div>
<table class="table table-bordered table-sm"><thead><tr><th>Empleado</th><th>Cédula</th><th>Salario base</th><th>Extras</th><th>Descuentos</th><th>Neto</th></tr></thead><tbody><?php foreach ($items as $row): $totalNet += (float)$row['net_pay']; ?><tr><td><?= e($row['full_name']) ?></td><td><?= e($row['document_number']) ?></td><td><?= money_gs($row['base_salary']) ?></td><td><?= money_gs($row['extras']) ?></td><td><?= money_gs($row['discounts']) ?></td><td><?= money_gs($row['net_pay']) ?></td></tr><?php endforeach; ?></tbody><tfoot><tr><th colspan="5" class="text-end">Total neto</th><th><?= money_gs($totalNet) ?></th></tr></tfoot></table>
<div class="mt-4"><strong>Observación:</strong> Documento interno generado por el ERP RRHH v4.</div>
<div class="mt-4 no-print"><button onclick="window.print()" class="btn btn-dark">Imprimir / Guardar PDF</button></div>
</body></html>
