<?php
 $sumador_saldo=0; 
$query=mysqli_query($con, "SELECT * FROM ventas where empresa='$empresa' and ven_estado='sin pagar' AND cliente_clie_id='$cliente'");
?>
<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="3" style="width: 30%; 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_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 $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 $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:#ffffff;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:#ffffff;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:#ffffff;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="4" style="width: 80%; color:white;background-color:#ffffff;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:#ffffff;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000">ABONO</strong>
			</td>
			<td style="width: 10%; color:white;background-color:#ffffff;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000">
					<?php 
						$sql=mysqli_query($con, "SELECT sum(veab_abono) as total from venta_abonos where ventas_ven_id='$ven_id'");
			            $sql=mysqli_fetch_array($sql);
			            $total_abono=$sql['total'];
			            echo number_format($total_abono,0); 
	        		?></strong>
			</td>
		</tr>
		<tr style="">
			<td colspan="4" style="width: 80%; color:white;background-color:#E83A0F;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:#E83A0F;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000">SALDO</strong>
			</td>
			<td style="width: 10%; color:white;background-color:#E83A0F;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000">
				<?php 
					$devo_total=0;
			        $sql1=mysqli_query($con, "SELECT * from devolucion where empresa='$empresa' and ventas_ven_id='$ven_id'");
			        while ($row=mysqli_fetch_array($sql1))
			        {
			            $dev_precio=$row['dev_precio'];
			            $devo_total+=$dev_precio; 
			        }

			        $saldo_t=$sumador_total-$total_abono-$devo_total;
			        if ($saldo_t<=0) 
			        {
			            $saldo='0';
			        }
			        else
			        {
			            $saldo=$saldo_t;
			        }
			           $sumador_saldo+=$saldo;
			        echo number_format($saldo,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($sumador_saldo,0); ?> </strong>
			</td>
		</tr>
	</table>
</page>	  