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

//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.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,D.TipoFolio FROM Movimientos V 

			JOIN Ventas D ON D.Serie = V.Serie AND D.FolioVenta = V.FolioVenta

	    	WHERE V.Activo = 1 AND V.FechaB BETWEEN '$desde' AND '$hasta' AND V.TipoMovimiento != '4' AND D.TipoFolio = '$tipop'  ORDER BY V.Fecha ASC");
	}else if($tipop == 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 '$desde' AND '$hasta' AND V.TipoMovimiento != '4'   ORDER BY V.Fecha ASC");
	}
}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.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,D.TipoFolio FROM Movimientos V 

			JOIN Ventas D ON D.Serie = V.Serie AND D.FolioVenta = V.FolioVenta

	    	WHERE V.Activo = 1 AND V.FechaB BETWEEN '$desde' AND '$hasta' AND V.TipoMovimiento != '4' AND D.TipoFolio = '$tipop' AND V.IdSucursal = '$sucbus' AND D.Activo = 1  ORDER BY V.Fecha ASC");
	}else if($tipop == 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 '$desde' AND '$hasta' AND V.TipoMovimiento != '4' AND V.IdSucursal = '$sucbus'  ORDER BY V.Fecha ASC");
	}
}



$d = mysqli_query($conexion,"SELECT * FROM Arqueo WHERE Fecha BETWEEN '$desde' AND '$hasta' AND IdSucursal = '$sucbus' order by IdArqueo desc limit 1");
if(mysqli_num_rows($d)>0){
$a = mysqli_fetch_array($d);
$cod = $a['IdArqueo'];
$mpeso = $a['Muno'];
$mdos = $a['Mdos'];
$mcin = $a['Mcinco'];
$mdiez = $a['Mdiez'];
$mveinte = $a['Mveinte'];
$bveinte = $a['Bveinte'];
$bcin = $a['Bcincuenta'];
$bcien = $a['Bcien'];
$bdosc = $a['Bdosc'];
$bquin = $a['Bquin'];
$cheque = $a['cheque'];
$total = $a['Saldo'];
$totali = $a['SaldoInicial'];
$flag = 1;

$j = mysqli_query($conexion,"SELECT * FROM Movimientos WHERE FechaB BETWEEN '$desde' AND '$hasta' AND Activo = 1 AND IdSucursal = '$sucbus'");
$totale = 0;
while($f = mysqli_fetch_array($j)){
    $anti = $f['Anticipo'];
    $totale = $anti + $totale;
    $tipomov = $f['TipoMovimiento'];
    $mp = $f['MetodoPago'];
    if($tipomov == 2){
        $salida += $f['Total'];
    }else{
        if($mp == 1){
            $tefe = $tefe + $anti;
        }else if($mp== 2){
            $tch = $tch + $anti;
        }else if($mp == 3){
            $ttar = $ttar + $anti;
        }
    }

}
$totalf = ($totali + $tefe) - $salida;
$dif = $total - $totalf;
$totalf = number_format($totalf,2,'.',',');
$totale = number_format($totale,2,'.',',');
$salidas = number_format($salida,2,'.',',');
$efectivo = number_format($tefe,2,'.',',');
$cheques = number_format($tch,2,'.',',');
$tarj = number_format($ttar,2,'.',',');

$total = number_format($total,2,'.',',');


$dif = number_format($dif,2,'.',',');

echo '
<div align="center">
<table class="table table-striped table-condensed table-hover" style="font-size:14px;width:100%;">
<tr>
            <td > M1:</td>
            <td ><input type="number" id="mpesoe" value="'.$mpeso.'" class="arqueo" disabled></td>
            <td > M2:</td>
            <td ><input type="number" id="mdose" value="'.$mdos.'" class="arqueo" disabled></td>
            <td > M5:</td>
            <td ><input type="number" id="mcine" value="'.$mcin.'" class="arqueo" disabled></td>
            <td > M10:</td>
            <td ><input type="number" id="mdieze" value="'.$mdiez.'" class="arqueo" disabled></td>
            <td > M20:</td>
            <td ><input type="number" id="mveintee" value="'.$mveinte.'" class="arqueo" disabled></td>
        </tr>
        <tr>
            <td > B20:</td>
            <td ><input type="number" id="bveintee" value="'.$bveinte.'" class="arqueo" disabled></td>
            <td > B50:</td>
            <td ><input type="number" id="bcine" value="'.$bcin.'" class="arqueo" disabled></td>
            <td > B100:</td>
            <td ><input type="number" id="bciene" value="'.$bcien.'" class="arqueo" disabled></td>
            <td > B200:</td>
            <td ><input type="number" id="bdosce" value="'.$bdosc.'" class="arqueo" disabled></td>
            <td > B500:</td>
            <td ><input type="number" id="bquine" value="'.$bquin.'" class="arqueo" disabled></td>
        </tr>
        <tr>
            
            <td ><input type="number" id="cod" value="'.$cod.'" class="arqueo" hidden></td>            
            <td colspan="5"></td>
           
        </tr>
        <tr>
            <td colspan="10"> ENTRADAS</td>

        </tr>
        <tr>
            <td colspan="1">Efectivo:</td>
            <td colspan="2">'. $efectivo.'</td>
            
            <td> Depositos:</td>
            <td colspan="2"> '.$cheques.'</td>
            
            
            <td>Tarjeta:</td>
            <td colspan="1">'.$tarj.'</td>

            
        </tr>
        <tr>
            <td colspan="10"> RESUMEN</td>

        </tr>
        <tr>
            <td colspan="1">Entradas:</td>
            <td colspan="1">'.$totale.'</td>
            <td> Saldo Inicial:</td>
            <td> '.$totali.'</td>
            <td>Salidas:</td>
            <td>'.$salidas.'</td>
            

            
            

        </tr>
        <tr>
            <td>Total Caja:</td>
            <td>'.$totalf.'</td>
            <td>Total Arqueo:</td>
            <td>'.$total.'</td>
            <td>Diferencia:</td>


            <td>'.$dif.'</td>
        </tr>
        </table>
        ';
}else{
    $flag = 2;
}


	//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-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>';*/
	echo '</tbody></table></div>';



?>