<?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 * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
    $registro = mysqli_query($conexion,"SELECT SUM(IF(V.IdSucursal = 1, D.Cantidad, 0)) AS 'CUL',SUM(IF(V.IdSucursal=2, D.Cantidad, 0)) AS 'MAZ',SUM(IF(V.IdSucursal=3, D.Cantidad, 0)) AS 'HMO', D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', P.Nombre as 'Producto' FROM Ventas V
    JOIN VentasDetalle D ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
    JOIN Productos P ON D.CodProd = P.Codigo
    WHERE V.TipoOrden ='1' and P.IdCategoria IN ('3','6') AND V.ImpresoProduccion = 0 AND V.Activo = 1 AND D.Activo = 1 AND D.TiraC = 0
	GROUP BY D.Largo, D.Ancho,Umed,Producto
	ORDER BY Producto, Largo, Ancho DESC");
    /* if($desde != '' && $hasta != ''){
        
        $registro = mysqli_query($conexion,"SELECT D.Cantidad, D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', D.CodProd as 'MOLDURA', V.FechaBusq, V.Serie, V.FolioVenta,CASE WHEN D.TiraC = 1 THEN 'SI' WHEN D.TiraC != 1 THEN 'NO' END AS 'TiraCompleta' FROM VentasDetalle D
        JOIN Ventas V ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
        JOIN Productos P ON D.CodProd = P.Codigo
        WHERE V.FechaBusq BETWEEN '$desde' AND '$hasta' AND D.CodProd != '' AND V.Serie != 'COT-' and P.IdCategoria = '3' and V.ImpresoProduccion = 0
        Order by D.CodProd");
    } */

}else if($sucbus != 0){
    $registro = mysqli_query($conexion,"SELECT SUM(IF(V.IdSucursal = 1, D.Cantidad, 0)) AS 'CUL',SUM(IF(V.IdSucursal=2, D.Cantidad, 0)) AS 'MAZ',SUM(IF(V.IdSucursal=3, D.Cantidad, 0)) AS 'HMO', D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', P.Nombre as 'Producto' FROM Ventas V
    JOIN VentasDetalle D ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
    JOIN Productos P ON D.CodProd = P.Codigo
    WHERE V.TipoOrden ='1' and P.IdCategoria IN ('3','6') AND V.ImpresoProduccion = 0 AND V.Activo = 1 AND D.Activo = 1 AND D.TiraC = 0 AND V.IdSucursal = '$sucbus'
	GROUP BY D.Largo, D.Ancho,Umed,Producto
	ORDER BY Producto, Largo, Ancho DESC");
    //$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
    /* if($desde != '' && $hasta != ''){
        
        $registro = mysqli_query($conexion,"SELECT D.Cantidad, D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'Umed', D.CodProd as 'MOLDURA', V.FechaBusq, V.Serie, V.FolioVenta,CASE WHEN D.TiraC = 1 THEN 'SI' WHEN D.TiraC != 1 THEN 'NO' END AS 'TiraCompleta' FROM VentasDetalle D
        JOIN Ventas V ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
        JOIN Productos P ON D.CodProd = P.Codigo
        WHERE V.FechaBusq BETWEEN '$desde' AND '$hasta' AND D.CodProd != '' AND V.Serie != 'COT-' and P.IdCategoria = '3' AND V.IdSucursal = '$sucbus' and V.ImpresoProduccion = 0
        Order by D.CodProd");
    } */
}

	//CREAMOS NUESTRA VISTA Y LA DEVOLVEMOS AL AJAX
	$contador = mysqli_num_rows($registro);
	$d = mysqli_fetch_array($registro);
    /* $consulta = "SELECT D.Cantidad, D.Largo, D.Ancho, CASE WHEN D.Umed = 1 THEN 'CM' WHEN D.Umed = 2 THEN 'IN' END AS 'UNIDAD DE MEDIDA', D.CodProd as 'MOLDURA', V.FechaBusq, V.Serie, V.FolioVenta,CASE WHEN D.TiraC = 1 THEN 'SI' WHEN D.TiraC != 1 THEN 'NO' END AS 'TiraCompleta' FROM VentasDetalle D
    JOIN Ventas V ON V.Serie = D.Serie AND V.FolioVenta = D.IdVenta
    JOIN Productos P ON D.CodProd = P.Codigo
    WHERE V.FechaBusq BETWEEN '$desde' AND '$hasta' AND D.CodProd != '' AND V.Serie != 'COT-' and P.IdCategoria = '3'
    Order by D.CodProd  "; */
	
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>CUL</th>
					<th>MAZ</th>
	            	<th>Largo</th>
	            	<th>Ancho</th>
	                <th>Unidad Medida</th>
	                <th>Producto</th>
	            </tr></thead>
	            <tbody>';


	$url_actual = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	if($contador >0){
			
		do{
			$cantcul = $d['CUL'];
			$cantmaz = $d['MAZ'];
			$largo = $d['Largo'];
			$ancho = $d['Ancho'];
			$umed = $d['Umed'];
			$moldura = $d['Producto'];


			echo '
			<tr>	
			<td class="'.$folio.'">'.$cantcul.'</td>
			<td class="'.$folio.'">'.$cantmaz.'</td>
			<td class="'.$folio.'">'.$largo.'</td>
			<td class="'.$folio.'">'.$ancho.'</td>
			<td class="'.$folio.'">'.$umed.'</td>
			<td class="'.$folio.'">'.$moldura.'</td>

            <tr>
			';			
							

			
			}while(($d = mysqli_fetch_array($registro)));
		}else{
			echo '<tr>
					<td colspan="8">No se encontraron resultados '.$contador.' '.$desde.' '.$hasta.'</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>';



?>