<?php
	session_start();
	include("plan_authen.php");  //session_
	$workgroup=$_REQUEST['workgroup'];
	$workgroup_desc=$_REQUEST['workgroup_desc'];
	$proj_year=$_SESSION["budget_year"];
?>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<body>
<p align="right" STYLE="font-family: 'sans-serif', fantasy; font-size: 8pt; color:blue;font-weight: normal;">:::  ปีงบประมาณ : <?=$proj_year?> :::</Font></p>
<p align="center" STYLE="font-family: 'sans-serif', fantasy; font-size: 18pt; color:blue;font-weight: bold;"><BR>:::  รายละเอียดแยกตามงาน/โครงการ :::</Font></p>
<!-- Part2	## -->
<FORM  name="frm1" >
 <p valign="midle" align="left" STYLE="font-family: 'sans-serif', fantasy; font-size: 12pt; color:maroon;">&nbsp;&nbsp;
เลือกงาน <sup>### </sup><Select  name="workgroup" id="clus" size="1"  STYLE="font-family: 'sans-serif', fantasy; font-size: 12pt; border:0px" onChange="openDir( this.form )"=>
					<?php
						require_once("dbconfig.inc.php");
						echo  '<option  style="background-color:navy; color:white;"  value ="" >  # เลือกงานในสถานศึกษา </option>' ;
						$sql = "SELECT *  FROM   system_workgroup";
						$dbquery =DBfieldQuery($sql);
						While ($result = mysql_fetch_array($dbquery))
							{ 
							if ($workgroup==$result[workgroup]){
						echo '<option style="background-color:wheat; color:maroon;" value="' . $result[workgroup] .'|'. $result[workgroup_desc]. '" selected>'. $result[workgroup_desc] . '</option>'; 
					}else{echo '<option style="background-color:wheat; color:maroon;" value="' . $result[workgroup] .'|'. $result[workgroup_desc]. ' ">'. $result[workgroup_desc] . '</option>'; 
					}

					}
					echo "</select>";
	ShowProj($workgroup,$workgroup_desc,$proj_year);
?>     
</p>
</td>
</TR></FORM>
<?
#######################################
function ShowProj($workgroup,$workgroup_desc){
		global $workgroup,$workgroup_desc,$proj_year;
 if($workgroup!="")
$sql = "select  *  from  plan_proj  where (code_clus='$workgroup')  and  (budget_year='$proj_year') order by code_proj ";
else
$sql = "select  *  from  plan_proj  where (budget_year='$proj_year') order by code_clus ";
					$dbquery =DBfieldQuery($sql);
					$rd=1;
					While ($result = mysql_fetch_array($dbquery))
						{
							$code_clus_plan= $result[code_clus];
							$code_proj_plan= $result[code_proj];
							$name_proj_plan= $result[name_proj];
							$budget_proj_plan= $result[budget_proj];
							$owner_proj_plan =$result[owner_proj];
							$code_tegy_plan =$result[code_tegy];
							$file_detail_plan =$result[file_detail]; 
							$eval_activity_plan =$result[eval_activity];
							$allow_edit_plan =$result[allow_edit];
							$owner_proj_plan=trim($owner_proj_plan);
							$point=strpos($owner_proj_plan,chr('38'));
							$len=strlen($owner_proj_plan);
							if  (($point>0) and ($point<5))
								{	$owner_proj_plan=substr($owner_proj_plan,$point,$len);
									$point=strpos($owner_proj_plan,chr('38'));
									$len=strlen($owner_proj_plan);
								}
							$long=$len-$point;
							$fname=substr($owner_proj_plan,0,$point);
							$sname=substr($owner_proj_plan,-$long,$long);
							$cname=trim($sname);
							$Fcredit1=number_format($budget_proj);
//::: valiable array ::://
							$code_clus_plan_ar[$rd]=$code_clus_plan;
							$code_proj_plan_ar[$rd]=$code_proj_plan;
							$name_proj_plan_ar[$rd]=$name_proj_plan;
							$budget_proj_plan_ar[$rd]=$budget_proj_plan;
							$file_detail_plan_ar[$rd]=$file_detail_plan; 
							$eval_activity_plan_ar[$rd]=$eval_activity_plan;
							$allow_edit_plan_ar[$rd]=$allow_edit_plan;
							$owner_proj_plan_ar[$rd]=$fname.'&nbsp;'.$cname;
							$fname_plan_ar[$rd]=$fname;
							$cname_plan_ar[$rd]=$cname;
							if  (empty($code_tegy_plan))
								{   $code_tegy_plan="?";   }
							$code_tegy_plan_ar[$rd] =$code_tegy_plan;
							$code_tegy_planc_ar[$rd] =$code_tegy_plan.'-'.$code_proj_plan;
						$rd++;
	    				}
//::: End valiable array ::://						
					$sql = "select  *  from  plan_acti";
					$dbquery =DBfieldQuery($sql);
					$re=1;
					While ($result = mysql_fetch_array($dbquery))
						{
							$code_proj_acti= $result[code_proj];
							$code_acti= $result[code_acti];
							$name_acti= $result[name_acti];
							$budget_acti= $result[budget_acti];
							$budget_approve= $result[$budget_approve];
							
							$code_proj_acti_ar[$re]=$code_proj_acti;
							$code_acti_ar[$re]=$code_acti;
							$name_acti_ar[$re]=$name_acti;
							$budget_acti_ar[$re]=$budget_acti;
							$budget_approve_ar[$re]=$budget_approve;
						$re++;
	    				}

/*
						for($i=1;$i<$rd;$i++)
						{
						$proj_sum=0;
						for($x=1;$x<$re;$x++)
								{
										if($code_proj_plan_ar[$i]==$code_proj_acti_ar[$x])
										{
											$acti_sum=0;
											$sql = "select   id,  pj_activity , money  from  withdraw where  pj_activity=$code_acti_ar[$x]";
											$dbquery =DBfieldQuery($sql);
											While ($result = mysql_fetch_array($dbquery))
											{
											$id= $result[id];
											$money= $result[money];
											$acti_sum=$acti_sum+$money;
											}
											$acti_sum_ar[$x]=$acti_sum;
											$proj_sum=$proj_sum+$acti_sum;
										}  //  if($code_proj_plan_ar[$i]==$code_proj_acti_ar[$x])
								}  //  for($x=1;$x<$re;$x++) 
						$proj_sum_ar[$i]=$proj_sum;
						}  //  for($i=1;$i<$rd;$i++) */
$space="  ";
echo  "<table width=95% border=1  borderColor=#FF0033  align=center >";
echo   "<Tr><Td  width='3%'  bgcolor='#FBD562'  valign='center'  align='center' ><font  size='2'>รหัสกลุ่ม</font></td>";
echo   "<Td  width='3%'  bgcolor='#FBD562'  valign='center'  align='center' ><font  size='2'>รหัสโครงการ</font></td>";
echo   "<Td  width='64%'  bgcolor='#FBD562'  valign='center'  align='center' ><font  size='2'>ชื่อโครงการ</font></td>";
echo   "<Td  width='10%'  bgcolor='#FBD562'  valign='center'  align='center' ><font  size='2'>งบประมาณ</font></td>";
echo   "<Td  width='20%'  bgcolor='#FBD562'  valign='center'  align='center' ><font  size='2'>หัวหน้าโครงการ</font></td>";
echo   "<Td><IMG SRC='images/b_bookmark.png' WIDTH='16' HEIGHT='16' BORDER=0 ALT=''></td>";
echo   "<Td><IMG SRC='images/wzedit.gif' WIDTH='16' HEIGHT='17' BORDER=0 ALT=''></td></tr>";
$sum_momey_proj=0;
$total_withdraw=0;
for($i=1;$i<$rd;$i++)
{		
		$sum_momey_proj=$sum_momey_proj+$budget_proj_plan_ar[$i];
		$total_withdraw=$total_withdraw+$proj_sum_ar[$i];
		$budget_project=number_format($budget_proj_plan_ar[$i],2);
		if($code_clus_plan_ar[$i]=='01')
		$project_clus="  (กลุ่มอำนวยการ)";
		if($code_clus_plan_ar[$i]=='02')
		$project_clus="  (กลุ่มนโยบายและแผน)";
		if($code_clus_plan_ar[$i]=='03')
		$project_clus="  (กลุ่มบริหารงานบุคคล)";
		if($code_clus_plan_ar[$i]=='04')
		$project_clus="  (กลุ่มส่งเสริมการจัดการศึกษา)";
		if($code_clus_plan_ar[$i]=='05')
		$project_clus="  (กลุ่มนิเทศฯ)";
		if($code_clus_plan_ar[$i]=='06')
		$project_clus="  (หน่วยตรวจสอบภายใน)";
		if($code_clus_plan_ar[$i]=='07')
		$project_clus="  (กลุ่มเอกชน)";
		
		if($budget_proj_plan_ar[$i]>0)
		{
		$proj_percent=($proj_sum_ar[$i]/$budget_proj_plan_ar[$i])*100;
		$proj_percent=number_format($proj_percent,2);
		}
		else
		$proj_percent="0.00";

		$budget_proj=number_format($budget_proj_plan_ar[$i]);
		$proj_sum=number_format($proj_sum_ar[$i],2);
		$net_proj=$budget_proj_plan_ar[$i]-$proj_sum_ar[$i];
		$net_proj2=number_format($net_proj,2);
		$total_net=$total_net+$net_proj; 
		echo "<Tr bgcolor=#FFFFCC><Td align=center><font  size='2'>$code_clus_plan_ar[$i]</font></Td><Td align=center><font  size='2'> $code_tegy_planc_ar[$i]</font></Td><Td><font  size='2'>$name_proj_plan_ar[$i]</font></Td><Td align=right><font  size='2'>$budget_proj</font></Td><Td align=left><font  size='2'>$owner_proj_plan_ar[$i]</font></Td>";	
if  (empty($file_detail_plan_ar[$i]))
{
	if  ( $code_tegy_plan_ar[$i]=="?")
	{echo   "<Td align='center'><a OnMouseOut='na_restore_img_src('image4', 'document')' OnMouseOver='na_change_img_src('image4', 'document', 'images/s_okay.png', true)' target='_blank' title='ไม่ระบุกลยุทธ์'><IMG SRC='images/dot_green.png' WIDTH='10' HEIGHT='10' BORDER=0 name='image4'></a></td>";}else
	{echo   "<Td align='center'><a OnMouseOut='na_restore_img_src('image4', 'document')' OnMouseOver='na_change_img_src('image4', 'document', 'images/s_okay.png', true)' target='_blank' title='ไม่มีโครงการ'><IMG SRC='images/dot_red.png' WIDTH='10' HEIGHT='10' BORDER=0 name='image4'></a></td>";}
}
else
{
	if  ($file_detail_plan_ar[$i] !="")
	{
	echo   "<Td> <a href='$file_detail_plan_ar[$i]' OnMouseOut='na_restore_img_src('image4', 'document')' OnMouseOver='na_change_img_src('image4', 'document', 'images/s_okay.png', true)' target='_blank' title='อ่านโครงการ'><img src='images/b_browse.png' width='16' height='16' border='0' name='image4'> </a></td>"; }
}
//ส่วนรายงานผลการดำเนินงาน
	if  (($mpms_moderate == "1") and ($file_detail_plan_ar[$i] !=""))
				{
				if  ( (empty($eval_activity_plan_ar[$i])) and ($mpms_view == "1"))
					{echo   "<Td><A Href=\"budget_edit_eval.php?vcode_proj=$code_proj_plan_ar[$i]&vcode_clus=$code_clus_plan_ar[$i]&vcode_tegy=$code_tegy_plan_ar[$i]&vname_proj=$name_proj_plan_ar[$i]&vbudget_proj=$budget_proj_plan_ar[$i]&fname=$fname_plan_ar[$i]&cname=$cname_plan_ar[$i]\"  target= _top><IMG SRC='images/b_browse.png' WIDTH='16' HEIGHT='16' BORDER=0 ALT=''  title='รอการรายงาน'></A></td></Tr>"; }
		else {
			if (empty($allow_edit_plan_ar[$i]) or $allow_edit_plan_ar[$i]=="x")
			{echo   "<Td><A Href=\"budget_edit_eval.php?vcode_proj=$code_proj_plan_ar[$i]&vcode_clus=$code_clus_plan_ar[$i]&vcode_tegy=$code_tegy_plan_ar[$i]&vname_proj=$name_proj_plan_ar[$i]&vbudget_proj=$budget_proj_plan_ar[$i]&fname=$fname_plan_ar[$i]&cname=$cname_plan_ar[$i]\"  target=  _top><IMG SRC='images/b_edit.png' WIDTH='16' HEIGHT='16' BORDER=0 ALT=''  title='ปรับปรุง แก้ไข'></A></td></Tr>"; }
			else
			{echo   "<Td><A Href=\"budget_end_eval.php?vcode_proj=$code_proj_plan_ar[$i]&vcode_clus=$code_clus_plan_ar[$i]&vcode_tegy=$code_tegy_plan_ar[$i]&vname_proj=$name_proj_plan_ar[$i]&vbudget_proj=$budget_proj_plan_ar[$i]&fname=$fname_plan_ar[$i]&cname=$cname_plan_ar[$i]\"  target=  _top><IMG SRC='images/s_okay.png' WIDTH='16' HEIGHT='16' BORDER=0 ALT=''  title='จบการรายงาน'></A></td></Tr>";}
			}
	}
else
{
	if  ( (empty($file_detail_plan_ar[$i])) and ($mpms_view == "1"))
	{
		if  (  $code_tegy_plan_ar[$i]=="?")
		{echo   "<Td align='center'><IMG SRC='images/frm_linevrlt.png' WIDTH='10' HEIGHT='10' BORDER=0 ALT=''  title='ติดต่อเจ้าหน้าที่'></td></Tr>";}
		else
		{echo   "<Td align='center'><IMG SRC='images/dot_red.png' WIDTH='10' HEIGHT='10' BORDER=0 ALT=''  title='ไม่ส่งโครงการ'></td></Tr>";}
	}}
	}
		$acti_num=0;
		for($x=1;$x<$re;$x++)
		{
				if($code_proj_plan_ar[$i]==$code_proj_acti_ar[$x])
					{
					$acti_num=$acti_num+1;
					$budget_acti=number_format($budget_acti_ar[$x],2);
					$acti_sum=number_format($acti_sum_ar[$x],2);
					$net_acti=$budget_acti_ar[$x]-$acti_sum_ar[$x];
					$net_acti2=number_format($net_acti,2);
				}
		}
		
		if($sum_momey_proj>0)
		{
		$spt_percent=($total_withdraw/$sum_momey_proj)*100;
		$spt_percent=number_format($spt_percent,2);
		}
		else
		$spt_percent="0.00";
$sum_momey_proj=number_format($sum_momey_proj,2);
$total_withdraw=number_format($total_withdraw,2);
$total_net=number_format($total_net,2);
echo  "<table width=95% border=1  borderColor=#FF0033  align=center >";
echo "<Tr bgcolor=#FFCCCC align=center>";
echo "<Td width='30%'>รวม  : $sum_momey_proj&nbsp;</Td><Td  width='30%'>&nbsp;เบิกจ่าย :  $total_withdraw&nbsp;</Td><Td width='30%'>&nbsp;คงเหลือ : $total_net&nbsp;</Td><Td>$spt_percent&nbsp;%</Td></Tr></Table>";
echo "</Table>";

			 return 0;
		}
?>
	</font>
</body>


<script language="JavaScript">
function openDir( frm1 ) { 
	var newIndex = frm1.workgroup.selectedIndex; 
			if(newIndex==0)
			 { // Don't display anything if first option is selected form.size.value = ""; 
			frm1.workgroup.value = ""; 
			frm1.workgroup_desc.value = "";
			 alert( "Please select workgroup!" ); 
			 return;
			 }
		var v1 = frm1.workgroup.options[frm1.workgroup.selectedIndex].value.split("|")[0];
		var v2 = frm1.workgroup.options[frm1.workgroup.selectedIndex].value.split("|")[1];
		if ( v1.length>0)
						{
			callfrm("?option=plan&task=planproject/plan_show_plan&workgroup='+v1+'&workgroup_desc='+v2"); 
						}
	}
</script>