<? session_start();
//ini_set("display_errors",1);error_reporting(E_ALL); 
$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,$sello_f,$contenedor_f;;
//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();
 
	$Tbulk=0;
	$Tnetweight=0;
	$Tcu=0;
}
$suma_pza_mbl = "";
$suma_peso_mbl = "";

?>
<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;/&nbsp;<?=trim($referencia)?>&nbsp;&nbsp;
               
                
                
                <span style="text-align:center;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 />

<?

$link = con_ver();
//$bls = mysqli_query($link,"select * from hblexpint where referencia = '$referencia_cliente' and status = 'OK'  order by booking,tipo desc");
//$cantidad_hbl_ok = mysqli_num_rows($bls);

$bls = mysqli_query($link,"select * from hblexpint where referencia = '$referencia_cliente' and status = 'OK'  and tipo = 'HBL' order by booking,tipo desc");
//echo "select * from hblexpint where referencia = '$referencia_cliente' and status = 'OK'  and tipo = 'HBL' order by booking,tipo desc --";
 //$cantidad_hbl_ok = mysqli_num_rows($bls);
if(mysqli_num_rows($bls)<1){
$bls = mysqli_query($link,"select * from hblexpint where referencia = '$referencia_cliente' and status = 'OK'  and tipo = 'MBL' order by booking,tipo desc");
}

if(mysqli_num_rows($bls)>0){
	while($rbl = mysqli_fetch_array($bls) ){
		$idhbl[]=$rbl[idhbl];
		$cadena_idhbl.="'".$rbl[idhbl]."'".',';
		$cadena_booking.="'".$rbl[booking]."'".',';
	}
}else{
	//echo 'La referencia '.$referencia_cliente.' no tiene bookings registrados aún';
	//return;
}

$cadena_booking=substr($cadena_booking,0,strlen($cadena_booking)-1);
//echo "select * from hblexpint where booking not in (".$cadena_booking.") 
//and referencia = '$referencia_cliente' and status = 'OK'  and tipo = 'MBL'  order by booking,tipo desc";
//return;
$bls = mysqli_query($link,"select * from hblexpint where booking not in (".$cadena_booking.") 
and referencia = '$referencia_cliente' and status = 'OK'  and tipo = 'MBL'  order by booking,tipo desc");

if(mysqli_num_rows($bls)>0){
	while($rbl = mysqli_fetch_array($bls) ){
	$idhbl[]=$rbl[idhbl];
	$cadena_idhbl.="'".$rbl[idhbl]."'".',';
	}
}

# calculo de totales: bultos, peso y vol

$cadena_idhbl=substr($cadena_idhbl,0,strlen($cadena_idhbl)-1);

$bls = mysqli_query($link,"select * from hblexpint where idhbl in (".$cadena_idhbl.")  order by booking,tipo desc");

while($rbl = mysqli_fetch_array($bls) ){
 
	$datos = mysqli_query($link,"select * from detailcargaexpint where id_bl = '".$rbl[idhbl]."' and cargo <> '' "); 
	while($rk = mysqli_fetch_array($datos)){
		$tbulks+=$rk[bulk];
		$tpeso+=$rk[weight];
		$tbulks  = doubleval($rk[bulk])+doubleval($rk[no_piezasimoh]);
		$tpeso = doubleval($rk[weight])+doubleval($rk[gross_weightimoh]);
	}
	
	$tt=0;
	$dimen1 = mysqli_query($link,"select sum(largo*ancho*alto*numero_bultos)/1000000  from dimensiones where id_partida in (".$cadena_idhbl.") ");
			$rd1=mysqli_fetch_array($dimen1);
			$tt =  $rd1[0];
			
            $dimen = mysqli_query($link,"select distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida in (".$cadena_idhbl.") ");
			//echo "select distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida in (".$cadena_idhbl.") <br>";
			
			 
			if(strpos($rbl[booking],'EXIT')!==false){
				$booking = 	'BKG'.substr($rbl[booking],10,10);
			}	
			if(mysqli_num_rows($dimen)<1){
				$dimen = mysqli_query($link,"select distinct largo,ancho,alto,numero_bultos  from dimensiones where booking =  '".$booking."'");	
			}
			//echo "select distinct largo,ancho,alto,numero_bultos  from dimensiones where booking =  '".$booking."'<br>";
            while($rd=mysqli_fetch_array($dimen)){
				if($tt*1==0){
                    $tt +=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
				}
                    $Tcu+=$rd[largo]*$rd[ancho]*$rd[alto]*$rd[numero_bultos]/1000000;
					//echo  ' tt '.$tt.'<br>'; 
            } 
			
			
	
}


$detalle_consolidado = mysqli_query($link,"SELECT * FROM detallebooking where referencia_cliente = '$referencia_cliente' ");
while($rdc = mysqli_fetch_array($detalle_consolidado) ){
	$imo_dc[]=$rdc[imo];
}
$imo_c = array_unique($imo_dc);

for($k=0;$k<=count($imo_c);$k++){
	$imo_consolidado.= ($imo_c[$k]!='' ? $imo_c[$k].',' : '');
}
$imo_consolidado = substr($imo_consolidado,0,strlen($imo_consolidado)-1);
?>
	
 <table style="width: 100%;border: solid 1px #000000;border-collapse:collapse;font-size:10px; font-family:Arial, Helvetica, sans-serif;"   >
     
    <tr>
        <td  style="text-align: left; border: solid 1px #000000;">
        <div style="width:200px">
                <b>BUQUE:</b> <? echo $buque; ?> <br />
                
                <b>NUM VIAJE:</b> <?=$viaje?><br />
                
                <b>BOOKING NAVIERA:</b>       
                <?=$booking_naviera?>
                <br />
                <b>CIERRE DE BUQUE:</b> 
                <? echo $cierre_buque;?>
        </div> 
      </td>
        
        <td   style="text-align: left; border: solid 1px #000000;">
          <div style="width:200px">
          			 <b>CONTENEDOR:</b> &nbsp;
                            <?=$contenedor?>
                            <br />
                            <b>DIMENSION: </b> 
                            <?=$dimension?>
                            &nbsp;<br />
                            <b>TIPO:</b>&nbsp;<?=$tipo?><br />
                            <b>SELLO: </b>
                            <?=$sello?>
          </div>
        </td>
        <td   style="text-align: left; border: solid 1px #000000;"><div style="width:110px"> <b>PESO:</b> <?=$tpeso?><br />
          <b>VOLUMEN: </b><?= $tt!='' ? number_format($tt,3) : ''?><br />
          <b>BULTOS: </b><?=$tbulks?><br />
          <b>TARA: </b><?=$tara?></div>
        </td>
        <td   style="text-align: left; border: solid 1px #000000;"><div style="width:214px"> <b>DESTINO FINAL:</b> &nbsp; <? echo $pod;?> <br />
          <b>EJECUTIVO: </b><?=$_SESSION[nombre_usuario] ?><br />
          <b>IMO:</b> <?=$imo_consolidado ?> <br />
  <b>FOTOS CONSOLIDADO: </b>
  <? if($fotos_consolidado!=''){ ?>
  <a href="<?=$fotos_consolidado?>" target="_blank">Fotos consol</a>
  <? } ?>
  </div>
  
  </td>
    </tr>
     
     
  </table>

<BR />

<?
$z=0;
$link = con_ver();

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

	$suma_pza_mbl  = doubleval($rk[bulk])+doubleval($rk[no_piezasimoh]);
	$suma_peso_mbl = doubleval($rk[weight])+doubleval($rk[gross_weightimoh]);
?>
 
 	 
 
<table width="1450px"  border="1" style="border:solid 1px #000000;border-collapse:collapse;font-size:10px; font-family:Arial, Helvetica, sans-serif; ">
  <tr>
    <td rowspan="8" valign="middle"><strong>
      <?=$z?>
    </strong></td>
    <td bgcolor="#FFFF00"><div style="width:112px"><strong>BOOKING</strong></div></td>
    <td bgcolor="#FFFF00"><div style="width:112px"><?=$rbl[booking] ?></div></td>
  <?  if($rbl[finaldestination]==''){

	$finaldestination = busquedaB($link_impo,"select finaldestination from bookingexpint where booking_interteam = '".trim($rbl[booking])."' ","finaldestination");
}
?>
    <td bgcolor="#FFFF00"><strong>MARCAS</strong></td>
    <td bgcolor="#FFFF00"><div style="width:227px; max-width:227px"><?=$rk[markandnos]?></div></td>
    <td bgcolor="#FFFF00"><strong>PEDIMENTO</strong></td>
    <td bgcolor="#FFFF00"><div style="width:85px; max-width:85px"><?=trim($rbl[pedimento])?></div></td>
  </tr>
  <tr>
    <td><strong>CANTIDAD</strong></td>
    <td><?=$suma_pza_mbl ?></td>
    <td><strong>EMBARCADOR</strong></td>
    <td><div style="width:227px; max-width:227px"><?=$rbl[shipper] ?></div></td>
    <td><strong>IMO</strong></td>
    <td><span style="width:227px; max-width:227px">
      <?=$rk[imo]?>
    </span>
    </td>
  </tr>
  <tr>
    <td><strong>EMBALAJE</strong></td>
    <td><?=$rk[type_of_bulks]?></td>
    <td><strong>CONSIGNATARIO</strong></td>
    <td><div style="width:227px; max-width:227px"><?=$rbl[consignee] ?></div></td>
    <td><!--<strong>UN</strong>--agregar despues cuando complete el registro del booking de parte del aa con UN y otros datos de imo--><strong>UN</strong></td>
    <td><span style="width:227px; max-width:227px">
      <?=$rk[un]?>
    </span></td>
  </tr>
  
  <tr>
    <td><strong>PESO</strong></td>
    <td><?=($suma_peso_mbl!='' ? number_format($suma_peso_mbl,3) : '')?></td>
    <td><strong>DESTINO FINAL</strong></td>
    <td><?=$rbl[finaldestination]=='' ? $finaldestination : $rbl[finaldestination]?></td>
    <td><strong>FECHA INGRESO</strong></td>
    <td><?=$rbl[fecha_recepcion]?></td>
  </tr>
  <?
  if($rbl[fecha_recepcion]!='0000-00-00' and $rbl[fecha_recepcion]!=''){
  $libre_hasta = sumarDias($rbl[fecha_recepcion],14);
  }
  ?>
  <tr>
    <td><strong>CONSECUTIVO</strong></td>
    <td><div style="width:112px"><?=trim($rbl[tarja])?></div></td>
    <td><strong>FRACC. ARANC.</strong></td>
    <td><div style="width:227px; max-width:227px"><?=$rk[harmonized_code]?></div></td>
    <td><strong>LIBRE HASTA</strong></td>
    <td><?=$libre_hasta?></td>
  </tr>
  <tr>
    <td><strong>TARJA</strong></td>
    <td><div style="width:112px"><?=$rbl[tarja_recepcion]?>&nbsp;<?=$tarja_averia!='' ? '// TARJA AVERIA:&nbsp;'.$tarja_averia : ''?></div></td>
    <td><strong>FOTOS</strong></td>
    <td><div style="width:227px; max-width:227px">
      <? if($rbl[fotos]!=''){ ?>
      <a href="<?=$rbl[fotos]?>" target="_blank">Fotos booking</a>
      <? } ?>
    </div></td>
    <td><strong>VOLUMEN</strong></td>
    <td>
    <? 
            $tt=0;
            $dimen = mysqli_query($link,"select distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida =  '".$rk[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;
            } 
			
			if(strpos($rbl[booking],'EXIT')!==false){
				$booking = 	'BKG'.substr($rbl[booking],10,10);
			}
				
			if(mysqli_num_rows($dimen)<1){
				$dimen = mysqli_query($link,"select distinct largo,ancho,alto,numero_bultos  from dimensiones where booking =  '".$booking."'");	
			}
            ?>
            <?=($tt!='' ? number_format($tt,3) :'')?>
    </td>
  </tr>
 
  <tr>
    <td><strong>DESCRIPCION</strong></td> 
    <td colspan="3"><div style="width:420px; max-width:420px; text-align:justify">WITH <?=$rk[number_of_pieces]?>&nbsp;<?=$rk[cargo]?></div></td>
    <td><strong>MEDIDAS</strong></td>
    <td>
    <? 
            $dd=0;
            
            $dimen = mysqli_query($link,"select distinct largo,ancho,alto,numero_bultos  from dimensiones where id_partida = '".$rk[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;
            } ?>
    </td>
  </tr>
  <tr>
    <td><strong>COMENTARIOS</strong></td>
    <td colspan="5"><div style="width:227px; max-width:227px"><?=$rbl[comentarios]?></div></td>
    </tr>
   
</table>
<br /> 
<? } 

?>
<!--<br /><br />
<div style="width:100%; max-width:100%; text-align:center"> 
     <span style="font-weight:bold">
     <strong><U><?=$_SESSION[nombre_usuario] ?><br /></U></strong><br />
     DEPTO. DE EXPORTACION<br />
     CD <?=$_SESSION[gafete] ?>
     </span>
</div>--> 
 
</page>
 