<?php
include('conexion.php');
$area = $_SESSION['id_area'];
date_default_timezone_set('America/Mazatlan');
$fech = date("Y-m-d");
//EJECUTAMOS LA CONSULTA DE BUSQUEDA

	//$registro = mysqli_query($conexion,"SELECT * FROM factGDL JOIN  ORDER BY IdCliente ASC ");
	$registro = mysqli_query($conexion,"SELECT V.IdVenta AS 'Consecutivo',V.FechaVenta AS 'FechaVenta', V.Serie AS 'Serie', V.FolioVenta AS 'Folio' ,V.IdSucursal AS 'Sucursal',V.IdCliente AS 'Cliente' ,V.Subtotal AS 'Subtotal' ,V.Descuento AS 'Descuento',V.Total AS 'Total',V.Anticipo AS 'Anticipo',V.Saldo AS 'Saldo',V.Obs AS 'Obs',V.HoraEntrega,V.FechaEntrega,C.Nombre AS 'NomCliente',C.Domicilio AS 'DomCliente',V.NumFact,V.EdoFact,V.FechaBusq,V.EstadoPago,V.EstadoOrden,V.TipoOrden,V.Activo  FROM Ventas V 

		JOIN Clientes C ON V.IdCliente = C.IdCliente
		WHERE V.Activo = 1 AND V.FechaBusq BETWEEN '$fech' AND '$fech' AND V.TipoOrden = '1'  ORDER BY V.IdVenta 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 " style="font-size:14px;width:100%;">
	        	<thead>
	        	<tr class="pagado">
	            	<th colspan="14" style="font-size:20px;" >Busqueda por folio</th>
	            </tr>

	        	<tr >
	        		<th ></th>
	            	<th >Folio</th>
	            	<th >Fecha</th>
	            	<th >Cliente</th>
	            	<th >Sucursal</th>
	                <th >Total</th>
	                <th>Anticipo</th>
	                <th >Saldo</th>
	                <th >Fecha Entrega</th>
	                <th colspan="3">Acciones</th>
	                <th  colspan="1">Factura</th>
	                <th  colspan="1">Pago</th>
	                <th  colspan="1">Estado</th>
	            </tr></thead>
	            <tbody>';


	$url_actual = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
	if($contador >0){
			
		do{
			$edopp = $d['EstadoPago'];
			$ed = $d['EstadoOrden'];
			$EdoFact= $d['EdoFact'];
			$folio= $d['Serie'].$d['Folio'];
			$s = $d['Serie'];
			$f = $d['Folio'];
			$fechav= $d['FechaVenta'];
			$suc= $d['Sucursal'];
			if($suc == 1){
			    $sucn = 'CUL';
			}else if ($suc == 2){
			    $sucn = 'MAZ';
			}else if($suc == 3){
			    $sucn = 'HER';
			}
			$total = $d['Total'];
			$anti = $d['Anticipo'];
			$saldo = $d['Saldo'];
			$fechae = $d['FechaEntrega'];
			$id = $d['Consecutivo'];
			$fact = $d['NumFact'];
			$cliente = $d['NomCliente'];

			$subtotalg = $subtotalg + $total;
			$subtotald = $subtotald + $anti;
			$subtotalt = $subtotalt + $saldo;
			$total = number_format($total,2,'.',',');
			$anti = number_format($anti,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.'">'.$folio.'</td>
			<td class="'.$id.'">'.$fechav.'</td>
			<td class="'.$id.'">'.$cliente.'</td>
			<td class="'.$id.'">'.$sucn.'</td>
			<td class="'.$id.'">$'.$total.'</td>
			<td class="'.$id.'">$'.$anti.'</td>
			<td class="'.$id.'">$'.$saldo.'</td>
			<td class="'.$id.'">'.$fechae.'</td>
			<td class="'.$id.'"><a href="formato.php?serie='.$s.'&folio='.$f.'" target="_blank"><img src="imagenes/impr.png"></a></td>
			<td class="'.$id.'"><a href="editarorden.php?id='.$id.'" target="_blank"><img src="imagenes/edit.png"></a></td>
			<td class="'.$id.'"><a href="#" onclick="cancel('.$id.')" target="_blank"><img src="imagenes/cancel.png"></a></td>
			';

			if($fact == NULL){
				echo'<td id="f'.$id.'" class="'.$id.'"><button onclick="caja('.$id.')" class="btn btn-danger">Factura</button></td>
				';
			}else{
				echo'<td class="'.$id.'">'.$fact.'</td>
				';
				
			}

			if($edopp == 0){
			echo'<td class="'.$id.'"><input type="button" name="editar" value="Pagar" id="npagado" onclick="pagoeditar('.$id.')" class="btn btn-primary" data-toggle="modal" data-target="#modalAsistencia"></td>
					
				';
			}else if($edopp == 1){
				echo'<td class="'.$id.'"><input type="button" name="editar" value="Pagar" id="pagoparcial" onclick="pagoeditar('.$id.')" class="btn btn-warning" data-toggle="modal" data-target="#modalAsistencia"></td>
					
				';
			}else if($edopp == 2){
				echo'<td class="'.$id.'"><input type="button" name="editar" value="Pagado" id="Pagado" onclick="pagoeditar('.$id.')" class="btn btn-success" data-toggle="modal" data-target="#modalAsistencia" disabled></td>
					
				';
			}
			
			echo'<td class="'.$id.'"><select id="s'.$id.'" style="width:150px" onchange="estadoguardar('.$id.')" >
						<option value="-1" >Selecciona el estado del pedido</option>
						<option value="0" '.(($ed=='0')?'selected="selected"':"").'>Pedido capturado</option>
						<option value="1" '.(($ed=='1')?'selected="selected"':"").'>En proceso TALLER</option>
						<option value="2" '.(($ed=='2')?'selected="selected"':"").'>En bodega CULIACAN</option>
						<option value="3" '.(($ed=='3')?'selected="selected"':"").'>En transito</option>
						<option value="4" '.(($ed=='4')?'selected="selected"':"").'>En sucursal destino</option>
						<option value="5" '.(($ed=='5')?'selected="selected"':"").'>Listo para entrega</option>
						<option value="6" '.(($ed=='6')?'selected="selected"':"").'>Entregado</option>
					</select></td>
					
				';
			
				
					

			
			}while(($d = mysqli_fetch_array($registro)));
		}else{
			echo '<tr>
					<td colspan="10">No se encontraron resultados</td>
				</tr>';
			}

	$subtotalg = number_format($subtotalg,2,'.',',');
	$subtotald = number_format($subtotald,2,'.',',');
	$subtotalt = number_format($subtotalt,2,'.',',');
	echo '<tr>
				<td colspan="5"></td>
				<td>$'.$subtotalg.'</td>
				<td>$'.$subtotald.'</td>
				<td>$'.$subtotalt.'</td>
				<td colspan="5"></td>
			</tr>';
	echo '<tr>
				<td colspan="10"></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>
				<td colspan="4"></td>
			</tr>';
	echo '</tbody></table></div>';


?>