<?php
session_start();
session_cache_limiter("private");
if(!isset($_SESSION['user']))
	header("Location: login.php");
if($_SESSION['user'] == "expert")
	header("Location: index.php");
$login="<p>Вы зашли, как ".$_SESSION['user'].".<input type='button' onclick='location.href=\"login.php?logoff\";'/ value='Выход'>";
?>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<?php 
$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(!$c)
	{
	echo "Невозможно подключится к базе: " . var_dump( OCIError() );
	die();
	}
if(isset($_GET["id"]))
	{
	$id=htmlspecialchars($_GET["id"]);
	$sql="select products_type.type_name , product.status_str, product.status, ";
	$sql=$sql." product.start_date, product.end_date, product.notes, product.id_product from product";
	$sql=$sql." join products_type on products_type.id_type=product.id_type ";
	$fl=0;
	if(isset($_GET["ready"]))
		{
		$fl=1;
		$sql=$sql." where products_type.id_type=".$id." and status=1000 order by product.status, product.id_product";
		}
	elseif(isset($_GET["brak"]))
		{
		$fl=1;
		$sql=$sql." where products_type.id_type=".$id." and status=-1 order by product.status, product.id_product";
		}
	//else
		//{
		//$sql=$sql." where products_type.id_type=".$id." and (status=0 or status=1000) order by product.status, product.id_product";
		//}
	if($fl==1)
	{
	$s = OCIParse($c, $sql);
	OCIExecute($s, OCI_DEFAULT);
	$i=0;
	while(OCIFetch($s))
		{
		$type_name[$i]=htmlspecialchars(ociresult($s,"TYPE_NAME"));
		$status_str[$i]=htmlspecialchars(ociresult($s,"STATUS_STR"));
		$status[$i]=htmlspecialchars(ociresult($s,"STATUS"));
		$start[$i]=htmlspecialchars(ociresult($s,"START_DATE"));
		$end[$i]=htmlspecialchars(ociresult($s,"END_DATE"));
		$notes[$i]=htmlspecialchars(ociresult($s,"NOTES"));
		$id_product[$i]=htmlspecialchars(ociresult($s,"ID_PRODUCT"));
		if($type_name[$i]=="")
			$type_name[$i]="—";
		
		if($status[$i]=="")
			$status[$i]="—";
			
		if($status_str[$i]=="")
			$status_str[$i]="—";
			
		if($start[$i]=="")
			$start[$i]="0";
			
		if($end[$i]=="")
			$end[$i]="—";
			
		if($notes[$i]=="")
			$notes[$i]="—";
			
		$i++;
		}
	}else{
	$i=0;
	$sql="select * from (select products_type.type_name, operations.id_operation, product.status_str, product.status, ";
	$sql=$sql." product.start_date, product.end_date, product.notes, product.id_product from product";
	$sql=$sql." join products_type on products_type.id_type=product.id_type ";
	$sql=$sql." join operations on operations.id_product=product.id_product ";
	$sql=$sql." join TP on TP.id_type=product.id_type and TP.nom=product.status and TP.id_listop=operations.id_listop";
	$sql=$sql." where products_type.id_type=".$id." and (status>0) and (status<>1000) order by product.status, product.id_product)";
	$sql=$sql." union all select * from (select products_type.type_name, operations.id_operation, product.status_str, product.status, ";
	$sql=$sql." product.start_date, product.end_date, product.notes, product.id_product from product";
	$sql=$sql." join products_type on products_type.id_type=product.id_type ";
	$sql=$sql." left join operations on operations.id_product=product.id_product ";
	$sql=$sql." left join TP on TP.id_type=product.id_type and TP.nom=product.status and TP.id_listop=operations.id_listop";
	$sql=$sql." where products_type.id_type=".$id." and (status=0)  order by product.status, product.id_product)";
	
	$s = OCIParse($c, $sql);
	OCIExecute($s, OCI_DEFAULT);
	while(OCIFetch($s))
		{
		$type_name[$i]=htmlspecialchars(ociresult($s,"TYPE_NAME"));
		$status_str[$i]=htmlspecialchars(ociresult($s,"STATUS_STR"));
		$status[$i]=htmlspecialchars(ociresult($s,"STATUS"));
		$start[$i]=htmlspecialchars(ociresult($s,"START_DATE"));
		$end[$i]=htmlspecialchars(ociresult($s,"END_DATE"));
		$notes[$i]=htmlspecialchars(ociresult($s,"NOTES"));
		$id_product[$i]=htmlspecialchars(ociresult($s,"ID_PRODUCT"));
		$id_oper[$i]=htmlspecialchars(ociresult($s,"ID_OPERATION"));
		if($type_name[$i]=="")
			$type_name[$i]="—";
		
		if($status[$i]=="")
			$status[$i]="—";
			
		if($status_str[$i]=="")
			$status_str[$i]="—";
			
		if($start[$i]=="")
			$start[$i]="0";
			
		if($end[$i]=="")
			$end[$i]="—";
			
		if($notes[$i]=="")
			$notes[$i]="—";
			
		if($id_product[$i]=="")
			$id_product[$i]="—";
			
		if($id_oper[$i]=="")
			$id_oper[$i]="—";
		
		$i++;
		}
	}
	$s = OCIParse($c, "select type_name from products_type where id_type=".$id);
	OCIExecute($s, OCI_DEFAULT);OCIFetch($s);
	$tp_name=htmlspecialchars(ociresult($s,"TYPE_NAME"));
	echo "<title>Производство ".$tp_name."</title>";
	}
elseif(isset($_GET["deleted"]))
	{
	$id=htmlspecialchars($_GET["deleted"]);
	$s = OCIParse($c, "select products_type.type_name, product.status_str, product.start_date, product.end_date, product.notes, product.id_product from product join products_type on products_type.id_type=product.id_type where products_type.id_type=".$id." and status=-2");
	OCIExecute($s, OCI_DEFAULT);
	$i=0;
	while(OCIFetch($s))
		{
		$type_name[$i]=htmlspecialchars(ociresult($s,"TYPE_NAME"));
		$status[$i]=0;
		$status_str[$i]=htmlspecialchars(ociresult($s,"STATUS_STR"));
		$start[$i]=htmlspecialchars(ociresult($s,"START_DATE"));
		$end[$i]=htmlspecialchars(ociresult($s,"END_DATE"));
		$notes[$i]=htmlspecialchars(ociresult($s,"NOTES"));
		$id_product[$i]=htmlspecialchars(ociresult($s,"ID_PRODUCT"));
		if($type_name[$i]=="")
			$type_name[$i]="—";
		
		if($status_str[$i]=="")
			$status_str[$i]="—";
			
		
		if($end[$i]=="")
			$end[$i]="—";
			
		if($notes[$i]=="")
			$notes[$i]="—";
			
		if($id_product[$i]=="")
			$id_product[$i]="—";
		
		$i++;
		}
	$s = OCIParse($c, "select type_name from products_type where id_type=".$id);
	OCIExecute($s, OCI_DEFAULT);OCIFetch($s);
	$tp_name=htmlspecialchars(ociresult($s,"TYPE_NAME"));
	echo "<title>Удаленные с производства ".$tp_name."</title>";
	}
elseif(isset($_GET["start"]))
	{
	$start=htmlspecialchars($_GET["start"]);
	$s = OCIParse($c, "select type_name from products_type where id_type=".$start);
	OCIExecute($s, OCI_DEFAULT);
	OCIFetch($s);
	$type_name=htmlspecialchars(ociresult($s,"TYPE_NAME"));
	echo "<title>Создание устройств ".$type_name."</title>";
	}
elseif(isset($_GET["goman"]))
	{
	$str=htmlspecialchars($_GET["goman"]);
	$arr=explode(";",$str);
	for($i=0;$i<count($arr)-1;$i++)
		{
		$s = OCIParse($c, " begin go_man(".$arr[$i]."); end; ");
		OCIExecute($s, OCI_DEFAULT);
		}
	$s = OCIParse($c, "select products_type.type_name,products_type.id_type from products_type join product on product.id_type=products_type.id_type where id_product=".$arr[0]);
	OCIExecute($s, OCI_DEFAULT);
	OCIFetch($s);
	$typ_name=htmlspecialchars(ociresult($s,"TYPE_NAME"));
	$id_typ=htmlspecialchars(ociresult($s,"ID_TYPE"));
	echo "<title>Передача на производство ".$typ_name."</title>";
	echo "<meta http-equiv='refresh' content='0; url=current.php?id=".$id_typ."'>";
	}
elseif(isset($_GET["id_pr"]))
	{
	$id_type=htmlspecialchars($_GET["id_pr"]);
	$nom=htmlspecialchars($_GET["nom"]);
	
		$sql="select op.id_operation, list_of_operation.name as op, staff.fio ,op.op_status, op.op_start_date, op.op_end_date, op.op_status_str , op.notes";
		$sql=$sql." from operations op join list_of_operation on list_of_operation.id_listop=op.id_listop";
		$sql=$sql." left join staff on staff.id_staff=op.id_staff";
		$sql=$sql." join product on product.id_product=op.id_product";
		$sql=$sql." where  op.id_listop in (select id_listop from tp where nom=".$nom." and id_type=".$id_type.") and op.op_status>=0  and product.id_type=".$id_type." order by op.id_operation";
		$s = OCIParse($c, $sql);

		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while(OCIFetch($s))
			{
			$id_operation[$i]=htmlspecialchars(ociresult($s,"ID_OPERATION"));
			$op[$i]=htmlspecialchars(ociresult($s,"OP"));
			$fio[$i]=htmlspecialchars(ociresult($s,"FIO"));
			$op_status[$i]=htmlspecialchars(ociresult($s,"OP_STATUS"));
			$op_start_date[$i]=htmlspecialchars(ociresult($s,"OP_START_DATE"));
			$op_end_date[$i]=htmlspecialchars(ociresult($s,"OP_END_DATE"));
			$op_status_str[$i]=htmlspecialchars(ociresult($s,"OP_STATUS_STR"));
			$notes[$i]=htmlspecialchars(ociresult($s,"NOTES"));
			if($op[$i]=="")
				$op[$i]="—";
			if($fio[$i]=="")
				$fio[$i]="—";
			if($op_status[$i]=="")
				$op_status[$i]="—";
			if($op_start_date[$i]=="")
				$op_start_date[$i]="—";
			if($op_end_date[$i]=="")
				$op_end_date[$i]="—";
			if($op_status_str[$i]=="")
				$op_status_str[$i]="—";
			if($notes[$i]=="")
				$notes[$i]="—";
			$i++;
			}
		
	
	$s = OCIParse($c, "select type_name from products_type where id_type=".$id_type);
	OCIExecute($s, OCI_DEFAULT);	
	OCIFetch($s);
	$type_name=htmlspecialchars(ociresult($s,"TYPE_NAME"));
	if(isset($op))
		{
		echo "<title>Операция ".$op[0]." для ".$type_name."</title>";
		}
	elseif($nom==1000)
		{
		echo "<title>Готовые ".$type_name."</title>";
		}
	}
elseif(isset($_POST["ID_TYPE"]))
	{
	$id_type=htmlspecialchars($_POST["ID_TYPE"]);
	$cnt=htmlspecialchars($_POST["CNT"]);
	$notes=htmlspecialchars($_POST["NOTES"]);
	$s = OCIParse($c, "declare i number:=0; begin loop i:=i+1; start_product(".$id_type.",'".$notes."'); exit when(i>=".$cnt."); end loop; end;");
	OCIExecute($s, OCI_DEFAULT);
	echo "<meta http-equiv='refresh' content='0; url=current.php?id=".$id_type."'>"; 
	echo "<title>Запущено производство</title>";
	}
elseif(isset($_POST["IDS"]))
	{
	$ids=$_POST["IDS"];
	for($i=0;$i<count($ids);$i++)
		{
		$s = OCIParse($c, "begin delete_product(".$ids[$i]."); end;");
		OCIExecute($s, OCI_DEFAULT);
		}
	$s = OCIParse($c, "select id_type from product where id_product=".$ids[0]);
	OCIExecute($s, OCI_DEFAULT);
	OCIFetch($s);
	$id_pr=htmlspecialchars(ociresult($s,"ID_TYPE"));
	echo "<meta http-equiv='refresh' content='0; url=current.php?id=".$id_pr."'>"; 
	echo "<title>Устройства удалены</title>";
	}
elseif(isset($_GET["ops"]))
	{
	$ops=$_GET["ops"];
	$arr="(";
	$cnt_gr=0;
	if(count($ops)>1)
		{
		for($i=0;$i<count($ops)-1;$i++)
			$arr=$arr.$ops[$i].",";
		$arr=$arr.$ops[$i].")";
		$s = OCIParse($c, "select op_status from operations where id_operation in ".$arr." group by op_status, notes, id_staff");
		OCIExecute($s, OCI_DEFAULT);
		while(OCIFetch($s))
			{
			$cnt_gr++;
			}
		}
	
		$s = OCIParse($c, "select type_name,id_type from products_type where id_type in (select id_type from product where id_product in (select id_product from operations where id_operation=".$ops[0]."))");
		OCIExecute($s, OCI_DEFAULT);
		OCIFetch($s);
		$type=htmlspecialchars(ociresult($s,"TYPE_NAME"));
		$idtype=htmlspecialchars(ociresult($s,"ID_TYPE"));
		
		$s = OCIParse($c, "select name,id_listop from list_of_operation where id_listop in(select id_listop from operations where id_operation=".$ops[0].")");
		OCIExecute($s, OCI_DEFAULT);
		OCIFetch($s);
		$op_name=htmlspecialchars(ociresult($s,"NAME"));
		$list_op=htmlspecialchars(ociresult($s,"ID_LISTOP"));
		
		$s = OCIParse($c, "select nom from tp where id_listop=".$list_op." and id_type=".$idtype);
		OCIExecute($s, OCI_DEFAULT);
		OCIFetch($s);
		$nom=htmlspecialchars(ociresult($s,"NOM"));
		
		$s = OCIParse($c, "select name from equipment where id_equipment in (select id_equipment from match_equip_op where id_listop in(select id_listop from operations where id_operation=".$ops[0]."))");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while(OCIFetch($s))
			{
			$eq[$i]=htmlspecialchars(ociresult($s,"NAME"));
			$i++;
			}
		$sql="select name from resources join res_usage on res_usage.id_res=resources.id_res";
		$sql=$sql." join TP_res_us on TP_res_us.id_resus=res_usage.id_resus";
		$sql=$sql." join TP on  TP.id_tp=TP_res_us.id_tp";
		$sql=$sql." where TP.id_listop=(select id_listop from operations where id_operation=".$ops[0].")";
		$sql=$sql." and TP.id_type=".$idtype;
		$s = OCIParse($c, $sql);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while(OCIFetch($s))
			{
			$res[$i]=htmlspecialchars(ociresult($s,"NAME"));
			$i++;
			}
		if(($cnt_gr==1)or(count($ops)==1))
		{	
		$s = OCIParse($c, "select op.op_status_str,op.op_start_date, op.notes,staff.fio from operations op left join staff on staff.id_staff=op.id_staff where op.id_operation=".$ops[0]);
		OCIExecute($s, OCI_DEFAULT);
		OCIFetch($s);
		$status=htmlspecialchars(ociresult($s,"OP_STATUS_STR"));
		$start_date=htmlspecialchars(ociresult($s,"OP_START_DATE"));
		$fio=htmlspecialchars(ociresult($s,"FIO"));
		$notes=htmlspecialchars(ociresult($s,"NOTES"));
		}
		else
		{
		$status="—";
		$start_date="—";
		$notes=" ";
		}
		$s = OCIParse($c, "select fio, id_staff from staff where id_dep=(select id_dep from list_of_operation  where list_of_operation.id_listop=".$list_op.")");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while(OCIFetch($s))
			{
			$fio_staff[$i]=htmlspecialchars(ociresult($s,"FIO"));
			$staff_id[$i]=htmlspecialchars(ociresult($s,"ID_STAFF"));
			$i++;
			}
		echo "<title>Внесение данных на операции ".$op_name."</title>";
	}
elseif(isset($_POST["op"]))
	{
	$str=$_POST["op"];
	$op=explode(";",$str);
	$staff=htmlspecialchars($_POST["staff"]);
	$notes=htmlspecialchars($_POST["notes"]);
	$nomer=htmlspecialchars($_POST["nom"]);
	$id_prod=htmlspecialchars($_POST["id_pr"]);
	for($k=0;$k<count($op)-1;$k++)
		{
		$s = OCIParse($c, "update operations set id_staff=".$staff.", notes='".$notes."', op_status=1, op_status_str='В процессе' where id_operation=".$op[$k]);
		OCIExecute($s, OCI_DEFAULT);
		}
	for($k=0;$k<count($op)-1;$k++)
		{
		$s = OCIParse($c, "select CHECK_OP(".$op[$k].") as st from dual");
		OCIExecute($s, OCI_DEFAULT);
		OCIFetch($s);
		if(ociresult($s,"ST")==1)
			{
			$s = OCIParse($c, "update operations set op_status=2, op_status_str='Готов к переходу' where id_operation=".$op[$k]);
			OCIExecute($s, OCI_DEFAULT);
			}
		}
	echo "<meta http-equiv='refresh' content='0; url=current.php?id_pr=".$id_prod."&nom=".$nomer."'>"; 
	echo "<title>Сохранение данных об операции</title>";
	}
elseif(isset($_GET["next"]))
	{
	$str=$_GET["next"];
	$op=explode(";",$str);
	for($k=0;$k<count($op)-1;$k++)
		{
		$s = OCIParse($c, "begin next_op(".$op[$k]."); end;");
		OCIExecute($s, OCI_DEFAULT);
		}
	$s = OCIParse($c, "select status,id_type from product where id_product=(select id_product from operations where id_operation=".$op[0].")");
	OCIExecute($s, OCI_DEFAULT);
	OCIFetch($s);
	$nom=ociresult($s,"STATUS");
	$id_type=ociresult($s,"ID_TYPE");
	if($nom!=1000)
		{
		echo "<meta http-equiv='refresh' content='0; url=current.php?id_pr=".$id_type."&nom=".$nom."'>"; 
		}
	else
		{
		echo "<meta http-equiv='refresh' content='0; url=current.php?id=".$id_type."'>"; 
		}
	}
elseif(isset($_GET["edit"]))
	{
	$id_edit=htmlspecialchars($_GET["edit"]);
	$s = OCIParse($c, "select pr.status_str,pr.start_date,pr.end_date,pr.notes,pt.type_name,pr.id_type from product pr join products_type pt on pt.id_type=pr.id_type where pr.id_product=".$id_edit);
	OCIExecute($s, OCI_DEFAULT);
	OCIFetch($s);
	$info_edit["status"]=htmlspecialchars(ociresult($s,"STATUS_STR"));
	$info_edit["start_date"]=htmlspecialchars(ociresult($s,"START_DATE"));
	$info_edit["end_date"]=htmlspecialchars(ociresult($s,"END_DATE"));
	$info_edit["notes"]=htmlspecialchars(ociresult($s,"NOTES"));
	$info_edit["type_name"]=htmlspecialchars(ociresult($s,"TYPE_NAME"));
	$info_edit["id_type"]=htmlspecialchars(ociresult($s,"ID_TYPE"));
	}
elseif(isset($_GET["brak"]))
	{
	$str=$_GET["brak"];
	$op=explode(";",$str);
	for($k=0;$k<count($op)-1;$k++)
		{
		$s = OCIParse($c, "begin do_brak(".$op[$k]."); end;");
		OCIExecute($s, OCI_DEFAULT);
		}
	$s = OCIParse($c, "select id_type from product where id_product=(select id_product from operations where id_operation=".$op[0].")");
	OCIExecute($s, OCI_DEFAULT);
	OCIFetch($s);
	$type=ociresult($s,"ID_TYPE");
	echo "<meta http-equiv='refresh' content='0; url=current.php?id=".$type."'>"; 
	}
elseif((count($_POST)==0)and(count($_GET)==0)){
	$s = OCIParse($c, "select * from stat");
	OCIExecute($s, OCI_DEFAULT);
	$n=0;
	while (OCIFetch($s))
		{
		$name[$n]=htmlspecialchars(ociresult($s,"TYPE_NAME"));
		$all[$n]=htmlspecialchars(ociresult($s,"ALL_P"));
		$made[$n]=htmlspecialchars(ociresult($s,"MADE"));
		$brak[$n]=htmlspecialchars(ociresult($s,"BRAK"));
		$pr[$n]=htmlspecialchars(ociresult($s,"PR_BRAK"));
		$del[$n]=htmlspecialchars(ociresult($s,"DELETED"));
		$id_type[$n]=htmlspecialchars(ociresult($s,"ID_TYPE"));
		$n++;
		}
}

OCICommit($c);
OCILogoff($c);
?>
 <script language="JavaScript">
	function check()
		{
		fft=document.forms[0].elements[1].value;
		document.forms[0].elements[1].value=fft.replace(/[^0-9]/gi,"");
		return true;
		}
	function get_ids(count)
		{
		str="";
		for(var k=0;k<count;k++)
				{
				if(document.forms[0].elements[k].checked){
					 str=str+document.forms[0].elements[k].value+";";
					}		
				}
		return str;	
		}
	function num(count){
			for(var k=0;k<count;k++)
				{
				if(document.forms[0].elements[k].checked){
					return document.forms[0].elements[k].value;
					}
				}
			}
 </script>
 <link rel="stylesheet" href="/style/style.css" type="text/css" />
 </head>
 <body>
	<table border=1>
	<tr>
	<td width=300 valign=top>
   <iframe src="menu.php" height=550 id="myIframe" scrolling=auto frameborder=0 ></iframe>
	</td>
	<td width=900 valign=top><table width="700"><td align='right'>Вы зашли как <?php echo $_SESSION['user'];?>, <input type='button' onclick='location.href="login.php?logoff";'/ value='Выход'></td></table><br/>
<?php
	if(isset($id))
		{
		echo "<form action='current.php' method='post'>\n<table border=1><tr><th></th><th>Номер</th><th>Устройство</th><th>Статус</th><th>Дата запуска</th><th>Дата окончания</th><th>Примечание</th></tr>";
		$rn=0;
		for($k=0;$k<$i;$k++)
			{
			
			if($rn>3)
				$rn=0;
			if($rn==0)
				$color="fbec5d";
			if($rn==1)
				$color="fcdd76";
			if($rn==2)
				$color="ffdb58";
			if($rn==3)
				$color="f0dc82";
			$rn++;
			echo "\n<tr><td><input type='checkbox' name='IDS[]' value=".$id_product[$k]."></td><td bgcolor=#".$color.">";
			if(($status[$k]==0)or($status[$k]==1000)or($status[$k]==-1))
				{
				echo $id_product[$k];
				}
			else
				{
				echo "<a href='current.php?ops[]=".$id_oper[$k]."&nom=".$status[$k]."'>".$id_product[$k]."</a>";
				}
			echo "</td><td bgcolor=#".$color.">".$type_name[$k]."</td><td bgcolor=#".$color.">";
			if(($status[$k]==0)or($status[$k]==1000)or($status[$k]==-1))
				{
				echo $status_str[$k];
				}
			else
				{
				echo "<a href='current.php?id_pr=".$id."&nom=".$status[$k]."'>".$status_str[$k]."</a>";
				}
			echo "</td><td bgcolor=#".$color.">".$start[$k]."</td><td bgcolor=#".$color.">".$end[$k]."</td><td bgcolor=#".$color.">".$notes[$k]."</td></tr>";
			}
		echo "\n</table>";
		if(isset($fl)and(!isset($_GET["brak"]))and(!isset($_GET["ready"])))
			{
			if($fl==0)
				{
				if($_SESSION['user']=='technolog'){
				echo "\n<input type='button' onclick='location.href=\"current.php?start=".$id."\"' value='Создать'>";
				echo "\n<input type='button' onclick='location.href=\"current.php?goman=\"+get_ids(".$i.")' value='На производство'>";
				echo "<input type='button' value='Редактировать' onclick='location.href=\"current.php?edit=\"+num(".$i.");'>";
				echo "<input type='submit' value='Удалить'>";
				}
				echo "<input type='button' value='К удаленным' onclick='location.href=\"current.php?deleted=".$id."\";'></form>";
				}
			}
		elseif(isset($_GET["brak"]))
			{
			if($_SESSION['user']=='technolog')
				echo "<input type='button' value='Редактировать' onclick='location.href=\"current.php?edit=\"+num(".$i.");'>";
			echo "<input type='button' value='Перейти к производству' onclick='location.href=\"current.php?id=".$id."\";'>";
			}
		elseif(isset($_GET["ready"]))
			{
			if($_SESSION['user']=='technolog')
				echo "<input type='button' value='Редактировать' onclick='location.href=\"current.php?edit=\"+num(".$i.");'>";
			echo "<input type='button' value='Перейти к производству' onclick='location.href=\"current.php?id=".$id."\";'>";
			}
		else
			{
			echo "<input type='button' value='Перейти к производству' onclick='location.href=\"current.php?id=".$id."\";'>";
			}
		}
	elseif(isset($start))
		{
		echo "\n<form method='post' action='current.php' onsubmit='return check();'><input type='hidden' name='ID_TYPE' value=".$start."><br/>";
		echo "\n<table border=1><tr><td>Тип устройства</td><td>".$type_name."</td></tr>";
		echo "\n<tr><td>Количество</td><td><input type='text' name='CNT'><br/></td></tr>";
		echo "\n<tr><td>Примечание</td><td><textarea name='NOTES' cols='30' rows='7'></textarea></td></tr></table>";
		if($_SESSION['user']=='technolog')
			echo "\n<input type='submit' value='Создать'><input type='button' value='Назад' onclick='location.href=\"current.php?id=".$start."\";'<br/></form>";
		}
	elseif(isset($cnt))
		{
		echo "<p>Устройства успешно созданы.</p>";
		}
	elseif(isset($ids))
		{
		echo "<p>Устройства успешно удалены.</p>";
		}
	elseif(isset($typ_name))
		{
		echo "<p>Устройства успешно переданы на производство.</p>";
		}
	elseif(isset($id_operation))
		{
		echo "<h2>Операция ".$op[0]."</h2>";
		echo "<h3>".$type_name."</h3>";
		echo "<form action='current.php' method='get'>";
		echo "<table border=1><th></th><th>Номер</th><th>Статус</th><th>Сотрудник</th><th>Дата начала</th><th>Заметка</th>";
		$rn=0;
		for($k=0;$k<$i;$k++)
			{
			if($rn>3)
				$rn=0;
			if($rn==0)
				$color="#fbec5d";
			if($rn==1)
				$color="#fcdd76";
			if($rn==2)
				$color="#ffdb58";
			if($rn==3)
				$color="#f0dc82";
			$rn++;
				
			echo "\n<tr>";
			echo "<td><input type='checkbox' name='ops[]' value=".$id_operation[$k]."></td>";
			echo "\n<td bgcolor=".$color."><a href='current.php?ops[]=".$id_operation[$k]."'>".$id_operation[$k]."</a></td>";
			echo "\n<td bgcolor=".$color.">".$op_status_str[$k]."</td>";
			echo "\n<td bgcolor=".$color.">".$fio[$k]."</td>";
			echo "\n<td bgcolor=".$color.">".$op_start_date[$k]."</td>";
			echo "\n<td bgcolor=".$color.">".$notes[$k]."</td>";
			echo "\n</tr>";
			}
		echo "</table>";
		if($_SESSION['user']=='technolog'){
		echo "<input type='submit' value='Внести данные'>";
		echo "<input type='button' value='На след. операцию' onclick='location.href=\"current.php?next=\"+get_ids(".$i.")' >";
		}
		echo "<input type='button' value='Назад' onclick='location.href=\"current.php?id=".$id_type."\";'  >";
		echo "</form>";
		}
	elseif(isset($cnt_gr))
		{
		echo "\n<h2>".$op_name."</h2>";
		echo "\n<h3>".$type."</h3>";
		echo "\n<form action='current.php' method='post'>";
		$str="";
		for($k=0;$k<count($ops);$k++)
			$str=$str.$ops[$k].';';
		echo "\n<input type='hidden' name='op' value=".$str.">";
		echo "\n<input type='hidden' name='nom' value=".$nom.">";
		echo "\n<input type='hidden' name='id_pr' value=".$idtype.">";
		echo "\n<table border=1>";
		echo "\n<tr><td>Оборудование</td><td><table border=1>";
		if(isset($eq))
		{
		for ($i=0;$i<count($eq);$i++)
			echo "\n<tr><td>".$eq[$i]."</td></tr>";
		}	
		echo "\n</table></td></tr>";
		echo "\n<tr><td>Ресурсы</td><td><table border=1>";
		if(isset($res))
		{
		for ($i=0;$i<count($res);$i++)
			echo "\n<tr><td>".$res[$i]."</td></tr>";
		}
		echo "\n</table></td></tr>";
		echo "\n<tr><td>Сотрудник</td><td><select name='staff'>";
		if($fio=="")
			{
			echo "\n<option selected value='NULL'></option>";
			}
		else
			{
			echo "\n<option value='NULL'></option>";
			}
		for($i=0;$i<count($fio_staff);$i++)
			{
			if($fio==$fio_staff[$i])
				{
				echo "\n<option selected value=".$staff_id[$i].">".$fio_staff[$i]."</option>";
				}
			else
				{
				echo "\n<option value=".$staff_id[$i].">".$fio_staff[$i]."</option>";
				}
			}
		echo "\n</select></td></tr>";
		echo "\n<tr><td>Статус</td><td>".$status."</td></tr>";
		echo "\n<tr><td>Дата начала</td><td>".$start_date."</td></tr>";
		echo "\n<tr><td>Примечание</td><td><textarea name='notes' cols='30' rows='7'>".$notes."</textarea></td></tr>";
		echo "\n</table>";
		if($_SESSION['user']=='technolog'){
		echo "\n<input type='submit' value='Сохранить'>";
		echo "\n<input type='button' value='! БРАК !' onclick='location.href=\"current.php?brak=".$str."\";'>";
		}
		echo "\n<input type='button' value='Назад' onclick='location.href=\"current.php?id_pr=".$idtype."&nom=".$nom."\";'>";
		echo "\n</form>";
		}
	elseif(isset($op))
		{
		echo "<p>Данные об операции успешно сохранены.</p>";
		}
	elseif(isset($info_edit))
		{
		echo "<form action='current.php' methpd='post'><table border=1>";
		echo "<tr><td>Тип устройства</td><td>".$info_edit["type_name"]."</td></tr>";
		echo "<tr><td>Номер</td><td>".$id_edit."</td></tr>";
		echo "<tr><td>Статус</td><td>".$info_edit["status"]."</td></tr>";
		echo "<tr><td>Дата старта</td><td>".$info_edit["start_date"]."</td></tr>";
		if($info_edit["end_date"]!="") 
			echo "<tr><td>Дата завершения</td><td>".$info_edit["end_date"]."</td></tr>";
		echo "<tr><td>Примечание</td><td><textarea name='NOTES' cols='30' rows='7'>".$info_edit["notes"]."</textarea></td></tr>";
		echo "</table>";
		if($_SESSION['user']=='admin')
			echo "<input type='submit' value='Сохранить'>";
		echo "<input type='button' value='Назад' onclick='location.href=\"current.php?id=".$info_edit["id_type"]."\";'>";
		}
	elseif(isset($made)){
	echo "<table border=1>\n<tr><th>Устройство</th>\n<th>Удаленных</th>\n<th>Готовых</th>\n<th>Брака</th>\n<th>Всего</th>\n<th>Брак в процентах</th>\n</tr>";
	$rn=0;
	for($j=0;$j<$n;$j++)
		{
		if($rn>3)
			$rn=0;
		if($rn==0)
				$color="#fbec5d";
			if($rn==1)
				$color="#fcdd76";
			if($rn==2)
				$color="#ffdb58";
			if($rn==3)
				$color="#f0dc82";
		$rn++;
		echo "<tr bgcolor=".$color.">\n<td><a href='current.php?id=".$id_type[$j]."'>".$name[$j]."</a></td>\n<td>".$del[$j]."</td>\n<td>".$made[$j]."</td>\n<td>".$brak[$j]."</td>\n<td>".$all[$j]."</td>\n<td>".$pr[$j]."%</td>\n</tr>";
		}
	echo "\n</table>";
	}

	?>
	</td>
	</tr>
	</table>
</body>
</html>