<?php
$total_rets=0;
$query=mysqli_query($con, "SELECT * FROM ventas v , retencion_venta r where v.ven_fecha BETWEEN '$desde' AND '$hasta' and v.sucursal='$sucursal' and v.empresa='$empresa' and v.ven_estado<>'anulada' and r.ventas_ven_id=v.ven_id order by v.ven_id desc");
?>
<page backtop="1mm" backbottom="5mm" backleft="5mm" backright="5mm" style="font-size: 11pt; font-family: arial" >
<table cellspacing="0" style="width: 100%; padding-top: 5px;">
	<tr>
		<td style="width: 15%;color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
			<strong style="font-size:10px;color: #000">N° FACTURA</strong>
		</td>
		<td style="width: 35%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
			<strong style="font-size:10px;color: #000">CLIENTE </strong>
		</td>
		<td style="width: 20%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
			<strong style="font-size:10px;color: #000">BASE </strong>
		</td>
		<td style="width: 10%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
			<strong style="font-size:10px;color: #000">% RET</strong>
		</td>
		<td style="width: 20%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
			<strong style="font-size:10px;color: #000">VALOR RETENCION</strong>
		</td>
	</tr>
<?php
while ($roww=mysqli_fetch_array($query))    
{
	$ven_id=$roww['ven_id'];
	$sumador_combo=0;
	$sumador_total=0;

	$neto=0;
	$total_descuento=0;
	$total_iva=0;
	$total_retencion=0;
	$sumador_articulos=0;

	$ret=mysqli_query($con, "SELECT * FROM retencion_venta where ventas_ven_id='$ven_id' ") or die(mysqli_error($con)); 
	$rets=mysqli_fetch_array($ret);
	$ret_porcentaje=$rets['porcentaje'];

	$sql=mysqli_query($con, "SELECT * from venta_detalle where ventas_ven_id='$ven_id'");
	while ($row=mysqli_fetch_array($sql))
	{
	    
	    $vend_referencia=$row['vend_referencia'];
	    $vend_nombre=$row['vend_nombre'];
	    $vend_cantidad=$row['vend_cantidad'];
	    $vend_precio=$row['vend_precio']; //1.000.000
	    $vend_iva=$row['vend_iva']; //19%
	    $vend_descuento=$row['vend_desc']; //10%

	    $iva0=$vend_iva/100; //0.19
	    $iva1=1+$iva0; //1.19

	    $precio_total_item=$vend_precio*$vend_cantidad;//precio normal 1.000.000
		$precio_total_sin_iva_item=($vend_precio/$iva1)*$vend_cantidad; //precio sin iva  840.336
		$descuento_item=($vend_descuento/100)*$precio_total_sin_iva_item; //descuento_item 84.033
		$total_iva_item=$precio_total_sin_iva_item*$iva0; // total iva  159.336

		$neto+=$precio_total_sin_iva_item;
		$total_descuento+=$descuento_item;
		$total_iva+=$total_iva_item;

		$sumador_articulos=($neto+$total_iva)-$total_descuento;
		$sumador_articulos=($neto+$total_iva)-$total_descuento;
	}
	$base=$neto;
	$total_retencion+=($neto+$sumador_combo-$total_descuento)*($ret_porcentaje/100);
	$sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;
	$total_rets+=$total_retencion;
?>
	<tr style="">
		<td style="width: 15%;color:white;background-color:#fff;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
			<strong style="font-size:10px;color: #000"><?php echo $roww['ven_prefijo']; echo '-'; echo $roww['ven_n_venta']?></strong>
		</td>
		<td style="width: 35%; color:white;background-color:#fff;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
			<strong style="font-size:10px;color: #000"><?php echo $roww['ven_nombre'];?></strong>
		</td>
		<td style="width: 20%; color:white;background-color:#fff;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
			<strong style="font-size:10px;color: #000"><?php echo number_format($base,0);?></strong>
		</td>
		<td style="width: 10%; color:white;background-color:#fff;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
			<strong style="font-size:10px;color: #000"><?php echo $ret_porcentaje;?></strong>
		</td>
		<td style="width: 20%; color:white;background-color:#fff;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;;border-right:solid 1px #bdc3c7;"> 
			<strong style="font-size:10px;color: #000"><?php echo number_format($total_retencion,0);?></strong>
		</td>
	</tr>
	<?php  } ?>
   
	<tr style="">
		<td colspan="4" style="width: 80%; color:white;background-color:#DFCA19;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
			<strong style="font-size:10px;color: #000"> </strong>
		</td>
		<td  style="width: 20%; color:white;background-color:#DFCA19;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
			<strong style="font-size:10px;color: #000"><?php echo number_format($total_rets,0); ?> </strong>
		</td>
	</tr>
</table>
</page>	  