<?php
date_default_timezone_set ( "America/Lima" );

error_reporting(E_ALL);
ini_set('display_errors','1'); //PARA PRODUCCION "Off"

ini_set("allow_url_fopen", 1);
if (ini_get("allow_url_fopen") == 1) {
echo "allow_url_fopen is ON<br>";
} else {
echo "allow_url_fopen is OFF<br>";
}
print ini_get("allow_url_fopen");

function curl( $url )
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $return = curl_exec($ch);
    curl_close ($ch);
    return $return;
}

//$cn = mysql_connect( "localhost", "root", "" );
$cn = mysql_connect( "localhost", "micaela_user", "MHWinv2854624" );
mysql_select_db( "micaela_database" );

function RecuperarDia( $s_Anio, $s_Mes, $s_Dia, &$a_TpCam )
{
    //$s_RespuestaContenido = file_get_contents( "http://code.staffsystems.us/webservices/tipo-de-cambio/serverside.php?work=get_sbs&anho=$s_Anio&mes=$s_Mes&dia=$s_Dia" );
    $s_RespuestaContenido = curl( "http://code.staffsystems.us/webservices/tipo-de-cambio/serverside.php?work=get_sbs&anho=$s_Anio&mes=$s_Mes&dia=$s_Dia" );
    
    $a_Result = json_decode( $s_RespuestaContenido, true );
    $b_Control = true;
    if( isset( $a_Result['success'] ) )
    {
        if( isset( $a_Result['data'] ) )
        {
            $a_rs = array_shift( $a_Result['data'] );
            //$a_TpCam[] = $a_rs;
            echo "<pre>"; print_r( $a_rs ); echo "</pre>";
            
            $b_Res1 = strpos( $a_rs['compra'], "Noexiste" );
            $b_Res2 = strpos( $a_rs['venta'], "Noexiste" );
            
            if( $b_Res1 !== false || $b_Res2 !== false )
            {
                $b_Control = false;
            }
        }
        else 
        {
            $b_Control = false;
        }
    }
    else 
    {
        $b_Control = false;
    }
    
    if( !$b_Control )
    {
        $i_Dia = ( int ) $s_Dia;
        if( $i_Dia == 1 )
        {
            $i_Dia = 33;
            
            $i_Mes = ( int ) $s_Mes;
            $i_Mes = $i_Mes - 1;
            $s_Mes = str_pad( $i_Mes, 2, "0", STR_PAD_LEFT );
        }
        $i_Dia = $i_Dia - 1;
        $s_Dia = str_pad( $i_Dia, 2, "0", STR_PAD_LEFT );
        $a_rs = RecuperarDia( $s_Anio, $s_Mes, $s_Dia, $a_TpCam );
    }
    
    return $a_rs;
}

$s_Fecha  = date( "Y-m-d" );
$s_Dia   = date( "d" );
$s_Mes   = date( "m" );
$s_Anio  = date( "Y" );
$a_TpCam = array();

$a_Result = RecuperarDia( $s_Anio, $s_Mes, $s_Dia, $a_TpCam );

asort ( $a_TpCam );

if( $a_Result )
{
    $s_Sql = "SELECT IdCompania, Codigo, DescripcionCorta, DescripcionLarga FROM GSS_Compania;";
    $rs = mysql_query( $s_Sql );
    $cnt = mysql_num_rows( $rs );
    if( $cnt > 0 )
    {
        while ( $a_row = mysql_fetch_array( $rs, MYSQL_ASSOC ) ) 
        {
            //foreach ( $a_TpCam as $i_Key => $a_Valores )
            //{
                //$s_Fecha = $a_Valores['fecha'];
            	$IdCompania = $a_row['IdCompania'];
            	
            	$SunatCompra = $a_Result['compra'];
            	$SunatVenta  = $a_Result['venta'];
            	
            	$s_Sql = "
                            REPLACE INTO GSS_TipoCambio 
                            (IdMoneda, IdMonedaCambio, IdFechaCambio, IdCompania, SunatCompra, SunatVenta, TipoCambioCobranza, Estado, FechaCreacion, UsuarioCreacion) 
                            VALUES
                            ('EX', 'LO', '$s_Fecha', $IdCompania, '$SunatCompra', '$SunatVenta', '$SunatVenta', 'A', NOW(), 'cron');
            	           ";
            	mysql_query( $s_Sql );
            	echo "<pre>"; print_r( $IdCompania." - ".$s_Fecha." -> OK" ); echo "</pre>";
            //}
        }
    }
}