<?php
$resol=mysqli_query($con,"SELECT * FROM serie_factura where ser_estado='activo' and ser_tipo='electronica' and ser_tipo_documento='1' and empresa='$empresa'") or die(mysqli_error($con)); 
$res=mysqli_fetch_array($resol);
$result = mysqli_num_rows($resol);
$ser_inicio=$res['ser_inicio'];
$ser_resolucion=$res['ser_resolucion'];
$ser_fecha=$res['ser_fecha'];
$ser_fin=$res['ser_fin'];
$ser_fecha_ven=$res['ser_fecha_ven'];

$to=0;
$query=mysqli_query($con,"SELECT * , DATE_FORMAT(`ven_fecha`,'%d-%m-%Y') as ven_fecha FROM ventas where ven_id='$ven_id' and empresa='$empresa' ");
$roww=mysqli_fetch_array($query);
$ven_cc=$roww['ven_cc'];
$estado_dian=$roww['estado_dian'];
$Cliente_clie_id=$roww['Cliente_clie_id'];
$ven_nombre=$roww['ven_nombre'];
$ven_telefono=$roww['ven_telefono'];
$ven_fecha=$roww['ven_fecha'];
$ven_estado=$roww['ven_estado'];
$ven_prefijo=$roww['ven_prefijo']; 
$ven_n_venta=$roww['ven_n_venta']; 
$ven_forma_de_pago=$roww['ven_forma_de_pago'];
$ven_dias=$roww['ven_dias'];
$cufe=$roww['cufe'];
$vendedor=$roww['vendedor'];
$ven_tipo=$roww['ven_tipo'];
$observaciones=$roww['observaciones'];

$dir=mysqli_query($con,"SELECT * FROM cliente where clie_id='$Cliente_clie_id'") or die(mysqli_error($con)); 
$dire=mysqli_fetch_array($dir);
$clie_direccion=$dire['clie_direccion'];
$clie_correo=$dire['clie_correo'];
$clie_ciudad=$dire['clie_ciudad'];

$ciu=mysqli_query($api,"SELECT * from municipalities where id='$clie_ciudad'");
$ciud=mysqli_fetch_array($ciu);
$name=$ciud['name'];

$sql3=mysqli_query($con,"SELECT * from usuario where usu_id='$vendedor'");
$usu=mysqli_fetch_array($sql3);
$usu_nombre=$usu['usu_nombre'];

$vence = strtotime($ven_fecha."+ ".$ven_dias." days");
                date("Y-m-d",$vence) . "\n";
                $fecha=date('Y-m-d ');

$hasta=date("d-m-Y",$vence) . "\n";

          
$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;
}

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

$dir = '../../temp/';
if (!file_exists($dir))
    mkdir($dir);
$filename = $dir.'test.png';
$numfac=$ven_prefijo.$ven_n_venta;//numero de factura
$fecfac=$ven_fecha;//fecha de facturas
$nitfac=$emp_nit;//nit de empresa
$docadq=$ven_cc;//documento del clienta
$valfac=round($neto,2);//neto
$valiva=round($total_iva,2);//iva
$valotroim=round($total_descuento,2);//descuento
$valfaclm=round($sumador_total,2);//total
$cufe=$cufe;//cufe
$tamaño = 10; //Tamaño de Pixel
$level = 'L'; //Precisión Baja
$framSize = 3; //Tamaño en blanco
$contenido = 'NumFac: '.$numfac.PHP_EOL.'FecFac: '.$fecfac.PHP_EOL.'NitFac: '.$nitfac.PHP_EOL.'DocAdq: '.$docadq.PHP_EOL.'ValFac: '.$valfac.PHP_EOL.'ValIva: '.$valiva.PHP_EOL.'ValOtroIm: '.$valotroim.PHP_EOL.'ValTotal: '.$valfaclm.PHP_EOL.'CUFE: '.$cufe.PHP_EOL.'https://catalogo-vpfe.dian.gov.co/document/searchqr?documentkey='.$cufe; //Texto

QRcode::png($contenido, $filename, $level, $tamaño, $framSize); 
$qr=$dir.basename($filename);
?>
<page backtop="1mm" backbottom="5mm" backleft="5mm" backright="5mm" style="font-size: 11pt; font-family: arial" >

    <table cellspacing="0" style="width: 100%;" >
        <tr>
            <td style="width: 33%; color: #444444;">
                <img style="width: 80%;" src="../../../img/<?php echo $empresa;?>.png" alt="Logo"><br>
            </td>
			<td style="width: 34%; font-size:12px;text-align:center;">
				<strong style="font-size:15px"><?php echo $emp_nombre;?></strong><br>
				<strong>Nit: <?php echo $emp_nit;?></strong> <br>
				<strong><?php echo $emp_direccion;?></strong><br>
				<strong><?php echo $emp_correo;?></strong><br>
				<strong>Tel: <?php echo $emp_cel;?> / <?php echo $emp_telefono;?></strong><br>
				<!--<strong>Responsable de IVA </strong><br> 
				<strong>Agente de retención en el impuesto sobre las ventas</strong>-->
			</td>
			<td style="width: 21%;"></td>
			<?php if($estado_dian=='Aprobada'){?>
			<td style="width: 12%;"><img style="width: 100%;" align="right" src="<?php echo $qr; ?>" alt="Logo"></td>
		    <?php } ?>
        </tr>
    </table>
    <div style="text-align: center;font-size:9px;padding-bottom:0px;">
		<p><?php echo $texto_cabeza; ?></p>
	</div>
	<?php if($ven_tipo=='electronica'){?>
		<div style="text-align: right;font-size:8px;padding-bottom:0px;"><p>Resolucion de Facturación Electronica No. <?php echo $ser_resolucion; ?> de <?php echo $ser_fecha; ?>, Rango <?php echo $ser_inicio; ?> Al <?php echo $ser_fin; ?> - Vigencia Desde: <?php echo $ser_fecha ?> Hasta: <?php echo $ser_fecha_ven; ?></p></div>
	<?php } else {?>
		<hr style="display: block;height: 1px;border: 1px solid #bdc3c7;">
 	<?php } ?>
 	<table cellspacing="0" style="width: 100%;" >
        <tr>
            <td style="width: 70%;">
                <table cellspacing="0">
                    <tr>
                        <td  style="background-color:#bdc3c7;padding:2px;text-align:center;"> 
                            <strong style="font-size:10px;color: #000" >DATOS DEL CLIENTE</strong>
                        </td>
                    </tr>
					<tr>
						<td style="width: 100%;text-align:left;border:solid 1px #bdc3c7;font-size:10px ;padding-left: 15px"> 
							<p style="padding:-10px;"><strong>NOMBRE: </strong><?php echo $ven_nombre;?></p>
							<p style="padding:-10px;"><strong>DOCUMENTO: </strong><?php echo $ven_cc;?></p>
							<p style="padding:-10px;"><strong>DIRECCION: </strong><?php echo $clie_direccion;?> <?php echo mb_strtoupper($name);?></p>
							<p style="padding:-10px;"><strong>TELEFONO: </strong><?php echo $ven_telefono;?></p>
							<p style="padding:-10px;"><strong>CORREO: </strong><?php echo mb_strtoupper($clie_correo);?></p>
							<p style="padding:-10px;"><strong>FORMA DE PAGO: </strong><?php echo mb_strtoupper($ven_forma_de_pago);?></p>
						</td>
					</tr>
			    </table>
            </td>
			<td style="width: 30%;">
				<table cellspacing="0" style="width: 100%; padding-left:5px">
			        <tr>
						<td style="width: 100%;color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
							<strong style="font-size:10px;color: #000" >FACTURA DE VENTA N°</strong>
						</td>
					</tr>
					<tr>
						<td style="width: 100%;padding:2px;text-align:center;border:solid 1px #bdc3c7;font-size:12px"> 
							<strong style="color: red"><?php echo $ven_prefijo;echo ' - '; echo $ven_n_venta;?></strong>
						</td>
					</tr>
			    </table>
			    <table cellspacing="0" style="width: 100%;padding-left: 5px; padding-top: 5px;">
					<tr>
						<td style="width: 100%;text-align:left;border:solid 1px #bdc3c7;font-size:10px ;padding-left: 15px;"> 
							<p style="padding:-10px;"><strong>FECHA FACTURACION: </strong><?php echo $ven_fecha;?></p>
							<p style="padding:-10px;"><strong>FECHA VENCIMIENTO: </strong><?php echo $hasta;?></p>
							<p style="padding:-10px;"><strong>VENDEDOR: </strong><?php echo $usu_nombre;?></p>
						</td>
					</tr>
			    </table>
			</td>
		</tr>
    </table>



	<table cellspacing="0" style="width: 100%; padding-top: 5px;">
        <tr>
			<td style="width: 10%;color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">REF</strong>
			</td>
			
			<td style="width: 5%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">GTIA</strong>
			</td>
			<td style="width: 40%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">DESCRIPCIÓN</strong>
			</td>
			<td style="width: 5%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">CANT</strong>
			</td>
			<td style="width: 8%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">V X UNI</strong>
			</td>
			<td style="width: 8%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">V BRUTO</strong>
			</td>
			<td style="width: 5%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">IVA</strong>
			</td>
			<td style="width: 5%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">DESC</strong>
			</td>
			<td style="width: 5%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">V IVA</strong>
			</td>
			<td style="width:9%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">V NETO</strong>
			</td>
		</tr>
<?php
$suma=0;

$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_art_id=$row['vend_art_id'];
    $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%
    $suma+=$row['vend_cantidad'];

    $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;


    $gar=mysqli_query($con,"SELECT * FROM articulos where art_id='$vend_art_id' and empresa='$empresa'") ;
    $gar=mysqli_fetch_array($gar);

?>		
		<tr>
			<td style="width:10%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $vend_referencia;?>
			</td>
			<td style="width:5%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $gar['art_garantia'];?>
			</td>
			<td style="width: 40%;padding:2px;text-align:left;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $vend_nombre;?>
				<?php 
				$sql4=mysqli_query($con,"SELECT * from seriales where venta_detalle_vend_id='$vend_id'");
				$result4=mysqli_num_rows($sql4);
				if($result4>0)
				{ 
					echo '(- ';
					while ($row4=mysqli_fetch_array($sql4))
					{
						echo $row4['ser_codigo'];
						echo ' - ';
					}
					echo ')';
				}
				?>
			</td>
			<td style="width:5%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $vend_cantidad;?>
			</td>
			<td style="width:8%;padding:2px;text-align:right;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo number_format($precio_unit_sin_iva_item,0);?>
			</td>
			<td style="width:8%;padding:2px;text-align:right;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo number_format($precio_unit_sin_iva_item*$vend_cantidad,0);?>
			</td>
			<td style="width:5%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $vend_iva;?>
			</td>
			<td style="width:5%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $vend_descuento;?>
			</td>
			<td style="width:5%;padding:2px;text-align:right;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo number_format($total_iva_item,0);?>
			</td>
			<td style="width: 9%;padding:2px;text-align:right;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo number_format($precio_total_item,0);?>
			</td>
		</tr>
<?php } 
$total_retencion+=($neto+$sumador_combo-$total_descuento)*($ret_porcentaje/100);
$sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;

?>		
		<tr>
			<td style="width: 10%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;font-size:8px">Total Items</td>
			<td style="width: 5%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;font-size:8px"><?php echo $suma; ?></td>
			<td colspan="8" style="width: 10%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:8px"></td>
		</tr>
    </table>


    <table cellspacing="0" style="width: 100%;padding-top: 5px;">
        <tr>
            <td style="width: 69%;">
            	<table cellspacing="0" style="width: 100%;">
			        <tr>
						<td style="width: 100%;color:white;background-color:#bdc3c7;padding:2px;text-align:center;"> 
							<strong style="font-size:10px;color: #000" >OBSERVACIONES</strong>
						</td>
					</tr>
				
					<tr>
						<td style="width: 100%;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;"><strong style="font-size:8px;"><?php echo $observaciones;?> </strong>
						</td>
					</tr>
					<tr>
						<td>
							<table cellspacing="0" style="width: 100%;" >
								<tr>
									<td colspan="3" style="width: 100%;padding-bottom: 36px"></td>
								</tr>
								<tr>
									<td style="width: 45%;">_______________________________</td>
									<td style="width: 5%;"></td>
									<td style="width: 45%;">_____________________________</td>	
								</tr>
								<tr>
									<td style="width: 45%;font-size:8px;">Cliente C.C</td>
									<td style="width: 5%;"></td>
									<td style="width: 45%;font-size:8px;">Vendedor C.C</td>		
								</tr>
						    </table>
						</td>
					</tr>
			    </table>
            </td>
            <td style="width: 1%;"></td>
			<td style="width: 30%;">
				<table cellspacing="0" style="width: 100%;">
			        <tr>
						<td colspan="2" style="width: 100%;color:white;background-color:#bdc3c7;text-align:center;padding:2px;"> 
							<strong style="font-size:10px;color: #000" >TOTALES</strong>
						</td>
					</tr>
					<tr>
						<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
							NETO
						</td>
						<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
							<?php echo number_format($neto,0); ?>
						</td>
					</tr>
					<tr>
						<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
							DESCUENTO
						</td>
						<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
							<?php echo number_format($total_descuento,0); ?>
						</td>
					</tr>

				<?PHP		
					$sqliva=mysqli_query($con,"SELECT * from venta_detalle where ventas_ven_id='$ven_id'and vend_iva<>'00' group by vend_iva") or die(mysqli_error($con)); 
					while ($iva=mysqli_fetch_array($sqliva))
					{
					    $iva_porcentaje=$iva['vend_iva'];
					    $iva0=$iva_porcentaje/100; //0.19
					    $iva1=1+$iva0; //1.19

					    $neto_i=0;
					    $total_iva=0;
					    $sqliva1=mysqli_query($con,"SELECT * from venta_detalle where ventas_ven_id='$ven_id' and vend_iva='$iva_porcentaje' order by vend_id asc");
					    while ($rowi=mysqli_fetch_array($sqliva1))
					    {
					        $vend_cantidad=$rowi['vend_cantidad'];
					        $vend_precio=$rowi['vend_precio']; //1.000.000
					        $vend_iva=$rowi['vend_iva']; //19%
					        $vend_descuento=$rowi['vend_desc']; //10%

					        $precio_total_sin_iva_item=($vend_precio/$iva1)*$vend_cantidad; //precio sin iva  840.336
					        $total_iva_item=$precio_total_sin_iva_item*$iva0; // total iva  159.336

					        $neto_i+=$precio_total_sin_iva_item;
					        $total_iva+=$total_iva_item;
					    }
					    ?>
					    <tr>
							<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
								IVA (<?php echo $iva_porcentaje; ?>)
							</td>
							<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
								<?php echo number_format($total_iva,0); ?>
							</td>
						</tr>
					<?PHP } ?>

					<tr>
						<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
							RETENCION
						</td>
						<td style="width: 50%;padding:2px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
							<?php echo number_format($total_retencion,0); ?>
						</td>
					</tr>
					<tr>
						<td style="width: 50%;padding:4px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;font-size:10px;text-align:right;"> 
							TOTAL
						</td>
						<td style="width: 50%;padding:4px;text-align:center;border-bottom:solid 1px #bdc3c7;border-left:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;font-size:11px;text-align:right; color: red;"> 
							<strong><?php echo number_format($sumador_total,0); ?></strong>
						</td>
					</tr>
			    </table>
			</td>
		</tr>
    </table>

    <table cellspacing="0" style="width: 100%;padding-top: 5px">
        <tr>
			<td style="width: 100%;color:white;background-color:#bdc3c7;padding:2px;text-align:center;"> 
				<strong style="font-size:10px;color: #000" >CONDICIONES DE GARANTIA</strong>
			</td>
		</tr>
	
		<tr>
			<td style="width: 100%;border-left:solid 1px #bdc3c7;border-bottom:solid 1px #bdc3c7;border-right:solid 1px #bdc3c7;"><strong style="font-size:8px;"><?php echo $condiciones;?> </strong>
			</td>
		</tr>
    </table>
    <?php if($ven_tipo=='electronica'){?>
		<div style="font-size:8px;text-align: center;padding: 2px">Cufe: <?php echo $cufe;?></div>
	<?php } ?>
	
    <div style="font-size:8px; padding: 2px;text-align: center;">Impreso por | <?php echo $emp_nombre;?> - VICPOS - V 3.5. <?php echo $emp_url;?></div>
	<div style="width:100%; border-top:dashed 1px;margin-top:1mm;"></div>
	

	
</page>	  