<?php
$total=0;
$query=mysqli_query($con, "SELECT * FROM ventas where ven_fecha BETWEEN '$desde' AND '$hasta' and sucursal='$sucursal' and empresa='$empresa' and sucursal='$sucursal' and ven_estado<>'anulada'  and ven_nombre<>'CONSUMIDOR FINAL' order by 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;">
		<?php
		while ($roww=mysqli_fetch_array($query))    
		{
		?>
        <tr>
			<td style="width: 10%;color:white;background-color:#ABE9F8;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000"># <?php echo $roww['ven_prefijo'];?> <?php echo $roww['ven_n_venta'];?></strong>
			</td>
			<td style="width: 50%; color:white;background-color:#ABE9F8;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000"><?php echo $roww['ven_nombre'];?></strong>
			</td>
			<td colspan="2" style="width: 20%; color:white;background-color:#ABE9F8;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">CC/NIT: <?php echo $roww['ven_cc'];?></strong>
			</td>
			
			<td style="width: 10%; color:white;background-color:#ABE9F8;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000"><?php echo $roww['ven_forma_de_pago'];?></strong>
			</td>
			<td style="width: 10%; color:white;background-color:#ABE9F8;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000"><?php echo $roww['ven_fecha'];?></strong>
			</td>
		</tr>
		<tr>
			<td style="width: 10%;color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">REF</strong>
			</td>
			<td style="width: 50%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">NOMBRE</strong>
			</td>
			<td style="width: 10%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">CANT</strong>
			</td>
			<td style="width: 10%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">VALOR</strong>
			</td>
			<td style="width: 10%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">DESC %</strong>
			</td>
			<td style="width: 10%; color:white;background-color:#CACDCD;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">TOTAL</strong>
			</td>
		</tr>
		<?php
		$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_id=$row['vend_id'];
	    	$vend_referencia=$row['vend_referencia'];
	        $vend_nombre=$row['vend_nombre'];
	        $vend_cantidad=$row['vend_cantidad'];
	        $vend_precio=$row['vend_precio'];
	        $vend_iva=$row['vend_iva'];
	        $vend_descuento=$row['vend_desc'];
	        
	        $iva0=$vend_iva/100; //0.19
		    $iva1=1+$iva0; //1.19
			
			$precio_total_item=$vend_precio*$vend_cantidad;//precio normal 1.000.000
			$precio_unit_sin_iva_item=($vend_precio/$iva1); //precio sin iva  840.336
			$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;
	    ?>

		<tr style="">
			<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 $vend_referencia;?></strong>
			</td>
			<td style="width: 50%; color:white;background-color:#fff;padding:2px;text-align:left;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000"><?php echo $vend_nombre;?>  
					(
	                <?php   
	                    $art=mysqli_query($con,"SELECT * FROM venta_detalle_sabores_productos WHERE vend_id='$vend_id'")or die(mysqli_error($con));
	                    while ($row1=mysqli_fetch_array($art))
	                        {
	                            echo utf8_encode($row1['sab_nombre']);
	                            echo '-';
	                        }
	                ?>)
            	</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 $vend_cantidad;?></strong>
			</td>
			<td style="width: 10%; 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($vend_precio,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 $vend_descuento;?></strong>
			</td>
			<td style="width: 10%; 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($precio_total_item,0);?></strong>
			</td>
		</tr>
	<?php }
    $sql=mysqli_query($con, "SELECT * from venta_detalle_combo where ven_id='$ven_id'");
    while ($row1=mysqli_fetch_array($sql))
    {
        $vdc_id=$row1['vdc_id'];
        $combo_referencia=$row1["combo_referencia"];
        $combo_cantidad=$row1["combo_cantidad"];
        $combo_precio=$row1['combo_precio'];
        $combo_nombre=$row1['combo_nombre'];       
        $precio_total_c=$combo_cantidad*$combo_precio;
        $sumador_combo+=$precio_total_c;
    ?>
    <tr style="">
			<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 $combo_referencia;?></strong>
			</td>
			<td style="width: 50%; color:white;background-color:#fff;padding:2px;text-align:left;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000"><?php echo $combo_nombre;?>  
					(
	               		<?php   
                        $art=mysqli_query($con,"SELECT * FROM venta_detalle_sabores_combo WHERE vdc_id='$vdc_id'")or die(mysqli_error($con));
                            while ($row1=mysqli_fetch_array($art))
                                {
                                  echo utf8_encode($row1['sab_nombre']);
                                  echo '-';
                                }
                    	?>
                    )
            	</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 $combo_cantidad;?></strong>
			</td>
			<td style="width: 10%; 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($combo_cantidad,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 $vend_descuento;?></strong>
			</td>
			<td style="width: 10%; 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($precio_total_c,0);?></strong>
			</td>
		</tr>
      
    <?php }

    $total_retencion+=($neto+$sumador_combo-$total_descuento)*($ret_porcentaje/100);
	$sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;

    $total+=$sumador_total;
	?>		
		<tr style="">
			<td colspan="4" style="width: 80%; color:white;background-color:#B7ECC4;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: 10%; color:white;background-color:#B7ECC4;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000">TOTAL</strong>
			</td>
			<td style="width: 10%; color:white;background-color:#B7ECC4;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($sumador_total,0); ?></strong>
			</td>
		</tr>
		<tr style="">
			<td colspan="6" style="width: 80%; 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"> </strong>
			</td>
		</tr>

	<?php }?>
	<tr style="">
			<td colspan="5" style="width: 90%; 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: 10%; 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,0); ?> </strong>
			</td>
		</tr>
	</table>
</page>	  