<?
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"];	

?>
<?
include("../../conex.php");
?>
<!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>

<?
$cant = 1;
$flag=0;
/******************************  Descuento en pesos *********************************************************************/
	$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");
		$sql_num_bol = mysql_result($res_dcto,0 ,"correlativo");
		$sql_num_bol = $sql_num_bol + 1;
		$row=$row+1;
		}
	
	mysql_query("UPDATE tipo_dcto SET correlativo=$sql_num_bol WHERE id = $id_tipo_dcto",$enlace);
	
/******************************  Id tipo pago *********************************************************************/		
	$sql_pago = "SELECT id FROM tipo_pago WHERE descripcion = 'CREDITO'";
	$res_pago = mysql_query($sql_pago,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_pago))
		{
		$id_tipo_pago=mysql_result($res_pago, $row, "id");
		$row=$row+1;
		}
	/************************************************** sacar valor de iva ***********************************************/	
	$sql_par = "SELECT valor FROM parametros WHERE descripcion = 'IVA'";
	$res_par = mysql_query($sql_par,$enlace);
	$row=0;
	while (mysql_fetch_object ($res_par))
		{
		$par_iva=mysql_result($res_par, $row, "valor");
		$row=$row+1;
		}
	/************************************************** calculo neto e iva ***********************************************/	
	
	$sql_total = ($txttotal*$par_iva);
	$sql_iva=(int)$sql_total;
	$r=($sql_total-intval($sql_total))*10;
	$r=(int)$r;
	if ($r>=5)
		{
		$sql_iva+=1;
		$sql_neto = $txttotal-$sql_iva; 
		}
	else
		{
		$sql_neto = $txttotal-$sql_iva;
		}
		$sql_total = $sql_neto + $sql_iva;


	/************************************************** insertar salida ***********************************************/	
	$dia = substr ($fecha, 0, 2);
	$mes = substr ($fecha, 3, 2);
	$aņo = substr ($fecha, 6, 4); 
	$fecha_em = "$aņo-$mes-$dia";
	$wsql = "INSERT INTO salida (empresa, num_dcto, tipo_dcto, fecha_emision, tipo_pago, neto, iva, total, productos, usuario, bodega, vendedor, zona) 
	VALUES ($id_empresa, $sql_num_bol, $id_tipo_dcto, '$fecha_em', $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 onLoad="fun_mail();">

<table width="640px" align="center" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="25%"><div align="center"><img src="../Images/logo.gif" width="60px" height="60px"></div></td>
	<td width="50%"><div align="center"><strong><font size="6">BOLETA NUMERO <?=$sql_num_bol?></font></strong></div></td>
    <td width="25%">&nbsp;</td>
  </tr>
</table>

<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="18%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">C&oacute;digo</font></strong></div></td>
		<td width="40%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Detalle</font></strong></div></td>
		<td width="15%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Precio $ </font></strong></div></td>
	<? /*	<td width="10%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Desc. $</font></strong></div></td> */?>
		<td width="17%"><div align="center"><strong><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Total $</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,numero_dcto, tipo_dcto, descuentos, bodega, id_soe) 
		VALUES ($id_empresa, 'S', '$fecha_em', $codigo[$cant], $cantidad[$cant], $precio[$cant], $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);
		?>
		<tr> 
			<td width="10%"><div align="left"><font color="#333333" face="Arial, Helvetica, sans-serif">
			<? echo "$cantidad[$cant]";?>&nbsp;
			  </font></div></td>
			  
			<td width="18%"><div align="left"><font color="#333333" face="Arial, Helvetica, sans-serif">
				<? echo "$codigo[$cant]";?>&nbsp;
			</font></div></td>
			
			<td width="40%"><div align="left"><font color="#333333" face="Arial, Helvetica, sans-serif">
				<? echo "$detalle[$cant]";?>&nbsp;
			</font></div></td>
			
			<td width="10%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
				<? echo "$precio[$cant]";?>&nbsp;
			</font></div></td>
			<? /*		
				<td width="10%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
				<? echo "$desporprod[$cant]";?>&nbsp;
			</font></div></td>
			*/?>
			<td width="12%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
				<? echo "$total[$cant]";?>&nbsp;
			</font></div></td>
		</tr>
		<? 
		}
	$cant=$cant+1;
	}
	?>
</table>
<table width="640px" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">SUB TOTAL</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<?=$txtsubtotal?>
        </font></div></td>
    </tr>
	<tr>
	  	<td width="5%">&nbsp;</td>
	  	<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">DESCUENTO</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<?=$cal_descuento;?>
        </font></div></td>
    </tr>

	<tr>
	  	<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">NETO</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
        $<?=$sql_neto?>
        </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
		<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">IVA</font></strong></div></td>
		<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
		  $<?=$sql_iva?>
	    </font></div></td>
    </tr>
	<tr>
		<td width="5%">&nbsp;</td>
		<td width="5%">&nbsp;</td>
		<td width="35%">&nbsp;</td>
	  	<td width="40%"><div align="right"><strong><font color="#333333" face="Arial, Helvetica, sans-serif">TOTAL </font></strong></div></td>
	  	<td width="15%"><div align="right"><font color="#333333" face="Arial, Helvetica, sans-serif">
	    $<? if ($txttotal=='')$txttotal=0;?>
	    <?=$sql_total?>
      </font></div></td>
    </tr>
</table>
</body>
</html>
<script>
function fun_mail()
	{
	window.print();
	}
</script>



