<?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 Symfony\Component\HttpFoundation\JsonResponse;
use CmBundle\Controller\PDFBachillerato;
use Picqer\Barcode\BarcodeGeneratorPNG;

date_default_timezone_set('America/Mexico_City');

/**
 * Description of BachilleratoController
 *
 * @author Omar
 */
class BachilleratoController extends Controller {

    //put your code here

    public function impFichaAction(Request $request) {
        $session = $request->getSession();
        if (($session->has('ideAlu'))| ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1 || $session->get("rol") == 3 || $session->get("rol") == 4 || $session->get("rol") == 101 )) ) {

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

            $em = $this->getDoctrine()->getManager();
            $alumno = $em->getRepository('CmBundle:Alumnos')
                    ->findOneBy(array('ideAlu' => $ideAlu));
            $pagos = $em->getRepository('CmBundle:Pagos')
                    ->findBy(array('cveAlu' => $alumno->getIdeAlu()));
            $plantel = $em->getRepository('CmBundle:Planteles')
                    ->findOneBy(array('idePla' => $alumno->getCvePla()));
            
            $generator = new BarcodeGeneratorPNG();
            $imagenBarra = "data:image/png;base64," . base64_encode($generator->getBarcode($alumno->getCveCtl(), $generator::TYPE_CODE_128));
            $codigoBarras = $this->base64_to_png($imagenBarra, 'barras/' . $alumno->getCveCtl() . '.png');

            $pdf = new PDFBachillerato('P', 'mm', 'Letter');
            $pdf->AddPage();
            //$pdf->AddFont('Calibri', '', 'calibri.php');
            $pdf->SetFont('Helvetica');
            $pdf->fichaBachilleratoPaginaUno($alumno);
            $pdf->AddPage();
            $pdf->fichaBachilleratoPaginaDos($alumno);
            if ($alumno->getTipoAlumno() == 2) {
                $pdf->AddPage();
                $pdf->OficioAplicacion($alumno, $pagos, $codigoBarras);
                $pdf->AddPage();
                $pdf->CartaCompromiso($alumno, $pagos);
                $pdf->AddPage();
                $pdf->ResguardoDocumentos($alumno,$plantel);
            }


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

    public function impComprobanteAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1 || $session->get("rol") == 3 || $session->get("rol") == 4 || $session->get("rol") == 99)) {
            $idePag = $request->get('idePag');
            $ideAlu = $request->get('ideAlu');

            $em = $this->getDoctrine()->getManager();
            $alumno = $em->getRepository('CmBundle:Alumnos')
                    ->findOneBy(array('ideAlu' => $ideAlu));
            $pago = $em->getRepository('CmBundle:Pagos')
                    ->findOneBy(array('idePag' => $idePag));
            $listaPagos = $em->getRepository('CmBundle:Ingresos')
                    ->findBy(array('cveAlu' => $ideAlu, 'cvePag' => $idePag));

            $generator = new BarcodeGeneratorPNG();
            $imagenBarra = "data:image/png;base64," . base64_encode($generator->getBarcode($alumno->getCveCtl(), $generator::TYPE_CODE_128));
            $codigoBarras = $this->base64_to_png($imagenBarra, 'barras/' . $alumno->getCveCtl() . '.png');

            $pdf = new PDFBachillerato('P', 'mm', 'letter');
            $pdf->banRecibo = 1;
            $pdf->AddPage();
            //$pdf->AddFont('Calibri', '', 'calibri.php');
            $pdf->SetFont('Helvetica');
            $pdf->ComprobanteDePagoFinalizado($alumno, $pago, $listaPagos, $codigoBarras);

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

    public function costosEuAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1 || $session->get("rol") == 3 || $session->get("rol") == 4 )) {
            $ideBecBac = $request->get('ideBecBac');

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

            $inscripcion = $em->getRepository('CmBundle:ConceptoPagosBachillerato')
                    ->findOneBy(array('cveBeca' => $ideBecBac, 'concepto' => "INSCRIPCIÓN"));
            $dql = "SELECT SUM(c.monto) costo "
                    . "FROM CmBundle:ConceptoPagosBachillerato c "
                    . "WHERE c.concepto <> 'INSCRIPCIÓN' "
                    . "AND c.cveBeca = :clave";
            $query = $em->createQuery($dql);
            $query->setParameter('clave', $ideBecBac);
            $costoExamen = $query->getResult();

            return new JsonResponse(array('inscripcion' => $inscripcion->getMonto(), 'examen' => $costoExamen[0]['costo']), 200);
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    function base64_to_png($base64_string, $output_file) {
        // open the output file for writing
        $ifp = fopen($output_file, 'wb');

        // split the string on commas
        // $data[ 0 ] == "data:image/png;base64"
        // $data[ 1 ] == <actual base64 string>
        $data = explode(',', $base64_string);

        // we could add validation here with ensuring count( $data ) > 1
        fwrite($ifp, base64_decode($data[1]));

        // clean up the file resource
        fclose($ifp);

        return $output_file;
    }

}
