<?php

/**
 * novedad_embargo_form_pago actions.
 *
 * @package    radicacionVirtualCuentas
 * @subpackage novedad_embargo_form_pago
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class novedad_embargo_form_pagoActions 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 novemb.id_contrato, novemb.fecha_embargo ';
        }


        /*
         * Manejo de errores
         */
        try {

            $fecha_proceso = CN_TABLE::fechaSistema();

            /*
             * Variables obligatorias
             */
            $id_contrato = $request['id_contrato'];

            $consulta_sql = <<<EOD
select
novemb.*,

case when novemb.valor_mensual < novemb.valor_total_saldo  
    then novemb.valor_mensual else novemb.valor_total_saldo end as valor_embargo
    
from rvc_novedad_embargo_v novemb

where
novemb.estado = 1
and novemb.id_contrato = $id_contrato 
and novemb.valor_total_saldo > 0                      
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->status = 0;
            $this->totalRows = $total_record_count;
            $this->startRow = $rowStart;
            $this->endRow = $rowStart + count($resultado);
            $this->list_data = $resultado;
            $this->logMessage("[novedad_embargo_form_pago] 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());
        }
    }

    /**
     * Executes update action
     *
     * @param sfRequest $request A request object
     */
    public function executeUpdate(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");

        /*
         * Manejo de errores
         */
        try {

            $this->conn = Doctrine_Manager::getInstance()->getCurrentConnection();

            $fecha_proceso = CN_TABLE::fechaSistema();

            /*
             * Variables obligatorias
             */
            $id_novedad_embargo = $request['id_novedad_embargo'];
            $motivo_anu = $request['motivo_anu'];

            //Actualiza estado de rvc_novedad_embargo
            $sql = <<<EOD
update rvc_novedad_embargo 
set estado = 0,
fecha_anu = sysdate,
motivo_anu = '$motivo_anu'
where
id_novedad_embargo = $id_novedad_embargo
and estado = 1                     
EOD;

            $this->conn->execute($sql);

            $this->status = 0;
            $this->totalRows = 1;
            $this->startRow = 0;
            $this->endRow = 0;
            $this->list_data = array();
            $this->logMessage("[novedad_embargo_form_pago] 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());
        }
    }

}
