<?php

/**
 * ci_det_tabla_obl actions.
 *
 * @package    nomina
 * @subpackage ci_det_tabla_obl
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ci_det_tabla_oblActions 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");

        /*
         * 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.tipo_desc ';
        }



        /*
         * Manejo de errores
         */
        try {
            /*
             * Variables obligatorias
             */
            $periodo_filtro = $request['id_periodo_filtro'];
            $id_obligacion = $request['id_obligacion'];
            $secuencia = $request['secuencia'];
            $id_vigencia = $request['id_vigencia'];
            $ano_fiscal = $request['ano_fiscal'];

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


            /**
             * Consulta Estado de la Cuenta - 20160808
             */
            $sql = <<<EOD
select count(*) as count from (

select tab0.* 
from(

    select
    1 as tipo_desc,
    'Radicado' as descripcion,
    obl.id_cpx as numero,
    null as secuencia,
    cxp.fecha_proceso as fecha,
    nvl(cxp.valor_cxp,0) as valor
    from ts_obligacion obl    
    
    left join ts_cxp cxp on obl.id_cpx = cxp.id_cxp  
    and obl.id_vigencia = cxp.id_vigencia 
    and obl.ano_fiscal = cxp.ano_fiscal
    
    where
    obl.id_obligacion = $id_obligacion
    and obl.id_vigencia = $id_vigencia
    and obl.ano_fiscal = $ano_fiscal
    
    union

    select
    2 as tipo_desc,
    'Obligación' as descripcion,
    obl.id_obligacion as numero,
    obl.secuencia as secuencia,
    obl.fecha_proceso as fecha,
    nvl(oval.valor_obligacion,0) as valor
    from ts_obligacion obl    
        
    left join 
    (
        select oval.id_obligacion,oval.secuencia,oval.id_vigencia,oval.ano_fiscal,
        sum(oval.valor_obligacion) as valor_obligacion
        from ts_obligacion_val oval group by oval.id_obligacion, oval.secuencia, oval.id_vigencia, oval.ano_fiscal 
    )oval on obl.id_obligacion = oval.id_obligacion
    and obl.secuencia = oval.secuencia
    and obl.ano_fiscal = oval.ano_fiscal
    and obl.id_vigencia = oval.id_vigencia
                    
    where
    obl.id_obligacion = $id_obligacion
    and obl.id_vigencia = $id_vigencia
    and obl.ano_fiscal = $ano_fiscal
    
    union

    select
    4 as tipo_desc,
    'Orden de Pago' as descripcion,
    pag.nro_orden_pago as numero,
    pag.secuencia as secuencia,
    emi.fecha_proc as fecha,
    nvl(pag.valor,0) as valor
    from ts_obligacion obl    
    
    left join ts_pago_obl pag
    on obl.id_obligacion = pag.id_obligacion 
    and obl.secuencia = pag.secuencia 
    and obl.ano_fiscal = pag.vigencia 
    and obl.id_vigencia = pag.id_vigencia
    and pag.estado is null
    
    left join ts_emision_pago emi
    on emi.id_emision_pago = pag.id_emision_pago
    
    where
    obl.id_obligacion = $id_obligacion
    and obl.id_vigencia = $id_vigencia
    and obl.ano_fiscal = $ano_fiscal
                    
)tab0
   
)
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 (
    
select tab0.* 
from(

    select
    1 as tipo_desc,
    'Radicado' as descripcion,
    obl.id_cpx as numero,
    null as secuencia,
    cxp.fecha_proceso as fecha,
    nvl(cxp.valor_cxp,0) as valor
    from ts_obligacion obl    
    
    left join ts_cxp cxp on obl.id_cpx = cxp.id_cxp  
    and obl.id_vigencia = cxp.id_vigencia 
    and obl.ano_fiscal = cxp.ano_fiscal
    
    where
    obl.id_obligacion = $id_obligacion
    and obl.id_vigencia = $id_vigencia
    and obl.ano_fiscal = $ano_fiscal
    
    union

    select
    2 as tipo_desc,
    'Obligación' as descripcion,
    obl.id_obligacion as numero,
    obl.secuencia as secuencia,
    obl.fecha_proceso as fecha,
    nvl(oval.valor_obligacion,0) as valor
    from ts_obligacion obl    
        
    left join 
    (
        select oval.id_obligacion,oval.secuencia,oval.id_vigencia,oval.ano_fiscal,
        sum(oval.valor_obligacion) as valor_obligacion
        from ts_obligacion_val oval group by oval.id_obligacion, oval.secuencia, oval.id_vigencia, oval.ano_fiscal 
    )oval on obl.id_obligacion = oval.id_obligacion
    and obl.secuencia = oval.secuencia
    and obl.ano_fiscal = oval.ano_fiscal
    and obl.id_vigencia = oval.id_vigencia
                    
    where
    obl.id_obligacion = $id_obligacion
    and obl.id_vigencia = $id_vigencia
    and obl.ano_fiscal = $ano_fiscal
    
    union

    select
    4 as tipo_desc,
    'Orden de Pago' as descripcion,
    pag.nro_orden_pago as numero,
    pag.secuencia as secuencia,
    emi.fecha_proc as fecha,
    nvl(pag.valor,0) as valor
    from ts_obligacion obl    
    
    left join ts_pago_obl pag
    on obl.id_obligacion = pag.id_obligacion 
    and obl.secuencia = pag.secuencia 
    and obl.ano_fiscal = pag.vigencia 
    and obl.id_vigencia = pag.id_vigencia
    and pag.estado is null
    
    left join ts_emision_pago emi
    on emi.id_emision_pago = pag.id_emision_pago
    
    where
    obl.id_obligacion = $id_obligacion
    and obl.id_vigencia = $id_vigencia
    and obl.ano_fiscal = $ano_fiscal
                    
)tab0
                    
$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("[ci_det_tabla_obl] 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());
        }
    }

}
