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

require_once("comun/actions.php");

require_once("comun/funciones.php");
function valorate($valor){
	$valor = str_replace("'","\'",$valor);
	return $valor;
}

$action = new actions();
$link_impo=con_impo();$link=con_ver();
$usuario= strtoupper($_GET['idusuario']);

$historial_consol = "";
$historial_consol2 = "";
$historial_val = "inicia";

$txtreferencia_cliente= trim(strtoupper($_POST['txtreferencia_cliente'])); // ejemplo: VER1813-CTG
$txtreferencia_lpv= trim(strtoupper($_POST['txtreferencia_lpv'])); 
  
 $action->text('txtbandera_de_consolidado',''); 
 $action->text('LOCATION_CODE_DESTINO_FINAL','');
 $action->text('LOCATION_CODE_POD','');
 
	$action->text('txtcliente','');
	$action->text('txtsolicitud_vacio','');
	$action->text('txtprogramado','');
	$action->text('txtliberado','');
	$action->text('txtdocumentado','');
	$action->text('txtcancelado','');
	
	$action->text('txtbuque','');
	$action->text('txtets','');
	$action->text('txtbooking_naviera','');
	$action->text('txtnum_viaje','');
	$action->text('txtpod','');
	$action->text('txtdestino_final','');
	$action->text('txtnaviera','');
	$action->text('txtalmacen','');
	$action->text('txtlinea','');
	$action->text('txtpedimento','');
	$action->text('txtcontenedor','');
	$action->text('txtdimension','');
	$action->text('txttara','');
	$action->text('txtsello','');
	$action->text('txttipo','');
	$action->text('txtregimen1','');
	$action->text('txtregimen2','');
	$action->text('txtregimen3','');
	$action->text('txtregimen4','');
	$action->text('txtregimen5','');
	$action->text('txtregimen6','');
	$action->text('txtregimen7','');
	$action->text('txtregimen8','');
	
	$action->text('procedencia_vacio','');
	$action->text('almacen_consolidacion','');
	$action->text('desde','');
	$action->text('hacia','');
	$action->text('txttipo_mercancia','');
 $action->text('hora_zarpe','');
 

$datos_lpv=data($link,"select * from datosbooking where referencia_cliente = '$txtreferencia_cliente' and referencia_cliente<>''");	

	$q=data($link_impo,"select * from datosexpint where referencia_interteam = '$txtreferencia_cliente' and referencia_interteam<>''");

if(count($datos_lpv)==0){
	
	if(count($q)>0){
		$cliente = 'INTERTEAM S.A. DE C.V.';
	
 
		$action->text('cierre_documentos_ag_ad',valorate($datos_lpv[closingdocs]));
		$action->text('cierre_pedimentos_ag_ad',valorate($datos_lpv[closingcargoes]));
	
	$action->text('hora_zarpe',valorate($datos_lpv[hora_zarpe]));
		$action->text('txtcliente',valorate($cliente));
		$action->text('txtbuque',valorate($q[vessel]));
		$action->text('txtets',valorate(str_replace('/','-',$q[ets])));

		
		$action->text('txtdestino_final',valorate($q[finaldestination]));
		$action->text('txtpod',valorate($q[pod].', '.$q[pod_country]));
  //$action->text('LOCATION_CODE_DESTINO_FINAL','');
 //$action->text('LOCATION_CODE_POD','');
		$action->text('LOCATION_CODE_DESTINO_FINAL',valorate($q[uncodepod]));
		$action->text('LOCATION_CODE_POD',valorate($q[uncodepod]));


		$booking_ssl_Q = data($link_impo,"SELECT * FROM containerdetailexpint where files like '".$q[files]."%' AND bookingssl<>''");
		
		$tmp_type = explode(" ",$booking_ssl_Q[type]);
		$action->text('txtcontenedor',valorate($booking_ssl_Q[containerno]));
		$action->text('txtdimension',valorate($tmp_type[0]));
		$action->text('txtsello',valorate($booking_ssl_Q[containerseal]));
		$action->text('txttipo',valorate($tmp_type[1]));
		$action->text('txtbooking_naviera',valorate($booking_ssl_Q[bookingssl]));
		$historial_val = $booking_ssl_Q[bookingssl];
		$qhisto=mysqli_query($link,"select * from booking_historial_ata where booking = '".$historial_val."' and concepto = 'REGISTRO CONSOLIDADO'");
		if(mysqli_num_rows($qhisto)>0){

			while($rhisto=mysqli_fetch_array($qhisto)){
				$id=$rhisto[0];
				$booking=$rhisto[1];
				$fecha_registro=$rhisto[2];
				$usuario_registro=$rhisto[5];
				$impresion=$rhisto[6];
				$historial_consol = $historial_consol.$usuario_registro." ".$fecha_registro."</br>";
			}
		}
		$qhisto2=mysqli_query($link,"select * from booking_historial_ata where booking = '".$historial_val."' and concepto = 'FECHA ZARPE'");
		if(mysqli_num_rows($qhisto2)>0){

			while($rhisto2=mysqli_fetch_array($qhisto2)){
				$id=$rhisto2[0];
				$booking=$rhisto2[1];
				$zarpe=$rhisto2[3];
				$hora_zarpe=$rhisto2[4];
				$usuario_registro=$rhisto2[5];
				$impresion=$rhisto2[6];
				$historial_consol2 = $historial_consol2.$usuario_registro." ".$zarpe." ".$hora_zarpe."</br>";
			}
		}
		
		$action->text('txtnum_viaje',valorate($q[voyage]));
		$action->text('txtpod',valorate($q[pod]));
		 
		//$action->text('txtnaviera',valorate($q[shippingline]));
		$action->text('txtalmacen',valorate($q[cfsgeneral]));
		$action->add_js_code("document.getElementById('txtalmacen').options[0].value='".$datos_lpv[cfsgeneral]."';
		document.getElementById('txtalmacen').options[0].innerHTML='".$datos_lpv[cfsgeneral]."';");
		
		//	$action->add_js_code("document.getElementById('txtlinea').options[0].value='".$datos_lpv[cfsgeneral]."';
		//document.getElementById('txtlinea').options[0].innerHTML='".$datos_lpv[cfsgeneral]."';");
		
		$action->add_js_code("document.getElementById('txtnaviera').options[0].value='".valorate($q[shippingline])."';
		document.getElementById('txtnaviera').options[0].innerHTML='".valorate($q[shippingline])."';");
	}
}else{
	con_ver();


	$ref = data($link,"select referencia from referencias_veracruz where referencia_cliente = '$txtreferencia_cliente'");
	$action->text('txtreferencia_lpv',valorate($datos_lpv[referencia_lpv]));
	
	 
if(count($q)>0){
	$action->text('cierre_documentos_ag_ad',str_replace('/','-',$q[closingdocs]));
	$action->text('cierre_pedimentos_ag_ad',str_replace('/','-',$q[closingcargoes]));
}else{
	$action->text('cierre_documentos_ag_ad',valorate($datos_lpv[cierre_documentos_ag_ad]));
	$action->text('cierre_pedimentos_ag_ad',valorate($datos_lpv[cierre_pedimentos_ag_ad]));
}

$action->text('fotos',$datos_lpv[fotos_consolidado]);
	if(trim(valorate($datos_lpv[referencia_lpv]))=='') $action->text('txtreferencia_lpv',valorate($ref[0]));
	$action->text('txtreferencia_lpv',valorate($datos_lpv[referencia_lpv]));
	$action->text('txtcliente',valorate($datos_lpv[cliente]));
	$action->text('txtbuque',valorate($datos_lpv[vessel]));
	
	$action->text('hora_zarpe',valorate($datos_lpv[hora_zarpe]));
	$action->text('LOCATION_CODE_DESTINO_FINAL',valorate($datos_lpv[LOCATION_CODE_DESTINO_FINAL]));
	$action->text('LOCATION_CODE_POD',valorate($datos_lpv[LOCATION_CODE_POD]));
		
		
	//added on 5.3.2018
	$action->text('fotos',valorate($datos_lpv[fotos]));
	$action->text('txtpod',valorate($datos_lpv[pod]));
	$action->text('txtdestino_final',valorate($datos_lpv[finaldestination]));
	
	$action->text('atraque',($datos_lpv[atraque]=='0000-00-00' or $datos_lpv[atraque]=='') ? '' : $datos_lpv[atraque]);
	$action->text('zarpe',($datos_lpv[zarpe]=='0000-00-00' or $datos_lpv[zarpe]=='') ? '' : $datos_lpv[zarpe]); 
	$action->text('cierre_documentacion',($datos_lpv[cierre_documentacion]=='0000-00-00' or $datos_lpv[cierre_documentacion]=='') ? '' : $datos_lpv[cierre_documentacion]);   
	$action->text('cierre_vgm',($datos_lpv[cierre_vgm]=='0000-00-00' or $datos_lpv[cierre_vgm]=='') ? '' : $datos_lpv[cierre_vgm]);  
	$action->text('cierre_buque',($datos_lpv[cierre_buque]=='0000-00-00' or $datos_lpv[cierre_buque]=='') ? '' : $datos_lpv[cierre_buque]);   
	//////////	
	
	$action->text('txtsolicitud_vacio',valorate($datos_lpv[solicitud_vacio]));
	$action->text('txtprogramado',valorate($datos_lpv[programado]));
	$action->text('txtliberado',valorate($datos_lpv[liberado]));
	$action->text('txtdocumentado',valorate($datos_lpv[documentado]));
	$action->text('txtcancelado',valorate($datos_lpv[cancelado]));
	
	$action->text('txtets',valorate(str_replace('/','-',$datos_lpv[ets])));
	$action->text('txtbooking_naviera',valorate($datos_lpv[booking_ssl]));
		$historial_val = $datos_lpv[booking_ssl];
		$qhisto=mysqli_query($link,"select * from booking_historial_ata where booking = '".$historial_val."' and concepto = 'REGISTRO CONSOLIDADO'");
		if(mysqli_num_rows($qhisto)>0){

			while($rhisto=mysqli_fetch_array($qhisto)){
				$id=$rhisto[0];
				$booking=$rhisto[1];
				$fecha_registro=$rhisto[2];
				$usuario_registro=$rhisto[5];
				$impresion=$rhisto[6];
				$historial_consol = $historial_consol.$usuario_registro." ".$fecha_registro."</br>";
			}
		}
		$qhisto2=mysqli_query($link,"select * from booking_historial_ata where booking = '".$historial_val."' and concepto = 'FECHA ZARPE'");
		if(mysqli_num_rows($qhisto2)>0){

			while($rhisto2=mysqli_fetch_array($qhisto2)){
				$id=$rhisto2[0];
				$booking=$rhisto2[1];
				$zarpe=$rhisto2[3];
				$hora_zarpe=$rhisto2[4];
				$usuario_registro=$rhisto2[5];
				$impresion=$rhisto2[6];
				$historial_consol2 = $historial_consol2.$usuario_registro." ".$zarpe." ".$hora_zarpe."</br>";
			}
		}
	$action->text('txtnum_viaje',valorate($datos_lpv[voyage]));

	// quite 23 12 $action->text('txtnaviera',valorate($datos_lpv[shippingline]));
	
	
	$action->text('txtalmacen',valorate($datos_lpv[cfsgeneral]));
	$action->text('almacen_consolidacion',valorate($datos_lpv[almacen_consolidacion]));	
	
  
  //////////////
   	    $action->add_js_code("document.getElementById('txtlinea').options[0].value='".valorate($datos_lpv[linea])."';
		document.getElementById('txtlinea').options[0].innerHTML='".valorate($datos_lpv[linea])."';");
		
		$action->add_js_code("document.getElementById('txtnaviera').options[0].value='".$datos_lpv[shippingline]."';
		document.getElementById('txtnaviera').options[0].innerHTML='".$datos_lpv[shippingline]."';");
  ////////////		

		$action->add_js_code("document.getElementById('txtalmacen').options[0].value='".$datos_lpv[cfsgeneral]."';
		document.getElementById('txtalmacen').options[0].innerHTML='".$datos_lpv[cfsgeneral]."';");
		$action->add_js_code("
		document.getElementById('almacen_consolidacion').options[0].innerHTML='".$datos_lpv[almacen_consolidacion]."';
		document.getElementById('almacen_consolidacion').options[0].value='".$datos_lpv[almacen_consolidacion]."';");	
	
	$action->text('procedencia_vacio',valorate($datos_lpv[procedencia_vacio]));
	$action->text('desde',valorate($datos_lpv[desde]));
	$action->text('hacia',valorate($datos_lpv[hacia]));
	
	 $action->add_js_code("
		document.getElementById('desde').options[0].innerHTML='".$datos_lpv[desde]."';
		document.getElementById('desde').options[0].value='".$datos_lpv[desde]."';");
	$action->add_js_code("
		document.getElementById('hacia').options[0].innerHTML='".$datos_lpv[hacia]."';
		document.getElementById('hacia').options[0].value='".$datos_lpv[hacia]."';"); 
		
	$action->text('txttipo_mercancia',valorate($datos_lpv[tipo_mercancia]));
	$action->text('txtpedimento',valorate($datos_lpv[pedimento]));
	// quite 23 12 $action->text('txtlinea',valorate($datos_lpv[linea]));
	$action->text('txtcontenedor',valorate($datos_lpv[contenedor]));
	$action->text('txtdimension',valorate($datos_lpv[dimension]));
	$action->text('txttara',valorate($datos_lpv[tara]));
	$action->text('txtsello',valorate($datos_lpv[sello]));
	$action->text('txttipo',valorate($datos_lpv[tipo]));
	$action->text('txtbandera_de_consolidado',valorate($datos_lpv[bandera_de_consolidado]));
 
	//$action->alert($datos_lpv[bandera_de_consolidado]);
	$valro=valorate($datos_lpv[contenedor]);
	if(trim($valro)==''){
		if(trim($q[files])!=''){
		con_impo();
		$booking_ssl_Q = data($link_impo,"SELECT * FROM containerdetailexpint where files like '".$q[files]."%' AND bookingssl<>'' and files<>''");
		$tmp_type = explode(" ",$booking_ssl_Q[type]);
		$action->text('txtcontenedor',valorate($booking_ssl_Q[containerno]));
		$action->text('txtdimension',valorate($tmp_type[0]));
		$action->text('txtsello',valorate($booking_ssl_Q[containerseal]));
		$action->text('txttipo',valorate($tmp_type[1]));
		$action->text('txtbooking_naviera',valorate($booking_ssl_Q[bookingssl]));
		$historial_val = $booking_ssl_Q[booking_ssl];
		$qhisto=mysqli_query($link,"select * from booking_historial_ata where booking = '".$historial_val."' and concepto = 'REGISTRO CONSOLIDADO'");
		if(mysqli_num_rows($qhisto)>0){

			while($rhisto=mysqli_fetch_array($qhisto)){
				$id=$rhisto[0];
				$booking=$rhisto[1];
				$fecha_registro=$rhisto[2];
				$usuario_registro=$rhisto[5];
				$impresion=$rhisto[6];
				$historial_consol = $historial_consol.$usuario_registro." ".$fecha_registro."</br>";
			}
		}
		$qhisto2=mysqli_query($link,"select * from booking_historial_ata where booking = '".$historial_val."' and concepto = 'FECHA ZARPE'");
		if(mysqli_num_rows($qhisto2)>0){

			while($rhisto2=mysqli_fetch_array($qhisto2)){
				$id=$rhisto2[0];
				$booking=$rhisto2[1];
				$zarpe=$rhisto2[3];
				$hora_zarpe=$rhisto2[4];
				$usuario_registro=$rhisto2[5];
				$impresion=$rhisto2[6];
				$historial_consol2 = $historial_consol2.$usuario_registro." ".$zarpe." ".$hora_zarpe."</br>";
			}
		}
		con_ver();
		}
	}
	$tmp=explode(',',$datos_lpv[regimen]);
	$action->text('txtregimen1',$tmp[0]);
	$action->text('txtregimen2',$tmp[1]);
	$action->text('txtregimen3',$tmp[2]);
	$action->text('txtregimen4',$tmp[3]);
	$action->text('txtregimen5',$tmp[4]);
	$action->text('txtregimen6',$tmp[5]);
	$action->text('txtregimen7',$tmp[6]);
	$action->text('txtregimen8',$tmp[7]);
}
	//$action->text('txtcomentarios',valorate($datos_lpv[comentarios]));
	$a=valorate($datos_lpv[comentarios]);
	//$a=$datos_lpv[comentarios];
$action->add_js_code("document.getElementById('txtcomentarios').value='$a';document.getElementById('mensaje').innerHTML='<br>';
document.getElementById('submit').disabled=false;
LstAjax('divPartidas','partidas_booking.php?referencia_cliente=".$txtreferencia_cliente."');");

$action->add_js_code("document.getElementById('divHistorialUpdatesATD2').innerHTML=('$historial_consol2');");

$action->add_js_code("document.getElementById('divHistorialUpdatesATD').innerHTML=('$historial_consol');");
	

$action->add_js_code("if(document.getElementById('txtdestino_final').value==''){
	document.getElementById('txtdestino_final').value='".valorate($q[finaldestination])."';
}
if(document.getElementById('txtpod').value==''){
	document.getElementById('txtpod').value='".valorate($q[pod].', '.$q[pod_country])."';
}

");
 
 
 
 	 
$action->send_response();

return;

?> 

