<?php
include('conexion.php');
$desde = $_POST['desde'];
$hasta = $_POST['hasta'];
$tipop = $_POST['tipo'];
$nom = $_SESSION['nom_admin'];
$iduser = $_SESSION['id_user'];
$suc = $_SESSION['IdSuc'];
$tipou= $_SESSION['tipouser'];
$sucbus = $_POST['sucbus'];

//EJECUTAMOS LA CONSULTA DE BUSQUEDA
if($sucbus == 0){
	if($tipop != 0){
	//$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
	$registro = mysqli_query($conexion,"SELECT V.FechaVenta,V.Serie,V.FolioVenta,C.Nombre,V.Total AS 'Venta',V.CostoTotal AS 'Costo',V.Utilidad,V.Anticipo AS 'Cobrado',V.Saldo AS 'Saldo' FROM Ventas V
	JOIN Clientes C ON V.IdCliente = C.IdCliente
	WHERE V.Activo = 1  AND V.TipoOrden = '1' AND V.EstadoPago != 2 AND V.TipoFolio = '$tipop' AND V.FechaBusq BETWEEN '$desde' AND '$hasta' ORDER BY V.IdSucursal,V.Serie,V.FolioVenta");
	}else if($tipop == 0){
		$registro = mysqli_query($conexion,"SELECT V.FechaVenta,V.Serie,V.FolioVenta,C.Nombre,V.Total AS 'Venta',V.CostoTotal AS 'Costo',V.Utilidad,V.Anticipo AS 'Cobrado',V.Saldo AS 'Saldo' FROM Ventas V
		JOIN Clientes C ON V.IdCliente = C.IdCliente
	WHERE V.Activo = 1  AND V.TipoOrden = '1' AND V.EstadoPago != 2 AND V.FechaBusq BETWEEN '$desde' AND '$hasta' ORDER BY V.IdSucursal,V.Serie,V.FolioVenta");
	}
}else if($sucbus != 0){
	if($tipop != 0){
	//$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
	$registro = mysqli_query($conexion,"SELECT V.FechaVenta,V.Serie,V.FolioVenta,C.Nombre,V.Total AS 'Venta',V.CostoTotal AS 'Costo',V.Utilidad,V.Anticipo AS 'Cobrado',V.Saldo AS 'Saldo' FROM Ventas V
	JOIN Clientes C ON V.IdCliente = C.IdCliente
	WHERE V.Activo = 1  AND V.TipoOrden = '1' AND V.EstadoPago != 2 AND V.TipoFolio = '$tipop' AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.IdSucursal = '$sucbus' ORDER BY V.IdSucursal,V.Serie,V.FolioVenta");
	}else if($tipop == 0){
		$registro = mysqli_query($conexion,"SELECT V.FechaVenta,V.Serie,V.FolioVenta,C.Nombre,V.Total AS 'Venta',V.CostoTotal AS 'Costo',V.Utilidad,V.Anticipo AS 'Cobrado',V.Saldo AS 'Saldo' FROM Ventas V
		JOIN Clientes C ON V.IdCliente = C.IdCliente
	WHERE V.Activo = 1  AND V.TipoOrden = '1' AND V.EstadoPago != 2 AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.IdSucursal = '$sucbus' ORDER BY V.IdSucursal,V.Serie,V.FolioVenta");
	}
}



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

	        	<tr >
	            	<th>Fecha de venta</th>
	            	<th>Folio</th>
	            	<th>Cliente</th>
	                <th>Venta</th>
	                <th>Costo</th>
	                <th>Utilidad</th>
	                <th>Cobrado</th>
	                <th>Saldo</th>
	            </tr></thead>
	            <tbody>';


	$url_actual = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	if($contador >0){
			
		do{
			$fechav = $d['FechaVenta'];
			$folio = $d['Serie'].$d['FolioVenta'];
			$cliente = $d['Nombre'];
			$venta = $d['Venta'];
			$costo = $d['Costo'];
			$util = $d['Utilidad'];
			$cobrado = $d['Cobrado'];
			$saldo = $d['Saldo'];


			$ventat = $ventat + $venta;
			$costot = $costot + $costo;
			$utilt = $utilt + $util;
			$cobradot = $cobradot + $cobrado;
			$saldot = $saldot + $saldo;
			$venta = number_format($venta,2,'.',',');
			$costo = number_format($costo,2,'.',',');
			$util = number_format($util,2,'.',',');
			$cobrado = number_format($cobrado,2,'.',',');
			$saldo = number_format($saldo,2,'.',',');


			echo '
			<tr>	
			<td class="'.$folio.'">'.$fechav.'</td>
			<td class="'.$folio.'">'.$folio.'</td>
			<td class="'.$folio.'">'.$cliente.'</td>
			<td class="'.$folio.'">$'.$venta.'</td>
			<td class="'.$folio.'">$'.$costo.'</td>
			<td class="'.$folio.'">$'.$util.'</td>
			<td class="'.$folio.'">$'.$cobrado.'</td>
			<td class="'.$folio.'">$'.$saldo.'</td>

			<tr>
			';			
							

			
			}while(($d = mysqli_fetch_array($registro)));
		}else{
			echo '<tr>
					<td colspan="8">No se encontraron resultados</td>
				</tr>';
			}
			$ventat = number_format($ventat,2,'.',',');
			$costot = number_format($costot,2,'.',',');
			$utilt = number_format($utilt,2,'.',',');
			$cobradot = number_format($cobradot,2,'.',',');
			$saldot = number_format($saldot,2,'.',',');
	echo '
			<tr>
			<td colspan="8"></br></td>
			</tr>
			<tr>
				<td colspan="3"></td>
				<td>$'.$ventat.'</td>
				<td>$'.$costot.'</td>
				<td>$'.$utilt.'</td>
				<td>$'.$cobradot.'</td>
				<td>$'.$saldot.'</td>
			</tr>';
	/*echo '<tr>
				<td colspan="3"></td>
				<td>Accion: </td>
				<td><select folio="accnor">
					<option value="0" selected>Selecciona una opcion</option> 
					<option value="1">Revisado</option>
					<option value="2">No revisado</option>
				 </td>
				<td colspan="1" ><button folio="aplicaracc" onclick="revisado()" >Aceptar</button></td>
				
			</tr>';*/
	echo '</tbody></table></div>';



?>