<?php
require_once __DIR__ . '/../includes/bootstrap.php'; require_login(); ensure_can_manage_payroll();
$rows = [];
$stmt = db()->query("SELECT p.year,p.month,p.status,e.full_name,pi.base_salary,pi.extras,pi.discounts,pi.net_pay FROM payroll_items pi JOIN payroll_periods p ON p.id=pi.period_id JOIN employees e ON e.id=pi.employee_id ORDER BY p.year DESC, p.month DESC, e.full_name");
foreach ($stmt as $r) { $rows[] = [month_name_es($r['month']).' '.$r['year'],$r['status'],$r['full_name'],$r['base_salary'],$r['extras'],$r['discounts'],$r['net_pay']]; }
download_xlsx('nomina.xlsx', ['Período','Estado','Empleado','Salario base','Extras','Descuentos','Neto'], $rows);
