<?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 CmBundle\Entity\Becas;

date_default_timezone_set('America/Mexico_City');

/**
 * Description of BecasController
 *
 * @author omar
 */
class BecasController extends Controller{
    //put your code here
    public function altaBecasAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1)  ) {
            $session->set("vista", "beca");
            if($request->getMethod() == "POST"){
                
                $em = $this->getDoctrine()->getManager();
                
                $cveBec = $request->get('cveBec');
                $desBec = $request->get('desBec');
                $monto = $request->get('monto');
                $tipo = $request->get('estatus');
                
                $estatus = $em->getRepository('CmBundle:Estatus')
                        ->findOneBy(array('ideEst' => $tipo));
                
                $beca = new Becas();
                $beca->setCveBec($cveBec);
                $beca->setDesBec($desBec);
                $beca->setMonto($monto);
                $beca->setEstatus($estatus);
                $beca->setFecMov(new \DateTime(date('Y-m-d') ) );
                $beca->setCveUsu( $session->get('identificador') );
                
                $em = $this->getDoctrine()->getManager();
                $em->persist($beca);
                $em->flush();
                
                return $this->redirect($this->generateUrl('consulta_becas'));
            }else{
                // Renderizar formulario de Estatus
                $em = $this->getDoctrine()->getManager();
            
                $estatus = $em->getRepository('CmBundle:Estatus')->findAll();
                return $this->render('CmBundle:Becas:alta-beca.html.twig',array('estatus' => $estatus));
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }
    
    public function consultaBecasAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1)  ) {
            $session->set("vista", "beca");
            
            $em = $this->getDoctrine()->getManager();
            
            $becas = $em->getRepository('CmBundle:Becas')->findAll();
            return $this->render('CmBundle:Becas:consulta-beca.html.twig',array('becas' => $becas) );
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }
    
}
