File: /var/www/matriculas_api_dev/app/Http/Controllers/ReportController.php
<?php
namespace App\Http\Controllers;
use App\Services\ReportService;
use Illuminate\Http\Request;
use Exception;
class ReportController extends Controller
{
protected $reportService;
public function __construct(ReportService $reportService)
{
$this->reportService = $reportService;
}
/**
* Reporte maestro de matrículas.
*/
public function contractsReport(Request $request)
{
try {
$data = $this->reportService->contractsReport(
$request->query('period_id'),
$request->only([
'status_contract', 'status_payment', 'status_signature',
'concept', 'financial_parent_id', 'course_id', 'registration_years',
'fecha_pago_matricula_desde', 'fecha_pago_matricula_hasta',
])
);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Exportar reporte maestro a Excel/CSV.
*/
public function contractsReportExcel(Request $request)
{
try {
return $this->reportService->exportContractsReport(
$request->query('period_id'),
$request->only([
'status_contract', 'status_payment', 'status_signature',
'concept', 'financial_parent_id', 'course_id', 'registration_years',
'fecha_pago_matricula_desde', 'fecha_pago_matricula_hasta',
]),
$request->query('format', 'xlsx')
);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Reporte financiero (sin período específico).
*/
public function financialReport(Request $request)
{
try {
$data = $this->reportService->financialReport(
$request->query('period_id'),
$request->only([
'status_contract', 'status_payment', 'course_id',
'concept', 'search', 'fecha_desde', 'fecha_hasta', 'registration_years',
'fecha_pago_matricula_desde', 'fecha_pago_matricula_hasta',
])
);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Reporte financiero por período.
*/
public function financialReportByPeriod($periodId, Request $request)
{
try {
$data = $this->reportService->financialReport(
$periodId,
$request->only([
'status_contract', 'status_payment', 'course_id',
'concept', 'search', 'fecha_desde', 'fecha_hasta',
'fecha_pago_matricula_desde', 'fecha_pago_matricula_hasta',
])
);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Exportar reporte financiero a Excel/CSV.
*/
public function financialReportExcel(Request $request)
{
try {
return $this->reportService->exportFinancialReport(
$request->query('period_id'),
$request->only([
'status_contract', 'status_payment', 'course_id',
'concept', 'search', 'fecha_desde', 'fecha_hasta', 'registration_years',
'fecha_pago_matricula_desde', 'fecha_pago_matricula_hasta',
]),
$request->query('format', 'xlsx')
);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Reporte comparativo multi-período.
*/
public function comparativeReport(Request $request)
{
try {
$periods = $request->query('periods', '');
$periodIds = array_filter(explode(',', $periods), fn($v) => is_numeric($v));
$data = $this->reportService->comparativeReport($periodIds);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Reporte Odoo.
*/
public function odooReport(Request $request)
{
try {
$data = $this->reportService->odooReport(
$request->query('period_id'),
$request->only([
'status_contract', 'status_payment', 'course_id',
'financial_parent_id',
])
);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Exportar reporte Odoo a Excel/CSV.
*/
public function odooReportExcel(Request $request)
{
try {
return $this->reportService->exportOdooReport(
$request->query('period_id'),
$request->only([
'status_contract', 'status_payment', 'course_id',
'financial_parent_id',
]),
$request->query('format', 'xlsx')
);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
/**
* Exportar reporte comparativo a Excel.
*/
public function comparativeReportExcel(Request $request)
{
try {
$periods = $request->query('periods', '');
$periodIds = array_filter(explode(',', $periods), fn($v) => is_numeric($v));
return $this->reportService->exportComparativeReport(
$periodIds,
$request->query('format', 'xlsx')
);
} catch (Exception $e) {
return genericResponse(['error' => $e->getMessage()], $e->getCode() ?: 500);
}
}
}