<?php
$total=0;
$total_abono=0;
$total_saldo=0;
$query=mysqli_query($con, "SELECT * FROM ventas_apartadas where vena_fecha BETWEEN '$desde' AND '$hasta' and sucursal='$sucursal' and empresa='$empresa' and vena_estado='apartada' order by vena_fecha 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['vena_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['vena_nombre'];?></strong>
			</td>
			<td colspan="2" style="width: 10%; color:white;background-color:#ABE9F8;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">CC/NIT: <?php echo $roww['vena_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 strtoupper($roww['vena_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['vena_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
		$vena_id=$roww['vena_id'];
	    
            

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

			$sql=mysqli_query($con, "SELECT * from venta_apartadas_detalle where ventas_apartadas_vena_id='$vena_id'");
		    while ($row=mysqli_fetch_array($sql))
		    { 
		    	$venad_id=$row['venad_id'];
		    	$venad_referencia=$row['venad_referencia'];
		        $venad_nombre=$row['venad_nombre'];
		        $venad_cantidad=$row['venad_cantidad'];
		        $venad_precio=$row['venad_precio'];
		        $vend_descuento=$row['venad_desc'];
		        $vend_iva=$row['venad_iva'];

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

	                        $precio_total_item=$venad_precio*$venad_cantidad;//precio normal 1.000.000
	                        $precio_total_sin_iva_item=($venad_precio/$iva1)*$venad_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 $venad_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 $venad_nombre;?></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 $venad_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($venad_precio,0);?></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_descuento,0);?></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 sum(veaa_abono) as abono from  venta_apartada_abonos va where ventas_apartadas_vena_id='$vena_id' and empresa='$empresa' ");
		                    $sql=mysqli_fetch_array($sql);
		                    $abono=$sql['abono'];
		    				$saldo=$sumador_articulos-$abono;
			?>		
		<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_articulos,0); ?></strong>
			</td>
		</tr>
		<tr style="">
			<td colspan="4" style="width: 80%; color:white;background-color:#FFFEB3;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:#FFFEB3;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:#FFFEB3;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($abono,0); ?></strong>
			</td>
		</tr>
		<tr style="">
			<td colspan="4" style="width: 80%; color:white;background-color:#FFDFCB;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:#FFDFCB;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:#FFDFCB;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($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 
		$total+=$sumador_articulos;
		$total_abono+=$abono;
		$total_saldo+=$saldo;

		}
	 ?>
	<tr style="">
			<td colspan="5" style="width: 90%; 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"> TOTAL GENERAL </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($total,0); ?> </strong>
			</td>
		</tr> 
		<tr style="">
			<td colspan="5" style="width: 90%; color:white;background-color:#FFFEB3;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000">TOTAL ABONOS </strong>
			</td>
			<td  style="width: 10%; color:white;background-color:#FFFEB3;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_abono,0); ?> </strong>
			</td>
		</tr>
		<tr style="">
			<td colspan="5" style="width: 90%; color:white;background-color:#FFDFCB;padding:2px;text-align:right;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;"> 
				<strong style="font-size:10px;color: #000">TOTAL SALDO </strong>
			</td>
			<td  style="width: 10%; color:white;background-color:#FFDFCB;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_saldo,0); ?> </strong>
			</td>
		</tr>
	</table>
</page>	  