<?php


	session_start();
	$id_memo 	= $_GET["id_memo"];
	include 'plantilla_vertical.php';
	require 'conexion.php';
	
	$query2 = "SELECT *,
	(select (p.cod_des) as _emisor_profesional from profesionales p where p.id_profesional= m.id_prof_emisor) as emisor_profesional,
	(select (u.first_name ||' '|| u.last_name) as _emisor from users u where u.id = m.id_emisor) as emisor,
	(select (p.cod_des) as _receptor_profesional from profesionales p where p.id_profesional= m.id_prof_receptor) as receptor_profesional,
	(select (u.first_name ||' '|| u.last_name) as _receptor from users u where u.id = m.id_receptor) as receptor,
	(select (u.first_name ||' '|| u.last_name) as _usuario from users u where u.id = m.id_usuario) as usuario
	from memos m
	where m.id_memo  = " . $id_memo;
	


	$query1 = "SELECT row_number() OVER(order by e.codigo_unico), e.id as expediente, e.codigo_unico, e.nombre_completo, 
		e.codigo_meu, a.nombre as afectacion, t.cod_geografico as tramo, pr.descripcion as proyecto, m.id_memo, d.id_exp, 
		m.descripcion, m.referencia, m.nro_memo, m.anho_memo,  m.id_emisor, m.id_receptor, 
		m.id_usuario, d.notificacion, d.id_detalle,
		(select (p.cod_des) as _emisor_profesional from profesionales p where p.id_profesional= m.id_prof_emisor) as emisor_profesional,
		(select (u.first_name ||' '|| u.last_name) as _emisor from users u where u.id = m.id_emisor) as emisor,
		(select (p.cod_des) as _receptor_profesional from profesionales p where p.id_profesional= m.id_prof_receptor) as receptor_profesional,
		(select (u.first_name ||' '|| u.last_name) as _receptor from users u where u.id = m.id_receptor) as receptor,
		(select (u.first_name ||' '|| u.last_name) as _usuario from users u where u.id = m.id_usuario) as usuario
	from ide.expedientes e
		join memos_detalle d on d.id_exp = e.id
		join memos m on m.id_memo = d.id_memo
		join tramos t on  t.id_tramo = e.id_tramo
		join tipos_afeccion a on a.id_tipo_afeccion = e.id_tipo_afectacion
		join proyectos pr on pr.id_proyecto = e.id_proyecto
	where m.id_memo = " . $id_memo;

	
	$resultado = pg_query($query1);

	$pdf = new PDF(); //vertical
	//$pdf = new PDF('L'); //horizontal
	$pdf->AliasNbPages();
	$pdf->AddPage();
	     		
	$i = 0;
	$j = 0;
	$pagina1 = 1;
	while($row = pg_fetch_array($resultado))
	{
		$i = $i+1;
		$j = $j+1;

		if ($i == 1)
		{
			/** Cabecera == Título **/

			$pdf->SetFont('Arial','I',7);
		    $pdf->SetY(18);
		    $pdf->SetX(10);
			$pdf->Cell(180,6,'Impreso por: ' . $row['usuario'] ,0,1,0);

		    $pdf->Ln(10);
			$pdf->Cell(30);
			$pdf->SetFont('Arial','B',18);
			$pdf->Cell(135,10,utf8_decode('MEMO CDA - DBI N° ' . $row['nro_memo'] . '/' . $row['anho_memo'] ),0,0,'C');
			$pdf->Ln(8);
			$pdf->SetFont('Arial','I',5);
			$pdf->Cell(20,10, '____________________________________________________________________________________________________________________________________________________________________________________________');
			$pdf->Ln(8);
			$pdf->SetFont('Arial','B',12);
			$pdf->Ln(5);
			$pdf->SetFont('Arial','B',11);
			$pdf->Cell(20,10,'A   			                    :'.utf8_decode('     Asesoría Jurídica -  ').utf8_decode($row['emisor_profesional']). '. ' .utf8_decode($row['emisor']));
			$pdf->Ln(5);
			$pdf->SetFont('Arial','I',11);
			$pdf->Cell(20,10,'    			                 '.utf8_decode('           Dirección de Bienes Inmobiliarios'));
			$pdf->Ln(8);
			$pdf->SetFont('Arial','B',12);
			$pdf->Cell(20,10, 'DE    	                :'.utf8_decode('     Coordinación de Avalúo -  ').utf8_decode($row['receptor_profesional']). '. ' .utf8_decode($row['receptor']));
			$pdf->Ln(5);
			$pdf->SetFont('Arial','I',11);
			$pdf->Cell(20,10,'    			                 '.utf8_decode('           Dirección de Bienes Inmobiliarios'));
			$pdf->Ln(8);
			$pdf->SetFont('Arial','B',12);
			$pdf->Cell(20,10, 'FECHA  		         :');
			$pdf->Cell(58,10,date('d-m-Y'),0,1,'C');
			$pdf->SetFont('Arial','B',12);
			$pdf->Cell(20,10, 'REFERENCIA	 :'.'     '.utf8_decode($row['referencia']));
		   
			$pdf->Ln(8);
			$pdf->SetFont('Arial','I',5);
			$pdf->Cell(20,10, '____________________________________________________________________________________________________________________________________________________________________________________________');
			$pdf->Ln(8);
			$pdf->SetFont('Arial','I',11);
			$pdf->MultiCell(190,6,utf8_decode($row['descripcion']),0,'L',0);
			$pdf->Ln(6);
		/*	****	Fin Cabecera	*******	*/


		/* ******** Detalle	*************** */
					//$id_detalle = 0;
					$aux = 0;
					if ($row['id_detalle'] > 0) 
					{
						$aux = $query1;
						$pdf->SetFillColor(232,232,232);
						$pdf->SetFont('Arial','B',10);
						$pdf->Cell(10,6,utf8_decode('N°'),1,0,'C',1);
						$pdf->Cell(16,6,utf8_decode('Notif.'),1,0,'C',1);
						$pdf->Cell(18,6,utf8_decode('Codigo'),1,0,'C',1);
						$pdf->Cell(15,6,'M.E.U',1,0,'C',1);
						$pdf->Cell(20,6,'Tramo',1,0,'C',1);
						$pdf->Cell(25,6,utf8_decode('Afectación'),1,0,'C',1);
						$pdf->Cell(22,6,utf8_decode('Proyecto'),1,0,'C',1);
						$pdf->Cell(55,6,'Nombre Completo',1,1,'C',1);
						
						$pdf->SetFont('Arial','',8);
					}
						else {
							echo "sin datos";	
						}
			}
			
		
			//$id_detalle = 0;
			$aux = 0;
			if ($row['id_detalle'] > 0) 
			{
				$pdf->Cell(10,6,$row['row_number'],1,0,'C',0);
				$pdf->Cell(16,6,utf8_decode($row['notificacion']),1,0,'C',0);
				$pdf->Cell(18,6,$row['codigo_unico'],1,0,'C',0);
				$pdf->Cell(15,6,$row['codigo_meu'],1,0,'C',0);
				$pdf->Cell(20,6,$row['tramo'],1,0,'C',0);	
				$pdf->Cell(25,6,$row['afectacion'],1,0,'C',0);	
				$pdf->Cell(22,6,$row['proyecto'],1,0,'C',0);	
				$pdf->MultiCell(55,6,utf8_decode($row['nombre_completo']),1,'L',0);
			}

		else {
				echo "sin datos";	
			}
		/* ******** Fin Detalle	*************** */
		
		
	}
	if ($i==0){	
		$resultado = pg_query($query2);

		while($row = pg_fetch_array($resultado))
			{
			$i = $i+1;
			$j = $j+1;

			if ($i == 1)
			{
				/** Cabecera == Título **/

				$pdf->SetFont('Arial','I',7);
			    $pdf->SetY(18);
			    $pdf->SetX(10);
				$pdf->Cell(180,6,'Impreso por: ' . $row['usuario'] ,0,1,0);

			    $pdf->Ln(10);
				$pdf->Cell(30);
				$pdf->SetFont('Arial','B',18);
				$pdf->Cell(135,10,utf8_decode('MEMO CDA - DBI N° ' . $row['nro_memo'] . '/' . $row['anho_memo'] ),0,0,'C');
				$pdf->Ln(8);
				$pdf->SetFont('Arial','I',5);
				$pdf->Cell(20,10, '____________________________________________________________________________________________________________________________________________________________________________________________');
				$pdf->Ln(8);
				$pdf->SetFont('Arial','B',12);
				$pdf->Ln(5);
				$pdf->SetFont('Arial','B',11);
				$pdf->Cell(20,10,'A   			                    :'.utf8_decode('      Asesoría Jurídica - ').utf8_decode($row['emisor_profesional']). '. ' .utf8_decode($row['emisor']));
				$pdf->Ln(5);
				$pdf->SetFont('Arial','I',11);
				$pdf->Cell(20,10,'    			                 '.utf8_decode('            Dirección de Bienes Inmobiliarios'));
				$pdf->Ln(8);
				$pdf->SetFont('Arial','B',12);
				$pdf->Cell(20,10, 'DE    	                :'.utf8_decode('     Coordinación de Avalúo - ').utf8_decode($row['receptor_profesional']). '. ' .utf8_decode($row['receptor']));
				$pdf->Ln(5);
				$pdf->SetFont('Arial','I',11);
				$pdf->Cell(20,10,'    			                 '.utf8_decode('           Dirección de Bienes Inmobiliarios'));
				$pdf->Ln(8);
				$pdf->SetFont('Arial','B',12);
				$pdf->Cell(20,10, 'FECHA  		         :');
				$pdf->Cell(58,10,date('d-m-Y'),0,1,'C');
				$pdf->SetFont('Arial','B',12);
				$pdf->Cell(20,10, 'REFERENCIA	 :'.'     '.utf8_decode($row['referencia']));
			   
				$pdf->Ln(8);
				$pdf->SetFont('Arial','I',5);
				$pdf->Cell(20,10, '____________________________________________________________________________________________________________________________________________________________________________________________');
				$pdf->Ln(8);
				$pdf->SetFont('Arial','I',11);
				$pdf->MultiCell(190,6,utf8_decode($row['descripcion']),0,'L',0);
				$pdf->Ln(6);
			}
		}
	}

/*
	$pdf->Ln(18);
	$pdf->SetFont('Arial','I',12);
	$pdf->Cell(20,10,utf8_decode('Atentamente,'));
*/
	$pdf->Output();
	//$pdf->Output('../carpeta/archivos/' . $nombrepdf.'.pdf', 'F');
	//$pdf->Output("DBI.pdf". $nro_memo . '-'. $anho_memo ,'F');
	echo "<script language='javascript'>window.open('DBI.pdf','_self');</script>";//para el archivo pdf generado
	exit;
?>