<?php

/**
 * estado_legalizacion actions.
 *
 * @package    controlComisiones
 * @subpackage estado_legalizacion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class estado_legalizacionActions 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.no_comision, tab0.no_resolucion, tab0.no_reembolso, tab0.rubro, tab0.fecha_pago, tab0.no_pago ';
        }


        /*
         * Manejo de errores
         */
        try {
            /*
             * Variables obligatorias
             */
            $id_anhos = $request['id_anhos'];
            $fecha_ini = $request['fecha_ini'];
            $fecha_fin = $request['fecha_fin'];


            /*
             * Variables opcionales
             */
            //Caja Menor
            if (isset($request['id_proyecto'])) {
                $whereCajaMenor = " AND tab0.id_proyecto = " . $request['id_proyecto'];
            } else {
                $whereCajaMenor = ' ';
            }
            //area
            if (isset($request['id_area'])) {
                $whereArea = " AND tab0.id_area = " . $request['id_area'];
            } else {
                $whereArea = ' ';
            }
            $whereComisionado = "";
            if (isset($request['id_tercero'])) {
                $whereComisionado = " AND tab0.id_tercero = " . $request['id_tercero'];
            }
            $whereNoComision = "";
            if (isset($request['no_comision'])) {
                $whereNoComision = " AND tab0.no_comision = " . $request['no_comision'];
            }
            $whereNoResolucion = "";
            if (isset($request['no_resolucion'])) {
                $whereNoResolucion = " AND tab0.no_resolucion = " . $request['no_resolucion'];
            }
            $whereEstadoCumplido = "";
            if (isset($request['estado_cumplido'])) {
                $whereEstadoCumplido = " AND tab0.estado_cumplido = '" . $request['estado_cumplido'] . "'";
            }




            //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.*,
tab0.viaticos + tab0.otros as total
from(

    select 
    tab0.id_pago_egreso,
    tab4.no_reembolso,
    tab3.no_comision,
    tab2.no_resolucion,
    tab3.id_proyecto,
    tab0.id_anhos,
    tab3.id_area,
    tab1.id_tercero,
    tab6.num_doc,
    tab6.nombre,
    tab7.descripcion as dependencia,
    nvl(tab8.valor,0) as viaticos,
    nvl(tab9.valor,0) as otros,
    tab14.id_rubro,
    tab15.rubro,
    tab0.fecha_pago,
    tab0.no_pago,
    tab6.numero_cuenta,
    tab10.valor as valor_tiquete,
    
    case
    when tab3.estado=0 then 'ANULADO' else            
        case            
        when tab13.id_cierre is not null then 'CERRADO'
        when tab12.id_radicado is not null then 'RADICADO'
        when tab11.id_cumplido is not null then 'LEGALIZANDO'
        else 'PENDIENTE' end 
    end as estado_cumplido
    
    from com_pago_egreso tab0
    join com_cuenta tab1 on tab1.id_cuenta = tab0.id_cuenta and tab1.estado=1
    join com_resolucion tab2 on tab2.id_resolucion = tab1.id_resolucion and tab2.estado=1
    join com_comision tab3 on tab3.id_comision = tab2.id_comision
    
    left join(    
        select 
        tab0.id_cuenta,
        tab0.id_rubro,
        sum(tab0.valor_cuenta)    
        from com_deta_cuenta tab0 
        group by tab0.id_cuenta, tab0.id_rubro    
    )tab14 on tab14.id_cuenta = tab1.id_cuenta
    
    left join(
        select 
        deta.id_pago_egreso,
        reemb.no_reembolso,
        deta.id_rubro,
        sum(deta.valor_reembolso) as valor
        from com_deta_reembolso deta
        left join com_reembolso reemb on reemb.id_reembolso = deta.id_reembolso and reemb.estado=1 
        group by deta.id_pago_egreso, 
        reemb.no_reembolso, deta.id_rubro
    )tab4 on tab4.id_pago_egreso = tab0.id_pago_egreso
    
    left join com_tercero tab6 on tab6.id_tercero = tab1.id_tercero
    left join area tab7 on tab7.id_area = tab3.id_area
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor) as valor
        from com_comisionado_obj tab0
        join objetos_gasto tab1 on tab1.id = tab0.id_obj_gasto and tab1.viatico = 1
        group by tab0.id_comision,
        tab0.id_tercero    
    )tab8 on tab8.id_comision = tab3.id_comision and tab8.id_tercero = tab1.id_tercero
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor) as valor
        from com_comisionado_obj tab0
        join objetos_gasto tab1 on tab1.id = tab0.id_obj_gasto and tab1.viatico = 0
        group by tab0.id_comision,tab0.id_tercero    
    )tab9 on tab9.id_comision = tab3.id_comision and tab9.id_tercero = tab1.id_tercero
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor_tiquete) as valor    
        from com_tiquete tab0 
        group by tab0.id_comision, 
        tab0.id_tercero    
    )tab10 on tab10.id_comision = tab3.id_comision and tab10.id_tercero = tab1.id_tercero
    
    left join com_cumplido tab11 on tab11.id_pago_egreso = tab0.id_pago_egreso
    left join com_radicado tab12 on tab12.id_pago_egreso = tab0.id_pago_egreso
    left join com_cierre tab13 on tab13.id_pago_egreso = tab0.id_pago_egreso
    left join pr_rubro_presup_v tab15 on tab15.id_rubro = tab14.id_rubro

)tab0

where 
tab0.id_anhos = $id_anhos 
and tab0.fecha_pago between to_date( '$fecha_ini' , 'yyyy-mm-dd' ) and to_date( '$fecha_fin' , 'yyyy-mm-dd' )
$whereCajaMenor $whereArea 
$whereNoComision $whereNoResolucion                     
$whereComisionado $whereEstadoCumplido 
                    
)
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.*,
tab0.viaticos + tab0.otros as total
from(

    select 
    tab0.id_pago_egreso,
    tab4.no_reembolso,
    tab3.no_comision,
    tab2.no_resolucion,
    tab3.id_proyecto,
    tab0.id_anhos,
    tab3.id_area,
    tab1.id_tercero,
    tab6.num_doc,
    tab6.nombre,
    tab7.descripcion as dependencia,
    nvl(tab8.valor,0) as viaticos,
    nvl(tab9.valor,0) as otros,
    tab14.id_rubro,
    tab15.rubro,
    tab0.fecha_pago,
    tab0.no_pago,
    tab6.numero_cuenta,
    tab10.valor as valor_tiquete,
    
    case
    when tab3.estado=0 then 'ANULADO' else            
        case            
        when tab13.id_cierre is not null then 'CERRADO'
        when tab12.id_radicado is not null then 'RADICADO'
        when tab11.id_cumplido is not null then 'LEGALIZANDO'
        else 'PENDIENTE' end 
    end as estado_cumplido
    
    from com_pago_egreso tab0
    join com_cuenta tab1 on tab1.id_cuenta = tab0.id_cuenta and tab1.estado=1
    join com_resolucion tab2 on tab2.id_resolucion = tab1.id_resolucion and tab2.estado=1
    join com_comision tab3 on tab3.id_comision = tab2.id_comision
    
    left join(    
        select 
        tab0.id_cuenta,
        tab0.id_rubro,
        sum(tab0.valor_cuenta)    
        from com_deta_cuenta tab0 
        group by tab0.id_cuenta, tab0.id_rubro    
    )tab14 on tab14.id_cuenta = tab1.id_cuenta
    
    left join(
        select 
        deta.id_pago_egreso,
        reemb.no_reembolso,
        deta.id_rubro,
        sum(deta.valor_reembolso) as valor
        from com_deta_reembolso deta
        left join com_reembolso reemb on reemb.id_reembolso = deta.id_reembolso and reemb.estado=1 
        group by deta.id_pago_egreso, 
        reemb.no_reembolso, deta.id_rubro
    )tab4 on tab4.id_pago_egreso = tab0.id_pago_egreso
    
    left join com_tercero tab6 on tab6.id_tercero = tab1.id_tercero
    left join area tab7 on tab7.id_area = tab3.id_area
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor) as valor
        from com_comisionado_obj tab0
        join objetos_gasto tab1 on tab1.id = tab0.id_obj_gasto and tab1.viatico = 1
        group by tab0.id_comision,
        tab0.id_tercero    
    )tab8 on tab8.id_comision = tab3.id_comision and tab8.id_tercero = tab1.id_tercero
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor) as valor
        from com_comisionado_obj tab0
        join objetos_gasto tab1 on tab1.id = tab0.id_obj_gasto and tab1.viatico = 0
        group by tab0.id_comision,tab0.id_tercero    
    )tab9 on tab9.id_comision = tab3.id_comision and tab9.id_tercero = tab1.id_tercero
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor_tiquete) as valor    
        from com_tiquete tab0 
        group by tab0.id_comision, 
        tab0.id_tercero    
    )tab10 on tab10.id_comision = tab3.id_comision and tab10.id_tercero = tab1.id_tercero
    
    left join com_cumplido tab11 on tab11.id_pago_egreso = tab0.id_pago_egreso
    left join com_radicado tab12 on tab12.id_pago_egreso = tab0.id_pago_egreso
    left join com_cierre tab13 on tab13.id_pago_egreso = tab0.id_pago_egreso
    left join pr_rubro_presup_v tab15 on tab15.id_rubro = tab14.id_rubro

)tab0

where 
tab0.id_anhos = $id_anhos 
and tab0.fecha_pago between to_date( '$fecha_ini' , 'yyyy-mm-dd' ) and to_date( '$fecha_fin' , 'yyyy-mm-dd' )
$whereCajaMenor $whereArea 
$whereNoComision $whereNoResolucion                    
$whereComisionado $whereEstadoCumplido 
                    
$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("[estado_legalizacion] 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 tab0.no_comision, tab0.no_resolucion, tab0.no_reembolso, tab0.rubro, tab0.fecha_pago, tab0.no_pago ';
        }


        /*
         * Manejo de errores
         */
        try {
            /*
             * Variables obligatorias
             */
            $id_anhos = $request['id_anhos'];
            $fecha_ini = $request['fecha_ini'];
            $fecha_fin = $request['fecha_fin'];


            /*
             * Variables opcionales
             */
            //Caja Menor
            if (isset($request['id_proyecto'])) {
                $whereCajaMenor = " AND tab0.id_proyecto = " . $request['id_proyecto'];
            } else {
                $whereCajaMenor = ' ';
            }
            //area
            if (isset($request['id_area'])) {
                $whereArea = " AND tab0.id_area = " . $request['id_area'];
            } else {
                $whereArea = ' ';
            }
            $whereComisionado = "";
            if (isset($request['id_tercero'])) {
                $whereComisionado = " AND tab0.id_tercero = " . $request['id_tercero'];
            }
            $whereNoComision = "";
            if (isset($request['no_comision'])) {
                $whereNoComision = " AND tab0.no_comision = " . $request['no_comision'];
            }
            $whereNoResolucion = "";
            if (isset($request['no_resolucion'])) {
                $whereNoResolucion = " AND tab0.no_resolucion = " . $request['no_resolucion'];
            }
            $whereEstadoCumplido = "";
            if (isset($request['estado_cumplido'])) {
                $whereEstadoCumplido = " AND tab0.estado_cumplido = '" . $request['estado_cumplido'] . "'";
            }


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

'TOTALES: ' as no_reembolso,
sum(nvl(viaticos,0)) as viaticos,
sum(nvl(otros,0)) as otros,
sum(nvl(total,0)) as total,
sum(nvl(valor_tiquete,0)) as valor_tiquete

FROM (   

select 
tab0.*,
tab0.viaticos + tab0.otros as total
from(

    select 
    tab0.id_pago_egreso,
    tab4.no_reembolso,
    tab3.no_comision,
    tab2.no_resolucion,
    tab3.id_proyecto,
    tab0.id_anhos,
    tab3.id_area,
    tab1.id_tercero,
    tab6.num_doc,
    tab6.nombre,
    tab7.descripcion as dependencia,
    nvl(tab8.valor,0) as viaticos,
    nvl(tab9.valor,0) as otros,
    tab14.id_rubro,
    tab15.rubro,
    tab0.fecha_pago,
    tab0.no_pago,
    tab6.numero_cuenta,
    tab10.valor as valor_tiquete,
    
    case 
    when tab13.id_cierre is not null then 'CERRADO'
    when tab12.id_radicado is not null then 'RADICADO'
    when tab11.id_cumplido is not null then 'LEGALIZANDO'
    else 'PENDIENTE' end as estado_cumplido
    
    from com_pago_egreso tab0
    join com_cuenta tab1 on tab1.id_cuenta = tab0.id_cuenta
    join com_resolucion tab2 on tab2.id_resolucion = tab1.id_resolucion
    join com_comision tab3 on tab3.id_comision = tab2.id_comision
    
    left join(    
        select 
        tab0.id_cuenta,
        tab0.id_rubro,
        sum(tab0.valor_cuenta)    
        from com_deta_cuenta tab0 
        group by tab0.id_cuenta, tab0.id_rubro    
    )tab14 on tab14.id_cuenta = tab1.id_cuenta
    
    left join(
        select 
        deta.id_pago_egreso,
        reemb.no_reembolso,
        deta.id_rubro,
        sum(deta.valor_reembolso) as valor
        from com_deta_reembolso deta
        left join com_reembolso reemb on reemb.id_reembolso = deta.id_reembolso and reemb.estado=1 
        group by deta.id_pago_egreso, 
        reemb.no_reembolso, deta.id_rubro
    )tab4 on tab4.id_pago_egreso = tab0.id_pago_egreso
    
    left join com_tercero tab6 on tab6.id_tercero = tab1.id_tercero
    left join area tab7 on tab7.id_area = tab3.id_area
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor) as valor
        from com_comisionado_obj tab0
        join objetos_gasto tab1 on tab1.id = tab0.id_obj_gasto and tab1.viatico = 1
        group by tab0.id_comision,
        tab0.id_tercero    
    )tab8 on tab8.id_comision = tab3.id_comision and tab8.id_tercero = tab1.id_tercero
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor) as valor
        from com_comisionado_obj tab0
        join objetos_gasto tab1 on tab1.id = tab0.id_obj_gasto and tab1.viatico = 0
        group by tab0.id_comision,tab0.id_tercero    
    )tab9 on tab9.id_comision = tab3.id_comision and tab9.id_tercero = tab1.id_tercero
    
    left join(    
        select 
        tab0.id_comision,
        tab0.id_tercero,
        sum(tab0.valor_tiquete) as valor    
        from com_tiquete tab0 
        group by tab0.id_comision, 
        tab0.id_tercero    
    )tab10 on tab10.id_comision = tab3.id_comision and tab10.id_tercero = tab1.id_tercero
    
    left join com_cumplido tab11 on tab11.id_pago_egreso = tab0.id_pago_egreso
    left join com_radicado tab12 on tab12.id_pago_egreso = tab0.id_pago_egreso
    left join com_cierre tab13 on tab13.id_pago_egreso = tab0.id_pago_egreso
    left join pr_rubro_presup_v tab15 on tab15.id_rubro = tab14.id_rubro

)tab0

where 
tab0.id_anhos = $id_anhos 
and tab0.fecha_pago between to_date( '$fecha_ini' , 'yyyy-mm-dd' ) and to_date( '$fecha_fin' , 'yyyy-mm-dd' )
$whereCajaMenor $whereArea 
$whereNoComision $whereNoResolucion                    
$whereComisionado $whereEstadoCumplido 

) 
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());
        }
    }

}
