<?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.IdCliente,C.Nombre, SUM(V.Subtotal) AS 'Subtotal', SUM(V.Descuento) AS 'Descuento', SUM(V.Total) AS 'Total', SUM(V.Anticipo) AS 'Cobrado', SUM(V.Saldo) AS 'Pendiente', SUM(V.CostoTotal) AS 'Costo', SUM(V.Utilidad) AS 'Utilidad', COUNT(*) AS 'NumOperaciones', MAX(V.FechaVenta) AS 'UltimaVenta'
        FROM Ventas V
        JOIN Clientes C ON V.IdCliente = C.IdCliente
        WHERE V.TipoOrden = '1' and V.Activo = 1  AND V.TipoFolio = '$tipop' AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.Serie != 'COT-' 
        GROUP BY V.IdCliente
        ORDER BY V.Utilidad DESC ");
        }else if($tipop == 0){
            $registro = mysqli_query($conexion,"SELECT V.IdCliente,C.Nombre, SUM(V.Subtotal) AS 'Subtotal', SUM(V.Descuento) AS 'Descuento', SUM(V.Total) AS 'Total', SUM(V.Anticipo) AS 'Cobrado', SUM(V.Saldo) AS 'Pendiente', SUM(V.CostoTotal) AS 'Costo', SUM(V.Utilidad) AS 'Utilidad', COUNT(*) AS 'NumOperaciones', MAX(V.FechaVenta) AS 'UltimaVenta'
            FROM Ventas V
            JOIN Clientes C ON V.IdCliente = C.IdCliente
            WHERE V.TipoOrden = '1' and V.Activo = 1 AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.Serie != 'COT-' 
            GROUP BY V.IdCliente
            ORDER BY V.Utilidad DESC ");
    }
}else if($sucbus != 0){
    if($tipop != 0){
        $registro = mysqli_query($conexion,"SELECT V.IdCliente,C.Nombre, SUM(V.Subtotal) AS 'Subtotal', SUM(V.Descuento) AS 'Descuento', SUM(V.Total) AS 'Total', SUM(V.Anticipo) AS 'Cobrado', SUM(V.Saldo) AS 'Pendiente', SUM(V.CostoTotal) AS 'Costo', SUM(V.Utilidad) AS 'Utilidad', COUNT(*) AS 'NumOperaciones', MAX(V.FechaVenta) AS 'UltimaVenta'
        FROM Ventas V
        JOIN Clientes C ON V.IdCliente = C.IdCliente
        WHERE V.TipoOrden = '1' and V.Activo = 1  AND V.TipoFolio = '$tipop' AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.Serie != 'COT-' AND V.IdSucursal ='$sucbus'
        GROUP BY V.IdCliente
        ORDER BY V.Utilidad DESC");
        //$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
        }else if($tipop == 0){
            $registro = mysqli_query($conexion,"SELECT V.IdCliente,C.Nombre, SUM(V.Subtotal) AS 'Subtotal', SUM(V.Descuento) AS 'Descuento', SUM(V.Total) AS 'Total', SUM(V.Anticipo) AS 'Cobrado', SUM(V.Saldo) AS 'Pendiente', SUM(V.CostoTotal) AS 'Costo', SUM(V.Utilidad) AS 'Utilidad', COUNT(*) AS 'NumOperaciones', MAX(V.FechaVenta) AS 'UltimaVenta'
            FROM Ventas V
            JOIN Clientes C ON V.IdCliente = C.IdCliente
            WHERE V.TipoOrden = '1' and V.Activo = 1  AND V.FechaBusq BETWEEN '$desde' AND '$hasta' AND V.Serie != 'COT-' AND V.IdSucursal ='$sucbus'
            GROUP BY V.IdCliente
            ORDER BY V.Utilidad DESC");
    }
}

	//CREAMOS NUESTRA VISTA Y LA DEVOLVEMOS AL AJAX
	$contador = mysqli_num_rows($registro);
	$d = mysqli_fetch_array($registro);
	$subtotalt = 0;
	$totalt = 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>Num Cliente</th>
	            	<th>Cliente</th>
	            	<th>Num Operaciones</th>
	                <th>Subtotal</th>
	                <th>Descuento</th>
	                <th>Total</th>
	                <th>Cobrado</th>
	                <th>Pendiente</th>
	                <th>Costo</th>
	                <th>Utilidad</th>
	                <th>Ultima compra</th>
	            </tr></thead>
	            <tbody>';


	$url_actual = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	if($contador >0){
			
		do{
			$folio = $d['IdCliente'];
			$cliente = $d['Nombre'];
			$subtotal = $d['Subtotal'];
			$dcto = $d['Descuento'];
			$total = $d['Total'];
			$cobrado = $d['Cobrado'];
			$saldo = $d['Pendiente'];
			$costo = $d['Costo'];
			$utilidad = $d['Utilidad'];
			$numop = $d['NumOperaciones'];
			$ultv = $d['UltimaVenta'];


			$subtotalt = $subtotalt + $subtotal;
			$dctot = $dctot + $dcto;
            $totalt = $totalt + $total;
			$cobradot = $cobradot + $cobrado;
            $saldot = $saldot + $saldo;
            $costot = $costot + $costo;
            $utilidadt = $utilidadt + $utilidad;
			$subtotal = number_format($subtotal,2,'.',',');
			$total = number_format($total,2,'.',',');
			$costo = number_format($costo,2,'.',',');
			$utilidad = number_format($utilidad,2,'.',',');
			$cobrado = number_format($cobrado,2,'.',',');
			$saldo = number_format($saldo,2,'.',',');


			echo '
			<tr>	
			<td class="'.$folio.'">'.$folio.'</td>
			<td class="'.$folio.'">'.$cliente.'</td>
			<td class="'.$folio.'">'.$numop.'</td>
			<td class="'.$folio.'">$'.$subtotal.'</td>
			<td class="'.$folio.'">'.$dcto.'</td>
			<td class="'.$folio.'">$'.$total.'</td>
			<td class="'.$folio.'">$'.$cobrado.'</td>
			<td class="'.$folio.'">$'.$saldo.'</td>
			<td class="'.$folio.'">$'.$costo.'</td>
			<td class="'.$folio.'">$'.$utilidad.'</td>
			<td class="'.$folio.'">'.$ultv.'</td>

			<tr>
			';			
							

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



?>