<?php
session_start();

header("Content-Type: text/html; charset=utf-8");
date_default_timezone_set('America/Bogota');
//if (isset($_SESSION['user'])&&!empty($_SESSION['user'])) {

/* http://programarenphp.wordpress.com */
/* incluimos primeramente el archivo que contiene la clase fpdf */
include '../conector.php';

//echo $_POST['val9']." VAL9 <br>";

include ('fpdf.php');

class PDF extends FPDF
{
function Header()
{

$reporte='CONSOLIDADO DE NOTAS  X CURSO';
$salida='Consolidado_Curso.pdf';
$today_h = date('d-m-Y');	
$ancho_banner=29;
$sel_curso="select codigo,nombre from curso where curso_id=".$_POST['cur9']." ";
$res_sel_curso=mysql_query($sel_curso);
$curso=mysql_fetch_array($res_sel_curso);

$sel_planilla="select anno,periodo from planilla where id=".$_POST['per9']." ";
$res_sel_planilla=mysql_query($sel_planilla);
$planilla=mysql_fetch_array($res_sel_planilla);

if(isset($_POST['mat9_2'])&&!empty($_POST['mat9_2'])){
$reporte='CONSOLIDADO NOTAS DE REG. DIARIO X CURSOS';
$salida='Consolidado_Notas_RD_x_Cursos.pdf';
$sel_mat="select sigla,nombre from asignatura where subject_id=".$_POST['mat9_2']." ";
$res_sel_mat=mysql_query($sel_mat);
$mat=mysql_fetch_array($res_sel_mat);

// 1. valida tama�ao del arreglode encabezados
$vari3[0]='';
$cont3=1;
$arreglo_res2=explode(",", $_POST['val9']);
	for($k=0;$k<1;$k++){
	$arreglo_ins3=explode("|", $arreglo_res2[$k]);
		for($l=1;$l<=sizeof($arreglo_ins3);$l++){
				if((substr($arreglo_ins3[$l],0,3)=='Reg'||substr($arreglo_ins[$l],0,3)=='PRO')&&$k=1){
				$vari3[$cont3]=$arreglo_ins3[$l];
				$cont3++;
				}
		}
	}
	//asigna ancho de banner
	if(sizeof($vari3)>6){$ancho_banner=35;}else{$ancho_banner=29;}

// fin 1.



}
if(isset($_POST['mat9'])&&!empty($_POST['mat9'])){
$reporte='CONSOLIDADO DE NOTAS  X MATERIA';
$salida='Consolidado_Notas_x_Materia.pdf';
$sel_mat="select sigla,nombre from asignatura where subject_id=".$_POST['mat9']." ";
$res_sel_mat=mysql_query($sel_mat);
$mat=mysql_fetch_array($res_sel_mat);
}
     
	$this->Ln(0.2);
	$this->SetFillColor(198,217,241);
	$this->SetFont('Arial','B',13); 
	$this->Cell(3,1,' ',0,0,'C');
	if((isset($_POST['mat9'])&&!empty($_POST['mat9']))|| (isset($_POST['mat9_2'])&&!empty($_POST['mat9_2']))){    
        	$this->Cell($ancho_banner,1,' PLANILLA ELECTRONICA  CURSO:'.$curso[0]."   PERIODO:".$planilla[1]." - ".$planilla[0]." MAT: ".$mat[0],0,1,'L',TRUE); 
		$this->SetFillColor(255,255,255);
	}else{
        	$this->Cell($ancho_banner,1,' PLANILLA ELECTRONICA  CURSO:'.$curso[0]."   PERIODO:".$planilla[1]." - ".$planilla[0],0,1,'L',TRUE); 
		$this->SetFillColor(255,255,255);	

	}
	$this->SetFillColor(255,255,255);
	$this->Cell(3.1,1,' ',0,0,'C');
	$this->SetFont('Arial','b',10); 
        $this->Cell(8,1,' '.$reporte,0,0,'L',false);
        $this->Cell(0.1,1,' ',0,0,'C');        
        $this->Cell(2,0.5,'',0,0,'C',false);
        $this->Cell(0.1,1,' ',0,0,'C');        
        $this->Cell(2,0.5,'',0,0,'C',false);
        $this->Cell(0.1,1,' ',0,0,'C');        
        $this->Cell(2,0.5,'',0,0,'C',false);
        $this->Cell(0.1,1,' ',0,0,'C');        
        $this->Cell(3,0.5,'',0,1,'C',false);        

	$this->Ln(0.1);
	$this->Cell(9.1,0.9,' ',0,0,'C');
	$this->SetFont('Arial','b',8); 
        $this->Cell(3.8,0.9,'',0,0,'C',false);
        $this->Cell(3.8,0.9,'',0,0,'C',false);
        $this->Cell(3.8,0.9,'',0,1,'C',false);

	$this->Cell(9.1,0.5,' ',0,0,'C');
	$this->SetFont('Arial','b',10); 
        $this->Cell(3.8,0.5,'',0,0,'C',false);
        $this->Cell(3.8,0.5,'',0,0,'C',false);
        $this->Cell(3.8,0.5,'',0,1,'C',false);
        
	$this->SetFont('Arial','',7); 
      	$this->Cell(9.1,0.7,'',0,0,'L');
	$this->SetFont('Arial','b',7); 
        $this->Cell(3.8,0.7,'',0,0,'C',false);
        $this->Cell(3.8,0.7,'',0,0,'C',false);
        $this->Cell(3.8,0.7,'',0,1,'C',false);

	$this->Image('../img/escudo.jpg',0.5,0.7,3);
        $this->Ln(0.1);

}
//Pie de p�gina
function Footer()
{
$today_h = date('d-m-Y H:i:s');
$this->SetY(-1);
$this->SetTextColor(0,0,0);
$this->SetFont('Arial','',6.5); 
    $this->Cell(4.5,0.5,'Fecha Generacion: '.$today_h,0,0,'L'); 

   }
  
}

/* tenemos que generar una instancia de la clase */
//  $pdf=new PDF('P','cm','letter');

$color=242;$cont=1;
$vari[0]='';
$cont2=1;
$arreglo_res=explode(",", $_POST['val9']);

if(isset($_POST['mat9_2'])&&!empty($_POST['mat9_2'])){

	for($k=0;$k<1;$k++){
	$arreglo_ins3=explode("|", $arreglo_res[$k]);
		for($l=1;$l<=sizeof($arreglo_ins3);$l++){
				if((substr($arreglo_ins3[$l],0,3)=='Reg'||substr($arreglo_ins[$l],0,3)=='PRO')&&$k=1){
				$vari[$cont2]=$arreglo_ins3[$l];
				$cont2++;
				}
		}
	}
	
	if(sizeof($vari)>6){
	$pdf=new PDF('L','cm','a3');
	$pdf->AddPage();
	}else{
	$pdf=new PDF('L','cm','legal');
	$pdf->AddPage();
	}
	

	for($k=0;$k<1;$k++){
	$arreglo_ins2=explode("|", $arreglo_res[$k]);
	$pdf->Cell(7.2,0.5,'',1,0,'C',false);
		for($l=1;$l<sizeof($arreglo_ins2);$l++){
				if((substr($arreglo_ins2[$l],0,3)=='Reg'||substr($arreglo_ins2[$l],0,3)=='PRO')&&$k=1){
				//$vari[$cont2]=$arreglo_ins2[$l];
				$pdf->SetFont('Arial','b',7);
				$pdf->Cell(3.2,0.5," ".utf8_decode($arreglo_ins2[$l]),1,0,'C',false);
				//$cont2++;
				}
		}
		$pdf->Cell(0.5,0.5," ",0,1,'C',false);
	}


	for($i=0;$i<sizeof($arreglo_res);$i++){
		if($cont==1){$color=212;$cont=2;}elseif($cont==2){$color=242;$cont=1;}
		$pdf->SetFont('Arial','b',7);
		$pdf->SetFillColor($color,$color,$color);
		$arreglo_ins=explode("|", $arreglo_res[$i]);
		
			for($j=1;$j<sizeof($arreglo_ins);$j++){
			
			/*	if($arreglo_ins[$j]=='0'){
						$pdf->SetFont('Arial','b',6);
						$pdf->Cell(0.9,0.5," ".str_replace(".",",", $arreglo_ins[$j]),1,0,'L',true);
				}*/
				
				
				if(($arreglo_ins[$j]=='0')||(isset($arreglo_ins[$j])&&!empty($arreglo_ins[$j])&&$arreglo_ins[$j]!=' '&&$arreglo_ins[$j]!=''&&substr($arreglo_ins[$j],0,3)!='Reg'&&substr($arreglo_ins[$j],0,3)!='PRO')){
					$texto=str_replace(".", ".", $arreglo_ins[$j]);	
					$texto=str_replace("<td align=","", $texto);
					$texto=str_replace("<b>","", $texto);
					$texto=str_replace("</b>","", $texto);
					$texto=str_replace("N.Global 60% (Parcial)","60%P", $texto);			
					$texto=str_replace("N.Global 60%","60%G", $texto);			
					$texto=str_replace("N.Global 40%","40%G", $texto);						
					$texto=str_replace("INDICADOR","Ind", $texto);	
					$texto=str_replace("ACUM.GRAL","Acum", $texto);				
					$texto=str_replace("Acum.Total","Acum T", $texto);
					
					if($arreglo_ins[$j]=='0'){$pdf->SetTextColor(236,72,72);}else{{$pdf->SetTextColor(0,0,0);}}
					if($j!=3){	
						$pdf->SetFont('Arial','b',7);
						if($j==2){
						$pdf->SetFont('Arial','b',6);
						$pdf->Cell(0.9,0.5," ".str_replace(".",",", utf8_decode($arreglo_ins[$j])),1,0,'L',true);
						}
						elseif(($j+1)==sizeof($arreglo_ins)){
						$pdf->SetFont('Arial','b',7);
						$pdf->Cell(0.8,0.5," ".str_replace(".",",", $arreglo_ins[$j]),1,0,'C',true);}
						else{
						$pdf->SetFont('Arial','b',7);
						$pdf->Cell(0.8,0.5," ".str_replace(".",",", $arreglo_ins[$j]),1,0,'C',true);}
						
					     }
					if($j==3){
					
					$pdf->SetFont('Arial','b',6);
						$pdf->Cell(5.5,0.5," ".str_replace(".", ",", utf8_decode($arreglo_ins[$j])),1,0,'L',true);
					
					
					}						     
			    }	
			}
					$pdf->Cell(0.5,0.5," ",0,1,'C',false);
	}
}
elseif(isset($_POST['mat9'])&&!empty($_POST['mat9'])){

	$pdf=new PDF('L','cm','legal');
	$pdf->AddPage();


	for($i=0;$i<sizeof($arreglo_res);$i++){
	if($cont==1){$color=212;$cont=2;}elseif($cont==2){$color=242;$cont=1;}
	$pdf->SetFont('Arial','b',6);

	$arreglo_ins=explode("|", $arreglo_res[$i]);
	
		for($j=1;$j<(sizeof($arreglo_ins)-1);$j++){

			if($j!=4){
			$texto=str_replace(".", ".", $arreglo_ins[$j]);	
			$texto=str_replace("<b>","", $texto);
			$texto=str_replace("</b>","", $texto);
			$texto=str_replace("N.Global 60% (Parcial)","60%P", $texto);			
			$texto=str_replace("N.Global 60%","60%G", $texto);			
			$texto=str_replace("N.Global 40%","40%G", $texto);						
			$texto=str_replace("INDICADOR","Ind", $texto);	
			$texto=str_replace("ACUM.GRAL","Acum", $texto);				
			$texto=str_replace("Acum.Total","Acum T", $texto);		
										
			//$texto=str_replace("N.Global ","", $texto);

				$pdf->SetFillColor($color,$color,$color);
				$pdf->SetFont('Arial','b',6);
				if($j==3){
				$pdf->SetFont('Arial','b',7);
				$pdf->Cell(6.5,0.5," ".utf8_decode($texto),1,0,'L',true);}
				elseif($j==2||$j==1){
				$pdf->SetFont('Arial','b',6);
				$pdf->Cell(1,0.5," ".$texto,1,0,'C',true);}
				elseif($j==24){
				$pdf->SetFont('Arial','',6);
					if($i==0){$pdf->SetFont('Arial','b',6);}
				
				$pdf->Cell(1,0.5," ".$texto,1,0,'C',true);}
				elseif($j==28){
				$pdf->SetFont('Arial','b',6);
				$pdf->Cell(1,0.5," ".$texto,1,0,'C',true);}
				else{
				$pdf->SetFont('Arial','b',7);
				$pdf->Cell(1,0.5," ".$texto,1,0,'C',true);}
				
			}
		}
				$pdf->Cell(0.5,0.5," ",0,1,'C',false);
	}

}
else{
	$pdf=new PDF('L','cm','legal');
	$pdf->AddPage();

	for($i=0;$i<sizeof($arreglo_res);$i++){
	if($cont==1){$color=212;$cont=2;}elseif($cont==2){$color=242;$cont=1;}
	$pdf->SetFont('Arial','b',7);

	$arreglo_ins=explode("|", $arreglo_res[$i]);
	
		for($j=1;$j<sizeof($arreglo_ins);$j++){
			$pdf->SetFillColor($color,$color,$color);
			if($j!=3){	
				$pdf->SetFont('Arial','b',7);
				if($j==4){
				$pdf->SetFont('Arial','b',7);
				$pdf->Cell(7,0.5," ".str_replace(".", ",", utf8_decode($arreglo_ins[$j])),1,0,'L',true);}
				elseif(($j+1)==sizeof($arreglo_ins)){
				$pdf->SetFont('Arial','b',7);
				$pdf->Cell(1.5,0.5," ".str_replace(".", ",", $arreglo_ins[$j]),1,0,'C',true);}
				else{
				$pdf->SetFont('Arial','b',7);
				$pdf->Cell(1.5,0.5," ".str_replace(".", ",", $arreglo_ins[$j]),1,0,'C',true);}
				
			}
		}
				$pdf->Cell(0.5,0.5," ",0,1,'C',false);
	}

}

if(empty($salida)){$salida='Informe_Curso_Global.pdf';}
$pdf->Output($salida."",'I');



	?>