<? 
 
$booking = str_replace('\"','"',$booking);
$booking = str_replace("\'","'",$booking);
$txtbooking=$booking;
$referencia_cliente = str_replace('\"','"',$referencia_cliente);
$referencia_cliente = str_replace("\'","'",$referencia_cliente);
$referencia = str_replace('\"','"',$referencia);
$referencia = str_replace("\'","'",$referencia);
include("../comun/conexion3.php");
include("../comun/conex_ver.php");
include("../comun/funciones.php");
$link_impo=con_impo();$link=con_ver();
$tmp= explode('/', dirname(__FILE__)) ; 
$directorio_principal= $tmp[4];	
include("constantes.php"); 

global $tbulk,$tweight,$tnetweight;
global $tipos_de_imo;
global $buque,$viaje,$ets,$cliente,$booking_naviera,$pod,$naviera,$almacen,$procedencia_vacio,$almacen_consolidacion,$desde,$hacia,$tipo_mercancia,$pedimento,$linea,$contenedor,$dimension,$sello,$tipo,$tara,$destino_final;
//EXIT690113-HAM
con_ver();
if(trim($referencia)!=''){
$referencia_cliente = busquedaB($link,"select referencia_cliente from referencias_veracruz where referencia = '".trim($referencia)."'","referencia_cliente");
}
if($referencia_cliente==''){
	con_impo();
	$files = busquedaB($link_impo,"select files from bookingexpint where booking_interteam = '".trim($booking)."' ","files");
	$referencia_cliente = busquedaB($link_impo,"select referencia_interteam from datosexpint where files = '".trim($files)."' and referencia_interteam<>''","referencia_interteam");
}

if(trim($referencia)==''){
con_ver();	
$referencia = busquedaB($link,"select referencia from referencias_veracruz where referencia_cliente = '".trim($referencia_cliente)."'","referencia");
//echo "select referencia from referencias_veracruz where referencia_cliente = '".trim($referencia_cliente)."'";	
}
//echo 'ref '.$referencia;
if($referencia_cliente!=''){
	DATOSBOOKING($link,$link_impo,trim($referencia_cliente));
	con_impo();
	$q1=data($link_impo,"select files from datosexpint where referencia_interteam = '".$referencia_cliente."' or referencia_interteam = '".$referencia."'");
	
	$freightcode="FREIGHT PREPAID";
	
	

	$Tbulk=0;
	$Tnetweight=0;
	$Tcu=0;
}

?>
<page backtop="5mm" backbottom="10mm" backleft="10mm" backright="10mm">
    <page_header>
<table style="width: 100%;">
            <tr>
                <td style="text-align: left;    width: 1%">&nbsp;</td>
                <td style="text-align: LEFT;    width: 100%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="text-align:LEFT;font-size: 14px; font-weight: bold">
                MANIFIESTO&nbsp;<?=$referencia_cliente?>&nbsp;
                <span style="text-align:LEFT;font-weight: bold;font-size: 11px;">
                <?=$cliente?> / <?=$pod?>
                </span></span>
                <br />
               
</td>
                <td style="text-align: right;    width: 15%">&nbsp;</td>
            </tr>
             
      </table>
    </page_header>
    
    <page_footer>
<table style="width: 100%; border: solid 1px black;">
            <tr>
        <td align="left" style="text-align: left;    width: 50%"></td>
                <td style="text-align: left;    width: 50%"></td>
                <td style="text-align: right;    width: 50%">PAGE [[page_cu]]/[[page_nb]]</td>
            </tr>
      </table>
    </page_footer>
<BR />
 <div align="center">
      <span style="text-align: left;width: 30%"><img src="<?="https://".$_SERVER["SERVER_NAME"]?>/<?=$directorio_principal?>/images/logo_lpv.jpg" />
      </span> 
 </div>

<br />
 <table style="width: 100%;border: solid 1px #5544DD;border-collapse:collapse;" align="left" >
     
    <tr>
        <td  style="text-align: left; border: solid 1px #55DD44;">
        <div style="width:370px">
                <b>BUQUE:</b> <? echo $buque; ?> <br />
                
                <b>SSL BOOKING:</b> <?=$booking_naviera?><br />
                
                <b>PUERTO:</b>       <? echo $pod;?> <br />
                <b>ETS:</b> 
                <? echo $ets;?>
        </div> 
      </td>
        
        <td   style="text-align: left; border: solid 1px #55DD44;">
          <div style="width:355px">
          			 <b>CONTENEDOR:</b> &nbsp;
                            <?=$contenedor?>
                            <br />
                            <b>SELLO: </b> &nbsp;
                            <?=$sello?>
                            <br />
                            <b>TAMA&Ntilde;O / TIPO:</b> <?=$dimension?>&nbsp;<?=$tipo?><br />
                            <b>REFERENCIA: </b> &nbsp;<?=$referencia?> 
              
                   
          </div>
        </td>
    </tr>
     
     
  </table>
    

    
<?
$x=0;$z=0;
con_ver();
$bls = mysqli_query($link,"select distinct booking from hblexpint where referencia = '$referencia_cliente' and status = 'OK' order by booking");


//echo "select distinct booking from hblexpint where referencia = '$referencia_cliente' and status = 'OK' order by booking";
//echo mysqli_num_rows($bls);
while($rbl = mysqli_fetch_array($bls) and mysqli_num_rows($bls)>0){
		$booking = $rbl[0];
		
		$datos = mysqli_query($link,"select * from detailcargaexpint where bookingno = '$booking' and cargo <> '' and id_bl in (select idhbl from hblexpint where status = 'OK' and booking =  '$booking') order by bookingno"); 


if(mysqli_num_rows($datos)>0){
	
 $HBL=data($link,"select * from hblexpint where booking like '$booking%' and status = 'OK'"); //idhbl = '".$id_bl."' and tipo = 'MBL'	

$kilos = mysqli_query($link,"select * from detailcargaexpint where bookingno = '$booking' and cargo <> '' and id_bl in (select idhbl from hblexpint where status = 'OK' and booking =  '$booking') order by bookingno"); 
$rk = mysqli_fetch_array($kilos);

?><br />


<!--- aqui ----->
<!----------------------------------------- un solo bl registrado ------------------------------------>
<? if(mysqli_num_rows($datos)==1){ ?>

<?
while($rbooking=mysqli_fetch_array($datos) and mysqli_num_rows($datos)>0){
	$x++;$z++;
	$id_bl = $rbooking[id_bl];
	$HBL=data($link,"select * from hblexpint where booking like '$booking%' and idhbl = '".$id_bl."' and status = 'OK' ");
	$consignee = $HBL[consignee];
	$shipper = $HBL[shipper];
	$tarja = trim($HBL[tarja_recepcion]);
	$tarja_averia = trim($HBL[tarja_averia]);
	$pedimento = trim($HBL[pedimento]);
	$consecutivo = trim($HBL[tarja]);
	$fecha_recepcion = trim($HBL[fecha_recepcion]);
	$finaldestination= trim($HBL[finaldestination]);
	
		con_impo();
	

		$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<>0 and A.id=C.idaa and (A.registro = 'ALTAMIRA' or A.registro = 'INTERTEAM') and C.email_contacto LIKE '%".$HBL["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();
	?>
    <table style="width: 100%; border: solid 1px #5544DD;border-collapse:collapse; font-size:10px; font-family:Arial, Helvetica, sans-serif" align="left" >
  <tr>
  <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:25px; max-width:25px"><B>NO</B></div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:130px; max-width:130px"><B>MARCAS Y NUMEROS </B></div></td>
    <td   style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:220px; max-width:220px"><B>CONTENIDO (DICE CONTENER)</B></div> </td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:45px; max-width:45px"><B>TIPO Y  NUMERO</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:89px; max-width:89px"><?=$rk[bulk].' '.$rk[type_of_bulks]?></div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:30px; max-width:30px"><B>PESO</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:50px; max-width:50px"><?=($rk[weight]!='' ? number_format($rk[weight],3) : '')?></div></td>
    
    
     
    <td style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:30px; max-width:30px"><B>CBM</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6">
    <div style="width:50px; max-width:50px">
			<? 
            $tt=0;
            $dimen = mysqli_query($link,"select  distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida =  '".$id_bl."'");
            while($rd=mysqli_fetch_array($dimen)){
                    $tt +=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
                    $Tcu+=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
            } 
            ?>
            <?=($tt!='' ? number_format($tt,3) :'')?>
    </div>
    </td>
    
    <? 
	$Tbulk+=$rk[bulk];
	$Tnetweight+=$rk[weight];

	?>
  </tr>
  
  <tr>
    <td style="text-align: left; border: solid 1px #55DD44;"><div style="width:25px; max-width:25px">
      <?=$z?>
    </div></td>
    <td style="text-align: left; border: solid 1px #55DD44;"><div style="width:130px; max-width:130px"><?=$rbooking[markandnos]?><br /> </div></td>
    
    <td colspan="2" style="text-align: left; border: solid 1px #55DD44;">
    <div style="width:268.5px; max-width:268.5px">
	<? $cargo1=$rbooking[cargo]; ?>
    <? $cadena= 'WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code];//echo strlen($cadena);?>
       <? if(strlen($cadena)>500){?>
          (*)
       <? }else{?>
                <?='WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code]?>
       <? } ?>
    </div> 
    </td>
    
    <td style="text-align: left; border: solid 1px #55DD44;">
  
    <div style="width:89px; max-width:89px">
			<? 
            $dd=0;
            
            $dimen = mysqli_query($link,"select  distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida =  '".$id_bl."'");
        
            while($rd=mysqli_fetch_array($dimen)){
            ?>
            <?= ($rd[largo]=='' ? '' : $rd[largo]).'/'.($rd[ancho]=='' ? '' : $rd[ancho]).'/'.($rd[alto]=='' ? '' : $rd[alto]).'='.($rd[numero_bultos]=='' ? 1 : $rd[numero_bultos]).'<br>'?>
            <? 
            $hhh[] = $rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
            $Tcu+=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
            } ?>
    </div>
    </td>
    <td colspan="2" style="text-align: left; border: solid 1px #55DD44;"><div style="width:30px; max-width:30px"></div></td>
    <td style="text-align: left; border: solid 1px #55DD44;"><div style="width:30px;max-width:30px"></div></td>
    <td style="text-align: left; border: solid 1px #55DD44;">
    
    <div style="width:50px; max-width:50px">
			  <?
            $d=0;
            while($d<=mysqli_num_rows($dimen)){
            ?>
              <?=($hhh[$d]!='' ? number_format($hhh[$d],3) :'').'<br>'?>
              <?
            $d++;}
            
            unset($hhh);
            ?>
    </div>
    
    </td>
  </tr>
  <? } ?>
  
  <tr>
    <td colspan="3" style="text-align: left; border: solid 1px #55DD44;">
    <div style="width:370px; max-width:370px">EMBARCADOR:&nbsp;<?=$shipper?><BR />
      CONSIGNATARIO:&nbsp;<?=$consignee?> // <?=$finaldestination?><BR />
      BOOKING:&nbsp;<?=$booking?><BR /><BR />
      AG AD: &nbsp;<?=$nombre_empresa?>
      </div></td>
    <td colspan="4" style="text-align: left; border: solid 1px #55DD44;; font-size:10px">
           <div style="width:225px">
                    PEDIMENTO: &nbsp;<?=$pedimento?><BR />
                    NO TARJA:&nbsp; <?=$tarja?>
					  <? if($tarja_averia!=''){?>
                     // TARJA AVERIA:&nbsp; 
                      <?=$tarja_averia?>
					  <? } ?>
                      <BR />
                      CONSECUTIVO: <?=$consecutivo?><br />
                      FECHA DE RECEPCION: <?=$fecha_recepcion?>
		</div>
    
    </td>
    <td style="text-align: left; border: solid 1px #55DD44;"><?=$HBL[posicion_almacen]?></td>
    <td style="text-align: left; border: solid 1px #55DD44;">&nbsp;</td>
  </tr>
  
  </table>
  
  <?  if(strlen($cadena)>500){?>  
<!--<div style="width:268.5px; max-width:268.5px; font-size:9px">-->
    <B>(*) CONTENIDO (DICE CONTENER):</B><BR /><?=strip_tags(eregi_replace("[\n|\r|\n\r]", '      ---------      ',$cadena))?>
    <!--</div> -->
<? } ?>
<? } ?>
<!--- end aqui ----->


<!---------------------------------------- 1 mbl y un hbl ------------------------------------------------>

<? if(mysqli_num_rows($datos)==2){  ?>
<? 
		$buscar_mbl=0;
	$datos = mysqli_query($link,"select * from hblexpint where booking = '$booking' and comentarios like '%ESI ON%'
	and status = 'OK'");
	
		if(mysqli_num_rows($datos)>0){
				$buscar_mbl=1;
		}						 
	if($buscar_mbl==1){
		$datos1 = mysqli_query($link,"select * from detailcargaexpint where bookingno = '$booking' and cargo <> '' and id_bl in (select idhbl from hblexpint where status = 'OK' and booking =  '$booking' ) order by bookingno");
	}else{	
		$datos1 = mysqli_query($link,"select * from detailcargaexpint where bookingno = '$booking' and cargo <> '' and tipo_bl = 'HBL' and id_bl in (select idhbl from hblexpint where status = 'OK' and booking =  '$booking' ) order by bookingno");
	}
	?>
<?
while($rbooking=mysqli_fetch_array($datos1) and mysqli_num_rows($datos1)>0){
	$x++;$z++;
	$id_bl = $rbooking[id_bl];
	$HBL=data($link,"select * from hblexpint where booking like '$booking%' and idhbl = '".$id_bl."' and tipo = 'HBL' and status = 'OK' ");
	$consignee = $HBL[consignee];
	$shipper = $HBL[shipper];
	$tarja = trim($HBL[tarja_recepcion]);
	$tarja_averia = trim($HBL[tarja_averia]);
	$pedimento = trim($HBL[pedimento]);
	$consecutivo = trim($HBL[tarja]);
	$fecha_recepcion = trim($HBL[fecha_recepcion]);
	$finaldestination= trim($HBL[finaldestination]);
	 

		$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<>0 and A.id=C.idaa and (A.registro = 'ALTAMIRA' or A.registro = 'INTERTEAM') and C.email_contacto LIKE '%".$HBL["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();
	?>
    <table style="width: 100%; border: solid 1px #5544DD;border-collapse:collapse; font-size:10px; font-family:Arial, Helvetica, sans-serif" align="left" >
  <tr>
  <td  style="text-align: left; border: solid 1px #55DD44; background:#FF6"><div style="width:25px; max-width:25px"><B>NO</B></div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:130px; max-width:130px"><B>MARCAS Y NUMEROS</B></div></td>
    <td   style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:220px; max-width:220px"><B>CONTENIDO (DICE CONTENER)</B></div> </td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:45px; max-width:45px"><B>TIPO Y  NUMERO</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:89px; max-width:89px"><?=$rbooking[bulk].' '.$rbooking[type_of_bulks]?></div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:30px; max-width:30px"><B>PESO</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:50px; max-width:50px"><?=($rbooking[weight]!='' ?  number_format($rbooking[weight],3):'')?></div></td>
    
     <? $cu = $rbooking[largo]*$rbooking[ancho]*$rbooking[alto]*$rbooking[bulk]/1000000;?>
     
    <td style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:30px; max-width:30px"><B>CBM</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6">
    <div style="width:50px; max-width:50px">
			  <? 
            $tt=0;
            $dimen = mysqli_query($link,"select  distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida =  '".$id_bl."'");
            while($rd=mysqli_fetch_array($dimen)){
                    $tt +=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
                    $Tcu+=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
            } 
            ?>
              <?=($tt!='' ? number_format($tt,3) :'')?>
    </div>
    </td>
    
    <? 
	$Tbulk+=$rbooking[bulk];
	$Tnetweight+=$rbooking[weight];
	$Tcu+= $cu;
	?>
    
  </tr>
  
 
    
  <tr>
    <td style="text-align: left; border: solid 1px #55DD44;"><div style="width:25px; max-width:25px"><?=$z?></div></td>
    <td style="text-align: left; border: solid 1px #55DD44;"><div style="width:130px; max-width:130px"><?=$rbooking[markandnos]?><br /> </div></td>
    <td colspan="2" style="text-align: left; border: solid 1px #55DD44;"><div style="width:268.5px; max-width:268.5px">
    <? if(strlen('WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code])>500){?>
	(*) 
    <? }else{?>
    <?='WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code]?>
    <? } ?>
    </div> </td>
    
    <td style="text-align: left; border: solid 1px #55DD44;">
    		<div style="width:89px; max-width:89px">
					  <? 
                    $dd=0;
                    
                    $dimen = mysqli_query($link,"select  distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida =  '".$id_bl."'");
                    //echo 'cantidad '.mysqli_num_rows($dimen);
                    
                    while($rd=mysqli_fetch_array($dimen)){
                    ?>
                              <?= ($rd[largo]=='' ? '' : $rd[largo]).'/'.($rd[ancho]=='' ? '' : $rd[ancho]).'/'.($rd[alto]=='' ? '' : $rd[alto]).'='.($rd[numero_bultos]=='' ? 1 : $rd[numero_bultos]).'<br>'?>
                              <? 
                            $hhh[] = $rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
                            $Tcu+=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
                            } ?>
            </div>
      </td>
            
            <td colspan="2" style="text-align: left; border: solid 1px #55DD44;"><div style="width:30px; max-width:30px"></div></td>
            <td style="text-align: left; border: solid 1px #55DD44;"><div style="width:30px;max-width:30px"></div></td>
            <td style="text-align: left; border: solid 1px #55DD44;"><div style="width:50px; max-width:50px">
                      <?
                    $d=0;
                    while($d<=mysqli_num_rows($dimen)){
                    ?>
                      <?=($hhh[$d]!='' ? number_format($hhh[$d],3) :'').'<br>'?>
                      <?
                    $d++;}
                    
                    unset($hhh);
                    ?>
            </div>
            </td>
  </tr>
 
 
 
  <tr>
    <td colspan="3" style="text-align: left; border: solid 1px #55DD44;">
    <div style="width:370px; max-width:370px">
    EMBARCADOR:&nbsp;<?=$shipper?><BR />
    CONSIGNATARIO:&nbsp;<?=$consignee?> // <?=$finaldestination?><BR />
    BOOKING:&nbsp;<?=$booking?><br /><br />
    AG AD: &nbsp;<?=$nombre_empresa?>
    </div>
    </td>
    
    <td colspan="4" style="text-align: left; border: solid 1px #55DD44; font-size:10px">
           <div style="width:225px">
                    PEDIMENTO: &nbsp;<?=$pedimento?><BR />
                    NO TARJA:&nbsp; <?=$tarja?>
					  <? if($tarja_averia!=''){?>
                     // TARJA AVERIA:&nbsp; 
                      <?=$tarja_averia?>
					  <? } ?>
                      <BR />
                      CONSECUTIVO: <?=$consecutivo?><br />
                      FECHA DE RECEPCION: <?=$fecha_recepcion?>
		</div>
    
    
    </td>
    <td style="text-align: left; border: solid 1px #55DD44;"><?=$HBL[posicion_almacen]?></td>
    <td style="text-align: left; border: solid 1px #55DD44;">&nbsp;</td>
  </tr>
  
  </table>
  
  <? if(strlen('WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code])>500){?>  
<div style="width:268.5px; max-width:268.5px; font-size:9px">
   <B>(*) CONTENIDO (DICE CONTENER):</B><BR /> <?=nl2br('WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code])?>
    </div> 
<? } ?>
 <? } ?>

<? } ?>

<!------------------------------------------ 1 MBL Y VARIOS HBLS ----------------------------------------------------->

<? if(mysqli_num_rows($datos)>2){ ?>
<? 
$datos2 = mysqli_query($link,"SELECT *
FROM hblexpint
WHERE booking LIKE '$booking%' AND tipo='HBL' and status = 'OK'
GROUP BY shipper, shipper2, shipper3, shipper4, shipper5, shipper6, consignee, consignee2, consignee3, consignee4, consignee5, consignee6, notify, notify2, notify3, notify4, notify5, notify6"); 

?>
<?
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// cada grupo que ha sido agrupado segun distinto sh cn y n1
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

while($rs=mysqli_fetch_array($datos2) and mysqli_num_rows($datos2)>0){
	$x++;
	$datos3 = mysqli_query($link,"SELECT *
	FROM hblexpint
	WHERE booking LIKE '$booking%' AND tipo='HBL' and shipper='".ret($rs[shipper])."' and shipper2='".ret($rs[shipper2])."' and shipper3='".ret($rs[shipper3])."' 
	and shipper4='".ret($rs[shipper4])."' and shipper5='".ret($rs[shipper5])."' and shipper6='".ret($rs[shipper6])."' and consignee='".ret($rs[consignee])."' 
	and consignee2='".ret($rs[consignee2])."' and consignee3='".ret($rs[consignee3])."' and consignee4='".ret($rs[consignee4])."' 
	and consignee5='".ret($rs[consignee5])."' and consignee6='".ret($rs[consignee6])."' and notify='".ret($rs[notify])."' and notify2='".ret($rs[notify2])."' 
	and notify3='".ret($rs[notify3])."' and notify4='".ret($rs[notify4])."' and notify5='".ret($rs[notify5])."' and notify6='".ret($rs[notify6])."' and status = 'OK'");
 
	?>
    
  <?
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	// todos los hbls de este grupo
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	$j=1;
	while($rx=mysqli_fetch_array($datos3) and mysqli_num_rows($datos3)>0){
		$x++;
		$z++;
				$id_bl = $rx[idhbl]; 
				$consignee = $rx[consignee];
				$shipper = $rx[shipper];
				$tarja = trim($rx[tarja_recepcion]);
				$tarja_averia = trim($rx[tarja_averia]);
				
				$pedimento = trim($rx[pedimento]);
				$consecutivo = trim($rx[tarja]);
				$fecha_recepcion = trim($rx[fecha_recepcion]);
				$finaldestination= trim($rx[finaldestination]);
	
	con_impo();
	

		$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<>0 and A.id=C.idaa and (A.registro = 'ALTAMIRA' or A.registro = 'INTERTEAM') and C.email_contacto LIKE '%".$rx["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();
				$datos4 = mysqli_query($link,"SELECT * FROM detailcargaexpint WHERE id_bl ='".$id_bl."'");
				$rbooking = mysqli_fetch_array($datos4);
				
			     $cu = $rbooking[largo]*$rbooking[ancho]*$rbooking[alto]*$rbooking[bulk]/1000000;?>
  <table style="width: 100%; border: solid 1px #5544DD;border-collapse:collapse; font-size:10px; font-family:Arial, Helvetica, sans-serif" align="left" >
  <tr>
  <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:25px; max-width:25px"><B>NO</B></div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:130px; max-width:130px"><B>MARCAS Y NUMEROS</B> </div></td>
    <td   style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:220px; max-width:220px"><B>CONTENIDO (DICE CONTENER)</B></div> </td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:45px; max-width:45px"><B>TIPO Y  NUMERO</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:89px; max-width:89px"><?=$rbooking[bulk].' '.$rbooking[type_of_bulks]?></div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:30px; max-width:30px"><B>PESO</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:50px; max-width:50px"><?=$rbooking[weight]!='' ? number_format($rbooking[weight],3):''?></div></td>
    
    
     
    <td style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:30px; max-width:30px"><B>CBM</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#FF6"><div style="width:50px; max-width:50px">
      <? 
	$tt=0;
	$dimen = mysqli_query($link,"select  distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida =  '".$id_bl."'");
	while($rd=mysqli_fetch_array($dimen)){
			$tt +=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
			$Tcu+=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
	} 
	?>
      <?=($tt!='' ? number_format($tt,3) :'')?>
    </div></td>
    <? 
	$Tbulk+=$rbooking[bulk];
	$Tnetweight+=$rbooking[weight];
	$Tcu+= $cu;
	?>
    
  </tr>
  
     
			  <tr>
				<td style="text-align: left; border: solid 1px #55DD44;"><div style="width:25px; max-width:25px"><?=$z?> </div></td>
				<td style="text-align: left; border: solid 1px #55DD44;"><div style="width:130px; max-width:130px">
				<?=$rbooking[markandnos]?><br /> </div></td>
				<td colspan="2" style="text-align: left; border: solid 1px #55DD44;"><div style="width:268.5px; max-width:268.5px">
				 
                <? if(strlen('WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code])>500){?>
                (*) 
                <? }else{?>
                <?='WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code]?>
                <? } ?>
                </div> </td>
				<td style="text-align: left; border: solid 1px #55DD44;">
                <div style="width:89px; max-width:89px">
				  		<? 
						$dd=0;
						
						$dimen = mysqli_query($link,"select  distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida =  '".$id_bl."'");
					
						while($rd=mysqli_fetch_array($dimen)){
						?>
									  <?= ($rd[largo]=='' ? '' : $rd[largo]).'/'.($rd[ancho]=='' ? '' : $rd[ancho]).'/'.($rd[alto]=='' ? '' : $rd[alto]).'='.($rd[numero_bultos]=='' ? 1 : $rd[numero_bultos]).'<br>'?>
									  <? 
						$hhh[] = $rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
						$Tcu+=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
						} ?>
			    </div>
                </td>
				<td colspan="2" style="text-align: left; border: solid 1px #55DD44;"><div style="width:30px; max-width:30px"></div></td>
				<td style="text-align: left; border: solid 1px #55DD44;"><div style="width:30px;max-width:30px"></div></td>
				<td style="text-align: left; border: solid 1px #55DD44;">
                        <div style="width:50px; max-width:50px">
                                  <?
                                $d=0;
                                while($d<=mysqli_num_rows($dimen)){
                                ?>
                                              <?=($hhh[$d]!='' ? number_format($hhh[$d],3) :'').'<br>'?>
                                              <?
                                $d++;}
                                
                                unset($hhh);
                                ?>
                        </div>
                </td>
			  </tr> 
              
              
              <tr>
                    <td colspan="3" style="text-align: left; border: solid 1px #55DD44;">
                      <? //if($j==mysqli_num_rows($datos3)){?>
                      <div style="width:370px; max-width:370px">
                      EMBARCADOR:&nbsp;<?=$shipper?><BR />
                      CONSIGNATARIO:&nbsp;<?=$consignee?> // <?=$finaldestination?><BR />
                      BOOKING:&nbsp;<?=$booking?><BR /><BR />
                      AG AD: &nbsp;<?=$nombre_empresa?>
                      <? //} ?>
                      </div>
                </td>
                    <td colspan="4" style="text-align: left; border: solid 1px #55DD44; font-size:10px">
                    <div style="width:225px">
                    PEDIMENTO: &nbsp;<?=$pedimento?><BR />
                    NO TARJA:&nbsp; <?=$tarja?>
					  <? if($tarja_averia!=''){?>
                     // TARJA AVERIA:&nbsp; 
                      <?=$tarja_averia?>
					  <? } ?>
                      <BR />
                      CONSECUTIVO: <?=$consecutivo?><br />
                      FECHA DE RECEPCION: <?=$fecha_recepcion?>
 					</div>
                    </td>
                    <td style="text-align: left; border: solid 1px #55DD44;"><?=$rx[posicion_almacen]?></td>
                    <td style="text-align: left; border: solid 1px #55DD44;">&nbsp;</td> 
              </tr>
 
  </table>  
             
               <? if(strlen('WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code])>500){?>  
<div style="width:268.5px; max-width:268.5px; font-size:9px">
   <B>(*) CONTENIDO (DICE CONTENER):</B><BR /> <?=nl2br('WITH '.$rbooking[number_of_pieces].' OF '.$rbooking[cargo].' // H.S. '.$rbooking[harmonized_code])?>
    </div> 
<? } ?>
    <? $j++; }
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	// fin de todos los hbls de este grupo
	//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
	?>
  
 
 
    		
  
  <? } ?>
  

  <?  
  }
  ?>

<? 

} ?>
<?  
}
?>
<BR /> 
  <table style="width: 100%; border-collapse:collapse; font-size:10px; font-family:Arial, Helvetica, sans-serif" align="left" >
  <tr>
  <td  style="text-align: left; "><div style="width:25px; max-width:25px"><B></B></div></td>
    <td  style="text-align: left;"><div style="width:130px; max-width:130px"><B></B></div></td>
    <td   style="text-align: left;"><div style="width:220px; max-width:220px"><B></B></div> </td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#C1C1FF"><div style="width:53px; max-width:55px"><B>BULTOS TOTALES</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#C1C1FF"><div style="width:99px; max-width:99px"><?=$Tbulk?></div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#C1C1FF"><div style="width:35px; max-width:35px"><B>TOTAL PESO</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#C1C1FF"><div style="width:57px; max-width:57px"><?=($Tnetweight!='' ? number_format($Tnetweight,3):'')?></div></td>
    
    
     
    <td bgcolor="#C1C1FF"  style="text-align: left; border: solid 1px #55DD44;"><div style="width:30px; max-width:30px"><B>TOTAL CBM</B> </div></td>
    <td  style="text-align: left; border: solid 1px #55DD44;background:#C1C1FF"><div style="width:53px; max-width:53px"><?=($Tcu!='' ? number_format($Tcu/2,3):'')?></div></td>
  </tr>
  </table>

</page>
 