<?php

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


        /*
         * Manejo de errores
         */
        try {
            /*
             * Variables obligatorias
             */
            $periodo_filtro = $request['id_periodo_filtro'];
            $periodo = $request['id_periodo'];

            /*
             * Variables opcionales
             */
            //funcionario
            if (isset($request['num_doc'])) {
                $whereFuncionarioNumDoc = " AND tab0.num_doc = " . $request['num_doc'];
            } else {
                $whereFuncionarioNumDoc = ' ';
            }
            //funcionario
            if (isset($request['id_tipo_doc'])) {
                $whereFuncionarioTipoDoc = " AND tab0.id_tipo_doc = " . $request['id_tipo_doc'];
            } else {
                $whereFuncionarioTipoDoc = ' ';
            }
            //Clase Fondo
            if (isset($request['id_clase_fondo'])) {
                $whereClaseFondo = " AND tab0.id_clase_fondo = " . $request['id_clase_fondo'];
            } else {
                $whereClaseFondo = ' ';
            }
            //MaestroFondos
            if (isset($request['id_mae_fondos'])) {
                $whereMaestroFondos = " AND tab0.id_mae_fondos = " . $request['id_mae_fondos'];
            } else {
                $whereMaestroFondos = ' ';
            }
            //clase planta
            if (isset($request['id_clase_planta'])) {
                $whereClasePlanta = " AND tab0.id_clase_planta = " . $request['id_clase_planta'];
            } else {
                $whereClasePlanta = ' ';
            }

            //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 que trae los resultados
             * por los filtros, para totalizar las filas
             * Se toman solo aportes tipo A - 20140825
             */
            $sql = <<<EOD
select count(*) as count from (

select 
count(distinct tab0.num_doc) as cantidad,
count(clafon.codigo) as cantidad_clafon,
tab0.id_periodo,tab0.id_clase_fondo,
clafon.codigo as codigo_clafon,
clafon.descripcion,
clafon.codigo || ' - ' || clafon.descripcion as completo_clafon,
tab0.id_mae_fondos,
tab0.codigo, tab0.fondo,
tab0.num_doc_mae
,sum( nvl( tab0.APORTE_FUNC_PENSION ,0) ) APORTE_FUNC_PENSION
,sum( nvl( tab0.APORTE_PATRONO_PENSION ,0) ) APORTE_PATRONO_PENSION
,sum( nvl( tab0.TOTAL_PENSION ,0) ) TOTAL_PENSION
,sum( nvl( tab0.APORTE_FUNC_SALUD ,0) ) APORTE_FUNC_SALUD
,sum( nvl( tab0.APORTE_PATRONO_SALUD ,0) ) APORTE_PATRONO_SALUD
,sum( nvl( tab0.TOTAL_SALUD ,0) ) TOTAL_SALUD
,sum( nvl( tab0.UPC_ADICIO ,0) ) UPC_ADICIO
,sum( nvl( tab0.APORTE_PATRONO_ARP ,0) ) APORTE_PATRONO_ARP
,sum( nvl( tab0.TOTAL_ARP ,0) ) TOTAL_ARP
,sum( nvl( tab0.PARAFISCALES ,0) ) PARAFISCALES
,sum( nvl( tab0.TOTAL_EMPLEADO ,0) ) TOTAL_EMPLEADO
,sum( nvl( tab0.TOTAL_APORTES ,0) ) TOTAL_APORTES,
(select distinct periodo.periodo from no_liquida_v periodo where periodo.id_periodo = tab0.id_periodo) AS PERIODO,
(select distinct periodo.tipper from no_liquida_v periodo where periodo.id_periodo = tab0.id_periodo) AS TIPPER,
sum( nvl( tab0.APORTE_PATRONO_PENSION ,0) ) + sum( nvl( tab0.APORTE_PATRONO_SALUD ,0) ) + sum( nvl( tab0.APORTE_PATRONO_ARP ,0) ) + sum( nvl( tab0.PARAFISCALES ,0) ) total

    FROM(
    
    SELECT
    tab3.id_periodo,tab3.id_clase_fondo,
    tab3.id_mae_fondos,
    tab3.codigo,tab3.fondo,
    tab3.num_doc,
    tab3.num_doc_mae,
    tab3.APORTE_FUNC_PENSION,
    tab3.APORTE_PATRONO_PENSION,
    tab3.TOTAL_PENSION,
    tab3.APORTE_FUNC_SALUD,
    tab3.APORTE_PATRONO_SALUD,
    tab3.TOTAL_SALUD,
    tab3.UPC_ADICIO,
    tab3.APORTE_PATRONO_ARP,
    tab3.TOTAL_ARP,
    tab3.PARAFISCALES,
    ( nvl( APORTE_FUNC_PENSION ,0)  +  nvl( APORTE_FUNC_SALUD ,0) ) AS TOTAL_EMPLEADO,
    ( nvl( tab3.TOTAL_PENSION ,0)  +  nvl( tab3.TOTAL_SALUD ,0)  +  nvl( tab3.TOTAL_ARP ,0) +  nvl( tab3.PARAFISCALES ,0) ) AS TOTAL_APORTES
    
        FROM(
        
        select 
        tab1.id_periodo as id_periodo, 
        tab1.id_clase_fondo,
        tab2.id_mae_fondos,
        tab2.codigo as codigo,
        tab1.num_doc,
        tab2.num_doc as num_doc_mae,
        tab2.razon_social AS FONDO,
        case when tab1.id_clase_fondo =1 then tab1.aporte_func
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_FUNC_PENSION,
        case when tab1.id_clase_fondo =1 then tab1.aporte_patrono + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_PATRONO_PENSION,
        case when tab1.id_clase_fondo =1 then tab1.aporte_patrono+tab1.aporte_func + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS TOTAL_PENSION,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_func
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_FUNC_SALUD,
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_patrono + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_PATRONO_SALUD,
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_patrono+tab1.aporte_func+tab1.upc_adicio + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS TOTAL_SALUD,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.upc_adicio
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS UPC_ADICIO,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then tab1.aporte_patrono
        else 0 end AS APORTE_PATRONO_ARP,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then tab1.aporte_patrono
        else 0 end AS TOTAL_ARP,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else tab1.aporte_patrono end AS PARAFISCALES
        
        
        from no_liquida_aporte tab1
        
        left join no_mae_fondos tab2 ON tab2.id_mae_fondos = tab1.id_mae_fondos
        
        left join (

           select nom.*,
           case when tab.num_doc is not null then 1 else 0 end as pago
           from no_nombramiento_v nom

           left join (select distinct
           pago.id_tipo_doc,
           pago.num_doc 
           from no_pago_func pago
           where
           (select fecha_fin from no_periodo where id_periodo = $periodo_filtro) between pago.fecha_activacion and nvl(pago.fecha_terminacion, (select fecha_fin from no_periodo where id_periodo = $periodo_filtro))
           )tab 
           on tab.id_tipo_doc = nom.id_tipo_doc and tab.num_doc = nom.num_doc

        )tab0
        on tab0.id_tipo_doc = tab1.id_tipo_doc and tab0.num_doc = tab1.num_doc  
                    
        where
        tab1.tipo_planilla = 'A'   
        $whereClasePlanta
    
    )tab3

)tab0

join no_clase_fondo clafon
on tab0.id_clase_fondo = clafon.id_clase_fondo

where tab0.id_periodo = $periodo_filtro
$whereClaseFondo $whereMaestroFondos

group by tab0.id_periodo, 
tab0.id_clase_fondo, clafon.codigo,
clafon.descripcion, 
tab0.id_mae_fondos,  
tab0.num_doc_mae,
tab0.codigo, tab0.fondo
        
)
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 
count(distinct tab0.num_doc) as cantidad,
count(clafon.codigo) as cantidad_clafon,
tab0.id_periodo,tab0.id_clase_fondo,
clafon.codigo as codigo_clafon,
clafon.descripcion,
clafon.codigo || ' - ' || clafon.descripcion as completo_clafon,
tab0.id_mae_fondos,
tab0.codigo, tab0.fondo,
tab0.num_doc_mae
,sum( nvl( tab0.APORTE_FUNC_PENSION ,0) ) APORTE_FUNC_PENSION
,sum( nvl( tab0.APORTE_PATRONO_PENSION ,0) ) APORTE_PATRONO_PENSION
,sum( nvl( tab0.TOTAL_PENSION ,0) ) TOTAL_PENSION
,sum( nvl( tab0.APORTE_FUNC_SALUD ,0) ) APORTE_FUNC_SALUD
,sum( nvl( tab0.APORTE_PATRONO_SALUD ,0) ) APORTE_PATRONO_SALUD
,sum( nvl( tab0.TOTAL_SALUD ,0) ) TOTAL_SALUD
,sum( nvl( tab0.UPC_ADICIO ,0) ) UPC_ADICIO
,sum( nvl( tab0.APORTE_PATRONO_ARP ,0) ) APORTE_PATRONO_ARP
,sum( nvl( tab0.TOTAL_ARP ,0) ) TOTAL_ARP
,sum( nvl( tab0.PARAFISCALES ,0) ) PARAFISCALES
,sum( nvl( tab0.TOTAL_EMPLEADO ,0) ) TOTAL_EMPLEADO
,sum( nvl( tab0.TOTAL_APORTES ,0) ) TOTAL_APORTES,
(select distinct periodo.periodo from no_liquida_v periodo where periodo.id_periodo = tab0.id_periodo) AS PERIODO,
(select distinct periodo.tipper from no_liquida_v periodo where periodo.id_periodo = tab0.id_periodo) AS TIPPER,
sum( nvl( tab0.APORTE_PATRONO_PENSION ,0) ) + sum( nvl( tab0.APORTE_PATRONO_SALUD ,0) ) + sum( nvl( tab0.APORTE_PATRONO_ARP ,0) ) + sum( nvl( tab0.PARAFISCALES ,0) ) total

    FROM(
    
    SELECT
    tab3.id_periodo,tab3.id_clase_fondo,
    tab3.id_mae_fondos,
    tab3.codigo,tab3.fondo,
    tab3.num_doc,
    tab3.num_doc_mae,
    tab3.APORTE_FUNC_PENSION,
    tab3.APORTE_PATRONO_PENSION,
    tab3.TOTAL_PENSION,
    tab3.APORTE_FUNC_SALUD,
    tab3.APORTE_PATRONO_SALUD,
    tab3.TOTAL_SALUD,
    tab3.UPC_ADICIO,
    tab3.APORTE_PATRONO_ARP,
    tab3.TOTAL_ARP,
    tab3.PARAFISCALES,
    ( nvl( APORTE_FUNC_PENSION ,0)  +  nvl( APORTE_FUNC_SALUD ,0) ) AS TOTAL_EMPLEADO,
    ( nvl( tab3.TOTAL_PENSION ,0)  +  nvl( tab3.TOTAL_SALUD ,0)  +  nvl( tab3.TOTAL_ARP ,0) +  nvl( tab3.PARAFISCALES ,0) ) AS TOTAL_APORTES
    
        FROM(
        
        select 
        tab1.id_periodo as id_periodo, 
        tab1.id_clase_fondo,
        tab2.id_mae_fondos,
        tab2.codigo as codigo,
        tab1.num_doc,
        tab2.num_doc as num_doc_mae,
        tab2.razon_social AS FONDO,
        case when tab1.id_clase_fondo =1 then tab1.aporte_func
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_FUNC_PENSION,
        case when tab1.id_clase_fondo =1 then tab1.aporte_patrono + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_PATRONO_PENSION,
        case when tab1.id_clase_fondo =1 then tab1.aporte_patrono+tab1.aporte_func + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS TOTAL_PENSION,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_func
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_FUNC_SALUD,
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_patrono + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_PATRONO_SALUD,
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_patrono+tab1.aporte_func+tab1.upc_adicio + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS TOTAL_SALUD,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.upc_adicio
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS UPC_ADICIO,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then tab1.aporte_patrono
        else 0 end AS APORTE_PATRONO_ARP,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then tab1.aporte_patrono
        else 0 end AS TOTAL_ARP,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else tab1.aporte_patrono end AS PARAFISCALES
        
        
        from no_liquida_aporte tab1
        
        left join no_mae_fondos tab2 ON tab2.id_mae_fondos = tab1.id_mae_fondos
                            
        left join (

           select nom.*,
           case when tab.num_doc is not null then 1 else 0 end as pago
           from no_nombramiento_v nom

           left join (select distinct
           pago.id_tipo_doc,
           pago.num_doc 
           from no_pago_func pago
           where
           (select fecha_fin from no_periodo where id_periodo = $periodo_filtro) between pago.fecha_activacion and nvl(pago.fecha_terminacion, (select fecha_fin from no_periodo where id_periodo = $periodo_filtro))
           )tab 
           on tab.id_tipo_doc = nom.id_tipo_doc and tab.num_doc = nom.num_doc

        )tab0
        on tab0.id_tipo_doc = tab1.id_tipo_doc and tab0.num_doc = tab1.num_doc  
                    
        where
        tab1.tipo_planilla = 'A'   
        $whereClasePlanta   
    
    )tab3

)tab0

join no_clase_fondo clafon
on tab0.id_clase_fondo = clafon.id_clase_fondo

where tab0.id_periodo = $periodo_filtro
$whereClaseFondo $whereMaestroFondos

group by tab0.id_periodo, 
tab0.id_clase_fondo, clafon.codigo,
clafon.descripcion, 
tab0.id_mae_fondos,  
tab0.num_doc_mae,
tab0.codigo, tab0.fondo
    
$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("[res_autoliquida_consol] 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());
        }
    }

    /**
     * Metodo para sumar 
     *
     * @param sfWebRequest $request
     */
    public function executeSum(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 codigo ';
        }

        //manejo de errores
        try {
            /*
             * Variables obligatorias
             */
            $periodo_filtro = $request['id_periodo_filtro'];
            $periodo = $request['id_periodo'];

            /*
             * Variables opcionales
             */
            //funcionario
            if (isset($request['num_doc'])) {
                $whereFuncionarioNumDoc = " AND tab0.num_doc = " . $request['num_doc'];
            } else {
                $whereFuncionarioNumDoc = ' ';
            }
            //funcionario
            if (isset($request['id_tipo_doc'])) {
                $whereFuncionarioTipoDoc = " AND tab0.id_tipo_doc = " . $request['id_tipo_doc'];
            } else {
                $whereFuncionarioTipoDoc = ' ';
            }
            //Clase Fondo
            if (isset($request['id_clase_fondo'])) {
                $whereClaseFondo = " AND tab0.id_clase_fondo = " . $request['id_clase_fondo'];
            } else {
                $whereClaseFondo = ' ';
            }
            //MaestroFondos
            if (isset($request['id_mae_fondos'])) {
                $whereMaestroFondos = " AND tab0.id_mae_fondos = " . $request['id_mae_fondos'];
            } else {
                $whereMaestroFondos = ' ';
            }
            //clase planta
            if (isset($request['id_clase_planta'])) {
                $whereClasePlanta = " AND tab0.id_clase_planta = " . $request['id_clase_planta'];
            } else {
                $whereClasePlanta = ' ';
            }

            //obtener el listado de recaudos
            $sql = <<<EOD
SELECT

'Total: ' || count(*) as fondo,
SUM(aporte_func_pension)  as aporte_func_pension,
SUM(aporte_patrono_pension)  as aporte_patrono_pension,
SUM(total_pension)  as total_pension,
SUM(aporte_func_salud)  as aporte_func_salud,
SUM(aporte_patrono_salud)  as aporte_patrono_salud,
SUM(upc_adicio)  as upc_adicio,
SUM(total_salud)  as total_salud,
SUM(total_arp)  as total_arp,
SUM(parafiscales)  as parafiscales,
SUM(total_aportes)  as total_aportes

FROM (  

select 
count(distinct tab0.num_doc) as cantidad,
count(clafon.codigo) as cantidad_clafon,
tab0.id_periodo,tab0.id_clase_fondo,
clafon.codigo as codigo_clafon,
clafon.descripcion,
clafon.codigo || ' - ' || clafon.descripcion as completo_clafon,
tab0.id_mae_fondos,
tab0.codigo, tab0.fondo,
tab0.num_doc_mae
,sum( nvl( tab0.APORTE_FUNC_PENSION ,0) ) APORTE_FUNC_PENSION
,sum( nvl( tab0.APORTE_PATRONO_PENSION ,0) ) APORTE_PATRONO_PENSION
,sum( nvl( tab0.TOTAL_PENSION ,0) ) TOTAL_PENSION
,sum( nvl( tab0.APORTE_FUNC_SALUD ,0) ) APORTE_FUNC_SALUD
,sum( nvl( tab0.APORTE_PATRONO_SALUD ,0) ) APORTE_PATRONO_SALUD
,sum( nvl( tab0.TOTAL_SALUD ,0) ) TOTAL_SALUD
,sum( nvl( tab0.UPC_ADICIO ,0) ) UPC_ADICIO
,sum( nvl( tab0.APORTE_PATRONO_ARP ,0) ) APORTE_PATRONO_ARP
,sum( nvl( tab0.TOTAL_ARP ,0) ) TOTAL_ARP
,sum( nvl( tab0.PARAFISCALES ,0) ) PARAFISCALES
,sum( nvl( tab0.TOTAL_EMPLEADO ,0) ) TOTAL_EMPLEADO
,sum( nvl( tab0.TOTAL_APORTES ,0) ) TOTAL_APORTES,
(select distinct periodo.periodo from no_liquida_v periodo where periodo.id_periodo = tab0.id_periodo) AS PERIODO,
(select distinct periodo.tipper from no_liquida_v periodo where periodo.id_periodo = tab0.id_periodo) AS TIPPER,
sum( nvl( tab0.APORTE_PATRONO_PENSION ,0) ) + sum( nvl( tab0.APORTE_PATRONO_SALUD ,0) ) + sum( nvl( tab0.APORTE_PATRONO_ARP ,0) ) + sum( nvl( tab0.PARAFISCALES ,0) ) total

    FROM(
    
    SELECT
    tab3.id_periodo,tab3.id_clase_fondo,
    tab3.id_mae_fondos,
    tab3.codigo,tab3.fondo,
    tab3.num_doc,
    tab3.num_doc_mae,
    tab3.APORTE_FUNC_PENSION,
    tab3.APORTE_PATRONO_PENSION,
    tab3.TOTAL_PENSION,
    tab3.APORTE_FUNC_SALUD,
    tab3.APORTE_PATRONO_SALUD,
    tab3.TOTAL_SALUD,
    tab3.UPC_ADICIO,
    tab3.APORTE_PATRONO_ARP,
    tab3.TOTAL_ARP,
    tab3.PARAFISCALES,
    ( nvl( APORTE_FUNC_PENSION ,0)  +  nvl( APORTE_FUNC_SALUD ,0) ) AS TOTAL_EMPLEADO,
    ( nvl( tab3.TOTAL_PENSION ,0)  +  nvl( tab3.TOTAL_SALUD ,0)  +  nvl( tab3.TOTAL_ARP ,0) +  nvl( tab3.PARAFISCALES ,0) ) AS TOTAL_APORTES
    
        FROM(
        
        select 
        tab1.id_periodo as id_periodo, 
        tab1.id_clase_fondo,
        tab2.id_mae_fondos,
        tab2.codigo as codigo,
        tab1.num_doc,
        tab2.num_doc as num_doc_mae,
        tab2.razon_social AS FONDO,
        case when tab1.id_clase_fondo =1 then tab1.aporte_func
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_FUNC_PENSION,
        case when tab1.id_clase_fondo =1 then tab1.aporte_patrono + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_PATRONO_PENSION,
        case when tab1.id_clase_fondo =1 then tab1.aporte_patrono+tab1.aporte_func + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS TOTAL_PENSION,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_func
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_FUNC_SALUD,
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_patrono + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS APORTE_PATRONO_SALUD,
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.aporte_patrono+tab1.aporte_func+tab1.upc_adicio + tab1.aporte_patrono_licnorem
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS TOTAL_SALUD,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then tab1.upc_adicio
        when tab1.id_clase_fondo = 3 then 0
        else 0 end AS UPC_ADICIO,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then tab1.aporte_patrono
        else 0 end AS APORTE_PATRONO_ARP,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then tab1.aporte_patrono
        else 0 end AS TOTAL_ARP,
        
        case when tab1.id_clase_fondo =1 then 0
        when tab1.id_clase_fondo = 2 then 0
        when tab1.id_clase_fondo = 3 then 0
        else tab1.aporte_patrono end AS PARAFISCALES
        
        
        from no_liquida_aporte tab1
        
        left join no_mae_fondos tab2 ON tab2.id_mae_fondos = tab1.id_mae_fondos
                            
        left join (

           select nom.*,
           case when tab.num_doc is not null then 1 else 0 end as pago
           from no_nombramiento_v nom

           left join (select distinct
           pago.id_tipo_doc,
           pago.num_doc 
           from no_pago_func pago
           where
           (select fecha_fin from no_periodo where id_periodo = $periodo_filtro) between pago.fecha_activacion and nvl(pago.fecha_terminacion, (select fecha_fin from no_periodo where id_periodo = $periodo_filtro))
           )tab 
           on tab.id_tipo_doc = nom.id_tipo_doc and tab.num_doc = nom.num_doc

        )tab0
        on tab0.id_tipo_doc = tab1.id_tipo_doc and tab0.num_doc = tab1.num_doc  
                    
        where
        tab1.tipo_planilla = 'A'   
        $whereClasePlanta   
    
    )tab3

)tab0

join no_clase_fondo clafon
on tab0.id_clase_fondo = clafon.id_clase_fondo

where tab0.id_periodo = $periodo_filtro
$whereClaseFondo $whereMaestroFondos      

group by tab0.id_periodo, 
tab0.id_clase_fondo, clafon.codigo,
clafon.descripcion, 
tab0.id_mae_fondos,  
tab0.num_doc_mae,
tab0.codigo, tab0.fondo
    
$sortBy

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

            $this->totalRows = count($resultado);
            $this->startRow = 0;
            $this->endRow = count($resultado);
            $this->list_data = $resultado;
        } 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());
        }
    }

}
