<?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\RazonSocial;

date_default_timezone_set('America/Mexico_City');

/**
 * Description of SocialController
 *
 * @author omar
 */
class SocialController extends Controller{
    //put your code here
    public function altaRsAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1)  ) {
            $session->set("vista", "social");
            if($request->getMethod() == "POST"){
                
                $em = $this->getDoctrine()->getManager();
                
                $rfc = $request->get('rfc');
                $razonSocial = $request->get('rs');
                $domFis = $request->get('domFis');
                $colLoc = $request->get('colLoc');
                $munDel = $request->get('munDel');
                $edo = $request->get('edo');
                $lic = $request->get('cveLic');
                $nomRectora = $request->get('nomRectora');
                $nomDirector = $request->get('nomDirector');
                
                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                        ->findOneBy(array('ideLic' => $lic));
                
                $razon = new RazonSocial();
                $razon->setRfc($rfc);
                $razon->setRazonSocial($razonSocial);
                $razon->setDomFis($domFis);
                $razon->setColLoc($colLoc);
                $razon->setMunDel($munDel);
                $razon->setEdo($edo);
                //$razon->setCveLic($cveLic);
                $razon->setContador(1);
                $razon->setNomRectora($nomRectora);
                $razon->setNomDirector($nomDirector);
                
                $razon->setFecMov(new \DateTime(date('Y-m-d') ) );
                $razon->setCveUsu( $session->get('identificador') );
                
                $em = $this->getDoctrine()->getManager();
                $em->persist($razon);
                $em->flush();
                
                return $this->redirect($this->generateUrl('consulta_rs'));
            }else{
                // Renderizar formulario de RS
                $em = $this->getDoctrine()->getManager();
                
                $licenciaturas = $em->getRepository('CmBundle:Licenciaturas')->findAll();
                return $this->render('CmBundle:Rs:alta-rs.html.twig',
                        array('licenciaturas' => $licenciaturas) );
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }
    
    public function consultaRsAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1)  ) {
            $session->set("vista", "social");
            
            $em = $this->getDoctrine()->getManager();
            
            $rs = $em->getRepository('CmBundle:RazonSocial')->findAll();
            return $this->render('CmBundle:Rs:consulta-rs.html.twig',array('rs' => $rs) );
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }
    
}
