<?php

/**
 * Incidente #32207 - 20221025::LAVILA
 * validar_pago_primero actions.
 *
 * @package    radicacionVirtualCuentas
 * @subpackage validar_pago_primero
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class validar_pago_primeroActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeList(sfWebRequest $request) {

        //Guardar los parametros del request en el log
        $this->logMessage("---BEGIN Logging request");
        foreach ($request->getPostParameters() as $key => $value) {
            $this->logMessage("request[$key] = $value");
        }
        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 fecha_radicado, radicado';
        }
        $sortBy = ' ';



        /*
         * Manejo de errores
         */
        try {
            /*
             * Variables obligatorias 
             * Incidente #37914, se deja con el firmado del formato pago - 20240402::LAVILA
             */
            $id_contratista = $request['id_contratista'];

            $consulta_sql = <<<EOD
select
* 
from rvc_formato_pago
where
id_contrato in ( select id_contrato from rvc_contrato where num_contrato in (

    select
    num_contrato
    from rvc_contrato
    where
    id_contratista = $id_contratista
    and estado = 1

)and id_contratista = $id_contratista)
--and id_numero_pago = 1
--and aprobado = 1
and firmado = 1
and estado = 1
EOD;

            /*
             * Sql que trae los resultados
             * por los filtros, para totalizar las filas
             */
            $sql = <<<EOD
select count(*) as count from (
                    
    $consulta_sql    
                    
)
EOD;


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


            //obtener el listado de recaudos
            $sql = <<<EOD
SELECT * FROM ( SELECT row_.*, ROWNUM rownum_ FROM (

    $consulta_sql

    $sortBy
          
) row_ WHERE ROWNUM <=  $rowEnd   ) where rownum_ >  $rowStart
EOD;
            //ejecuta y obtiene el resultado del sql
            $resultado = CN_TABLE::executeSQL($sql);


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

}
