<?php
require('../../fpdf/fpdf.php');

class PDF extends FPDF
{
	var $pdf2;
	var $pdf3;
		
	function Header()
	{
		$this->Image('../imagenes_estilos/pleca_pdf.jpg', 10, 10, 190, 0);
		$this->Ln(5);
	}

	function Footer()
	{
		$this->SetY(-28);
		$this->SetFont('Arial','',8);
		$this->Cell(0,10,$this->PageNo(),0,0,'C');
		$this->Image(('../imagenes_estilos/pleca_footer_reportes.jpg',10 , 260, 190, 0);
		
	}
	
	function carta($id_carta, $pdf3)
	{
		$pdf = null;
		if($pdf3 == null)
		{
			$this->pdf3 = new PDF();
			$pdf =& $this->pdf3;
			$pdf->SetAutoPageBreak(true , 30);
			$pdf->SetLeftMargin(25);
			$pdf->SetRightMargin(25);
			$pdf->AddPage('P', 'Letter');
		}
		else
		{		
			$pdf =& $this->pdf3;
			//$pdf->SetY(270);
			$pdf->Ln(10);
		}
		
		$tabla = "cartas";
		$nombre_id = "id_carta";
		
		
		//Obtenemos datos de los alumnos
		$query_alumno = mysql_query("SELECT matricula, nombre_alumno, fecha_clinica, fecha_final, id_escenario_clinica FROM $tabla AS t, alumnos AS a WHERE  t.id_escenario_clinica = a.id_escenario_clinica AND $nombre_id = $id_carta");
		$result_alumno = mysql_fetch_assoc($query_alumno);
		$id_alumno = $result_alumno['id_alumno'];
		// Obtenemos datos del escenario
		$query_escenario = mysql_query("SELECT nombre_escenario_clinica, resp_institucion, puesto_resp, dia_escenario, horario FROM $tabla AS t, escenarios_clinica AS ec WHERE  t.id_escenario_clinica = ec.id_escenario_clinica AND $nombre_id = $id_carta");
		$result_escenario = mysql_fetch_assoc($query_escenario);
		$id_escenario = $result_escenario['id_escenario_clinica'];
		
		$fecha_carta = "";
		$numero_carta = "";
		$resp_institucion = "";
		$puesto_resp = "";
		$texto_carta = "";
		$texto_final = "";
		$texto_firma = "";
		
		
		//Buscamos si ya hay una carta existente asignada a ese reporte
		$query = mysql_query("SELECT * FROM cartas WHERE id_carta = $id_carta");
		if(mysql_num_rows($query) > 0)
		{
			
			$result = mysql_fetch_assoc($query);
			$fecha_carta = $result['fecha_carta'];
			$numero_carta = $result['numero_carta'];
			$resp_institucion = $result['resp_institucion'];
			$puesto_resp = $result['puesto_resp'];
			$texto_carta = $result['texto_carta'];
			$texto_final = $result['texto_final'];
			$texto_firma = $result['texto_firma'];
			$pdf->SetFont('Arial','',11);
			$pdf->Cell(0,10, "Cuernavaca, Mor. $fecha_carta", 0, 0, 'L');
			$pdf->Ln(15);		
					
			$pdf->SetFont('Arial','B',12);
			//$pdf->Cell(0,10, "$result_escenario[nombre_escenario_clinica]", 0, 0, 'L');
			$pdf->Ln(5);		
			

			$pdf->SetFont('Arial','',11);
			
			/*$pdf->Cell(0,10, "$result_escenario[resp_institucion] - $result_escenario[puesto_resp]", 0, 0, 'L');
			$pdf->Ln(5);		
			$pdf->Cell(0,10, "$result_cliente[CiudadEmpresa], $result_cliente[EdoEmpresa]", 0, 0, 'L');
			$pdf->Ln(5);
			$pdf->Cell(0,10, "C.P. $result_cliente[CPEmpresa]", 0, 0, 'L');
			$pdf->Ln(5);
			$pdf->Cell(0,10, "Tel: $result_cliente[TelEmpresa]", 0, 0, 'L');
			$pdf->Ln(5);
			$pdf->Cell(0,10, "Fax. $result_cliente[FaxEmpresa]", 0, 0, 'L');
			$pdf->Ln(15);*/
			
			$pdf->Cell(0,10, "REF: $numero_carta", 0, 0, 'L');
			$pdf->Ln(15);

			$pdf->Cell(0,10, "AT'N: $resp_institucion", 0, 0, 'L');
			$pdf->Ln(5);
			
			$pdf->Cell(0,10, "$puesto", 0, 0, 'L');
			$pdf->Ln(15);
			
			$pdf->MultiCell(0, 5, "$texto_carta", 0, 'J', false);
			$pdf->Ln(5);

			

			$pdf->Cell(16,10, "Alumnos:", 0, 0, 'L');
			$pdf->SetFont('Arial','B',11);
			$pdf->Cell(0,10, "$result_alumno[id_alumno]", 0, 0, 'L');
			$pdf->Ln(5);
			
			/*$pdf->SetFont('Arial','',11);
			$pdf->Cell(23,10, "Contraseña:", 0, 0, 'L');
			$pdf->SetFont('Arial','B',11);
			$pdf->Cell(0,10, "$result_cliente[Contrasena]", 0, 0, 'L');
			$pdf->Ln(15);
			
			$pdf->SetFont('Arial','',11);
			$pdf->MultiCell(0, 5, "Agradecemos su atención prestada y quedamos a sus órdenes para cualquier aclaración.", 0, 'J', false);
			$pdf->Ln(20);
			
			if($firma == 1)
			{
				$pdf->Image('../../reportes_imagenes/firmahugo.jpg', 90, $pdf->GetY() + 7, 30, 0);
				$pdf->MultiCell(0, 5, "ATENTAMENTE: \n\n\n\n\nIng. Oscar Hugo Osorio\nGerente del Departamento de\nAsesoría y Servicios", 0, 'C', false);
				$pdf->Ln(5);
			}*/
			$pdf->MultiCell(0, 5, "$texto_final", 0, 'J', false);
			$pdf->Ln(5);
			$pdf->MultiCell(0, 5, "$texto_firma", 0, 'J', false);
			$pdf->Ln(5);
			$pdf->SetLeftMargin(10);
			$pdf->SetY(270);
			
			if($pdf3 == null)
			$pdf->Output();
		}
	}
}

function resize_img($goal_width,$goal_height,$width,$height)
{ 
    $return = array('width' => $width, 'height' => $height); 
    
    // If the ratio > goal ratio and the width > goal width resize down to goal width 
    if ($width/$height > $goal_width/$goal_height && $width > $goal_width) { 
        $return['width'] = $goal_width; 
        $return['height'] = $goal_width/$width * $height; 
    } 
    // Otherwise, if the height > goal, resize down to goal height 
    else if ($height > $goal_height) { 
        $return['width'] = $goal_height/$height * $width; 
        $return['height'] = $goal_height; 
    } 
    
    return $return; 
}
?>