<?php

/**
 * actuacion_proceso actions.
 *
 * @package    cobro_coactivo
 * @subpackage actuacion_proceso
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class actuacion_procesoActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeList(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");

        /*
         * Paginacion
         */
        if (isset($request['_startRow'])) {
            $rowStart = $request['_startRow'];
        } else {
            $rowStart = 0;
        }

        if (isset($request['_endRow'])) {
            $rowEnd = $request['_endRow'];
        } else {
            $rowEnd = 75;
        }

        //ordenar consulta
        if (isset($request['_sortBy'])) {
            $sortBy = " Order by " . $request['_sortBy'];
        } else {
            $sortBy = 'order by tab0.orden';
        }

        try {

            $id_proceso_coactivo = $request['id_proceso_coactivo'];



            //sentencia sql
            $sql = <<<EOD
ALTER SESSION SET NLS_DATE_FORMAT = "YYYY-MM-DD"
EOD;
            //ejecuta y obtiene el resultado del sql
            CN_TABLE::executeSQL($sql);

            $sql = <<<EOD
select count(*) as count from (
                    
select 
tab0.*    
from(
    
    select 
    tab0.*,
    ROW_NUMBER() OVER (
        partition by tab0.estado
        ORDER BY tab0.orden 
    ) AS rn
   
    from cbr_actuacion_proc tab0
    
    where 
    
    tab0.estado = 1
    and ( 
        tab0.orden >= (
    
            select min(ordenar.orden) 
            from cbr_actuacion_proc ordenar 
            where not exists(
                select proc.id_actuacion_proc 
                
                from cbr_registro_etapa proc 
                
                where 
                proc.id_actuacion_proc = ordenar.id_actuacion_proc 
                and proc.estado = 1 
                and proc.id_proceso_coactivo = $id_proceso_coactivo
            )
            and ordenar.estado = 1
            and obligatoria = 1
            
        )
    )

)tab0

where
tab0.rn = 1 or tab0.obligatoria = 0
                    
)
EOD;

            $total_record_count = CN_TABLE::executeSQL($sql);
            $total_record_count = $total_record_count[0]["count"];


            $sql = <<<EOD
SELECT * FROM ( SELECT row_.*, ROWNUM rownum_ FROM (

select 
tab0.*    
from(
    
    select 
    tab0.*,
    ROW_NUMBER() OVER (
        partition by tab0.estado
        ORDER BY tab0.orden 
    ) AS rn
   
    from cbr_actuacion_proc tab0
    
    where 
    
    tab0.estado = 1
    and ( 
        tab0.orden >= (
    
            select min(ordenar.orden) 
            from cbr_actuacion_proc ordenar 
            where not exists(
                select proc.id_actuacion_proc 
                
                from cbr_registro_etapa proc 
                
                where 
                proc.id_actuacion_proc = ordenar.id_actuacion_proc 
                and proc.estado = 1 
                and proc.id_proceso_coactivo = $id_proceso_coactivo
            )
            and ordenar.estado = 1
            and obligatoria = 1
            
        )
    )

)tab0

where
tab0.rn = 1 or tab0.obligatoria = 0

$sortBy                
          
) row_ WHERE ROWNUM <=  $rowEnd   ) where rownum_ >  $rowStart
EOD;

            $resultado = CN_TABLE::executeSQL($sql);



            $this->totalRows = $total_record_count;
            $this->startRow = $rowStart;
            $this->endRow = $rowStart + count($resultado);
            $this->list_data = $resultado;
            $this->logMessage("[actuacion_proceso] Consulta exitosa.");
        } catch (Exception $e) {

            $this->status = 1;
            $this->totalRows = 0;
            $this->startRow = 0;
            $this->endRow = 0;
            $this->list_data = array();
            $this->logMessage("ERROR!!!");
            $this->logMessage($e->getMessage());
        }
    }

}
