<?php
require_once("../comun/conex_ver.php");
require_once("../comun/conexion3.php");

require_once("../comun/actions.php");

require_once("../comun/funciones.php");

$action = new actions();
$link_impo=con_impo();$link=con_ver();

function valorate($valor){
	$valor = trim(str_replace("'","\'",strtoupper($valor)));
	return $valor;
}

$rol= valorate($_GET['rol']);
$user= valorate($_GET['user']);
$email_aa= valorate(strtolower($_GET['email']));
$txtbooking= trim(valorate($_POST['booking']));

if($txtbooking==''){
	$txtbooking= trim(valorate($_GET['booking']));
}

 
$q=data($link_impo,"select * from bookingexpint where booking_interteam like '$txtbooking%'");
 
$qZ=data($link ,"select * from booking_ssl where booking_ssl like '$txtbooking%'");
 
// si ingresa un booking que no existe ni en interteam ni en veracruz envio un mensaje de error y regreso
if($q[files]=='' and $qZ[booking_ssl]==''){
 $action->alert('BOOKING inexistente, asegúrese de haberlo escrito correctamente y/o póngase en contacto con su cliente.');
 $action->text('booking',''); 
 $action->add_js_code("deshabilitar_mbl(true);deshabilitar_hbl(true);");  
 $action->send_response();
 return;
}else{
	$action->add_js_code("deshabilitar_mbl(false);deshabilitar_hbl(false);");  
}
// ----------------------------------------------------------------------------------------------------------------------	 
	 
if($q[files]==''){
	$action->checked('cargo_vacio',0);
	$action->add_js_code("limpiar_hbl();");
	$action->add_js_code("limpiar_mbl();");
}else{
		$datos=data($link_impo ,"select * from datosexpint where files = '".$q[files]."'");
		$POL_POL = $datos[pol];
        // ams 1
		$ams1 = busquedaB($link_impo ,"select scac from bookingexpint where booking_interteam like '".$txtbooking."%'","scac");
		$ams=$ams1;
		$action->text('ams',valorate($ams1)); 
		if($datos[files]==''){
			$action->checked('cargo_vacio',0);
			$action->add_js_code("limpiar_hbl();");
			$action->add_js_code("limpiar_mbl();");

			$action->text('ams',valorate($ams)); 
		}else{
	 
						
		$demas_datos = data($link ,"select * from hblexpint where booking = '".$txtbooking."' and tipo = 'MBL' and registrado_por = '".$email_aa."' ");
		
		$action->text('booking',valorate($txtbooking));
		$action->text('pol',valorate($datos[pol]).', '.valorate($datos[pol_country]));
		$action->text('pod',valorate($datos[pod]).', '.valorate($datos[pod_country]));
		$action->text('final_destination',valorate($demas_datos[finaldestination])); // ok
		$action->text('h_final_destination',valorate($demas_datos[finaldestination])); //ok
		$action->text('export_reference',valorate($demas_datos[export_reference])); //ok
		$action->text('h_export_reference',valorate($demas_datos[export_reference])); //ok
		$action->text('vessel',valorate($datos[vessel]));
		$action->text('referencia',valorate($datos[referencia_interteam]));
		
		 // ams 2
		$ams2 = busquedaB($link ,"select * from detailcargaexpint where bookingno = '".$txtbooking."'  ",'ams');
		
		if($ams2!=''){$ams=$ams2;

		$action->text('ams',valorate($ams2)); 
		}
		
		
		if($demas_datos[booking]==''){
			$action->checked('cargo_vacio',0);
			$action->add_js_code("limpiar_mbl();");
			$action->add_js_code("limpiar_hbl();");
			$action->text('ams',valorate($ams)); 
		}else{
					if(count($demas_datos)>0){
						$action->text('registrado_por',valorate($demas_datos[registrado_por]));			
						$action->text('status',$demas_datos[status]);	
						$action->text('tarja',$demas_datos[tarja]);
						$action->text('shipper_file',valorate($demas_datos[shipper_file]));	
						
						$action->checked('cargo_vacio',$demas_datos[cargo_vacio]);
						
						// &&&&& DATOS DE SHIPPER CONSIGNEE Y NOTIFY &&&&&&&&&&&&&&
						$action->text('shipper1',valorate($demas_datos[shipper]));
						$action->text('shipper2',valorate($demas_datos[shipper2]));
						$action->text('shipper3',valorate($demas_datos[shipper3]));
						$action->text('shipper4',valorate($demas_datos[shipper4]));
						$action->text('shipper5',valorate($demas_datos[shipper5]));
						$action->text('shipper6',valorate($demas_datos[shipper6]));
						
						$action->text('consignee1',valorate($demas_datos[consignee]));
						$action->text('consignee2',valorate($demas_datos[consignee2]));
						$action->text('consignee3',valorate($demas_datos[consignee3]));
						$action->text('consignee4',valorate($demas_datos[consignee4]));
						$action->text('consignee5',valorate($demas_datos[consignee5]));
						$action->text('consignee6',valorate($demas_datos[consignee6]));
						
						$action->text('notify1',valorate($demas_datos[notify]));
						$action->text('notify2',valorate($demas_datos[notify2]));
						$action->text('notify3',valorate($demas_datos[notify3]));
						$action->text('notify4',valorate($demas_datos[notify4]));
						$action->text('notify5',valorate($demas_datos[notify5]));
						$action->text('notify6',valorate($demas_datos[notify6]));
			
						# datos del articulo 23
						$action->text('descripcion_espanol_articulo_23',valorate($demas_datos[descripcion_espanol_articulo_23]));
						$action->text('tipo_mercancia_articulo_23',valorate($demas_datos[tipo_mercancia_articulo_23]));
						$action->text('regimen_articulo_23',valorate($demas_datos[regimen_articulo_23]));
						$action->text('factura_articulo_23',valorate($demas_datos[factura_articulo_23]));
						$action->text('imo_articulo_23',valorate($demas_datos[imo_articulo_23]));
						$action->text('un_articulo_23',valorate($demas_datos[un_articulo_23]));
						$action->text('transportista_articulo_23',valorate($demas_datos[transportista_articulo_23]));
						$action->text('nombre_del_chofer_articulo_23',valorate($demas_datos[nombre_del_chofer_articulo_23]));
						$action->text('no_licencia_articulo_23',valorate($demas_datos[no_licencia_articulo_23]));
						$action->text('placas_articulo_23',valorate($demas_datos[placas_articulo_23]));
						$action->text('economico_articulo_23',valorate($demas_datos[economico_articulo_23]));

						# datos de la carga
						$cargo = data($link ,"select * from detailcargaexpint where bookingno = '".$txtbooking."' and 
									  tipo_bl = 'MBL' and id_bl in (select idhbl from hblexpint where registrado_por = '".$email_aa."')");
					
						
						$action->text('marks',valorate($cargo[markandnos]));
						$action->text('quantity',valorate($cargo[bulk]));
						$action->text('type_of_bulks',valorate($cargo[type_of_bulks]));
						$action->text('description',valorate($cargo[cargo]));
						$action->text('no_piezas',valorate($cargo[number_of_pieces]));
						$action->text('gross_weight',valorate($cargo[weight]));
						$action->text('net_weight',valorate($cargo[netweight]));
						$action->text('volume',valorate($cargo[measurement]));
						$action->text('harmonized_code',valorate($cargo[harmonized_code]));
						$action->text('marks',valorate($cargo[markandnos]));
				$action->add_js_code("document.getElementById('correcciones').value='".valorate($cargo[correcciones])."'");
						
						
						
						
					}
		}
		// &&&&& DATOS DE SHIPPER CONSIGNEE Y NOTIFY &&&&&&&&&&&&&&
			$action->add_js_code("limpiar_hbl();");
		 
			$action->text('h_booking',valorate($txtbooking));
		/*$action->text('h_pol',valorate($datos[pol]).', '.valorate($datos[pol_country]));
		$action->text('h_pod',valorate($datos[pod]).', '.valorate($datos[pod_country]));
		$action->text('h_final_destination',valorate($demas_datos[finaldestination])); // ok
		$action->text('final_destination',valorate($demas_datos[finaldestination])); //ok*/
		$action->text('export_reference',valorate($demas_datos[export_reference])); //ok
		$action->text('h_export_reference',valorate($demas_datos[export_reference])); //ok
		$action->text('h_vessel',valorate($datos[vessel]));
		
		$action->text('h_referencia',valorate($datos[referencia_interteam]));
		$action->text('h_shipper_file',valorate($demas_datos[shipper_file]));
	
		$action->text('h_ams',valorate($ams)); 
		 
		
  
	}
}

// referencia que no es de interteam	
$action->add_js_code("limpiar_hbl();limpiar_mbl();");
con_ver();
$referencia_interteam = busquedaB($link ,"select referencia from  hblexpint where booking = '".trim($txtbooking)."'","referencia");

if($referencia_interteam==''){

}

if(trim($referencia_interteam)!=''){
$datum=data($link ,"select * from datosbooking where referencia_cliente = '".$referencia_interteam."' and referencia_cliente<>''"); 
//$action->text('h_bandera_de_consolidado',valorate($datum[bandera_de_consolidado]));
//$action->text('bandera_de_consolidado',valorate($datum[bandera_de_consolidado]));
if($datum[referencia_cliente]!=''){
			$referencia_cliente = $datum[referencia_cliente];
			# ok
			$datos=data($link ,"select * from datosbooking where referencia_cliente = '".$referencia_cliente."' and referencia_cliente<>''"); 	
			
 
			$action->text('h_vessel',valorate($datos[vessel]));
			$action->text('h_referencia',valorate($datos[referencia_cliente]));
 
			$action->text('vessel',valorate($datos[vessel]));
			$action->text('referencia',valorate($datos[referencia_cliente]));
}
}

 
		 $ams2 = data($link ,"select * from detailcargaexpint where bookingno = '".$txtbooking."'");
		 
		 $action->text('ams',valorate($ams2[ams]));


			$action->add_js_code("limpiar_hbl();");
			$action->text('h_booking',valorate($txtbooking));
		 
			$action->text('h_shipper_file',valorate($demas_datos[referencia_cliente]));
			
			$action->text('booking',valorate($txtbooking));
			
			
			
			// cambio
			$action->text('h_status','REVISANDO');
			
			$demas_datos = data($link ,"select * from hblexpint where booking = '".$txtbooking."' and tipo = 'MBL' and (registrado_por = '".strtolower($email_aa)."' or registrado_por = '".strtoupper($email_aa)."') ");
			
			//$action->text('final_destination',valorate($demas_datos[finaldestination])); // ok
			//$action->text('h_final_destination',valorate($demas_datos[finaldestination])); //ok
			$action->text('export_reference',valorate($demas_datos[export_reference])); //ok
			$action->text('h_export_reference',valorate($demas_datos[export_reference])); //ok
			
			if(count($demas_datos)==0){
			$action->text('status','REVISANDO');	
			}
			
			if(count($demas_datos)>0){ //$action->alert(count($demas_datos));
					$action->text('registrado_por',valorate($demas_datos[registrado_por]));			
					$action->text('status',$demas_datos[status]);	

					$action->text('shipper_file',valorate($demas_datos[referencia_cliente]));
					$action->checked('cargo_vacio',$demas_datos[cargo_vacio]);
					
						# datos del articulo 23
						$action->text('descripcion_espanol_articulo_23',valorate($demas_datos[descripcion_espanol_articulo_23]));
						$action->text('tipo_mercancia_articulo_23',valorate($demas_datos[tipo_mercancia_articulo_23]));
						$action->text('regimen_articulo_23',valorate($demas_datos[regimen_articulo_23]));
						$action->text('factura_articulo_23',valorate($demas_datos[factura_articulo_23]));
						$action->text('imo_articulo_23',valorate($demas_datos[imo_articulo_23]));
						$action->text('un_articulo_23',valorate($demas_datos[un_articulo_23]));
						$action->text('transportista_articulo_23',valorate($demas_datos[transportista_articulo_23]));
						$action->text('nombre_del_chofer_articulo_23',valorate($demas_datos[nombre_del_chofer_articulo_23]));
						$action->text('no_licencia_articulo_23',valorate($demas_datos[no_licencia_articulo_23]));
						$action->text('placas_articulo_23',valorate($demas_datos[placas_articulo_23]));
						$action->text('economico_articulo_23',valorate($demas_datos[economico_articulo_23]));
						
					// &&&&& DATOS DE SHIPPER CONSIGNEE Y NOTIFY &&&&&&&&&&&&&&
					$action->text('shipper1',valorate($demas_datos[shipper]));
					$action->text('shipper2',valorate($demas_datos[shipper2]));
					$action->text('shipper3',valorate($demas_datos[shipper3]));
					$action->text('shipper4',valorate($demas_datos[shipper4]));
					$action->text('shipper5',valorate($demas_datos[shipper5]));
					$action->text('shipper6',valorate($demas_datos[shipper6]));
					
					$action->text('consignee1',valorate($demas_datos[consignee]));
					$action->text('consignee2',valorate($demas_datos[consignee2]));
					$action->text('consignee3',valorate($demas_datos[consignee3]));
					$action->text('consignee4',valorate($demas_datos[consignee4]));
					$action->text('consignee5',valorate($demas_datos[consignee5]));
					$action->text('consignee6',valorate($demas_datos[consignee6]));
					
					$action->text('notify1',valorate($demas_datos[notify]));
					$action->text('notify2',valorate($demas_datos[notify2]));
					$action->text('notify3',valorate($demas_datos[notify3]));
					$action->text('notify4',valorate($demas_datos[notify4]));
					$action->text('notify5',valorate($demas_datos[notify5]));
					$action->text('notify6',valorate($demas_datos[notify6]));
		
					$cargo = data($link ,"select * from detailcargaexpint where bookingno = '".$txtbooking."' and 
								  tipo_bl = 'MBL' and id_bl in (select idhbl from hblexpint where registrado_por = '".$email_aa."')");
				
					
					$action->text('marks',valorate($cargo[markandnos]));
					$action->text('quantity',valorate($cargo[bulk]));
					$action->text('type_of_bulks',valorate($cargo[type_of_bulks]));
					$action->text('description',valorate($cargo[cargo]));
					$action->text('no_piezas',valorate($cargo[number_of_pieces]));
					$action->text('gross_weight',valorate($cargo[weight]));
					$action->text('net_weight',valorate($cargo[netweight]));
					$action->text('volume',valorate($cargo[measurement]));
					$action->text('harmonized_code',valorate($cargo[harmonized_code]));
					$action->add_js_code("document.getElementById('correcciones').value='".valorate($cargo[correcciones])."'");
					//$action->text('ams',valorate($cargo[ams]));
					  
			 
}

if(trim($referencia_interteam)==''){
	
			// cambio
			$action->text('h_booking',valorate($txtbooking));
			$action->text('pol','ALTAMIRA, TAMAULIPAS');
			$action->text('h_export_reference',valorate($demas_datos[export_reference])); //ok
			$action->text('export_reference',valorate($demas_datos[export_reference])); //ok
			$action->text('h_vessel',valorate($datos[vessel]));
			$action->text('h_referencia',valorate($datos[referencia_cliente]));
			$action->text('h_shipper_file',valorate($demas_datos[referencia_cliente]));
			
			$action->text('booking',valorate($txtbooking));
			
			$action->text('vessel',valorate($datos[vessel]));
			$action->text('referencia',valorate($datos[referencia_cliente]));
			
}

if(trim($demas_datos[status])==''){
			$action->text('status','REVISANDO');
			$action->text('h_status','REVISANDO');
		}

/////////////////////////////////////////////
$action->add_js_code("document.getElementById('submit2').disabled=false;document.getElementById('submit1').disabled=false;");
if(trim($POL_POL)!=''){

$pos = strpos($POL_POL, 'ATM');
if ($pos === false) {
	if(strpos($POL_POL, 'MAN') === false){
	}else{
	$action->alert('EL BOOKING '.$txtbooking.' PERTENECE A '.$POL_POL.'. POR FAVOR DOCUMENTE EN https://'.$_SERVER["SERVER_NAME"].'/ZLO/agenteaduanal' );
	}
	if(strpos($POL_POL, 'VER') === false){
	}else{
	$action->alert('EL BOOKING '.$txtbooking.' PERTENECE A '.$POL_POL.'. POR FAVOR DOCUMENTE EN https://'.$_SERVER["SERVER_NAME"].'/VER/agenteaduanal');
	}
	$action->add_js_code("document.getElementById('submit2').disabled=true;document.getElementById('submit1').disabled=true;");  
	 $action->send_response();
	 return;
}
}
////////////////////////////////////////////

$action->text('h_pol','ALTAMIRA, TAMAULIPAS');
$action->text('pol','ALTAMIRA, TAMAULIPAS');
 

if($referencia_interteam!=''){
	$datum=data($link ,"select * from datosbooking where referencia_cliente = '".$referencia_interteam."' and referencia_cliente<>''");
	$action->text('bandera_de_consolidado',valorate($datum[bandera_de_consolidado]));
    $action->text('h_bandera_de_consolidado',valorate($datum[bandera_de_consolidado]));	
	$action->text('h_pod',valorate($datum[pod]) );
	$action->text('pod',valorate($datum[pod]) );
}else{
 
	$datos=data($link_impo ,"select * from datosexpint where files = '".$q[files]."'");
	$action->text('h_pod',valorate($datos[pod]) );
	$action->text('pod',valorate($datos[pod]) );
}
 
$demas_datos = data($link ,"select * from hblexpint where booking = '".$txtbooking."' and finaldestination<>''  "); //and tipo = 'MBL'
$action->text('final_destination',valorate($demas_datos[finaldestination])); // ok
$action->text('h_final_destination',valorate($demas_datos[finaldestination])); // ok

$action->add_js_code("document.getElementById('booking').click();");  
$action->send_response();

return;

?> 

