<?

require_once("comun/funciones.php");
  //echo '4';
con_ver();
if(trim($referencia)!=''){
 
$referencia_cliente = busquedaB("select referencia_cliente from referencias_veracruz where referencia = '".trim($referencia)."'","referencia_cliente");
}

$queryX = mysql_query("select distinct booking from hblexpint where referencia like '".trim($referencia_cliente)."' and referencia<>''  order by booking");
 if( mysql_num_rows($queryX)==0 ) {//echo '<br>No se encontraron resultados<br>';return;
 }


//echo 'Partidas 4';
?>
<style>
table.tablas_con_bordes_ tr.yellow td {
	 
	background: #FFC;
	 
	font-weight:bold; 
	text-align:center;
	 border:1PX solid #333;
	vertical-align:middle;
	/*border-collapse:collapse*/
	
	}

	table.tablas_con_bordes_ tr.comun td {
	 border:1PX solid #333;
	text-align:left;
	
	vertical-align:middle;
	 
	/*background: #FFC;*/
	}
	
	table.tablas_con_bordes_{
		  border-collapse:collapse;
		 /* border-collapse:collapse;border-collapse:collapse; */
	}
	</style>
 <br />
<table width="2000" id="exportar" border="1" class="tablas_con_bordes_" style="font-size:11px" >

  
  <tr class="yellow" style="text-align:center">

    <td width="36" style="border:none; background-color:transparent;text-align:left" colspan="2" ><B>REFERENCIA CLIENTE:<br /> <?=$referencia_cliente?></B><br />
    <a style="color:#039; text-decoration:underline"  onClick="todos(this);" >Seleccionar todos los bls</a>
    </td>
    <td width="250"  align="center" >Booking</td>
    <td width="100"  >IMO</td>
    <td width="100"  >Fotos</td>
    <td width="262"  >Marcas</td>
    <td width="79"  >Gross weight</td>
    <td width="75" ><div align="center">Quantity</div></td>
    <td width="87" ><div align="center">Volume</div></td>
    <td width="127" >Pedimento entregado</td>
    <td width="124" >Fecha documentaci&oacute;n</td>
    <td width="101" >Fecha de registro</td>
    <!--<td width="124" >Registr&oacute;</td>-->
    <td width="101" >Revisi&oacute;n</td>
    <td width="126" >Fecha Revisi&oacute;n</td>
   <td width="200" >Agencia Aduanal</td>
   <td width="176" >Registrado por</td>
   <td width="176" >Comentarios</td>
       <td width="100"  >Pago MEI</td>
    <td width="100"  >Autorizar 
    <input type="checkbox" name="checkbox" id="checkbox" title="Autorizar" /></td>
    <td width="100"  >Fecha/Usuario Autorización</td>
  </tr>
  

   <? $k = 1;
   
   while($rowx=mysql_fetch_array($queryX)){ $esi=0;$sh='';
 
	con_impo();
	$qry3=mysql_query("select booking_interteam from booking_canceled where booking_interteam='".$rowx[booking]."' and booking_interteam<>''");
	$cancelado=0;
		/* esis dhl 25.6.2018 */ 
	if(strpos($rowx[booking],'EXIT')!==false){
	$queryInt=mysql_query("SELECT booking FROM `bookingexpint` WHERE booking_interteam LIKE '".$rowx[booking]."'");
	 if(mysql_num_rows($queryInt)>0){
		 $rqueryInt=mysql_fetch_array($queryInt);
		 $bkg=$rqueryInt[0];
		 $sql = mysql_query("SELECT *  FROM hblexpint_actualizaciones where booking= '$bkg' order by id desc limit 1"); 
		
		if(mysql_num_rows($sql)>0){
			$rs=mysql_fetch_array($sql);
			$sh=$rs[shipper];
			$sh1=explode(' ',$rs[shipper]);
			$cn=$rs[consignee];
			$n1=$rs[notify];
			$sh='';
			if(strpos($sh,'DHL')!==false or strpos($sh,'DHL')!==false or strpos($sh,'DHL')!==false){
			$sh='DHL';	
			}
			if(strpos($sh,'KUEHNE')!==false or strpos($sh,'KUEHNE')!==false or strpos($sh,'KUEHNE')!==false){
			$sh='KUEHNE';	
			}
			if(strpos($sh,'PANALPINA')!==false or strpos($sh,'PANALPINA')!==false or strpos($sh,'PANALPINA')!==false){
			$sh='PANALPINA';	
			}
			if(strpos($sh,'AGILITY')!==false or strpos($sh,'AGILITY')!==false or strpos($sh,'AGILITY')!==false){
			$sh='AGILITY';	
			}
			if($sh==''){
				$sh=$sh1[0];
			}
			$esi=1;
		}
	 }
	}
	/* end o esis dhl */
	
	con_ver();	
	if(mysql_num_rows($qry3)>0){
	//	echo 'in';
	$cancelado=1;	
	mysql_query("update hblexpint set status='CANCELADO' where booking = '".$rowx[booking]."' and booking<>'' "); 
	//echo "update hblexpint set status='CANCELADO' where booking = '".$rowx[booking]."' and booking<>'' ";
	}
	/* fin de cambio 25.1.2017 */  

   
 
   con_ver();
   if(trim($referencia_cliente)!=''){
	   if($rowx[booking]!=''){
   $query1= mysql_query("select D.*,H.* from detailcargaexpint D,hblexpint H where D.bookingno like '".$rowx[booking]."%' and D.bookingno<>''  
   and H.booking<>'' and H.idhbl = D.id_bl 
   and H.referencia like '".trim($referencia_cliente)."%' and  H.referencia<>'' order by H.idhbl");	   
	   }
 
   }else{
	   $query1= mysql_query("select D.*,H.* from detailcargaexpint D,hblexpint H where D.bookingno = '".$rowx[booking]."' and H.idhbl = D.id_bl and H.referencia = '' order by H.idhbl");
   }
   
   ?>
   
   <? 
   
   while($row1=mysql_fetch_array($query1)){
	 
	//echo '4';
	
	if(trim($row1[status])=='ROLADO' or trim($row1[status])=='CANCELADO' or $cancelado==1 ){ 
		$style='style="color:red; text-decoration:line-through;text-align:center;  background-color:#CECE9D;"';
	}else{
		$style='style="text-align:center;  background-color:#CECE9D;"';
	}
	//echo trim($bl[status]).' '.$style;
	
	$array[]=$row1[bookingno];
	$array1[$row1[bookingno]]=$row1[referencia];
   ?>	   
   <tr class="comun" <?=$style?>  >
    <TD align="center" style="text-align:center;" width="40px"><?=$k?>
     <img align="absbottom" title="Borrar BL" onclick="
    <? if($row1[tipo_bl]=='HBL') { ?>
    if(confirm('Borra este House BL?')){ 
        document.getElementById('form1').action='../borra_hbl.php?idhbl=<?=$row1[id]?>&booking=<?=$row1[bookingno]?>&idpater=<?=$row1[id_bl]?>&referencia=<?=$referencia?>';
        document.getElementById('submit').click();
    }
    <? } elseif($row1[tipo_bl]=='MBL') { ?>
    if(confirm('Borra este Master BL?')){ 
       document.getElementById('form1').action='../borra_mbl.php?idhbl=<?=$row1[id]?>&booking=<?=$row1[bookingno]?>&idpater=<?=$row1[id_bl]?>&referencia=<?=$referencia?>';
       document.getElementById('submit').click();
    }
    <? } ?>" src="http://<?=$_SERVER["SERVER_NAME"]?>/js/img/eliminar.png" border="0" style="cursor:pointer" /> 
    </TD>
    
    <TD align="center" style="text-align:center" width="150px"> 
     <? if($row1[tipo_bl]=='MBL'){ ?>
    <input name="<?=$k?>" id="<?=$k?>" type="checkbox" value="../pdf/mbl.php?booking=<?=base64_encode($row1[bookingno])?>&idhbl=<?=base64_encode($row1[id_bl])?>" />
     <a target="_blank" href="../pdf/mbl.php?booking=<?=base64_encode($row1[bookingno])?>&idhbl=<?=base64_encode($row1[id_bl])?>" style="color:#039;text-align:center;text-decoration:underline; cursor:pointer;" title="Exportar a PDF" id="pdf">Recibase MBL ID <?=$row1[id_bl]?></A> 
     <? }else{ ?>
     
    <input name="<?=$k?>" id="<?=$k?>" type="checkbox" value="../pdf/hbl.php?booking=<?=base64_encode($row1[bookingno])?>&idhbl=<?=base64_encode($row1[id_bl])?>" /> 
     <a  target="_blank" href="../pdf/hbl.php?booking=<?=base64_encode($row1[bookingno])?>&idhbl=<?=base64_encode($row1[id_bl])?>" style="color:#039;text-align:center;text-decoration:underline; cursor:pointer;" title="Exportar a PDF" id="pdf">Recibase HBL ID <?=$row1[id_bl]?></A> 
     <? } ?>
    </TD>
        <input name="jid_bl[]" type="hidden" id="jid_bl[]" value="<?=$row1[id_bl]?>" />
    <input name="jbookingno[]" type="hidden"  id="jbookingno[]" value="<?=$row1[bookingno]?>" />
    <input name="jtipo[]" type="hidden" id="jtipo[]" value="<?=$row1[tipo_bl]?>"  />
    <? $k++;?>
    
    
    <!--<TD align="center" colspan="2" style="text-align:center">
 REVISION</a>
    </TD>-->
    <td style="text-align:center"><a style="text-decoration:underline; color:#039;text-align:center;cursor:POINTER" title="REVISION" href="../registro_manifiesto.php?referencia=<?=$referencia?>&id=<?=$row1[id_bl]?>&id_bl=<?=$row1[id_bl]?>"/><?=$row1[bookingno]?></a>
    
     <? if(strpos($row1[comentarios],'**ESI ON')!==false){ ?>
 
    <SPAN style="background-color:white; border:2PX SOLID blue; cursor:pointer;text-align:center;">
    <a  title="<?=$sh!='DHL' ? $sh : 'DHL'?>" href="../registro_manifiesto.php?bkg=<?=$bkg?>&esi=1&referencia=<?=$referencia?>&id=<?=$row1[id_bl]?>&id_bl=<?=$row1[id_bl]?>#ancla_esi"/>DHL <?//=$sh=='DHL' ? 'DHL' : ''?>&nbsp;<?=$row1[CommunicationReference]=='' ? $row1[CustomerReference] : $row1[CommunicationReference]?></a> </SPAN>
    <? } ?>
    </td>
    <td style="text-align:center"><?=$row1[imo]?></td>
    <td style="text-align:center"><?= $row1[fotos]!='' ? '<a href="'.$row1[fotos].'" target="_blank">Fotos</a>' : ''?></td> 
    <td align="center" ><?=$row1[markandnos]?></td>
    <td align="center"><?=$row1[weight]?></td>
           <?
				//$medidas='';
				$volumen='';$numero_bultos=''; 
				
				
				$dimen1 =	mysql_query("select DISTINCT D.booking,D.largo,D.ancho,D.alto,D.fecha_recepcion,D.hora_registro_ingreso,D.numero_bultos from dimensiones D where D.id_partida = '".$row1[id_bl]."' ");	
				
				 if(mysql_num_rows($dimen1)<1){
				$dimen1 =	mysql_query("select DISTINCT D.booking,D.largo,D.ancho,D.alto,D.fecha_recepcion,D.hora_registro_ingreso,D.numero_bultos from dimensiones D where  booking ='".$row1[bookingno]."' ");
				}
				if(mysql_num_rows($dimen1)>0){ 
				   while($rd1=mysql_fetch_array($dimen1)){
				   $volumen+=$rd1[largo]*$rd1[ancho]*$rd1[alto]*$rd1[numero_bultos]/1000000;
				   //$medidas.= $rd1[largo].'x'.$rd1[ancho].'x'.$rd1[alto].(mysql_num_rows($dimen1)>0 ? '<br>' : '');
				   $numero_bultos+=$rd1[numero_bultos];
				   }
				}
				
	?>
    
    <td><div align="center">
      <?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '<span style="color:green; font-style:italic">' : ''?>
	  <?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '**' : ''?><?=$numero_bultos?><?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '**' : ''?>
	  <?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '</span>' : ''?>
    </div></td>
    <td ><div align="center"> 
	<?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '<span style="color:green; font-style:italic">' : ''?>
      <?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '**' : ''?>
		<?=number_format($volumen,3)?>
		 <?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '**' : ''?>
	  <?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? '</span>' : ''?>
    </div></td>
    <td ><?=$row1[pedimento]?></td>
    <td width="150" style="text-align:center" ><?= ($row1[status]=='' ? 'REVISANDO' : $row1[status])?> </td>
    <td align="center"><div align="center">
      <?=$row1[fechaRegistro]?>
    </div></td>
    <!--<td ><?=$row1[registrado_por]?></td>-->
    <td ><div align="center">
      <?=$row1[revision]?>
    </div></td>
    <td ><div align="center">
      <?=$row1[fecha_revision]?>
    </div></td>
     <?
		 
		con_impo();
	

		$datos_aa = data("SELECT A.*,C.nombre_contacto,C.telefono_contacto FROM `agentes_aduanales` A 
		INNER JOIN contactos_agentes_aduanales C
		on A.patente=C.patente and A.id=C.idaa and (A.registro = 'VERACRUZ' or A.registro = 'INTERTEAM') and C.email_contacto LIKE '%".$row1["registrado_por"]."%' ");

		$agente_aduanal  = $datos_aa[nombre_agente_aduanal];
		$nombre_empresa  = $datos_aa[nombre_empresa];
		$nombre_contacto  = $datos_aa[nombre_contacto];
		$telefono_contacto  = $datos_aa[telefono_contacto];
		con_ver();
		?>
        
    <td style="font-size:10px"><?=$nombre_empresa?></td>
    <td align="center" ><?=$row1[registrado_por]?></td>
    <td >
	<?=($row1[ultimo_update_golmex]!='0000-00-00 00:00:00' and $row1[ultimo_update_golmex]!='') ? 
	'<span style="color:green; font-style:italic">**Webservice Golmex** '.$row1[ultimo_update_golmex].'</span>' : ''?>
	<?=str_replace("\n","<br>",$row1[comentarios])?></td>
    
         <td style="text-align:center"> 
      <div id="divPagoMei<?=$k?>"><?=$bl[pago_mei]?></div>
    </td>
    <td style="text-align:center"><select name="autorizar<?=$k?>" id="autorizar<?=$k?>">
      <option value=""  <?=$bl[autorizar]=='' ? 'selected="selected"' : ''?>></option>
      <option value="1" <?=$bl[autorizar]==1 ? 'selected="selected"' : ''?>>Autorizar</option>
      <option value="0" <?=$bl[autorizar]==1 ? 'selected="selected"' : ''?>>NO Autorizar</option>
    </select></td>
    <td style="text-align:center">
     <div id="divAutorizo<?=$k?>2">
      <?=$bl[autorizo]?>
    </div></td>
    
  </tr>

 
  <? } 
mysql_free_result( $query1);
  
  } ?> 
  
  
    <? 
	con_impo();
	$qry3=mysql_query("select files from datosexpint where referencia_interteam like '".trim($referencia_cliente)."%'");
	//ECHO "select files from datosexpint where referencia_interteam like '".trim($referencia_cliente)."%'";
	$r8=mysql_fetch_array($qry3);
	$files=$r8["files"];
	$unicos=array_unique($array);
	 
	$cantidad_bookings = count(array_unique($array));
	
	 
	$qry3=mysql_query("select booking_interteam,customsagent from bookingexpint where files='".$files."' order by booking_interteam");
//	ECHO "select booking_interteam,customsagent from bookingexpint where files='".$files."' order by booking_interteam";
	 
	if($cantidad_bookings<mysql_num_rows($qry3)){
		while($r8=mysql_fetch_array($qry3)){
			 
			if (! in_array($r8[booking_interteam], $unicos) and ($array1[$r8[booking_interteam]]==$referencia_cliente or !isset($array1[$r8[booking_interteam]]))) {
				$finales[]=$r8[booking_interteam];
				$customsagent[]=$r8[customsagent];
			}
		}
		
	}
	
	 

	$i=0;	
	if(count($finales)>0){
	while($i<count($finales)){
		?>
    <tr class="comun" <?=$style?>  >
    <TD align="center" style="text-align:center;" width="40px"><?=$k?></TD>
    
    <TD align="center" style="text-align:center" width="150px"></TD>
    
    <? $k++;?>
    
  
    <td style="text-align:center"> <SPAN style="background-color:#FFF; COLOR:blue; border:2PX SOLID blue">&nbsp;<a style="text-decoration:underline; color:#039;text-align:center;cursor:POINTER" title="REVISION" href="../registro_manifiesto3.php?referencia=<?=$referencia?>&referencia_cliente=<?=$referencia_cliente?>&booking_interteam=<?=$finales[$i]?>"/><?=$finales[$i]?></a>&nbsp;</SPAN> </td>
    <td style="text-align:center">&nbsp;</td>
    <td style="text-align:center">&nbsp;</td> 
    <td align="center" > <SPAN style="background-color:#FFF; COLOR:blue; border:2PX SOLID blue">&nbsp;SIN DOCUMENTAR&nbsp;</SPAN></td>
    <td align="center"><?=$row1[weight]?></td>
         
    
    <td ><div align="center">
      <? //=$numero_bultos?>
    </div></td>
    <td ><div align="center">
      <? //=number_format($volumen,3)?>
    </div></td>
    <td > </td>
    <td width="150" style="text-align:center" ></td>
    <td align="center"><div align="center">
      <?=$row1[fechaRegistro]?>
    </div></td>
  
    <td ><div align="center">
     
    </div></td>
    <td ><div align="center">
      
    </div></td>
    <td style="font-size:10px"> <SPAN style="background-color:#FFF; COLOR:blue; border:2PX SOLID blue">&nbsp;<?=$customsagent[$i]?>&nbsp;</SPAN></td>
    <td align="center" > </td>
    <td > </td> <td > </td> <td > </td> <td > </td>
  </tr>
    <? $i++;
	} }
		con_ver();
	?>

     <? 
	 
	//%%%%%%%%%%%%%%%%%%%%%% BOOKINGS SIN DOCUMENTAR DE REFERENCIAS DE VERACRUZ %%%%%%%%%%%%%%%%%%%%%% 
	con_ver();
 
	unset($finales);unset($customsagent);
	$unicos=array_unique($array);
	$cantidad_bookings = count(array_unique($array));

	$qry3=mysql_query("SELECT * FROM booking_ssl where referencia_cliente like '".trim($referencia_cliente)."%' order by booking_ssl");
	
 
		while($r8=mysql_fetch_array($qry3)){
			if (! in_array($r8[booking_ssl], $unicos)) {
				$finales[]=$r8[booking_ssl];
				 
			}
		}
		
 

	$i=0;	
	if(count($finales)>0){
	while($i<count($finales)){
		?>
    <tr class="comun" <?=$style?>  >
    <TD align="center" style="text-align:center;" width="40px"><?=$k?></TD>
    
    <TD align="center" style="text-align:center" width="150px"></TD>
    
    <? $k++;?>
    
  
    <td style="text-align:center"> <SPAN style="background-color:#FFF; COLOR:blue; border:2PX SOLID blue">&nbsp;<a style="text-decoration:underline; color:#039;text-align:center;cursor:POINTER" title="REVISION" href="../registro_manifiesto3.php?referencia=<?=$referencia?>&referencia_cliente=<?=$referencia_cliente?>&booking_interteam=<?=$finales[$i]?>"/><?=$finales[$i]?></a>&nbsp;</SPAN> </td>
    <td style="text-align:center">&nbsp;</td>
    <td style="text-align:center">&nbsp;</td> 
    <td align="center" > <SPAN style="background-color:#FFF; COLOR:blue; border:2PX SOLID blue">&nbsp;SIN DOCUMENTAR&nbsp;</SPAN></td>
    <td align="center"><?=$row1[weight]?></td>
         
    
    <td ><div align="center">
      <? //=$numero_bultos?>
    </div></td>
    <td ><div align="center">
      <? //=number_format($volumen,3)?>
    </div></td>
    <td > </td>
    <td width="150" style="text-align:center" ></td>
    <td align="center"><div align="center">
      <?=$row1[fechaRegistro]?>
    </div></td>
  
    <td ><div align="center">
     
    </div></td>
    <td ><div align="center">
      
    </div></td>
    <td style="font-size:10px"></td>
    <td align="center" > </td>
    <td > </td> <td > </td> <td > </td> <td > </td>
  </tr>
    <? $i++;
	} }
		?>
  </table>
  
 <div style="width:2000px" align="right"><input name="" type="button" value="Guardar" /></div>
<br />