<?

require_once("comun/funciones.php");
 
 // echo '1';
 if( trim($referencia_cliente)=='' ) return; 
$query=mysqli_query($link,"select distinct booking,idhbl from  hblexpint where referencia = '".$referencia_cliente."'");


//echo 'Partidas 1';
 if(trim($referencia_cliente)!=''){
$refer = busquedaB($link,"select referencia from referencias_veracruz where referencia_cliente = '".trim($referencia_cliente)."'","referencia");
}
?>
<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 colspan="2" style="border:none; background-color:transparent; text-align:left"><b>REFERENCIA PUERTO:<BR /><?=$refer?></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  nowrap="nowrap" >Pago MEI</td>
    <td  >Autorizar      </td>
    <td  nowrap="nowrap" >Fecha/Usuario Autorización</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="150" >Fecha documentaci&oacute;n</td>
    <td width="101" ><div align="center">Fecha de registro</div></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>
  </tr>
  

   <?   $k=1;while($row=mysqli_fetch_array($query)){  
   $array[]=$row[booking];
   $array1[$row[bookingno]]=$row[referencia];
   
   	$esi=0;$sh='';
	
	  //echo $row[idhbl];
	 // echo 'rc '.$referencia_cliente;
  $query2= mysqli_query($link,"select * from detailcargaexpint where bookingno = '".$row[booking]."'");
    
    //$query2= mysqli_query("select * from detailcargaexpint where id_bl = '".$row[idhbl]."'");
    
   if(mysqli_num_rows($query2)>0){
 	/* cambio 25.1.2017 */   
 
	$qry3=mysqli_query($link_impo,"select booking_interteam from booking_canceled where booking_interteam='".$row[booking]."'");
	$cancelado=0;
	
	/* esis dhl 25.6.2018 */ 
 
	/* end o esis dhl */

 
	if(mysqli_num_rows($qry3)>0){
	$cancelado=1;	
	mysqli_query($link,"update hblexpint set status='CANCELADO' where booking = '".$row[booking]."' and booking<>'' "); 
	}
	/* fin de cambio 25.1.2017 */ 
	
   while($row2=mysqli_fetch_array($query2)){

	//echo ' idhbl '.$row2[id] .' '.'&booking '.$row2[bookingno].' idpater '.$row2[id_bl];
	$markandnos=$row2[markandnos];
 
	if($row2[markandnos]=='' and $row2[id_carga_esi_interteam]*1!=0){
		  
			$q1=mysqli_query($link_impo,"select markandnos from detailcargaexpint where id='".$row2[id_carga_esi_interteam]."'");
			 
			if(mysqli_num_rows($q1)>0){
				$rq1=mysqli_fetch_array($q1);
				$markandnos=$rq1[markandnos];
			 
				mysqli_query($link,"update detailcargaexpint  set markandnos='".$rq1[markandnos]."' where 
				id_carga_esi_interteam='".$row2[id_carga_esi_interteam]."'");
				 
			}
 	 
	}
	   $bl = data($link,"select * from hblexpint where idhbl = '".$row2[id_bl]."'");
	   
	   //echo "select * from hblexpint where idhbl = '".$row2[id_bl]."'";
	//echo '1';
	if(trim($bl[status])=='ROLADO' or trim($bl[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;<?=$row2[id_bl] 
   ?>	   
   <tr  class="comun" <?=$style?> >
     <TD align="center" width="41" style="text-align:center;" ><?=$k?>
     <img title="Borrar BL" onclick="
    
    if(confirm('Borra este House BL?')){ 
        document.getElementById('form1').action='../borra_hbl.php?idpater=<?=$row[idhbl]?>';
        document.getElementById('form1').submit()
    }
    " src="https://<?= $_SERVER["SERVER_NAME"]?>/images/eliminar.png"  border="0" style="cursor:pointer" /> 
     </TD>

    
    
     <TD width="150"  align="center" style="text-align:center">
     <? if($row2[tipo_bl]=='MBL'){ ?>
     <input name="<?=$k?>" id="<?=$k?>" type="checkbox" value="../pdf/mbl.php?booking=<?=base64_encode($row2[bookingno])?>&idhbl=<?=base64_encode($row2[id_bl])?>" />
     <a  target="_blank" href="../pdf/mbl.php?booking=<?=base64_encode($row2[bookingno])?>&idhbl=<?=base64_encode($row2[id_bl])?>" style="color:#039;text-align:center;text-decoration:underline; cursor:pointer;" title="Exportar a PDF" id="pdf">Recibase MBL ID <?=$row[idhbl]?></a> 
     <? }else{ ?>
     <input name="<?=$k?>" id="<?=$k?>" type="checkbox" value="../pdf/hbl.php?booking=<?=base64_encode($row2[bookingno])?>&idhbl=<?=base64_encode($row2[id_bl])?>" />
     <a target="_blank" href="../pdf/hbl.php?booking=<?=base64_encode($row2[bookingno])?>&idhbl=<?=base64_encode($row2[id_bl])?>" style="color:#039;text-align:center;text-decoration:underline; cursor:pointer;" title="Exportar a PDF" id="pdf">Recibase HBL ID <?=$row[idhbl]?></a> 
     <? } ?>
     
     </TD>
  
    
  
    <!--<TD colspan="2" align="center" style="text-align:center">
    REVISION</a>
    </TD>-->
    
    <input name="jid_bl[]" type="hidden" id="jid_bl[]" value="<?=$row2[id_bl]?>" />
    <input name="jbookingno[]" type="hidden"  id="jbookingno[]" value="<?=$row2[bookingno]?>" />
    <input name="jtipo[]" type="hidden" id="jtipo[]" value="<?=$row2[tipo_bl]?>"  />
    
     
    <td style="text-align:center"><span <?=$style?> > <a style="text-decoration:underline; color:#039;text-align:center;cursor:POINTER" title="Modificar BL" href="../registro_manifiesto.php?files=<?=$files?>&referencia=<?=$referencia?>&referencia_cliente=<?=$referencia_cliente?>&id=<?=$row2[id]?>&id_bl=<?=$row2[id_bl]?>&booking_interteam=<?=$row2[bookingno]?>"/><?=$row2[bookingno]?></a>
      
      <? if((strpos($bl[registrado_por],'DHL')!==false or strpos($bl[registrado_por],'KUEHNE')!==false or strpos($bl[registrado_por],'JAS ')!==false) or strpos($bl[comentarios],'**ESI ON')!==false){ ?>
      
      <SPAN style="background-color:white; border:2PX SOLID blue; cursor:pointer;text-align:center;">
      <a  title="<?=$sh?>" href="../registro_manifiesto.php?files=<?=$files?>&referencia=<?=$referencia?>&referencia_cliente=<?=$referencia_cliente?>&id=<?=$row2[id]?>&id_bl=<?=$row2[id_bl]?>&booking_interteam=<?=$row2[bookingno]?>&bkg=<?=$bkg?>&esi=1#ancla_esi"/><?=$sh?>&nbsp;<?=$bl[CommunicationReference]=='' ? $bl[CustomerReference] : $bl[CommunicationReference]?>
        <? //$sh//=$sh=='DHL' ? 'DHL' : ''?>&nbsp;</a> </SPAN>
      <? } ?>
      
      
      <? if($row2[booking_cld]!=''){ ?>
      CLD: <?=$row2[booking_cld]?>
      <? } ?>
      
    </span></td>
    
    <input name="TDBOK<?=$k?>" type="hidden"  id="TDBOK<?=$k?>" value="<?=$bl[booking]?>" />
	<input name="TDIDBOK<?=$k?>" type="hidden"  id="TDIDBOK<?=$k?>" value="<?=$bl[idhbl]?>" />
    <input name="TDREG_POR<?=$k?>" type="hidden"  id="TDREG_POR<?=$k?>" value="<?=$bl[registrado_por]?>" />
    <input name="TDPED<?=$k?>" type="hidden"  id="TDPED<?=$k?>" value="<?=$bl[pedimento]?>" />
    
    <td style="text-align:center"  id="divPagoMei<?=$k?>" <?=$bl[pago_mei]=='PENDIENTE' ?
	' bgcolor="red" ' : ($bl[pago_mei]=='PAGADO' ?  ' bgcolor="green" ' : "") ?>   >
	<?= ($bl[autorizacion]!='Autorizar' and $bl[autorizacion]!='NO Autorizar' and $bl[autorizacion]!='')  ? 'NA' : $bl[pago_mei] ?>
    </td>
    <td style="text-align:center">
    <select name="autorizar<?=$k?>" id="autorizar<?=$k?>"
    onchange="/*REG_POR='<?=$bl[registrado_por]?>';PED='<?=$bl[pedimento]?>';TBOK='<?=$bl[booking]?>';
 
     for(t=1;t<=document.getElementById('cantFilas').value;t++){ 
            if(document.getElementById('TDPED'+t).value!=''){
                if(document.getElementById('TDPED'+t).value==PED && document.getElementById('TDPED'+t).value!='' && document.getElementById('TDBOK'+t).value==TBOK){
                document.getElementById('autorizar'+t).value=this.value;   
                }
            } 
            if(document.getElementById('TDPED'+t).value==''){
                if(document.getElementById('TDREG_POR'+t).value==REG_POR && document.getElementById('TDBOK'+t).value==TBOK){ 
                document.getElementById('autorizar'+t).value=this.value;
                }
            }
    }*/ ">
      <option value=""  <?=$bl[autorizacion]=='' ? 'selected="selected"' : ''?>></option>
      <option value="Autorizar" <?=$bl[autorizacion]=="Autorizar" ? 'selected="selected"' : ''?>>Autorizar</option>
      <option value="NO Autorizar" <?=$bl[autorizacion]=="NO Autorizar" ? 'selected="selected"' : ''?>>NO Autorizar</option>
      
         <option value="MEI EXENTO // LESCHACO" <?=$bl[autorizacion]=="MEI EXENTO // LESCHACO" ? 'selected="selected"' : ''?>>MEI EXENTO // LESCHACO</option>
      <option value="MEI EXENTO // DHL" <?=$bl[autorizacion]=="MEI EXENTO // DHL" ? 'selected="selected"' : ''?>>MEI EXENTO // DHL</option>
  	  <option value="MEI EXENTO // DSV" <?=$bl[autorizacion]=="MEI EXENTO // DSV" ? 'selected="selected"' : ''?>>MEI EXENTO // DSV</option>
      <option value="MEI EXENTO // PAC" <?=$bl[autorizacion]=="MEI EXENTO // PAC" ? 'selected="selected"' : ''?>>MEI EXENTO // PAC</option>
 	  <option value="MEI EXENTO // EMERSON" <?=$bl[autorizacion]=="MEI EXENTO // EMERSON" ? 'selected="selected"' : ''?>>MEI EXENTO // EMERSON</option>
      <option value="MEI EXENTO // KN" <?=$bl[autorizacion]=="MEI EXENTO // KN" ? 'selected="selected"' : ''?>>MEI EXENTO // KN</option>
      <option value="MEI EXENTO // SOLIC. ITT" <?=$bl[autorizacion]=="MEI EXENTO // SOLIC. ITT" ? 'selected="selected"' : ''?>>MEI EXENTO // SOLIC. ITT</option>
      <option value="MEI EXENTO // DESP. ITT" <?=$bl[autorizacion]=="MEI EXENTO // DESP. ITT" ? 'selected="selected"' : ''?>>MEI EXENTO // DESP. ITT</option> 
    </select></td>
    <td style="text-align:center"><div id="divAutorizo<?=$k?>">
      <?=$bl[usuario_autorizo].' '.$bl[fecha_usuario_autorizo]?>
    </div></td>
    
    <? $k++;?>
    
    <?
				//$medidas='';
				$volumen='';$numero_bultos=''; 
				$dimen1 =	mysqli_query($link,"select D.* from dimensiones D where D.id_partida = '".$row2[id_bl]."'  ");
				
				if(mysqli_num_rows($dimen1)>0){ 
				   while($rd1=mysqli_fetch_array($dimen1)){
				   $volumen+=$rd1[largo]*$rd1[ancho]*$rd1[alto]*$rd1[numero_bultos]/1000000;
				   //$medidas.= $rd1[largo].'x'.$rd1[ancho].'x'.$rd1[alto].(mysqli_num_rows($dimen1)>0 ? '<br>' : '');
				   $numero_bultos+=$rd1[numero_bultos];
				   }
				}
				/*if(mysqli_num_rows($dimen1)<1 and (strtoupper(strpos($row[registrado_por],'DHL'))!==false 
				or strtoupper(strpos($row[registrado_por],'KUEHNE'))!==false 
				or strtoupper(strpos($row[registrado_por],'JAS '))!==false
				) ){
				$dimen1 =	mysqli_query($link,"select * from detailcargaexpint where bookingno='".$row2[bookingno]."' ");
				$rd1=mysqli_fetch_array($dimen1);
				$numero_bultos=$rd1[bulk];
				//$volumen=$rd1[measurement];
 $weight=$rd1[weight];
				}*/
				
				if(mysqli_num_rows($dimen1)<1 /*and (strtoupper(strpos($row[registrado_por],'DHL'))!==false 
				or strtoupper(strpos($row[registrado_por],'KUEHNE'))!==false
				or strtoupper(strpos($row[registrado_por],'JAS'))!==false)*/ ){
				$dimen1 =	mysqli_query($link,"select * from detailcargaexpint where  id_bl= '".$row2[id_bl]."'");
				//echo "select * from detailcargaexpint where bookingno='".$row1[bookingno]."' and id_bl= '".$row1[id_bl]."' ";
				$rd1=mysqli_fetch_array($dimen1);
				$numero_bultos=$rd1[bulk];
				//$volumen=$rd1[measurement];
 $weight=$rd1[weight];
				}
				
	?>
    <td style="text-align:center"><?=$bl[imo]?></td>
    <td style="text-align:center"><?= $bl[fotos]!='' ? '<a href="'.$bl[fotos].'" target="_blank">Fotos</a>' : ''?></td> 
    <td align="center"><?=$markandnos?></td>
    <td align="center"><?=$row2[weight]?></td>

    
    <td ><div align="center">
    <?=($bl[ultimo_update_golmex]!='0000-00-00 00:00:00' and $bl[ultimo_update_golmex]!='') ? '<span style="color:green; font-style:italic">**' : ''?>
      <?=$numero_bultos?>
      <?=($bl[ultimo_update_golmex]!='0000-00-00 00:00:00' and $bl[ultimo_update_golmex]!='') ? '**</span>' : ''?>
    </div></td>
    <td ><div align="center">
    <?=($bl[ultimo_update_golmex]!='0000-00-00 00:00:00' and $bl[ultimo_update_golmex]!='') ? '<span style="color:green; font-style:italic">**' : ''?>
      <?=number_format($volumen*1,3)?>
      <?=($bl[ultimo_update_golmex]!='0000-00-00 00:00:00' and $bl[ultimo_update_golmex]!='') ? '**</span>' : ''?>
    </div></td>
    <td align="center"><?=$bl[pedimento]?></td>
    <td align="center" width="150" style="text-align:center" ><?=($bl[status]=='' ? 'REVISANDO' : $bl[status])?> </td>
    <td align="center"><div align="center">
      <?=$bl[fechaRegistro]?>
    </div></td>
    <!--<td ><?=$bl[registrado_por]?></td>-->
    <td align="center"><div align="center">
      <?=$bl[revision]?>
    </div></td>
    <td align="center"><div align="center">
      <?=$bl[fecha_revision]?>
    </div></td>
    
 
      <?
		 
	 
		$datos_aa = data($link_impo,"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 = 'ALTAMIRA' or A.registro = 'INTERTEAM') and C.email_contacto LIKE '%".$bl["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" ><span style="font-size:10px">
 
      <?=$bl[registrado_por]?>
    </span></td>
    
    <td align="center" >
	<?=($bl[ultimo_update_golmex]!='0000-00-00 00:00:00' and $bl[ultimo_update_golmex]!='') ? 
	'<span style="color:green; font-style:italic">**Webservice Golmex** '.$bl[ultimo_update_golmex].'</span>' : ''?>
	<?=$bl[comentarios]?></td>
  </tr>

  <? 
   
   }} ?>  

  <? }
  
  $z=$k; ?>  
  
  
 
  <? 
 
	$qry3=mysqli_query($link_impo,"select files from datosexpint where referencia_interteam like '".trim($referencia_cliente)."%'");
	$r8=mysqli_fetch_array($qry3);
	$files=$r8["files"];
	$unicos=array_unique($array);
	 
	$cantidad_bookings = count(array_unique($array));
	
 
	$qry3=mysqli_query($link_impo,"select booking_interteam,customsagent from bookingexpint where files='".$files."' order by booking_interteam");
 
	if($cantidad_bookings<mysqli_num_rows($qry3)){
		while($r8=mysqli_fetch_array($qry3)){
			 //echo $r8[booking_interteam].'  ---> r i '.$array1[$r8[booking_interteam]]. ' rc v '.$referencia_cliente.'<br>';
			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?booking_interteam=<?=$finales[$i]?>"/><?=$finales[$i]?></a>&nbsp;</SPAN> </td>
    <td ></td>
    <td ></td>
    <td ></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*1,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>
  </tr>
    <? $i++;
	} }
		con_ver();
	?>
    
     <? 
	 
	//%%%%%%%%%%%%%%%%%%%%%% BOOKINGS SIN DOCUMENTAR DE REFERENCIAS DE VERACRUZ %%%%%%%%%%%%%%%%%%%%%% 
 
	unset($finales);unset($customsagent);
	$unicos=array_unique($array);
	$cantidad_bookings = count(array_unique($array));
 
	$qry3=mysqli_query($link,"SELECT * FROM booking_ssl where referencia_cliente like '".trim($referencia_cliente)."%' order by booking_ssl");
 
		while($r8=mysqli_fetch_array($qry3)){
			if (! in_array($r8[booking_ssl], $unicos)) {
				$finales[]=$r8[booking_ssl];
				//$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?booking_interteam=<?=$finales[$i]?>"/><?=$finales[$i]?></a>&nbsp;</SPAN> </td>
    <td ></td>
    <td ></td>
    <td ></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*1,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>
  </tr>
    <? $i++;
	} }
		?>
  </table>
<input name="cantFilas" id="cantFilas" type="hidden" value="<?=$z-1?>" />
<div align="left"><span style="width:2000px">
  <input name="input" type="button" value="Guardar" 
    onclick="
    for(t=1;t<=document.getElementById('cantFilas').value;t++){
        realizaProceso(t);
    } "/>
</span></div>
<br />

<br />