<?php

/**
 * chequeo_documentos_remis actions.
 *
 * @package    cobro_coactivo
 * @subpackage chequeo_documentos_remis
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class chequeo_documentos_remisActions 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 descripcion';
        }

        try {

            $id_remision_titulo_det = $request['id_remision_titulo_det'];

            //$this->logMessage("ID_MODALIDAD_SEL".$id_modalidad_sel);
            //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);

            
            /**
             * and tab0.aplica_radicacion = 1 se agrega - 20200505::LAVILA
             */
            $sql = <<<EOD
select count(*) as count from (

    select 
    tab0.*
    from cbr_documentos tab0

    where not exists (
        select 
        chequeo.id_chequeo_documentos, 
        docum.descripcion 
        from cbr_chequeo_documentos chequeo 
        left join cbr_documentos docum ON docum.id_documentos = chequeo.id_documentos
        where chequeo.id_remision_titulo_det = $id_remision_titulo_det and docum.id_documentos = tab0.id_documentos and docum.descripcion!='Otros'
    )
                    
    and tab0.aplica_radicacion = 1

)
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 cbr_documentos tab0

    where not exists (
        select chequeo.id_chequeo_documentos, docum.descripcion 
        from cbr_chequeo_documentos chequeo 
        left join cbr_documentos docum ON docum.id_documentos = chequeo.id_documentos
        where chequeo.id_remision_titulo_det = $id_remision_titulo_det and docum.id_documentos = tab0.id_documentos and docum.descripcion!='Otros'
    )
                    
    and tab0.aplica_radicacion = 1
                    
    $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("[chequeo_documento] 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());
        }
    }

}
