<?php

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

    /**
     * Executes list 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");

        /*
         * Manejar Errores
         */
        try {


            //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);

            if (isset($request['id_username'])) {
                $id_username = $request['id_username'];
            }

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

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

            //ordenar consulta
            if (isset($request['_sortBy'])) {
                $sortBy = " Order by " . $request['_sortBy'];
            } else {
                $sortBy = 'order by det.anho_remision, det.numero_remision, det.cod_titulo ';
            }

            /*
             * Filtros
             */
            $whereRemision = "";
            if (isset($request['id_remision_titulo'])) {
                $whereRemision = " AND det.id_remision_titulo = " . $request['id_remision_titulo'];
            }

            $whereRemisionIN = "";
            if (isset($request['id_remision_titulo_in'])) {
                $whereRemisionIN = " AND det.id_remision_titulo in (" . $request['id_remision_titulo_in'] . ")";
            }

            //input
            $whereInput = "";

            /*
             * Ejemplo:JKL-11561
             * Consulta de resolucioes con filtro de varias remisiones
             * Solo la ultima recepcion por titulo - 20220607::LAVILA
             */
            $consulta = <<<EOD
select
det.*
from(
    
    select distinct
                    
    det.id_remision_titulo,
    det.id_remision_titulo_det,
    det.cod_titulo,
    det.cod_titulo as titulo_minero,
    det.numero_remision,
    det.anho_remision,
    det.numero_remision || ' - ' || det.anho_remision as completo_remision,
    det.fecha_remision,
    det.observaciones,
    det.numero_remision || ' - ' || det.anho_remision || ' - ' || to_char(det.fecha_remision,'dd/mm/yyyy') as completo_deta_text,
    ROW_NUMBER() OVER (
        partition by det.cod_titulo
        ORDER BY det.anho_remision desc, det.numero_remision desc
    ) AS rn

    from cbr_remision_titulo_det_v det

    where
    1 = 1
    $whereRemision                     
    $whereRemisionIN  
    $whereInput   

)det
    
where
det.rn = 1      
EOD;

            /*
             * sql que trae los resultados
             * por los filtros, para totalizar las filas
             */

            $sql = <<<EOD
select count(*) as count from (

$consulta
                    
)
EOD;

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


            /*
             * Obtiene los registros de debito/vencimiento con su saldo respectivo
             */
            $sql = <<<EOD

SELECT * FROM ( SELECT row_.*, ROWNUM rownum_ FROM (

$consulta
                        
$sortBy                        

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

            //ejecuta y obtiene el resultado del sql
            $records = CN_TABLE::executeSQL($sql);


            $this->totalRows = $total_record_count;
            $this->startRow = $rowStart;
            $this->endRow = $rowStart + count($records);
            $this->list_data = $records;
            $this->logMessage("[remision_detalle_cc] 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());
        }
    }
}
