<?php session_start();
if(empty($_SESSION['user_vcrx_id'])){die('<resultado><error>Su sesion expiro.</error></resultado>');}
require('../cfg/res.php');

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

$car_sql='update tvir2 set cant='.$_POST['cant'].' where idnum2="'.$car1_idn.'" and iddet="'.$_POST['idd'].'"';

if($_POST['opt']=='d'){$car_sql='delete from tvir2 where idnum2="'.$car1_idn.'" and iddet="'.$_POST['idd'].'"';}
if($_POST['opt']=='e'){$car_sql='update tvir1 set adrs='.$_POST['idn'].' where enti="'.$_SESSION['user_vcrx_id'].'" ';}

$car2_subt=0;

$error=0;

if(!mysql_query($car_sql)){$error='☆Ocurrio un error al registrar. '.$car_sql;}
//$error=$car_sql;

if($_POST['opt']==''){
	$car_sql='select *,(pvp1*cant)as subt from tvir2 where idnum2="'.$car1_idn.'" and iddet="'.$_POST['idd'].'"';
	$car_res=mysql_query($car_sql,$link_vcrx);
	$car2_subt=mysql_result($car_res,0,'subt');
}

if($_POST['opt']!='e'){
$car2_sql='select sum(pvp1*cant)as subtotal from tvir2 where idnum2="'.$car1_idn.'"';
$car2_res=mysql_query($car2_sql,$link_vcrx);
$car2_subtot=mysql_result($car2_res,0,'subtotal');

$car2_sql='select sum((pvp1*cant)*((iva/100)))as ivat from tvir2 where idnum2="'.$car1_idn.'"';
$car2_res=mysql_query($car2_sql,$link_vcrx);
$car2_ivat=mysql_result($car2_res,0,'ivat');
$car2_total=$car2_subtot+$car2_ivat;
}

echo '<resultado>';
echo '<error>'.$error.'</error>';
echo '<subt>'.number_format($car2_subt,2).'</subt>';
echo '<subtotal>'.number_format($car2_subtot,2).'</subtotal>';
echo '<ivat>'.number_format($car2_ivat,2).'</ivat>';
echo '<total>'.number_format($car2_total,2).'</total>';
//echo '<sql>'.$car_sql.'</sql>';
echo '</resultado>';

?>
