<?php session_start();
//$_SESSION['user_vcrx_id']
//$_SESSION['user_vcrx_empr']
//echo 'Se agrego'.$_POST['item'];
if(empty($_SESSION['user_vcrx_id']) || !isset($_SESSION['user_vcrx_id'])){die('Debe iniciar sesion');}
require('../cfg/res.php');
$car1_sql='select count(*)as total from tvir1 where enti="'.$_SESSION['user_vcrx_id'].'"';
$car1_res=mysql_query($car1_sql,$link_vcrx);
$car1_t=mysql_result($car1_res,0,'total');
if($car1_t==0){
$car1_sql='insert into tvir1(empr,docu,enti,fecha,status,iuser,idate)values(
"'.$_SESSION['user_vcrx_empr'].'",
"OP20",
"'.$_SESSION['user_vcrx_id'].'",
NOW(),
"-",
"'.$_SESSION['user_vcrx_id'].'",
NOW()
)';
mysql_query($car1_sql,$link_vcrx);
}

$car1_sql='select idnum1 from tvir1 where enti="'.$_SESSION['user_vcrx_id'].'"';
$car1_res=mysql_query($car1_sql,$link_vcrx);
$car1_idn=mysql_result($car1_res,0,'idnum1');






$car2_sql='select count(*)as total from tvir2 where idnum2="'.$car1_idn.'" and item="'.$_POST['item'].'"';
$car2_res=mysql_query($car2_sql,$link_vcrx);
$car2_tot=mysql_result($car2_res,0,'total');
if($car2_tot==0){
	$car2_sql='select max(iddet) as idd from tvir2 where idnum2="'.$car1_idn.'"';
	$car2_res=mysql_query($car2_sql,$link_vcrx);
	$car2_idd=mysql_result($car2_res,0,'idd');
	$car2_idd++;


	$item_sql='select *from item0 where item_codigo="'.$_POST['item'].'"';
	$item_res=mysql_query($item_sql);
	$item_puni=mysql_result($item_res,0,'item_puni');
	$item_dcto=mysql_result($item_res,0,'item_dcto');
	$item_ice=mysql_result($item_res,0,'item_ice');
	$item_iva=mysql_result($item_res,0,'item_iva');
	$item_irbp=mysql_result($item_res,0,'item_irbp');
	$item_pvp1=mysql_result($item_res,0,'item_pvp1');
	$item_cair=mysql_result($item_res,0,'item_cair');


	$car2_sql='insert into tvir2 (
	idnum2,
	iddet,
	cant,
	item,
	puni,
	dcto,
	ice,
	iva,
	irbp,
	pvp1,
	cair
	)values(
	"'.$car1_idn.'",
	"'.$car2_idd.'",
	"0",
	"'.$_POST['item'].'",
	"'.$item_puni.'",
	"'.$item_dcto.'",
	"'.$item_ice.'",
	"'.$item_iva.'",
	"'.$item_irbp.'",
	"'.$item_pvp1.'",
	"'.$item_cair.'"
	)';
	mysql_query($car2_sql,$link_vcrx); 
}




$car2_sql='update tvir2 set cant=cant+1 where idnum2="'.$car1_idn.'" and item="'.$_POST['item'].'"';
if(mysql_query($car2_sql,$link_vcrx)){echo 'Se agrego al carrito.';} 

?>