<?php
$to=0;
$cot_id=$_GET['cot_id'];
$query=mysqli_query($con, "SELECT *, DATE_FORMAT(cot_fecha, '%d-%m-%Y') AS nueva_fecha from cotizacion where cot_id='$cot_id'");
$roww=mysqli_fetch_array($query);
$cliente_id=$roww['cliente_id'];
$ven_cc=$roww['cot_cc'];
$ven_nombre=$roww['cot_nombre'];
$cot_dias=$roww['cot_validez'];
$vendedor=$roww['vendedor'];

$ven_telefono=$roww['cot_telefono'];
$ven_fecha=$roww['nueva_fecha'];
$ven_n_venta=$roww['cot_n_cotizacion'];
$dir=mysqli_query($con,"SELECT * FROM cliente where clie_id='$cliente_id'") or die(mysqli_error($con)); 
$dire=mysqli_fetch_array($dir);
$clie_direccion=$dire['clie_direccion'];
$clie_correo=$dire['clie_correo'];
$clie_barrio=$dire['clie_barrio'];
$clie_telefono=$dire['clie_telefono'];


$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."+ ".$cot_dias." days");
                date("Y-m-d",$vence) . "\n";
                $fecha=date('Y-m-d ');

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

?>
<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: 48%; color: #444444;">
                <img style="width: 60%;" src="../../../img/<?php echo $empresa;?>.png" alt="Logo"><br>
            </td>
             <td style="width: 12%; color: #444444;">
            </td>
			<td style="width: 40%;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_regimen; ?> </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>Agente de retención en el impuesto sobre las ventas</strong>-->
			</td>
        </tr>
    </table>
	<hr style="display: block;height: 1px;border: 1px solid #bdc3c7;">
 	<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($clie_barrio);?> <?php echo mb_strtoupper($name);?></p>
							<p style="padding:-10px;"><strong>TELEFONO: </strong><?php echo $clie_telefono;?></p>
							<p style="padding:-10px;"><strong>CORREO: </strong><?php echo $clie_correo;?></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">COTIZACION 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_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">CANT</strong>
			</td>
			<td style="width: 61%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">DESCRIPCIÓN</strong>
			</td>
			<td style="width: 8%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">PRECIO U IVA INCL</strong>
			</td>
			<td style="width: 7%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">DESC %</strong>
			</td>
			<td style="width:9%; color:white;background-color:#bdc3c7;padding:2px;text-align:center"> 
				<strong style="font-size:10px;color: #000">TOTAL</strong>
			</td>
		</tr>
<?php
	$suma=0;
    $neto=0;
    $total_descuento=0;
    $total_iva=0;
    $total_retencion=0;
    $sumador_articulos=0;

    $ret=mysqli_query($con, "SELECT * FROM retencion_cotizacion where cotizacion_cot_id='$cot_id' ") or die(mysqli_error($con)); 
    $rets=mysqli_fetch_array($ret);
    $ret_porcentaje=$rets['porcentaje'];

    $sql=mysqli_query($con, "SELECT * from cotizacion_detalle where cotizacion_cot_id='$cot_id' and empresa='$empresa' ");
    while ($row=mysqli_fetch_array($sql))
    {
    $id=$row["cotd_id"];
    $cotd_id=$row["cotd_id"];
    $cotd_referencia=$row['cotd_referencia'];
    $cotd_nombre=$row['cotd_nombre'];
    $cotd_cantidad=$row['cotd_cantidad'];
    $cotd_precio=$row['cotd_precio'];
    $cotd_iva=$row['cotd_iva'];
    $cotd_descuento=$row['cotd_desc'];
    $suma+=$row['cotd_cantidad'];

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

	$precio_total_item=$cotd_precio*$cotd_cantidad;//precio normal 1.000.000
	$precio_total_sin_iva_item=($cotd_precio/$iva1)*$cotd_cantidad; //precio sin iva  840.336
	$descuento_item=($cotd_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;

    if (($id % 2) == 0) {
    //Es un número par
  $color='#fff';
} else {
    //Es un número impar
    $color='#F3F2F2';
}
?>		
		<tr style="background:<?php echo $color;?>">
			<td style="width:10%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $cotd_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 $cotd_cantidad;?>
			</td>
			
			<td style="width: 61%;padding:2px;text-align:left;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $cotd_nombre;?>
			</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($cotd_precio,0);?>
			</td>
			<td style="width:7%;padding:2px;text-align:center;border-left:solid 1px #bdc3c7; border-bottom:solid 1px #bdc3c7;font-size:9px"> 
				<?php echo $cotd_descuento;?>
			</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-$total_descuento)*($ret_porcentaje/100);
$sumador_total=$sumador_articulos-$total_retencion;

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


    <table cellspacing="0" style="width: 100%;padding-top: 5px;">
        <tr>
            <td style="width: 64%;">
            	<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  $roww['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: 35%;">
				<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>
					<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
						</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>
					<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:10px;text-align:right;"> 
							<?php echo number_format($sumador_total,0); ?>
						</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</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_cot;?> </strong>
			</td>
		</tr>
    </table>
	
	<div style="font-size:8px; padding: 2px;text-align: center;">Impreso por | <?php echo $emp_nombre;?> - VICPOS - <?php echo $emp_url;?></div>
	<div style="width:100%; border-top:dashed 1px;margin-top:1mm;"></div>
	
</page>	  