<?php
$total_rets=0;
$query=mysqli_query($con, "SELECT * FROM compras c , retencion_compra r where c.com_fecha BETWEEN '$desde' AND '$hasta' and c.sucursal='$sucursal' and c.empresa='$empresa' and r.compras_com_id=c.com_id order by c.com_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° COMPRA</strong>
		</td>
		<td style="width: 35%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
			<strong style="font-size:10px;color: #000">PROVEEDOR </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))    
{
	$com_id=$roww['com_id'];
	$sumador_total=0;
    $neto=0;
    $total_retencion=0;

    $sql=mysqli_query($con, "SELECT * from compra_detalle where compras_com_id='$com_id' and empresa='$empresa'");
    while ($rew=mysqli_fetch_array($sql))
    {
        $comd_cantidad=$rew['comd_cantidad'];
        $comd_precio=$rew['comd_precio'];
        $comd_iva=$rew['comd_iva'];
        $precio_total=$comd_precio*$comd_cantidad;
        $iva=($precio_total*$comd_iva)/100;
        $neto+=$precio_total;//Sumador
        $sumador_total+=$precio_total+$iva;//Sumador
    }
	$base=$neto;
    $ret=mysqli_query($con,"SELECT * FROM retencion_compra where empresa='$empresa' and compras_com_id='$com_id'") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
    $total_retencion=($ret_porcentaje=$rets['ret_porcentaje']/100)*$neto;
    $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['com_facturaproveedor']; ?></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['proveedor_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 $rets['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>	  