<?php

/**
 * vwebsafi_proyectos actions.
 *
 * @package    costeoConocCient
 * @subpackage vwebsafi_proyectos
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class vwebsafi_proyectosActions extends sfActions {

    /**
     * Executes 
     * Incidente #39085, cambio de servidor a SIAPPI - 20250505::LAVILA 
     *
     * @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");

        /**
         * Abrir nueva conexion con la Base de Datos
         */
        $this->conn = Doctrine_Manager::getInstance()->getCurrentConnection();
        $this->conn->beginTransaction();

        try {


            /*
             *  Borrar datos anteriores 
             */
            //Sql
            $sql = <<<EOD
DELETE FROM CCG_PROYECTOS  
EOD;
            //Ejecutar SQL
            CN_TABLE::executeSQL($sql);

            //Sql
            $sql = <<<EOD
SELECT DISTINCT 
PROJECTKEY,
PROJECTNAME,
SGCCODE,
to_char(SCHEDULESTART,'YYYY-MM-DD') as SCHEDULESTART,
to_char(SCHEDULEFINISH,'YYYY-MM-DD') as SCHEDULEFINISH,
DURATION,
initcap(CONOCGEOCIENTIFICO) as CONOCGEOCIENTIFICO
FROM
SIAPPI2.V_WEBSAFI_PROYECTOS                
order by
SGCCode      
EOD;
            //Ejecutar SQL
            $records = CN_TABLE::executeOracle_connect($sql);

            $id_rownum = 1;

            foreach ($records as $value) {

                //var_dump($value);
                //die();

                $projectkey = $value['PROJECTKEY'];
                $projectName = $value['PROJECTNAME'];
                $sgccode = str_replace(" ", "", $value['SGCCODE']);
                $schedulestart = $value['SCHEDULESTART'];
                $schedulefinish = $value['SCHEDULEFINISH'];
                $duration = $value['DURATION'];
                $conocgeocientifico = $value['CONOCGEOCIENTIFICO'];

                //Sql - para los recursos de planview
                $sql = <<<EOD
Insert into CCG_PROYECTOS (ID_PROYECTOS,PROJECTKEY,PROJECTNAME,SGCCODE,
    SCHEDULESTART,SCHEDULEFINISH,DURATION,CONOCGEOCIENTIFICO) 
values ('$id_rownum','$projectkey','$projectName','$sgccode',
    to_date('$schedulestart','YYYY-MM-DD'),to_date('$schedulefinish','YYYY-MM-DD'),'$duration','$conocgeocientifico') 
EOD;
                //Ejecutar SQL
                $this->conn->execute($sql);

                $id_rownum++;

                //break;
            }

            /*
             * COMMIT DE TODAS LAS TRANSACCIONES
             */
            $this->conn->commit();

            /*
             * TRAE LOS REGISTROS
             */
            //codigo_proy
            $whereCodigoProy = " ";
            if (isset($request['codigo_proy'])) {
                $whereCodigoProy = " and tab0.sgccode = '" . $request['codigo_proy'] . "'";
            }

            //Sql
            $sql = <<<EOD
SELECT 
tab0.* 
FROM ccg_proyectos tab0

where
1 = 1
$whereCodigoProy    
                    
order by
tab0.SGCCode    
EOD;
            //Ejecutar SQL
            $records = CN_TABLE::executeSQL($sql);

            $this->status = 0;
            $this->list_data = $records;
            $this->logMessage("[vwebsafi_proyectos] Termino consulta.");
        } catch (Exception $e) {

            /**
             * ROLLBACK DE TODAS LAS TRANSACCIONES EN CASO DE QUE OCURRA ALGUN ERROR
             */
            $this->conn->rollback();

            /**
             * Respuesta del servicio
             */
            $this->status = 1;
            $this->mensajeError = "[vwebsafi_proyectos] Ocurrio un error: " . $e->getMessage();
            $this->list_data = array();
            $this->logMessage("[vwebsafi_proyectos] ERROR!");
            $this->logMessage($e->getMessage());
        }
    }

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

        /**
         * Abrir nueva conexion con la Base de Datos
         */
        $this->conn = Doctrine_Manager::getInstance()->getCurrentConnection();
        $this->conn->beginTransaction();

        try {


            //Sql
            $sql = <<<EOD
SELECT 
* 
FROM dbo.VWebSAFI_Proyectos                  
order by
SGCCode    
EOD;
            //Ejecutar SQL
//            $records = CN_TABLE::executeMssql_connect($sql);

            /*
             *  Borrar datos anteriores 
             */
            //Sql
            $sql = <<<EOD
DELETE FROM CCG_PROYECTOS  
EOD;
            //Ejecutar SQL
            CN_TABLE::executeSQL($sql);

            //Sql
            $sql = <<<EOD
SELECT 
ProjectKey,
ProjectName,
SGCCode,
CONVERT(VARCHAR(10), ScheduleStart, 120) as ScheduleStart,
CONVERT(VARCHAR(10), ScheduleFinish, 120) as ScheduleFinish,
Duration,
ConocGeocientifico 
FROM dbo.VWebSAFI_Proyectos                  
order by
SGCCode     
EOD;
            //Ejecutar SQL
            $records = CN_TABLE::executeMssql_connect($sql);

            $id_rownum = 1;

            foreach ($records as $value) {

                //var_dump($value);
                //die();

                $projectkey = $value['ProjectKey'];
                $projectName = $value['ProjectName'];
                $sgccode = str_replace(" ", "", $value['SGCCode']);
                $schedulestart = $value['ScheduleStart'];
                $schedulefinish = $value['ScheduleFinish'];
                $duration = $value['Duration'];
                $conocgeocientifico = $value['ConocGeocientifico'];

                //Sql - para los recursos de planview
                $sql = <<<EOD
Insert into CCG_PROYECTOS (ID_PROYECTOS,PROJECTKEY,PROJECTNAME,SGCCODE,
    SCHEDULESTART,SCHEDULEFINISH,DURATION,CONOCGEOCIENTIFICO) 
values ('$id_rownum','$projectkey','$projectName','$sgccode',
    to_date('$schedulestart','YYYY-MM-DD'),to_date('$schedulefinish','YYYY-MM-DD'),'$duration','$conocgeocientifico') 
EOD;
                //Ejecutar SQL
                $this->conn->execute($sql);

                $id_rownum++;

                //break;
            }

            /*
             * COMMIT DE TODAS LAS TRANSACCIONES
             */
            $this->conn->commit();

            /*
             * TRAE LOS REGISTROS
             */
            //codigo_proy
            $whereCodigoProy = " ";
            if (isset($request['codigo_proy'])) {
                $whereCodigoProy = " and tab0.sgccode = '" . $request['codigo_proy'] . "'";
            }

            //Sql
            $sql = <<<EOD
SELECT 
tab0.* 
FROM ccg_proyectos tab0

where
1 = 1
$whereCodigoProy    
                    
order by
tab0.SGCCode    
EOD;
            //Ejecutar SQL
            $records = CN_TABLE::executeSQL($sql);

            $this->status = 0;
            $this->list_data = $records;
            $this->logMessage("[vwebsafi_proyectos] Termino consulta.");
        } catch (Exception $e) {

            /**
             * ROLLBACK DE TODAS LAS TRANSACCIONES EN CASO DE QUE OCURRA ALGUN ERROR
             */
            $this->conn->rollback();

            /**
             * Respuesta del servicio
             */
            $this->status = 1;
            $this->mensajeError = "[vwebsafi_proyectos] Ocurrio un error: " . $e->getMessage();
            $this->list_data = array();
            $this->logMessage("[vwebsafi_proyectos] ERROR!");
            $this->logMessage($e->getMessage());
        }
    }
}
