<?php
include('conexion.php');
date_default_timezone_set('America/Mazatlan');
$fech = date("Y-m-d");
$nom = $_SESSION['nom_admin'];
$iduser = $_SESSION['id_user'];
$suc = $_SESSION['IdSuc'];
$tipou= $_SESSION['tipouser'];
$sucbus = $_POST['sucbus'];
$flagb = $_POST['flagb'];

//EJECUTAMOS LA CONSULTA DE BUSQUEDA

	//$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
 if($sucbus == 0){
 	if($flagb == 1){
 		$registro = mysqli_query($conexion,"SELECT V.IdMovimiento AS 'Consecutivo',V.Fecha AS 'Fecha', V.Serie AS 'Serie', V.FolioVenta AS 'Folio' ,V.IdSucursal AS 'Sucursal',V.Total AS 'Total',V.Anticipo AS 'Anticipo',V.Saldo AS 'Saldo',V.IdUsuario,V.Descripcion,V.TipoMovimiento,V.FechaB FROM Movimientos V 

    	WHERE V.Activo = 1 AND V.FechaB BETWEEN '$fech' AND '$fech' AND V.TipoMovimiento != '4' AND V.IdSucursal = '$suc'  ORDER BY V.Fecha ASC");
 	}else{
 		$registro = mysqli_query($conexion,"SELECT V.IdMovimiento AS 'Consecutivo',V.Fecha AS 'Fecha', V.Serie AS 'Serie', V.FolioVenta AS 'Folio' ,V.IdSucursal AS 'Sucursal',V.Total AS 'Total',V.Anticipo AS 'Anticipo',V.Saldo AS 'Saldo',V.IdUsuario,V.Descripcion,V.TipoMovimiento,V.FechaB FROM Movimientos V 

    	WHERE V.Activo = 1 AND V.FechaB BETWEEN '$fech' AND '$fech' AND V.TipoMovimiento != '4'  ORDER BY V.Fecha ASC");
 	}
 }else if($sucbus != 0){
 		$registro = mysqli_query($conexion,"SELECT V.IdMovimiento AS 'Consecutivo',V.Fecha AS 'Fecha', V.Serie AS 'Serie', V.FolioVenta AS 'Folio' ,V.IdSucursal AS 'Sucursal',V.Total AS 'Total',V.Anticipo AS 'Anticipo',V.Saldo AS 'Saldo',V.IdUsuario,V.Descripcion,V.TipoMovimiento,V.FechaB FROM Movimientos V 

    	WHERE V.Activo = 1 AND V.FechaB BETWEEN '$fech' AND '$fech' AND V.TipoMovimiento != '4' AND V.IdSucursal = '$sucbus' ORDER BY V.Fecha 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 class="table table-striped table-condensed table-hover" style="font-size:14px;width:100%;">
	        	<thead>
	        	<tr class="pagado">
	            	<th colspan="14" style="font-size:20px;" >Busqueda general</th>
	            </tr>

	        	<tr >
	        		<th ></th>
	            	<th >Fecha</th>
	            	<th >Descripcion</th>
	            	<th ></th>
	                <th >Venta</th>
	                <th>Entrada</th>
	                <th >Saldo</th>
	                <th>Salida</th>
	            </tr></thead>
	            <tbody>';


	$url_actual = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	if($contador >0){
			
		do{
			$ea = 0;
			$total = 0;
			$saldo = 0;
			$i = 0;
			$fecha= $d['Fecha'];
			$des = $d['Descripcion'];
			$i = $d['Anticipo'];
			$s = $d['Serie'];
			$f = $d['Folio'];
			
			$tipo = $d['TipoMovimiento'];
			if($tipo == 2){
				$ea = $d['Total'];
			}else{
				$total = $d['Total'];
				$saldo = $d['Saldo'];
			}
			$suc= $d['Sucursal'];
			if($suc == 1){
			    $sucn = 'CUL';
			}else if ($suc == 2){
			    $sucn = 'MAZ';
			}else if($suc == 3){
			    $sucn = 'HER';
			}
			
			$id = $d['Consecutivo'];

			$subtotalg = $subtotalg + $i;
			$subtotalu = $subtotalu + $ea;
			$subtotalt = $subtotalt + $total;
			$subtotals = $subtotals + $saldo;
			$acu = $subtotalg - $subtotalu;
			$total = number_format($total,2,'.',',');
			$i = number_format($i,2,'.',',');
			$ea = number_format($ea,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.'">'.$fecha.'</td>
			<td class="'.$id.'">'.$des.'</td>
			<td class="'.$id.'"></td>
			<td class="'.$id.'">$'.$total.'</td>
			<td class="'.$id.'">$'.$i.'</td>
			<td class="'.$id.'">$'.$saldo.'</td>
			<td class="'.$id.'">$'.$ea.'</td>


			';			
			if($tipo == 1){
				if($suc == 1){
					echo '<td class="'.$id.'"><a href="formato.php?serie='.$s.'&folio='.$f.'" target="_blank"><img src="imagenes/impr.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>';
					}
			}					

			
			}while(($d = mysqli_fetch_array($registro)));
		}else{
			echo '<tr>
					<td colspan="10">No se encontraron resultados</td>
				</tr>';
			}
	$tota = $subtotalg - $subtotalu;
	$subtotalg = number_format($subtotalg,2,'.',',');
	$subtotalu = number_format($subtotalu,2,'.',',');
	$subtotalt = number_format($subtotalt,2,'.',',');
	$subtotals = number_format($subtotals,2,'.',',');
	
	$totf = number_format($tota,2,'.',',');
	echo '
			<tr>
			<td colspan="6"></br></td>
			</tr>
			<tr>
				<td colspan="4"></td>
				<td>$'.$subtotalt.'</td>
				<td>$'.$subtotalg.'</td>
				<td>$'.$subtotals.'</td>
				<td>$'.$subtotalu.'</td>
				<td>$'.$totf.'</td>
			</tr>';
	/*echo '<tr>
				<td colspan="3"></td>
				<td>Accion: </td>
				<td><select id="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 id="aplicaracc" onclick="revisado()" >Aceptar</button></td>
				
			</tr>';*/
	$totales = '
	<div align="center">
	<table class="table table-striped table-condensed table-hover" style="font-size:14px;width:100%;">
					<thead>
	
					<tr >
					<th ></th>
						<th ></th>
						
						<th ></th>
						
						<th ></th>
						<th >Venta</th>
						<th>Entrada</th>
						<th >Saldo</th>
						<th>Salida</th>
					</tr></thead>
					<tbody>
	<tr>
		<td colspan="4"></td>
		<td>$'.$subtotalt.'</td>
		<td>$'.$subtotalg.'</td>
		<td>$'.$subtotals.'</td>
		<td>$'.$subtotalu.'</td>
		<td>$'.$totf.'</td>
		</tr>
		</tbody></table></div>';
	echo '</tbody></table></div>'.'|'.$totales;



?>