<?php

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

            /*
             * Variables opcionales
             */
            //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
             */
            $sql = <<<EOD
select count(*) as count from (

select 
tab0.id_periodo,tab0.id_clase_fondo,
tab0.id_mae_fondos,
tab0.CODIGO, tab0.FONDO
,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

FROM(

SELECT
tab3.id_periodo,tab3.id_clase_fondo,
tab3.id_mae_fondos,
tab3.CODIGO,tab3.FONDO,
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,
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

where tab0.id_periodo = $periodo_filtro
$whereClaseFondo $whereMaestroFondos

group by tab0.id_periodo, tab0.id_clase_fondo, tab0.id_mae_fondos, 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 
tab0.id_periodo,tab0.id_clase_fondo,
tab0.id_mae_fondos,
tab0.CODIGO, tab0.FONDO
,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

FROM(

SELECT
tab3.id_periodo,tab3.id_clase_fondo,
tab3.id_mae_fondos,
tab3.CODIGO,tab3.FONDO,
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,
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

where tab0.id_periodo = $periodo_filtro
$whereClaseFondo $whereMaestroFondos

group by tab0.id_periodo, tab0.id_clase_fondo, tab0.id_mae_fondos, 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 = ' ';
            }

            /*
             * Variables opcionales
             */
            //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 
tab0.id_periodo,tab0.id_clase_fondo,
tab0.id_mae_fondos,
tab0.CODIGO, tab0.FONDO
,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

FROM(

SELECT
tab3.id_periodo,tab3.id_clase_fondo,
tab3.id_mae_fondos,
tab3.CODIGO,tab3.FONDO,
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,
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

where tab0.id_periodo = $periodo_filtro
$whereClaseFondo $whereMaestroFondos

group by tab0.id_periodo, tab0.id_clase_fondo, tab0.id_mae_fondos, 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());
        }
    }

}
