<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace CmBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use CmBundle\Controller\PDF;

date_default_timezone_set('America/Mexico_City');

/**
 * Description of ReglamentoController
 *
 * @author omar
 */
class ReglamentoController extends Controller {

    //put your code here
    public function impReglamentoAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 3 || $session->get("rol") == 1)) {
            $session->set("vista", "caja");
            //if ($request->getMethod() == "POST") {

            $em = $this->getDoctrine()->getManager();

            $ideAlu = $request->get('ideAlu');


            $alumno = $em->getRepository('CmBundle:Alumnos')
                    ->findOneBy(array('ideAlu' => $ideAlu));
            $pago = $em->getRepository('CmBundle:Pagos')
                    ->findOneBy(array('cveAlu' => $ideAlu, 'staPag' => 1));
            $bandera = 0;
            $dia = date('d');
            if ($dia <= 6) {
                $bandera = 0;
            } else {
                $bandera = 1;
            }

            $pdf = new PDF('P', 'mm', 'Letter');
            $pdf->setTitulo("FORMATO DE INSCRIPCIÓN");
            $pdf->setPosicion("V");
            $pdf->AddPage();
            $pdf->AliasNbPages();
            $pdf->SetFont('Arial', '', 12);
            $pdf->Reglamento($alumno);

            return new Response($pdf->Output('reglamento.pdf', 'I'), 200, array('Content-Type' => 'application/pdf'));
            //}
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function impHistorialAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 3 || $session->get("rol") == 1 || $session->get("rol") == 99)) {
            $session->set("vista", "caja");
            //if ($request->getMethod() == "POST") {

            $em = $this->getDoctrine()->getEntityManager();

            $ideAlu = $request->get('ideAlu');


            $alumno = $em->getRepository('CmBundle:Alumnos')
                    ->findOneBy(array('ideAlu' => $ideAlu));

            $dql = "SELECT p "
                    . "FROM CmBundle:Pagos p "
                    . "WHERE p.cveAlu = :ideAlu ";
            //. "AND p.fecIni >= :fec";
            $query = $em->createQuery($dql);
            $query->setParameter('ideAlu', $ideAlu);
            //$query->setParameter('fec', $alumno->getFecMov());
            $pagos = $query->getResult();

            $pdf = new PDF('P', 'mm', 'Letter');
            $pdf->setTitulo("HISTORIAL DE PAGOS");
            $pdf->setPosicion("V");
            $pdf->AddPage();
            $pdf->AliasNbPages();
            $pdf->SetFont('Arial', '', 12);
            if ($alumno->getTipoAlumno() == 2) {
                $pagosConAbonos = array();
                foreach ($pagos as $key => $value) {
                    $listaPagos = $em->getRepository('CmBundle:Ingresos')
                            ->findBy(array('cveAlu' => $ideAlu, 'cvePag' => $value->getIdePag()));
                    $suma = 0;
                    foreach ($listaPagos as $k => $v) {
                        $suma = $suma + $v->getMonto();
                    }
                    $pagosConAbonos[$key] = array($value, $suma);
                }
                $listaIngresos = $em->getRepository('CmBundle:Ingresos')
                        ->findBy(array('cveAlu' => $ideAlu));
                $pdf->HistorialBachillerato($alumno, $pagosConAbonos, $listaIngresos);
            } else {
                $pdf->Historial($alumno, $pagos);
            }

            return new Response($pdf->Output('historialPagos.pdf', 'I'), 200, array('Content-Type' => 'application/pdf'));
            //}
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

}
