<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
?>
<?php 
include("../../conex.php");
$cant = "1";
$flag=0;
$id_empresa=$_SESSION['empresa'];
$num_tipo_dcto = $txtnumcot;

/********************************** Datos Empresa *********************************************************************/
	$sql_empresa = "SELECT * FROM empresa, comunas WHERE comunas.cod_comuna = empresa.comuna AND id=$id_empresa";
	$res_empresa = mysql_query($sql_empresa,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_empresa))
		{
		$empresa_rut=mysql_result($res_empresa, $row, "rut_empresa");
		$empresa_nombre=mysql_result($res_empresa, $row, "nombre");
		$empresa_direccion=mysql_result($res_empresa, $row, "direccion");
		$empresa_ciudad=mysql_result($res_empresa, $row, "ciudad");
		$empresa_comuna=mysql_result($res_empresa, $row, "comunas.comuna");
		$empresa_actividad=mysql_result($res_empresa, $row, "actividad");
		$empresa_rut_representante=mysql_result($res_empresa, $row, "rut_representante");
		$empresa_nombre_representante=mysql_result($res_empresa, $row, "nombre_representante");
		$row=$row+1;
		}


	/************************************************** calculo neto e iva ***********************************************/	
	$sql_total=$txttotal/(1+$par_iva);
	$sql_neto = number_format($sql_total,0,',','');
	$sql_iva=$txttotal-$sql_neto;
	$sql_total=$txttotal;
	

	/************************************************** insertar salida ***********************************************/	
	//$wsql = "INSERT INTO salida (num_dcto, tipo_dcto, productos) VALUES ($num_tipo_dcto, $id_tipo_dcto, 1)";
	//$consulta = mysql_query($wsql,$enlace);
?>

<table width="640" align="center" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="60px"><div align="center"><img src="../Images/logo.gif" width="60px" height="60px"></div></td>
	<td width="580px"><div align="left"><font size="1">
		<?=$empresa_nombre?><br>
		<?=$empresa_direccion?><br>
		<?=$empresa_ciudad;?>

		</font></div>
	</td>
    <td width="20%">&nbsp;</td>
  </tr>
</table>
<br>
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="25%">&nbsp;</td>
	<td width="50%"><div align="center"><strong><font size="6">COTIZACION Nº <?=$num_tipo_dcto;?></font></strong></div></td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>
<table width="640" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td width="100">&nbsp;</td>
	  <td width="157">&nbsp;</td>
		<td width="23">&nbsp;</td>
		<td width="82">&nbsp;</td>
		<td width="308">&nbsp;</td>
	</tr>
	<tr>
		<td width="100">R.U.T.</td>
		<td width="157">
			<?=$txtrut?>&nbsp;</td>
		<td width="23" align="left">&nbsp;</td>
		<td width="82" align="right"><div align="left">Señor: &nbsp;</div></td>
		<td width="308"><?=$txtrsoc?>&nbsp;</td>           
  </tr>
<tr>
		<td width="100">E-Mail</td>
		<td width="157"><?=$txtemail?>&nbsp;</td>
		<td width="23">&nbsp;</td>
		<td width="82" align="right">
			<div align="left">Fono: </div></td>
		<td width="308"><?=$txtfono?>&nbsp;</td>
	</tr>


</table>

<br>
<table width="640px" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#000000">
	<tr bgcolor="#000000"> 
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Cant.</font></strong></div></td>
		<td width="20%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">C&oacute;digo</font></strong></div></td>
		<td width="45%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
		<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Precio</font></strong></div></td>
		<td width="15%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Total $</font></strong></div></td>
    </tr>
<? 
while ($cant<=$cnt)
	{
	if ($total[$cant]>0)
		{
		//$psql = "INSERT INTO movimiento (entrada_salida_traspaso, cod_producto, cantidad, precio_unitario,numero_dcto, tipo_dcto, fecha) VALUES ('S', $codigo[$cant], $cantidad[$cant], $precio[$cant], $num_tipo_dcto, $id_tipo_dcto, '$fechacot')";
		//$sql_productos = mysql_query($psql,$enlace);
		?>
		<tr> 
			<td width="10%"> <? echo "$cantidad[$cant]";?> </td>
			  
			<td width="20%"><? echo "$codigo[$cant]";?>	</td>
			
			<td width="45%"><? echo "$detalle[$cant]";?></td>
			
			<td width="10%"><? echo "$precio[$cant]";?></td>
				
			<td width="15%"><? echo "$total[$cant]";?></td>
		</tr>
		<? 
		}
	$cant=$cant+1;
	}
	?>
</table>
<table width="640px" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  	<td width="10%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="20%">&nbsp;</td>
		<td width="50%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">SUB TOTAL &nbsp; &nbsp;</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<?=$sql_subt?>
        </font></div></td>
    </tr>
	<tr>
		<td width="10%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="20%">&nbsp;</td>
		<td width="50%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">DESCUENTOS &nbsp; &nbsp;</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<?=$sql_des?>
	    </font></div></td>
    </tr>
	<tr>
		<td width="10%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="20%">&nbsp;</td>
	  	<td width="50%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">TOTAL &nbsp; &nbsp;</font></strong></div></td>
	  	<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<?=$sql_tot?>
      </font></div></td>
    </tr>
</table>


<?
$cant = "1";
$flag=0;
$email = "$txtmail";
$asunto = "Cotización para $txtrsoc";
//para el envío en formato HTML
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

//$headers .= "From: drst23n@gmail.com \r\n";

$cabeceras = $headers;

$texto = "<table width='640' align='center' border='0' cellpadding='0' cellspacing='0'>
	<tr>
		<td width='60px'><div align='center'><img src='../Images/logo.gif' width='60px' height='60px'></div></td>
		<td width='580px'><div align='left'><font size='1'>
			$empresa_nombre<br>
			$empresa_direccion<br>
			$empresa_ciudad
		</font></div></td>
		<td width='20%'>&nbsp;</td>
	</tr>
</table>
<br>
<table width='90%' border='0' align='center' cellpadding='0' cellspacing='0'>
  <tr>
    <td width='25%'>&nbsp;</td>
	<td width='50%'><div align='center'><strong><font size='6'>COTIZACION Nº $num_tipo_dcto</font></strong></div></td>
    <td width='25%'>&nbsp;</td>
  </tr>
</table>
<table width='640' border='0' align='center' cellpadding='0' cellspacing='0'>
	<tr>
		<td width='100'>&nbsp;</td>
	  <td width='157'>&nbsp;</td>
		<td width='23'>&nbsp;</td>
		<td width='82'>&nbsp;</td>
		<td width='308'>&nbsp;</td>
	</tr>
	<tr>
		<td width='100'>R.U.T.</td>
		<td width='157'>$txtrut &nbsp;</td>
		<td width='23' align='left'>&nbsp;</td>
		<td width='82' align='right'><div align='left'>Señor: &nbsp;</div></td>
		<td width='308'>$txtrsoc &nbsp;</td>           
	</tr>
	<tr>
		<td width='100'>E-Mail</td>
		<td width='157'><?=$txtemail?>&nbsp;</td>
		<td width='23'>&nbsp;</td>
		<td width='82' align='right'><div align='left'>Fono: </div></td>
		<td width='308'>$txtfono &nbsp;</td>
	</tr>
</table>
<br>
<table width='640px' border='1' align='center' cellpadding='0' cellspacing='0' bordercolor='#000000'>
	<tr bgcolor='#000000'> 
		<td width='10%'><div align='center'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Cant.</font></strong></div></td>
		<td width='20%'><div align='center'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>C&oacute;digo</font></strong></div></td>
		<td width='45%'><div align='center'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Detalle</font></strong></div></td>
		<td width='10%'><div align='center'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Precio</font></strong></div></td>
		<td width='15%'><div align='center'><strong><font color='#FFFFFF' face='Arial, Helvetica, sans-serif'>Total $</font></strong></div></td>
    </tr>";

while ($cant<=$cnt)
	{
	if ($total[$cant]>0)
		{
		$texto=$texto."
		<tr> 
			<td width='10%'>$cantidad[$cant] &nbsp;</td>
			<td width='20%'>$codigo[$cant] &nbsp;/td>
			<td width='45%'>$detalle[$cant] &nbsp;</td>
			<td width='10%'><$precio[$cant] &nbsp;</td>
			<td width='15%'>$total[$cant] &nbsp;</td>
		</tr>";
		}
	$cant=$cant+1;
	}
$texto=$texto."
</table>
<table width='640px' border='0' align='center' cellpadding='0' cellspacing='0'>
	<tr>
	  	<td width='10%'>&nbsp;</td>
		<td width='5%'>&nbsp;</td>
		<td width='20%'>&nbsp;</td>
		<td width='50%'><div align='right'><strong><font color='#333333' face='Arial, Helvetica, sans-serif'>SUB TOTAL &nbsp; &nbsp;</font></strong></div></td>
		<td width='15%'><div align='right'><font color='#333333' face='Arial, Helvetica, sans-serif'>
        $ $sql_subt &nbsp;
        </font></div></td>
    </tr>
	<tr>
		<td width='10%'>&nbsp;</td>
		<td width='5%'>&nbsp;</td>
		<td width='20%'>&nbsp;</td>
		<td width='50%'><div align='right'><strong><font color='#333333' face='Arial, Helvetica, sans-serif'>DESCUENTOS &nbsp; &nbsp;</font></strong></div></td>
		<td width='15%'><div align='right'><font color='#333333' face='Arial, Helvetica, sans-serif'>
		  $ $sql_des &nbsp;
	    </font></div></td>
    </tr>
	<tr>
		<td width='10%'>&nbsp;</td>
		<td width='5%'>&nbsp;</td>
		<td width='20%'>&nbsp;</td>
	  	<td width='50%'><div align='right'><strong><font color='#333333' face='Arial, Helvetica, sans-serif'>TOTAL &nbsp; &nbsp;</font></strong></div></td>
	  	<td width='15%'><div align='right'><font color='#333333' face='Arial, Helvetica, sans-serif'>
		  $ $sql_tot &nbsp;
      </font></div></td>
    </tr>
</table>";
mail($email,$asunto,$texto,$cabeceras);
?>
<script>
	alert("El correo ha sido enviado");
	window.top.open('','_parent','');window.top.close();
</script>