<?
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
$id_bodega = $_POST["lstbodega"];	
$id_zona = $_POST["lstzona"];	
$id_vendedor = $_POST["lstvendedor"];	
$sql_num_bol=$_POST["txtndcto"];
$id_tipo_pago=$_POST["lstpago"];
include("../../conex.php");
?>
<?

$sql_comprueba = "SELECT * FROM salida AS s, tipo_dcto AS td WHERE s.empresa = $id_empresa AND td.empresa = $id_empresa AND td.descripcion = 'BOLETA' AND s.num_dcto = $sql_num_bol AND s.tipo_dcto = td.id";	
//		echo $sql_comprueba;
		$res_comprueba = mysql_query($sql_comprueba,$enlace);	
		$row_comprueba=0;
		while (mysql_fetch_object ($res_comprueba))
			{
			$row_comprueba +=1;
			}


if ($row_comprueba==0)
	{
	
		if ($txtivar=='') $txtivar=0;
		if ($txtactividad=='') $txtactividad=null;
		if ($txtemail=='') $txtemail=null;
		if ($txtfono=='') $txtfono=null;
		if ($txtfax=='') $txtfax=null;
			/************************************************** verifica cliente *********************************************/		
			$sql_be = "SELECT * FROM nombres WHERE rut='$txtrut'";
			$res_be = mysql_query($sql_be,$enlace);
			$row=0;
			while (mysql_fetch_object ($res_be))
				{
				$emp_codigo=mysql_result($res_be, $row, "codigo");
				$row=$row+1;
				}
			if ($row==0)
				{
				$sql_ins = "INSERT INTO nombres (rut, razon_social, direccion, email, region, comuna, ciudad, fono1, fax, cliente, rubro_giro, id_zona, id_vendedor)
				VALUES ('$txtrut', '$txtrsoc', '$txtdireccion', '$txtemail', $lstreg, $lstcom, '$txtciudad', '$txtfono', '$txtfax', 'True', '$txtactividad', $id_zona, $id_vendedor)";
				mysql_query($sql_ins,$enlace);
				}
			else
				{
				$sql_upd = "UPDATE nombres SET rut='$txtrut', razon_social='$txtrsoc', direccion='$txtdireccion', email='$txtemail',
				ciudad='$txtciudad', comuna=$lstcom, region=$lstreg, rubro_giro='$txtactividad', fono1='$txtfono', fax='$txtfax', cliente = 'True', id_zona = $id_zona, id_vendedor = $id_vendedor WHERE codigo = $emp_codigo";
				mysql_query($sql_upd,$enlace);
				}
				
	
	
	?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Contado</title>
	</head>
	<style type="text/css">
	<!--
	.Estilo18 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9px; }
	.Estilo19 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; }
	-->
	</style>
	<?
	
		
	$cant = 1;
	$flag=0;
	/******************************  Descuento en pesos *********************************************************************/
		$sql_descuento=$_POST["sql_descuento"];
		$sql_subtotal=$_POST["sql_subtotal"];
		$cal_descuento=$sql_descuento;
		$por_des=($cal_descuento*100)/$sql_subtotal;
		
		
		
		$txttotal=$sql_total;
		$txtsubtotal = $_POST["sql_subtotal"];
		$cal_descuento=($txtsubtotal-$txttotal);
		$por_des=($cal_descuento*100)/$txtsubtotal;
	/******************************  Total de productos *********************************************************************/
		
		$cant = "1";
	$ctot_prod=0;
	while ($cant<=$cnt)
		{
		if ($total[$cant]>0)
			{
			$ctot_prod+=1;
			}
		$cant=$cant+1;
		}
	/******************************  Id BOLETA *********************************************************************/	
		$sql_dcto = "SELECT id, correlativo FROM tipo_dcto WHERE descripcion = 'BOLETA' AND empresa = $id_empresa";
		$res_dcto = mysql_query($sql_dcto,$enlace);
		$row=0;
		while (mysql_fetch_object ($res_dcto))
			{
			$id_tipo_dcto = mysql_result($res_dcto, $row, "id");
			$row=$row+1;
			}
		
		mysql_query("UPDATE tipo_dcto SET correlativo=$sql_num_bol WHERE id = $id_tipo_dcto",$enlace);
		

		/************************************************** sacar valor de iva ***********************************************/	
		$sql_neto=$_POST["sql_neto"];
		$sql_iva=$_POST["sql_iva"];
		$sql_total=$_POST["sql_total"];
	
	
		/************************************************** insertar salida ***********************************************/	
		$fecha=$_POST["txtfecha"];
		$dia = substr ($fecha, 0, 2);
		$mes = substr ($fecha, 3, 2);
		$ano2 = substr ($fecha, 8, 2);
		$aņo = substr ($fecha, 6, 4); 
		$fecha_em = "$aņo-$mes-$dia";
		$fecha=$_POST["fechaven"];
		$dia = substr ($fecha, 0, 2);
		$mes = substr ($fecha, 3, 2);
		$aņo = substr ($fecha, 6, 4); 
		$fecha_ve = "$aņo-$mes-$dia";
		$wsql = "INSERT INTO salida (empresa, rut_cliente, num_dcto, tipo_dcto, fecha_emision, fecha_vencimiento, tipo_pago, neto, iva, total, productos, usuario, bodega, vendedor, zona) 
		VALUES ($id_empresa, '$txtrut', $sql_num_bol, $id_tipo_dcto, '$fecha_em', '$fecha_ve', $id_tipo_pago, $sql_neto, $sql_iva, $sql_total, 1, $id_user, $id_bodega, $id_vendedor,$id_zona)";
		$consulta = mysql_query($wsql,$enlace);
		$sql_ultimo = "SELECT id FROM salida WHERE id=last_insert_id()";
		$res_ultimo =  mysql_query($sql_ultimo, $enlace);		
		$sultimo_id = mysql_result($res_ultimo, 0, "id");
		
	?>
	<body>
	
	<table width="400px" align="center" border="0" cellpadding="0" cellspacing="0">
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
		<tr height="11px">
			<td width="100%">&nbsp;</td>
		</tr>
	</table>
	<table width="400px" align="center" border="0" cellpadding="0" cellspacing="0">
		<tr height="27px">
			<td width="280px">&nbsp;</td>
			<td width="40px" align="center"><?=$dia;?></td>
			<td width="40px" align="center"><?=$mes;?></td>
			<td width="40px" align="center"><?=$ano2;?></td>
		</tr>
	</table>
	<table width="400px" border="0" align="center" cellpadding="0" cellspacing="0">
		<tr height="11px"> 
			<td width="47px"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">&nbsp;</font></strong></div></td>
			<td width="39px"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">&nbsp;</font></strong></div></td>
			<td width="235px"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">&nbsp;</font></strong></div></td>
			<td width="79px"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">&nbsp;</font></strong></div></td>
		</tr>
	<? 
	$cant = 1;
	$cnt_tot_prod=0;
	$total_while=0;
	while ($cant<=$cnt)
		{
		if ($total[$cant]>0)
			{
			$cnt_tot_prod+=1;
			if($cnt_tot_prod<$ctot_prod){
				$porcentaje_valor=100-$por_des;
				$totalw=(int)($total[$cant]*$porcentaje_valor/100);
				$total_while+=$totalw;
				$desporprod[$cant]=$total[$cant]-$totalw;
				}
			else{
				$totalw=$txttotal-$total_while;
				$desporprod[$cant]=$total[$cant]-$totalw;
				
				}
			
			$psql = "INSERT INTO movimiento (empresa, entrada_salida_traspaso, fecha, cod_producto, cantidad, precio_unitario, rut, numero_dcto, tipo_dcto, descuentos, bodega, id_soe) 
			VALUES ($id_empresa, 'S', '$fecha_em', $codigo[$cant], $cantidad[$cant], $precio[$cant], '$txtrut', $sql_num_bol, $id_tipo_dcto, $desporprod[$cant], $id_bodega, $sultimo_id)";
			$sql_productos = mysql_query($psql,$enlace);
			$sql_sal = "UPDATE productos SET saldo=(saldo-$cantidad[$cant]), u_salida=(u_salida+$cantidad[$cant]) WHERE empresa=$id_empresa AND codigo = $codigo[$cant]";
			mysql_query($sql_sal,$enlace);
			}
		$cant=$cant+1;
		}
		for ($i=1;$i<=$cnt;$i++)
			{ 	
			for ($j=1;$j<$cnt;$j++)
				{
					$auxa = $j+1;
				if ($codigo[$j]=='')
					{
					$aux=$codigo[$j];
					$codigo[$j]=$codigo[$auxa];
					$codigo[$auxa]=$aux;
					
					$aux=$cantidad[$j];
					$cantidad[$j]=$cantidad[$auxa];
					$cantidad[$auxa]=$aux;
					
					$aux=$precio[$j];
					$precio[$j]=$precio[$auxa];
					$precio[$auxa]=$aux;
					
					$aux=$detalle[$j];
					$detalle[$j]=$detalle[$auxa];
					$detalle[$auxa]=$aux;
					}
				}
			}
		
		
		
		?>
		<tr height="11px"> 
			<td><div align="left" style=""><span class="Estilo19">
				&nbsp;<? echo "$codigo[1]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[1]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[1]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[1]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[2]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[2]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[2]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[2]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[3]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[3]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[3]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[3]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[4]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[4]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[4]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[4]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[5]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[5]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[5]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[5]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[6]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[6]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[6]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[6]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[7]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[7]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[7]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[7]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[8]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[8]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[8]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[8]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[9]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[8]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[9]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[9]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[10]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[10]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[10]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[10]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[11]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[11]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[11]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[11]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[12]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[12]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[12]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[12]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[13]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[13]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[13]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[13]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;<? echo "$codigo[14]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$cantidad[14]";?>&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				<? echo "$detalle[14]";?>&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<? echo "$precio[14]";?>&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				&nbsp;
			</span></div></td>
		</tr>	
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				&nbsp;
			</span></div></td>
		</tr>
		<tr height="11px"> 
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				&nbsp;
			</span></div></td>
			<td><div align="left"><span class="Estilo19">
				Descuento:&nbsp;
			</span></div></td>
			<td><div align="right"><span class="Estilo19">
				<?=$cal_descuento;?>&nbsp;
			</span></div></td>
		</tr>	
		
	</table>
	
	<table width="400px" align="center" border="0" cellpadding="0" cellspacing="0">
		<tr height="27px">
			<td width="47px">&nbsp;</td>
			<td width="39px" align="center">&nbsp;</td>
			<td width="235px" align="right">&nbsp;</td>
			<td width="79px" align="right"><?=$sql_total?></td>
		</tr>
	</table>
	</body>
	</html>

		<script>
	alert("Venta guardada con exito");
	window.top.open('','_parent','');window.top.close();
</script>
		
		<?
		
	}
	else {
		?>
<script>
	alert ("El numero de documento ingresado ya existe en el sistema");
	window.opener.document.getElementById("txtguardado").value = 0;
	window.top.open('','_parent','');window.top.close();
</script>

		<?
		}
		?>
		
		
