<?php
include('conexion.php');
$desde = $_POST['desde'];
$hasta = $_POST['hasta'];
$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){
	$registro = mysqli_query($conexion,"SELECT C.Nombre AS 'Cliente', COUNT(*) AS 'Cantidad de ordenes', SUM(V.Total) AS 'Total', SUM(V.Anticipo) AS 'Cobrado', SUM(V.Saldo) AS 'Saldo', MAX(V.FechaVenta) AS 'Ultima compra' FROM Ventas V
	JOIN Clientes C ON V.IdCliente = C.IdCliente
	WHERE V.Activo = 1  AND V.TipoOrden = 1 AND V.FechaBusq BETWEEN '$desde' AND '$hasta'
	GROUP BY Cliente
	ORDER BY Total DESC");
}else if($sucbus != 0){
	$registro = mysqli_query($conexion,"SELECT C.Nombre AS 'Cliente', COUNT(*) AS 'Cantidad de ordenes', SUM(V.Total) AS 'Total', SUM(V.Anticipo) AS 'Cobrado', SUM(V.Saldo) AS 'Saldo', MAX(V.FechaVenta) AS 'Ultima compra' FROM Ventas V
	JOIN Clientes C ON V.IdCliente = C.IdCliente
	WHERE V.Activo = 1  AND V.TipoOrden = 1 AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.IdSucursal = '$sucbus'
	GROUP BY Cliente
	ORDER BY Total DESC");
}


	//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>Cliente</th>
	            	<th>Cantidad de ordenes</th>
	            	<th>Total</th>
	                <th>Cobrado</th>
	                <th>Saldo</th>
	                <th>Ultima compra</th>
	            </tr></thead>
	            <tbody>';


	$url_actual = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	if($contador >0){
			
		do{
			$cant = $d['Cantidad de ordenes'];
			$cliente = $d['Cliente'];
			$total = $d['Total'];
			$cobrado = $d['Cobrado'];
			$saldo = $d['Saldo'];
			$ultima = $d['Ultima compra'];


			$totalt = $totalt + $total;
			$cobradot = $cobradot + $cobrado;
			$saldot = $saldot + $saldo;
			$total = number_format($total,2,'.',',');
			$cobrado = number_format($cobrado,2,'.',',');
			$saldo = number_format($saldo,2,'.',',');


			echo '
			<tr>	
			<td class="'.$folio.'">'.$cliente.'</td>
			<td class="'.$folio.'">'.$cant.'</td>
			<td class="'.$folio.'">$'.$total.'</td>
			<td class="'.$folio.'">$'.$cobrado.'</td>
			<td class="'.$folio.'">$'.$saldo.'</td>
			<td class="'.$folio.'">'.$ultima.'</td>

			<tr>
			';			
							

			
			}while(($d = mysqli_fetch_array($registro)));
		}else{
			echo '<tr>
					<td colspan="8">No se encontraron resultados</td>
				</tr>';
			}
			$ventat = number_format($totalt,2,'.',',');
			$cobradot = number_format($cobradot,2,'.',',');
			$saldot = number_format($saldot,2,'.',',');
	echo '
			<tr>
			<td colspan="8"></br></td>
			</tr>
			<tr>
				<td colspan="2"></td>
				<td>$'.$ventat.'</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>';



?>