<?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['h_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('h_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_mbl();");
}else{
	$datos=data($link_impo,"select * from datosexpint where files = '".$q[files]."'");
	$POL_POL = $datos[pol];
 
		if($datos[files]==''){
			$action->checked('cargo_vacio',0);
			$action->add_js_code("limpiar_mbl();");
			$action->text('h_booking','');
 
			
			$action->text('h_export_reference',''); //ok
			$action->text('h_vessel','');
			$action->text('h_referencia','');
			$action->text('h_shipper_file','');
			 
		}else{
			
			// ams 1
		$ams1 = busquedaB($link_impo,"select scac from bookingexpint where booking_interteam like '".$txtbooking."%'","scac");
		$ams=$ams1;
		$action->text('ams',valorate($ams1)); 
		$action->text('h_ams',valorate($ams1));
 
		$demas_datos = data($link,"select * from hblexpint where booking = '".$txtbooking."' and tipo = 'MBL' and registrado_por = '".$email_aa."' ");
		
		$action->add_js_code("limpiar_hbl();");
		$action->text('ams',valorate($ams)); 
		$action->text('h_ams',valorate($ams));
	 
			$action->text('status',$demas_datos[status]);
			$action->text('registrado_por',valorate($demas_datos[registrado_por]));
			$action->text('status_cambiado','');
 
			$action->text('h_status_cambiado','');
			
			$action->text('tarja',valorate($demas_datos[tarja]));
			
			$action->checked('cargo_vacio',$demas_datos[cargo_vacio]);
			
		$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])); */
		$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]));
		$action->text('shipper_file',valorate($demas_datos[shipper_file]));
		 
		
		// &&&&& DATOS DE SHIPPER CONSIGNEE Y NOTIFY &&&&&&&&&&&&&&
		if(count($demas_datos)>0){
		$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 SHIPPER CONSIGNEE Y NOTIFY &&&&&&&&&&&&&&
		
		
		 
		 $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])."'");
			if($cargo[ams]!=''){
			$action->text('ams',valorate($cargo[ams]));
			$action->text('h_ams',valorate($ams));
			}
			 
		}
		$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',''); // ok
		$action->text('final_destination',''); */
		$action->text('h_export_reference',''); //ok
		$action->text('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]));
		
		if($ams!=''){
			$action->text('ams',valorate($ams));
			$action->text('h_ams',valorate($ams));
		}
		
		if($demas_datos[status]==''){
			$action->text('status','REVISANDO');
			$action->text('h_status','REVISANDO');
		}
		
 	}
}

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

if($referencia_interteam==''){
//$referencia_interteam = $datos[referencia_interteam];
}
//$action->alert('ref '.$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];
					$datos=data($link,"select * from datosbooking where referencia_cliente = '".$referencia_cliente."' and referencia_cliente<>''");  	
					$action->text('h_vessel',valorate($datos[vessel]));
					/*	$action->text('h_bandera_de_consolidado',valorate($datos[bandera_de_consolidado]));
					 $action->text('bandera_de_consolidado',valorate($datos[bandera_de_consolidado]));
					 $action->text('pod',valorate($datos[pod]) );
					 $action->text('h_pod',valorate($datos[pod]) );
					 */
					$action->text('h_referencia',valorate($datos[referencia_cliente]));
					$action->text('h_shipper_file',valorate($demas_datos[referencia_cliente]));
					$action->text('vessel',valorate($datos[vessel]));
					$action->text('referencia',valorate($datos[referencia_cliente]));
					
		}

}

			 
			
	 
			//$action->add_js_code("limpiar_hbl();");
			$action->text('h_booking',valorate($txtbooking));
 
			
			$action->text('booking',valorate($txtbooking));
			//$action->text('pol','VERACRUZ, VERACRUZ');
			
			
			$action->text('h_status','REVISANDO');
	 
			$action->text('h_export_reference',valorate($demas_datos[export_reference])); //ok
			$action->text('export_reference',valorate($demas_datos[export_reference])); //ok
			
			if(count($demas_datos)==0){
			$action->text('status','REVISANDO');	
			}
			
			if(count($demas_datos)>0){
			$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 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('ams',valorate($cargo[ams]));
					$action->text('h_ams',valorate($cargo[ams]));
					$action->add_js_code("document.getElementById('correcciones').value='".valorate($cargo[correcciones])."'");
		 
}

if(trim($referencia_interteam)==''){
	
			
			$action->text('h_booking',valorate($txtbooking));

			$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_bandera_de_consolidado',valorate($datos[bandera_de_consolidado]));
			$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('bandera_de_consolidado',valorate($datos[bandera_de_consolidado]));
			
			$action->text('vessel',valorate($datos[vessel]));
			$action->text('referencia',valorate($datos[referencia_cliente]));
			
}

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

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

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

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();"); //LstAjax('divPartidas','partidas_bl.php?booking=".$txtbooking."');
		
$action->send_response();

return;

?> 

