<?
ini_set('error_reporting',1);
session_start();
if ($_SESSION['estado'] != "ON")
		{
		?>
		<script>
			parent.location.href = '../index.php';
		</script>
		<? 
		exit();
		}
$id_empresa = $_SESSION['ss_empresa'];
$id_user = $_SESSION['ss_user'];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Nota de Venta</title>
<?
include("../conex.php");
$bodega=$_POST["lstbodega"];
$vende =$_POST["lstvendedor"];
if ($vende==0) $vendedor='Todos';
else
	{
	$sql_bv = "SELECT * FROM vendedor WHERE id = $vende";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$vendedor = mysql_result($resbv, 0, "nombres");
			}
	}
	
if ($bodega==0) $bodegas='Todos';
else
	{
	$sql_bv = "SELECT * FROM bodega WHERE id = $bodega";
  	$resbv = mysql_query($sql_bv,$enlace);	
	while (mysql_fetch_object ($resbv))
			{
			$bodegas = mysql_result($resbv, 0, "descripcion");
			}
	}

$txtfecha=$_POST["txtfecha"];

$dia = substr ($txtfecha, 0, 2);
$mes = substr ($txtfecha, 3, 2);
$mes2=$mes;
$año = substr ($txtfecha, 6, 4);
if ($mes=='01') $mes = "ENERO";
if ($mes=='02') $mes = "FEBRERO";
if ($mes=='03') $mes = "MARZO";
if ($mes=='04') $mes = "ABRIL";
if ($mes=='05') $mes = "MAYO";
if ($mes=='06') $mes = "JUNIO";
if ($mes=='07') $mes = "JULIO";
if ($mes=='08') $mes = "AGOSTO";
if ($mes=='09') $mes = "SEPTIEMBRE";
if ($mes=='10') $mes = "OCTUBRE";
if ($mes=='11') $mes = "NOVIEMBRE";
if ($mes=='12') $mes = "DICIEMBRE";
?>
<style type="text/css">
<!--
.Estilo1 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px;}
.Estilo2 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; color:#FF0000}
-->
</style>
</head>  <br>
<body LINK="#FFFFFF" VLINK="#FFFFFF" ALINK="#FFFFFF">
<div align="center" class="titulos2">LISTADO DE NOTAS  MES DE  <?=$mes?> DEL <?=$año?> Bodega <?=$bodegas?> Vendedor <?=$vendedor?></div>
  <div align="center">

    <table width="80%" border="0">
      <tr bgcolor="#CCCCCC">
	  	<th width="5%" scope="col"><div align="center">Correlativo</div></th>
        <th width="5%" scope="col"><div align="center">Día</div></th>
        <th width="7%" scope="col"><div align="center">Hora</div></th>
        <th width="7%" scope="col"><div align="center">Vendedor</div></th>
        <th width="9%" scope="col"><div align="center">RUT</div></th>
        <th width="21%" scope="col"><div align="center">Cliente</div></th>
        <th width="8%" scope="col"><div align="center">Nro</div></th>
		<th width="10%" scope="col"><div align="center">Neto</div></th>
        <th width="9%" scope="col"><div align="center">Iva</div></th>
        <th width="10%" scope="col"><div align="center">Total</div></th>
		<th width="9%" scope="col"><div align="center">Estado</div></th>
        <th width="9%" scope="col"><div align="center">NºFactura</div></th>
		<th width="10%" scope="col"><div align="center">Anular</div></th>
		<th width="9%" scope="col"><div align="center">Facturar</div></th>
      </tr>
		<?	 
		$total_neto=0;
		$total_iva=0;
		$total_total=0;
  
	if ($vende==0  && $bodega==0) 
      {
			$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND tipo_dcto.descripcion='NOTA VENTA' AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND rut_cliente<>'00.000.000-0' ORDER BY num_dcto";
		}
			else
		if ($vende!=0 &&  $bodega==0) 

		{	$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND tipo_dcto.descripcion='NOTA VENTA' AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND vendedor = $vende  AND rut_cliente<>'00.000.000-0' ORDER BY num_dcto";
		}	
			else
		if ($vende==0 &&  $bodega!=0) 
			
		{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND tipo_dcto.descripcion='NOTA VENTA' AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa  AND salida.bodega = $bodega AND rut_cliente<>'00.000.000-0' ORDER BY num_dcto";
		}	
			else
		if ($vende!=0 &&  $bodega!=0) 
			
		{$sqlfs = "SELECT * FROM salida, tipo_dcto WHERE extract(month from fecha_emision)='$mes2' AND tipo_dcto.descripcion='NOTA VENTA' AND tipo_dcto.id = salida.tipo_dcto AND salida.empresa = $id_empresa AND vendedor = $vende  AND salida.bodega = $bodega AND rut_cliente<>'00.000.000-0' ORDER BY num_dcto";
		}	
			
		  	$resfs = mysql_query($sqlfs,$enlace);	
			$cont=0;
			while (mysql_fetch_object ($resfs))
				{
				$fecha = mysql_result($resfs, $cont, "fecha_emision");
				$dia = substr ($fecha, 8, 2);
				$iddcto = mysql_result($resfs, $cont, "tipo_dcto");
				if($iddcto==1)$dcto='B';
				if($iddcto==2)$dcto='F';
				if($iddcto==3)$dcto='NV';
				if($iddcto==4)$dcto='C';
				if($iddcto==5)$dcto='G';
				if($iddcto==6)$dcto='NC';
				if($iddcto==7)$dcto='ND';
				$id_vendedor = mysql_result($resfs, $cont, "vendedor");
				if ($id_vendedor!=''){					
					$sqlc = "SELECT * FROM vendedor WHERE id=$id_vendedor";
					$resc = mysql_query($sqlc,$enlace);					
					$nombre_vende = mysql_result($resc, 0, "nombres");
									}	
				
				$hora = mysql_result($resfs, $cont, "hora");
				$factura='';
				$estado2 = mysql_result($resfs, $cont, "documento_control");
				if ($estado2!=-1)
					{
					if ($estado2==null) 
						{
						$dir1="red_ball.gif";
						$dir2="delete.gif";
						$dir3="ir.gif";
						$estado1="Nota de venta sin facturar";
						}
					else
						{
						$estado1="Nota de venta facturada";
						$dir1="green_ball.gif";
						$dir2="";
						$dir3="";
						
						}
				$rut = mysql_result($resfs, $cont, "rut_cliente");
				if ($rut!=''){
				$sqlc = "SELECT * FROM nombres WHERE rut='$rut'";
		  		$resc = mysql_query($sqlc,$enlace);
				
				$nombre = mysql_result($resc, 0, "razon_social");
				}
				else 
					{
					$nombre = "Boleta";
					$rut="&nbsp;";
					
					}
				$nro = mysql_result($resfs, $cont, "num_dcto");
				$idasal = mysql_result($resfs, $cont, "id");	
				$iva = mysql_result($resfs, $cont, "iva");
				$total_iva+=$iva;
				$iva=number_format($iva,0,',','.');
				$neto= mysql_result($resfs, $cont, "neto");
				$total_neto+=$neto;
				$neto=number_format($neto,0,',','.');
				$total = mysql_result($resfs, $cont, "total");
				$total_total+=$total;
				$total=number_format($total,0,',','.');
				if ($estado2!=null) 
						{
                        $factura = mysql_result($resfs, $cont, "documento_control");
						$sqlfx = "SELECT num_dcto FROM salida WHERE id=$factura";
		  	            $resfx = mysql_query($sqlfx,$enlace);
						$factura = mysql_result($resfx, 0, "num_dcto");
						}

		  	?>
			<tr>
		        <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$cont+1?></strong></span></div></td>
                 <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$dia?></strong></span></div></td>
                <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$hora?> </strong></span></div></td>
                 <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong><?=$nombre_vende?> </strong></span></div></td>     
                
                
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	<?=$rut?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	<?=$nombre?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong>
	            	<?=$nro?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
	            	<?=$neto?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
	            	<?=$iva?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
	            	<?=$total?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px">
					<img name="foto3" id="foto3" align="top" border="0" src="../Images/<?=$dir1?>" style="cursor:pointer" title="<?=$estado1?>" />
				</td> 
                <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="center"><span class="Estilo1"><strong>
	            	<?=$factura?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px">
				<? if ($dir2 !=''){ ?>
				<a  href="javascript:fun_eliminar('<?=$idasal;?>');">
					<img name="foto4" id="foto4" align="top" border="0" src="../Images/<?=$dir2?>" style="cursor:pointer" title="Presione aqui para anular nota de venta" />				</a>
					<? }else {echo "&nbsp;";} ?>
				</td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px">
				<? if ($dir3 !=''){ ?>
				<a  href="javascript:fun_fact('<?=$nro;?>');">
					<img name="foto4" id="foto4" align="top" border="0" src="../Images/<?=$dir3?>" style="cursor:pointer" title="Presione aqui para facturar nota de venta" />				</a>
					<? }else {echo "&nbsp;";} ?>
				</td>
	        </tr>	
			<?
			}
			$cont+=1;	
			}
			$total_neto=number_format($total_neto,0,',','.');
			$total_iva=number_format($total_iva,0,',','.');
			$total_total=number_format($total_total,0,',','.');
			?>
			<tr>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong></strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong></strong></span></div></td>
                <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong></strong></span></div></td>
                <td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>Total:</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
	            	<?=$total_neto?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
					<?=$total_iva;?>
				</strong></span></div></td> 
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="right"><span class="Estilo1"><strong>
					<?=$total_total;?>
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	
				</strong></span></div></td>
				<td style="border-color:#CCCCCC; border:solid; border-width:2px; border-right-width:1px"><div align="left"><span class="Estilo1"><strong>
	            	
				</strong></span></div></td>
	        </tr>
			
			
    </table>
	
	
	<form name="form1" method="post" action="">
	  <label>
	  <input type="button" name="imprimir" id="imprimir" value="Imprimir" onClick="fun_imprimir();">
	  </label>
	  <input name="idsalidaant" type="text" id="idsalidaant" style="visibility:hidden" size="1">
	  <input name="txt_nv" id="txt_nv" type="text" style="visibility:hidden" size="1">
       <input name="id_bodega" id="id_bodega" type="text" value="<?=$bodega?>" style="visibility:hidden" size="1">
    </form>

  </div>
</body>
</html>

<script language="javascript" type="text/javascript">

function fun_eliminar(cual)
	{
	document.form1.idsalidaant.value=cual;
	document.form1.action="../nota_venta/anular.php";
	document.form1.target="_self";
	document.form1.submit();
	}
function fun_fact(cual)
	{
	document.form1.txt_nv.value=cual;
	document.form1.action="../nota_venta/index2.php";
	document.form1.target="_blank";
	document.form1.submit();
	}
function fun_imprimir()
	{
	document.getElementById('imprimir').style.display = "none";
	window.print();
	document.getElementById('imprimir').style.display = "block";
	};

</script>
</script>