<?php
require('fpdf/fpdf.php');   //Подключение класса FPDF
$pdf=new FPDF();   //Создание экземпляра класса FPDF
$pdf->AddFont('Arial', '', 'arial.php');   //Добавление шрифта Arial
$pdf->AddFont('Arial_bold', '', 'arial_bold.php');   //Добавление шрифта Arial
$fh = fopen('login.cfg', "r") or die("Can't open file!");
$login = fread($fh, filesize('login.cfg'));
fclose($fh);
$fh = fopen('pass.cfg', "r") or die("Can't open file!");
$pass = fread($fh, filesize('pass.cfg'));
fclose($fh);
$fh = fopen('sid.cfg', "r") or die("Can't open file!");
$sid = fread($fh, filesize('sid.cfg'));
fclose($fh);
  
$c=OCILogon($login, $pass, $sid);
if(!isset($_GET["pdf"]))
	{
	exit();
	}
	$id=htmlspecialchars($_GET["pdf"]);
	
	$s = OCIParse($c, "select type_name from products_type where id_type=".$id);
	OCIExecute($s, OCI_DEFAULT);
	if(OCIFetch($s))
		{
		$name=htmlspecialchars(ociresult($s,"TYPE_NAME"));
		}
	$sql="select TP.nom, loo.name from list_of_operation loo";
	$sql=$sql." join TP on TP.id_listop=loo.id_listop";
	$sql=$sql."  where tp.id_type=".$id." order by  nom";

	$s = OCIParse($c, $sql);
	OCIExecute($s, OCI_DEFAULT);
	$i=0;
	while(OCIFetch($s))
		{
		$op[$i]=htmlspecialchars(ociresult($s,"NAME"));
		$i++;
		}
		
	$sql="select TP.nom, equipment.name from list_of_operation loo";
	$sql=$sql." join TP on TP.id_listop=loo.id_listop";
	$sql=$sql." join match_equip_op on match_equip_op.id_listop=loo.id_listop";
	$sql=$sql." join equipment on equipment.id_equipment=match_equip_op.id_equipment";
	$sql=$sql." where tp.id_type=".$id." order by  nom";

	$s = OCIParse($c, $sql);
	
	for($i=0;$i<count($op);$i++)
		$str_eq[$i]="";
	$curr=1;	
	$i=0;
	OCIExecute($s, OCI_DEFAULT);
	while(OCIFetch($s))
		{
		//echo "nom=".htmlspecialchars(ociresult($s,"NOM")).";cur=".$curr.";i=".$i.";".htmlspecialchars(ociresult($s,"NAME"))."<br/>\n";
		if($curr==htmlspecialchars(ociresult($s,"NOM")))
			{
			$str_eq[$i]=$str_eq[$i].", ".htmlspecialchars(ociresult($s,"NAME"));
			}
		else
			{
			$curr++;
			$i++;
			$str_eq[$i]=htmlspecialchars(ociresult($s,"NAME"));
			}
		}
	$str_eq[0]=substr($str_eq[0],2,strlen($str_eq[0])-2);

	
	$sql="select TP.nom,Resources.name, res_usage.rcount from list_of_operation loo";
	$sql=$sql." join TP on TP.id_listop=loo.id_listop";
	$sql=$sql." join TP_res_us on TP_res_us.id_tp=TP.id_tp";
	$sql=$sql." join Res_usage on Res_usage.id_resus=TP_res_us.id_resus";
	$sql=$sql." join Resources on Resources.id_res=Res_usage.id_res";
	$sql=$sql." where tp.id_type=".$id." order by  nom";
	
	$s = OCIParse($c, $sql);
	
	for($i=0;$i<count($op);$i++)
		$str_res[$i]="";
	$curr=1;	
	$i=0;
	OCIExecute($s, OCI_DEFAULT);
	while(OCIFetch($s))
		{
		//echo "nom=".htmlspecialchars(ociresult($s,"NOM")).";cur=".$curr.";i=".$i.";".htmlspecialchars(ociresult($s,"NAME"))."<br/>\n";
		if($curr==htmlspecialchars(ociresult($s,"NOM")))
			{
			$str_res[$i]=$str_res[$i].", ".htmlspecialchars(ociresult($s,"NAME"))."(".htmlspecialchars(ociresult($s,"RCOUNT")).")";
			}
		else
			{
			$curr++;
			$i++;
			$str_res[$i]=htmlspecialchars(ociresult($s,"NAME"))."(".htmlspecialchars(ociresult($s,"RCOUNT")).")";
			}
		}
	$str_res[0]=substr($str_res[0],2,strlen($str_res[0])-2);

	
	$cnt=-1;
	$pos=300;
	/*$pdf->AddPage();
	$pdf->Image('mmap.png', 0, 0, 210, 297); */
	$bottom=260;
	for($i=0;$i<count($op);$i++)
		{
		if($pos>$bottom)
			{
			$pos=67;
			$pdf->AddPage();
			$pdf->Image('mmap.png', 0, 0, 210, 297); 
			$pdf->SetFont('Arial_bold', '', 10);
			$pdf->Text(25, 30, $name);
			}
		$pdf->SetFont('Arial_bold', '', 10);
		$pdf->Text(25, round($pos), $op[$i]);
		$pdf->Text(10, round($pos), "О".($i+1));
		if($pos>$bottom)
			{
			$pos=67;
			$pdf->AddPage();
			$pdf->Image('mmap.png', 0, 0, 210, 297); 
			$pdf->SetFont('Arial_bold', '', 10);
			$pdf->Text(25, 30, $name);
			}
		if($str_eq[$i]!="")
			{
			if($pos+7.35>$bottom)
				{
				$pos=59.65;
				$pdf->AddPage();
				$pdf->Image('mmap.png', 0, 0, 210, 297);
				$pdf->SetFont('Arial_bold', '', 10);
				$pdf->Text(25, 30, $name);			
				}
			$pdf->SetFont('Arial', '', 10);
			$pdf->Text(10, round($pos+7.35), "Д");
			while(strlen($str_eq[$i])>80)
				{
				
				$pos=$pos+7.35;
				$nom=strpos($str_eq[$i],',',70);
				$tmp=substr($str_eq[$i],0,$nom+1);
				$pdf->Text(25, round($pos), $tmp);
				$str_q[$i]=substr($str_eq[$i],$nom+2,strlen($str_eq[$i])-$nom-2);
				if($pos>$bottom)
					{
					$pos=67;
					$pdf->AddPage();
					$pdf->Image('mmap.png', 0, 0, 210, 297); 
					$pdf->SetFont('Arial_bold', '', 10);
					$pdf->Text(25, 30, $name);
					}
				}
			$pos=$pos+7.35;
			$pdf->Text(25, round($pos), $str_eq[$i]);
			}
		if($str_res[$i]!="")
			{
			if($pos>$bottom)
				{
				$pos=67;
				$pdf->AddPage();
				$pdf->Image('mmap.png', 0, 0, 210, 297);
				$pdf->SetFont('Arial_bold', '', 10);
				$pdf->Text(25, 30, $name);
				}
			$pdf->SetFont('Arial', '', 10);
			$pdf->Text(10, round($pos+7.35), "Н");
			while(strlen($str_res[$i])>80)
				{
				$pos=$pos+7.35;
				$nom=strpos($str_res[$i],',',70);
				$tmp=substr($str_res[$i],0,$nom+1);
				$pdf->Text(25, round($pos), $tmp);
				$str_res[$i]=substr($str_res[$i],$nom+2,strlen($str_res[$i])-$nom-2);
				if($pos>$bottom)
					{
					$pos=67;
					$pdf->AddPage();
					$pdf->Image('mmap.png', 0, 0, 210, 297); 
					$pdf->SetFont('Arial_bold', '', 10);
					$pdf->Text(25, 30, $name);
					}
				}
			$pos=$pos+7.35;
			$pdf->Text(25, round($pos), $str_res[$i]);
			}		
		$pos=$pos+7.35;
	}
	


/* Отображение сетки начало */

/*$pdf->SetLineWidth(0.2);
$pdf->SetFont('Arial', '', 8);
$pdf->Text(5, 4, '5');
$pdf->Text(10, 4, '10');
for ($i=5;$i<210;$i+=5)
{
  if (($i%25) == 0)
  {
    $pdf->SetLineWidth(0.2);
    $pdf->Text($i, 4, $i);
  }
  $pdf->Line($i, 0, $i, 297);
  $pdf->SetLineWidth(0.1);
}
$pdf->Text(0, 4, '5');
for ($i=5;$i<297;$i+=5)
{
  if (($i%25) == 0)
  {
    $pdf->SetLineWidth(0.2);
    $pdf->Text(0, $i-1, $i);
    }
  $pdf->Line(0, $i, 210, $i);
  $pdf->SetLineWidth(0.1);
}
*/
/* Отображение сетки конец */

$pdf->Output();  
OCICommit($c);
OCILogoff($c);
?>