<?php

/**
 * com_cronograma actions.
 *
 * @package    control comisiones
 * @subpackage com_cronograma
 * @author     Manuel Enrique Paredes Lopez
 * @version    SVN: $Id: actions.class.php 23810 2016-11-12 11:07:44Z Kris.Wallsmith $ 
 */
class com_cronogramaActions extends sfActions {

    /**
     * 
     * @param sfWebRequest $request
     */
    public function executePdf(sfWebRequest $request) {

        //Revisar que sea post
        //$this->forward404Unless($request->isMethod('post'));
        //Guardar los parametros del request en el log
        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("--------------------------------------------------* Ip: " . $_SERVER['REMOTE_ADDR']);
        foreach ($request->getGetParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("---END Logging request");

        try {
            /*
             * Formato de fecha
             */
            //Sql
            $sql = <<<EOD
ALTER SESSION SET NLS_DATE_FORMAT = "YYYY-MM-DD" 
EOD;
            //Ejecutar SQL
            CN_TABLE::executeSQL($sql);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }

    /**
     * 
     * @param sfWebRequest $request
     */
    public function executeExcel(sfWebRequest $request) {



        //Revisar que sea post
        //$this->forward404Unless($request->isMethod('post'));
        //Guardar los parametros del request en el log
        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("--------------------------------------------------* Ip: " . $_SERVER['REMOTE_ADDR']);
        foreach ($request->getGetParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        $this->logMessage("---END Logging request");


        $headfirst_caste = array(
            "LUNES",
            "MARTES",
            "MIERCOLES",
            "JUEVES",
            "VIERNES",
            "SABADO",
            "DOMINGO"
        );

         $headfirst_ingles = array(
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"
        );


        $id_anhos = $request['id_anho'];
        $id_meses = $request['id_mes'];

        if ($id_meses>0 && $id_meses<10) {
          
          $id_meses = "0".$id_meses;

        }
        
        $num_dias = cal_days_in_month(CAL_GREGORIAN,$id_meses,$id_anhos);




        try {

            //Cronograma
            $id_cronograma = null;
            $whereCronograma = "";
            if (isset($request['id'])) {
                $id_cronograma = $request['id'];
                $whereCronograma = " AND tab0.id_cronograma = '" . $request['id'] . "'";
            }

            //Sql
            $sql = <<<EOD
select tab0.id_veh,
       tab0.id_cronograma,
       tab0.id_ciudad,
       tab0.cod_veh,
       tab0.placa,
       tab0.id_tipo_servicio,
       tab0.usuario_log,
       tab1.consecutivo as consecutivo,
       to_char(tab1.fecha_log,'yyyy') as ano_solicitud,
       to_char(tab1.fecha_log,'MM') as mes_solicitud,
       to_char(tab1.fecha_log,'dd') as dia_solicitud,
       tab2.municipio as municipio
from com_vehiculo_cronograma tab0
left join com_trans_contratado_prog tab1 ON to_char(tab0.placa)=to_char(tab1.placa_vehiculo_asig)
left join municipios tab2 ON tab0.id_ciudad=tab2.codmun 
where 1=1 $whereCronograma
EOD;



            //Ejecutar SQL
            $columnas = CN_TABLE::executeSQL($sql);



            $html = '<table border="1">';

            $html .= '<tr> ';
                 $html .= '<td> Vigencia:'.$id_anhos.' </td>';
                 $html .= '<td> Mes:'.$id_meses.' </td>';
                 $html .= '<td> Informe Cronograma de Transporte Terrestre </td>';


            $html .= '</tr> ';


            $html .= '<tr> ';
               $html .= '<td>  </td>';
               $html .= '<td>  </td>';
            for ($i=1; $i<$num_dias+1 ; $i++) { 
              
              $jd = cal_to_jd(CAL_GREGORIAN,$id_meses,$i,$id_anhos);
              $dia_semana=jddayofweek($jd,1);
              $c=0;
              $dia_semana_esp = "";
              foreach ($headfirst_ingles as $value) {
                if ($value==$dia_semana) {
                  $dia_semana_esp = $headfirst_caste[$c];
                }
                $c++;
              }
               $html .= '<td>'.$dia_semana_esp.'</td>';              

            }
            $html .= '</tr> ';

            $html .= '<tr>';
              $html .= '<td>CIUDAD</td>';
              $html .= '<td>CODIGO VEHICULO </td>';

              for ($v=1; $v < $num_dias+1 ; $v++) { 
                $html .= '<td>'.$v.'/'.$id_meses.'/'.$id_anhos.'</td>';
              }


            $html .= '</tr> ';

            foreach ($columnas as $value) {
              $html .= '<tr>';
              $html .= '<td>'.$value['municipio'].'</td>';
              $html .= '<td>'.$value['cod_veh'].'</td>';
              $dia = $value['dia_solicitud'];
              $mes = $value['mes_solicitud'];
              $ano = $value['ano_solicitud'];
              for ($b=1; $b < $num_dias+1; $b++) { 
                
                if ($dia==$b && $mes==$id_meses && $ano==$id_anhos) {
                    $html .= '<td>'.$value['consecutivo'].'</td>';
                  }else{
                    $html .= '<td></td>';
                  }  
              }
              $html .= '</tr> ';  

                          
            }            
            





            $html .= '</table>';


            $nombre = "Cronograma.xls";
            header("Content-type: application/vnd.ms-excel; name='excel'");
            header("Content-Disposition: filename=" . $nombre);
            header("Pragma: no-cache");
            header("Expires: 0");
            echo $html;
            die();
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            die();
        }
    }






}
