<?php
define( 'SF_ROOT_DIR', "../../" );
//define( 'SF_ROOT_DIR', realpath( dirname( __FILE__ ) ) );
define( 'SF_APP',      'sys' );
require_once( SF_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'configGlobal.php' );

ini_set( 'mysql.connect_timeout', 300 );
ini_set( 'default_socket_timeout', 300 ); 

date_default_timezone_set ( "America/Lima" );

ini_set( 'memory_limit', '1024M' );
//ini_set( 'max_execution_time', 5000 );

ini_set( 'max_execution_time', 0 );
ini_set( 'log_errors', 'off' );
error_reporting( E_ALL & ~E_NOTICE );

$s_CodigoCliente = "20601925240";
$s_Contrasenia   = "ragatec0902";
$s_CodEcommerce  = "100008";

try
{
    $o_DBConnect = Conexion::getConexion();
    $cn = $o_DBConnect->getResource();

    mysql_query( "SET @@session.wait_timeout=1800", $cn );

    pr( "Inicia actualizacion de Stock [".date( "H:i:s" )."]" );
    //AHORA VAMOS A IMPORTAR EL STOCK
    $Contenido   = file_get_contents( "http://ghardsoft.com/webservice/ext/communication_bridge.php?type=stock&limit=99999" );
    $a_Contenido = json_decode( $Contenido, true );
    $a_Contenido = array_shift( $a_Contenido );
    
    if( isset( $a_Contenido['error'] ) ) { if( $a_Contenido['error'] ){ throw new Exception( $a_Contenido['message'] ); } }
    
    $a_Items     = $a_Contenido['Items'];
    foreach ( $a_Items as $cod => $val )
    {
        $s_Sql = "UPDATE exp_producto SET stock='$val' WHERE codigo='$cod';";
        mysql_query( $s_Sql, $cn );
        pr( "Update: [C-$cod] [Q-$val] " );
    }
    
    pr( "Fin de Carga Stock [".date( "H:i:s" )."]" );
}
catch ( Exception $e )
{
    pr( $e );
}