<?php
require('../cfg/res.php');
//$sql_clie1='select *from clie11 where idnum1='.$_POST['indx'];
//$res_clie1=mysql_query($sql_clie1,$link_vcrx);
//$xtice=mysql_result($res_clie1,0,'tice');

//$sql_clie2='select *from clie12 where idnum2='.$_POST['indx'];
//$res_clie2=mysql_query($sql_clie2,$link_vcrx);

?>

<div style=" max-height:600px; overflow-y:scroll; margin-top:5px;">

<table width="100%" border="1" cellspacing="0" cellpadding="2" style="border-collapse:collapse;font-size:12px; border:#000000 solid 1px;">


<?php

$xfecha=$_POST['fecha1'];
$sql_op50='select *from clie11 where left(docu,3)="OP5" and tcant>0 and fecha<="'.$xfecha.'" order by docu, numero';
$res_op50=mysql_query($sql_op50,$link_vcrx);
echo $sql_op50;

$filax=1;
echo 'Generando Avisos de Cobro...';
while($r_op50=mysql_fetch_assoc($res_op50))
{
	echo'<div align="left">';
	echo $r_op50['docu'].'-'.$r_op50['ptonumero'].'-'.$r_op50['fechadmy'].' /'.$r_op50['enti_descri'];				
	echo'</div>';
	
	$xidnum1=$r_op50['idnum1'];
	$xenti=$r_op50['enti'];
	$xzsub=$r_op50['zsub'];
	$xcontra=$r_op50['numero'];
	$xfecont=$r_op50['fecha'];

	$sql_op10 = 'select * from clie1 where docu="OP19" and fecha="'.$xfecha.'" and enti="'.$xenti.'" and contrato="'.$xcontra.'"';
	$res_op10=mysql_query($sql_op10,$link_vcrx);
	$xnumop10 = 0;

	//echo $sql_op10;
	if(mysql_num_rows($res_op10)==0)
	{
		$sql_iop10='insert into 
			clie1(docu,
				ptoe,
				numero,
				fecha,
				enti,
				zsub,
				contrato,
				status,idate)
			values("OP19",
				"001001",
				"'.$xnumop10.'",
				"'.$xfecha.'",
				"'.$xenti.'",
				"'.$xzsub.'",
				"'.$xcontra.'",
				"-"'.',now())';
		//echo $sql_iop10;
		$res_iop10=mysql_query($sql_iop10,$link_vcrx);
	}
	
	$res_op10=mysql_query($sql_op10,$link_vcrx);
	$xidop10=mysql_result($res_op10,0,'idnum1');
	$xstatop10 = mysql_result($res_op10,0,'status');

		$sql_uop10='update clie1 set 
			docu="OP19",
			idate=now()
			where idnum1='.$xidop10;
		$res_uop10=mysql_query($sql_uop10,$link_vcrx);
	
		$sql_dop10='delete from clie2 where idnum1='.$xidop10;
		$res_dop10=mysql_query($sql_dop10,$link_vcrx);
	
	$sql_op50d = 'select * from clie2 where idnum2='.$xidnum1.' and cant>0';
	//echo $sql_op50d;
	$res_op50d=mysql_query($sql_op50d,$link_vcrx);
	while($r_op50d=mysql_fetch_assoc($res_op50d))
	{
		
		$xitem=$r_op50d['item'];
		$xcant=$r_op50d['cant'];
		$xdcto=$r_op50d['dcto'];
		echo'<div align="left">';
		echo '>         '.$xitem.' /'.$xcant;				
		echo'</div>';

		$sql_iop10d='insert into 
			clie2(idnum2,
				item,
				cant,
				dcto
				)
			values("'.$xidop10.'",
				"'.$xitem.'",
				"'.$xcant.'",
				"'.$xdcto.'"
				)';
		//echo $sql_iop10d;
		$res_iop10d=mysql_query($sql_iop10d,$link_vcrx);
				
		$sql_uop10d = 'update clie2, item0 
				set puni=item_puni,
				ice=item_ice,
				iva=item_iva
				where item=item_codigo
				and idnum2="'.$xidop10.'"';
		//echo $sql_uop10d;
		$res_uop10d=mysql_query($sql_uop10d,$link_vcrx);
				

	}
	
}
?>


</table>


</div>

<div id="mpedido_d" style="padding-top:10px; "></div>
</div>