<?php
//  este ya no funciona
session_start();
include('../cfg/res.php');
$sql_iddet='SELECT MAX(iddet)as maxiddet from clie2 where idnum2='.$_POST['fk'];

$res_iddet=mysql_query($sql_iddet,$link_vcrx);
$iddet=mysql_num_rows($res_iddet);
if(empty($iddet))
{$iddet=1;}
else
{$iddet=mysql_result($res_iddet,0,'maxiddet')+1;}


$dcto=0;
$comi=0;
$drtr='';

$sql_dcto='select enti_dcto as dcto, enti_comi as comi, enti_referidox as drtr from enti0 where enti_codigo="'.$_SESSION['user_vcrx_id'].'"';
$res_dcto=mysql_query($sql_dcto,$link_vcrx);


if(mysql_num_rows($res_dcto)==0)
{$dcto=0;}
else
{$dcto=mysql_result($res_dcto,0,'dcto');}

if(empty($dcto))
{$dcto=0;}

// $dcto=0;


$sql_drtr='select enti,drtr from clie1 where idnum1='.$_POST['fk'];
$res_drtr=mysql_query($sql_drtr,$link_vcrx);
$enti1=mysql_result($res_drtr,0,'enti');
$drtr=mysql_result($res_drtr,0,'drtr');

if (empty($drtr))
{$drtr=$_SESSION['user_vcrx_id'];}


$xenviodom= 0;
if ($enti1==$drtr)
{$xenviodom = 1;}


$comi1=0;
$comi=0;

$sql_comi1='select enti_dcto as dcto, enti_comi as comi, enti_referidox as drtr, enti_drtr from enti0 where enti_codigo="'.$enti1.'" and enti_drtr="1"';
$res_comi1=mysql_query($sql_comi1,$link_vcrx);
if(mysql_num_rows($res_comi1)!=0)
{
	$comi1=mysql_result($res_comi1,0,'comi');
        if(empty($comi1))
        {$comi1=0;}
}

$comi=$comi1;
if ($comi1==0)
{
$sql_comi='select enti_dcto as dcto, enti_comi as comi, enti_referidox as drtr, enti_drtr from enti0 where enti_codigo="'.$drtr.'"';
$res_comi=mysql_query($sql_comi,$link_vcrx);
if(mysql_num_rows($res_comi)!=0)
{
	$comi=mysql_result($res_comi,0,'comi');
	$dcto=mysql_result($res_comi,0,'dcto');
        if(empty($comi))
        {$comi=0;}
}
}

$sql_drtr1='select enti_dcto as dcto, enti_comi as comi, enti_referidox as drtr, enti_drtr from enti0 where enti_codigo like "'.$enti1.'%"';
$res_drtr1=mysql_query($sql_drtr1,$link_vcrx);
//if(mysql_num_rows($res_drtr1)!=0)
//{
	if(mysql_result($res_drtr1,0,'enti_drtr')=="1")
        {$dcto=mysql_result($res_drtr1,0,'dcto');}
//}





$sql_item='SELECT 
			item_pvp1 as pvp ,
			item_iva as iva,
			item_ice as ice,
			item_puni as puni,
			item_dcto as dcto,
			item_comi as comi
			from item0
			where item_codigo="'.$_POST['ite'].'"';

$disponible=0;

//$sql_disp='select * from invdisp where item="'.$_POST['ite'].'"';

$sql_disp='select * from item0d where item_codigo="'.$_POST['ite'].'"';
$res_disp=mysql_query($sql_disp,$link_vcrx);

if(mysql_num_rows($res_disp)==0)
{$disponible=9999;}
else
{$disponible=mysql_result($res_disp,0,'dispo');}

//$ice = 0;
$res_item=mysql_query($sql_item,$link_vcrx);
$puni=mysql_result($res_item,0,'puni');
$ice=mysql_result($res_item,0,'ice');
$iva=mysql_result($res_item,0,'iva');
$pvp=mysql_result($res_item,0,'pvp');
//$comi=mysql_result($res_item,0,'comi');

if ($iva==0)
{$comi=0;}


if($_POST['cantidad']>$disponible)
{
	$cant=$disponible;
	//echo 'No Hay Disponible';
}
else
{
	$cant=$_POST['cantidad'];
}

if ($cant<0){$cant=0;}

// if ($dcto==0){$dcto=28;}
if ($dcto==0){$dcto=99;}
$dcto=99;

//+++++++ borrar para activar disponibles
$cant=$_POST['cantidad'];


$xitem=$_POST['ite'];
// TRANSPORTE
$xcelecl='TRAN';
if(mb_stristr($_POST['ite'],$xcelecl))
{
	$dcto=0;
	$cant=$_POST['cantidad'];
	
	if ($xenviodom==1)
	{$xitem=str_replace('400','100',$xitem); $puni=1;}
	
}

$xirbp=0;
// $dcto=0;

$sql_pedido_d='insert into clie2(
idnum2,
iddet,
cant,
puni,
dcto,
ice,
comi,
iva,
item,
irbp,
cantio
)
values(
'.$_POST['fk'].',
'.$iddet.',
'.$cant.',
'.$puni.',
'.$dcto.',
'.$ice.',
'.$comi.',
'.$iva.',
"'.$xitem.'",
"'.$xirbp.'",
'.$_POST['cantidad'].'
)';

if(!mysql_query($sql_pedido_d,$link_vcrx))
{
	echo $sql_pedido_d;
}

?>