HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux Bradford-Sitios 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: www-data (33)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
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);
        }
    }
}