<?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  = "100018";

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

    mysql_query( "SET @@session.wait_timeout=1800", $cn );
    
    $item_id = "";
    if( isset( $_REQUEST['item_id'] ) && $_REQUEST['item_id'] != "" )
    {
        $item_id = $_REQUEST['item_id'];
    }
    
    $s_Where = "";
    if( $item_id ) $s_Where = " and codigo='$item_id' ";

    pr( "Inicia actualizacion de Item [".date( "H:i:s" )."]" );
    //AHORA REGISTRAMOS EL ITEM
    $s_Sql = "SELECT * FROM corporac_hardsoft.exp_producto where 1=1 and estado=1  $s_Where   ;";
    $rs = mysql_query( $s_Sql, $cn );
    $i = 1;
    while ( $row = mysql_fetch_array( $rs, MYSQL_ASSOC ) )
    {
        //continue;
        if ( substr( $cod, 0, 2 ) == "ZZ" ){ continue; }
        
        $nombre            = addslashes( $row['nombre'] );
        //$descripcion_largo = addslashes( $row['TechInfoLarge'] );
        //$descripcion_corto = addslashes( $row['TechInfoSmall'] );
        
        $s_Codigo        = $row['codigo'];
        $s_IdProduct     = $row['id_product'];
        
        $s_TipoProc      = $s_IdProduct ? "act" : "nvo";
        
        $s_IdFamilia     = $row['linea_prod_id'];
        $s_IdLinea       = $row['item_tipo_id'];
        $s_IdMarca       = $row['marca_id'];
        $s_CodigoInterno = $row['minicodigo'];
        $s_numero_parte  = $row['numero_parte'];
        $s_DescripItem   = $nombre;
        $f_CostoItem     = 0.00;

        $f_PrecCostoDol  = $row['precio_dol'];
        $f_PrecCostoSol  = $row['precio_sol'];
        
        if( $s_TipoProc == "act" )
        {
            ParameterWR::getInstance()->setVar( "i_Id", $s_IdProduct );
        }
        ParameterWR::getInstance()->setVar( "IdFamilia", $s_IdFamilia );
        ParameterWR::getInstance()->setVar( "IdLinea", $s_IdLinea );
        ParameterWR::getInstance()->setVar( "IdMarca", $s_IdMarca );
        ParameterWR::getInstance()->setVar( "IdProcedencia", "LOC" );
        
        ParameterWR::getInstance()->setVar( "CodigoInterno", $s_CodigoInterno );
        ParameterWR::getInstance()->setVar( "IdItemTipo", "01" );
        ParameterWR::getInstance()->setVar( "DescripcionCompleta", $s_DescripItem );
        ParameterWR::getInstance()->setVar( "UnidadCompra", "58" );
        ParameterWR::getInstance()->setVar( "UnidadVenta", "58" );
        ParameterWR::getInstance()->setVar( "UnidadAlmacenaje", "58" );
        ParameterWR::getInstance()->setVar( "Estado", "A" );
        ParameterWR::getInstance()->setVar( "IdPersona", "30" );

        ParameterWR::getInstance()->setVar( "NumeroDeParte", $s_numero_parte );
        ParameterWR::getInstance()->setVar( "CodigoBarras2", $s_Codigo );
        
        
        ParameterWR::getInstance()->setVar( "PrecioCosto", $f_CostoItem );
        
        ParameterWR::getInstance()->setVar( "s_TipoProc", $s_TipoProc );
        
        ob_start();
        $o_itemActions = new itemActions();
        $o_itemActions->executeSaveItem();
        $out = trim( ob_get_clean() );

        $a_DataRs = json_decode( $out, true );

        if( $s_TipoProc == "nvo" )
        {
            pr( "INSERT Item: [$i] [$s_Codigo] [{$a_DataRs['id']}]" );
        }
        else
        {
            pr( "UPDATE Item: [$i] [$s_Codigo] [{$a_DataRs['id']}]" );
        }

        //se actualiza el costo
        $s_Sql = "update ALM_ItemMast set PrecioCosto='$f_PrecCostoDol', PrecioUnitarioLocal='$f_PrecCostoSol', PrecioUnitarioDolares='$f_PrecCostoDol' where IdItem='{$a_DataRs['id']}';";
        mysql_query( $s_Sql, $cn );
        
        if( !$s_IdProduct )
        {
            $s_Sql = "UPDATE exp_producto SET id_product='{$a_DataRs['id']}' WHERE codigo='$s_Codigo';";
            mysql_query( $s_Sql, $cn );
        }
        
        ParameterWR::getInstance()->destroy();
        
        $i++;
    }
    pr( "Fin de Carga Item [".date( "H:i:s" )."]" );
    echo "<br>";

}
catch ( Exception $e )
{
    pr( $row );
    pr( $row2 );
    pr( $o_MST_ClaseFamiliaDP );
    pr( $e );
}