<?php
include('conexion.php');
$nom = $_SESSION['nom_admin'];
$iduser = $_SESSION['id_user'];
$suc = $_SESSION['IdSuc'];
$tipou= $_SESSION['tipouser'];
$desde = $_POST['desde'];
$hasta = $_POST['hasta'];
$sucbus = $_POST['sucbus'];
//EJECUTAMOS LA CONSULTA DE BUSQUEDA

	//$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
if($sucbus== 0){
	$registro = mysqli_query($conexion,"SELECT V.IdVenta AS 'Consecutivo',V.FechaVenta AS 'FechaVenta', V.Serie AS 'Serie', V.FolioVenta AS 'Folio' ,V.IdSucursal AS 'Sucursal',V.IdCliente AS 'Cliente' ,V.Subtotal AS 'Subtotal' ,V.Descuento AS 'Descuento',V.Total AS 'Total',V.Anticipo AS 'Anticipo',V.Saldo AS 'Saldo',V.Obs AS 'Obs',V.HoraEntrega,V.FechaEntrega,C.Nombre AS 'NomCliente',C.Domicilio AS 'DomCliente',V.NumFact,V.EdoFact,V.FechaBusq,V.EstadoPago,V.EstadoOrden,V.TipoOrden,V.Activo  FROM Ventas V 

		JOIN Clientes C ON V.IdCliente = C.IdCliente
		WHERE V.Activo = 1 AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.TipoOrden = '3'  ORDER BY V.IdSucursal,V.Serie,V.FolioVenta ASC");
}else if($sucbus != 0){
	$registro = mysqli_query($conexion,"SELECT V.IdVenta AS 'Consecutivo',V.FechaVenta AS 'FechaVenta', V.Serie AS 'Serie', V.FolioVenta AS 'Folio' ,V.IdSucursal AS 'Sucursal',V.IdCliente AS 'Cliente' ,V.Subtotal AS 'Subtotal' ,V.Descuento AS 'Descuento',V.Total AS 'Total',V.Anticipo AS 'Anticipo',V.Saldo AS 'Saldo',V.Obs AS 'Obs',V.HoraEntrega,V.FechaEntrega,C.Nombre AS 'NomCliente',C.Domicilio AS 'DomCliente',V.NumFact,V.EdoFact,V.FechaBusq,V.EstadoPago,V.EstadoOrden,V.TipoOrden,V.Activo  FROM Ventas V 

		JOIN Clientes C ON V.IdCliente = C.IdCliente
		WHERE V.Activo = 1 AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.TipoOrden = '3' AND V.IdSucursal = '$sucbus'  ORDER BY V.IdSucursal,V.Serie,V.FolioVenta ASC");
}
	

	//CREAMOS NUESTRA VISTA Y LA DEVOLVEMOS AL AJAX
	$contador = mysqli_num_rows($registro);
	$d = mysqli_fetch_array($registro);
	$subtotalg = 0;
	$subtotald = 0;
	$subtotalt = 0;
	
echo '<div align="center">
<table  style="font-size:14px;width:100%;" class="table table-striped table-condensed table-hover">
	        	<thead>
	        	<tr class="pagado">
	            	<th colspan="14" style="font-size:20px;" >Busqueda PERDIDAS</th>
	            </tr>

	        	<tr >
	        		<th ></th>
	            	<th >Folio</th>
	            	<th >Fecha</th>
	            	<th >Cliente</th>
	            	<th >Sucursal</th>
	            	';
	            	if($iduser != 8){
	                echo '<th >Total</th>
	                <th>Anticipo</th>
	                <th >Saldo</th>';
	                }

	                echo '<th >Fecha Entrega</th>';
	                if($iduser != 8){
	                	echo'
	                <th colspan="4">Acciones</th>
	                ';}
	                '
	            </tr></thead>
	            <tbody>';


	$url_actual = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	if($contador >0){
			
		do{
			$edopp = $d['EstadoPago'];
			$ed = $d['EstadoOrden'];
			$EdoFact= $d['EdoFact'];
			$folio= $d['Serie'].$d['Folio'];
			$s = $d['Serie'];
			$f = $d['Folio'];
			$fechav= $d['FechaVenta'];
			$suc= $d['Sucursal'];
			if($suc == 1){
			    $sucn = 'CUL';
			}else if ($suc == 2){
			    $sucn = 'MAZ';
			}else if($suc == 3){
			    $sucn = 'HER';
			}
			$total = $d['Total'];
			$anti = $d['Anticipo'];
			$saldo = $d['Saldo'];
			$fechae = $d['FechaEntrega'];
			$id = $d['Consecutivo'];
			$fact = $d['NumFact'];
			$cliente = $d['NomCliente'];

			$subtotalg = $subtotalg + $total;
			$subtotald = $subtotald + $anti;
			$subtotalt = $subtotalt + $saldo;
			$total = number_format($total,2,'.',',');
			$anti = number_format($anti,2,'.',',');
			$saldo = number_format($saldo,2,'.',',');


			echo '
			<tr class="'.$id.'">
			<td class="'.$id.'"><input type="checkbox" id="'.$id.'" value="'.$id.'" name="cons[]" onclick="check(this,'.$id.');"></td>					
			<td class="'.$id.'" id="c'.$id.'" >'.$folio.'</td>
			<td class="'.$id.'">'.$fechav.'</td>
			<td class="'.$id.'">'.$cliente.'</td>
			<td class="'.$id.'">'.$sucn.'</td>
			';
			if($iduser != 8){
				echo'
			<td class="'.$id.'">$'.$total.'</td>
			<td class="'.$id.'">$'.$anti.'</td>
			<td class="'.$id.'">$'.$saldo.'</td>
			';}
			echo '
			<td class="'.$id.'">'.$fechae.'</td>';
			if($iduser != 8){
				if($suc == 1){
					echo '<td class="'.$id.'"><a href="formato.php?serie='.$s.'&folio='.$f.'" target="_blank"><img src="imagenes/impr.png"></a></td>';
					echo '<td class="'.$id.'"><a href="editarorden.php?id='.$id.'" target="_blank"><img src="imagenes/edit.png"></a></td>';
				}else if($suc ==2){
					echo '<td class="'.$id.'"><a href="formato_ticket.php?serie='.$s.'&folio='.$f.'" target="_blank"><img src="imagenes/impr.png"></a></td>';
				}
			if($iduser == 5 || $iduser == 2){
			echo '
				<td class="'.$id.'"><a href="#" onclick="cancel('.$id.')" ><img src="imagenes/cancel.png"></a></td>
				';
/*echo '
				<td class="'.$id.'"><a href="#" onclick="cambiarfactura('.$id.')" ><img src="imagenes/cambiar.png"></a></td>
				';*/
			}
		}
			
			
				
					

			
			}while(($d = mysqli_fetch_array($registro)));
		}else{
			echo '<tr>
					<td colspan="10">No se encontraron resultados</td>
				</tr>';
			}

	$subtotalg = number_format($subtotalg,2,'.',',');
	$subtotald = number_format($subtotald,2,'.',',');
	$subtotalt = number_format($subtotalt,2,'.',',');
	if($iduser != 8){
	echo '<tr>
				<td colspan="5"></td>
				<td>$'.$subtotalg.'</td>
				<td>$'.$subtotald.'</td>
				<td>$'.$subtotalt.'</td>
				<td colspan="7"></td>
			</tr>';
	}
	echo '</tbody></table></div>';


?>