<?php session_start();
require('../cfg/res.php');
require('../cfg/cfg.php');
$sql_num='select max(item_codigo) as nume   from item0 where CAST(item_codigo AS UNSIGNED) > 100000 and CAST(item_codigo AS UNSIGNED) < 200000';
if(mb_stristr('.gnya.gmny.',$empr_codigo))
{
	mysql_select_db('vcrxol_gmny',$link_vcrx);
}
$res_num=mysql_query($sql_num,$link_vcrx);

if(mysql_num_rows($res_num)== 0)
{

	$num=10000;
	
}
else
{
	$num=mysql_result($res_num,0,'nume');
	if(empty($num))
	{
		$num=10000;
	}
}



$num=substr($num, 0, 5);

$num++;


require('../../_lib/vrmod11.php');
require('../../_lib/ean.php');
$ean1=new ean();
$num=$num . vrmod11($num,false);

$sq_code=$_POST['sq_code'];
$sq_code=str_replace("*+abc+*",$num,$sq_code);

$ncode_sql='insert into item0 (item_codigo,item_descri,item_kldd,item_lsub,item_mrca) values '.$sq_code;



if(mysql_query($ncode_sql,$link_vcrx))
{

$cair_sql='update item0 set  item_cair="-",item_idate=NOW() , item_iuser="'.$_SESSION['user_vcrx_id'].'" where item_codigo like "'.$num.'%"';
mysql_query($cair_sql,$link_vcrx);
mysql_query('update item0 item0 set item_tlla=SUBSTR(item_codigo,7,2)where item_codigo like "'.$num.'%"',$link_vcrx);
$ean_sql='select item_codigo from item0 where item_codigo like "'.$num.'%"';
$ean_res=mysql_query($ean_sql,$link_vcrx);
if(mysql_num_rows($ean_res)!=0)
{
	while($ean_r=mysql_fetch_assoc($ean_res))
	{
		mysql_query('update item0  set item_ean="'.$ean1->set($ean_r['item_codigo']).'" where item_codigo="'.$ean_r['item_codigo'].'"',$link_vcrx);
	}
	
}






	echo $num;
	$sql_code='INSERT into vcrxol_gnya.item0
				(
					vcrxol_gnya.item0.item_codigo,
					vcrxol_gnya.item0.item_descri,
					vcrxol_gnya.item0.item_ean,
					vcrxol_gnya.item0.item_kldd,
					vcrxol_gnya.item0.item_cair
				)
				SELECT
				vcrxol_gmny.item0.item_codigo,
				vcrxol_gmny.item0.item_descri,
				vcrxol_gmny.item0.item_ean,
				vcrxol_gmny.item0.item_kldd,
				vcrxol_gmny.item0.item_cair
				FROM   
				vcrxol_gmny.item0
				where 
				vcrxol_gmny.item0.item_codigo not in (select vcrxol_gnya.item0.item_codigo from vcrxol_gnya.item0)';
				if(mb_stristr('.gnya.gmny.',$empr_codigo))
				{
					mysql_query($sql_code,$link_vcrx);
				}


}
else
{
	echo 'Ocurrio un error al registar los datos';
	//echo $ncode_sql;
	
}

?>