<?php
require_once("plan_authen.php");  //session_
require_once("time_inc.php");
	if(isset($_REQUEST['workgroup'])){
	$workgroup=$_REQUEST['workgroup'];
	}
	
	if(isset($_POST['year_index'])){
	$year_index=$_POST['year_index'];
	}
	
	$proj_year=$_SESSION["budget_year"];
	
	if(isset($year_index)){
	$proj_year=$year_index;
	}
	
if($proj_year==""){
	echo "<br><div align='center'>ยังไม่ได้กำหนดปีงบประมาณ</div>";
	exit();
}

?>
<FORM  name="frm1" >
<?php
//เลือกปี และกลุ่ม
require_once("dbconfig.inc.php");


/////////////ให้แสดงปีการศึกษาตามปีปฏิทิน
$sql_year_active = "SELECT *  FROM  plan_year  where year_active='1'";
$dbquery_year_active = mysqli_query($connect,$sql_year_active);
$result_year_active  = mysqli_fetch_array($dbquery_year_active);

$cur_date = date("Y-m-d");
$f_date=explode("-", $cur_date);
$month_num=$f_date[1];

if($month_num==1 or $month_num==2 or $month_num==3 or $month_num==4 or $month_num==10 or $month_num==11 or $month_num==12){
$ed_year=$result_year_active['budget_year']-1; 
}
else{
$ed_year=$result_year_active['budget_year'];
}

if(!(isset($year_index))){
$proj_year=$ed_year;
}

//////////////////////////

?>
<p align="center"><font color='#006666' size='3'><strong>โครงการ  ปีการศึกษา<?php echo $proj_year?></strong></font></p>
<?php

	echo "<table width='95%' align='center'><tr><td align='right'>";
//////////////////	
	echo "ปีการศึกษา&nbsp";
	echo "<Select  name='year_index' size='1'>";
	echo  '<option value ="" >เลือก</option>' ;
	$sql_year = "SELECT *  FROM  plan_year order by budget_year";
	$dbquery_year =DBfieldQuery($sql_year);
	While ($result_year = mysqli_fetch_array($dbquery_year)){
			 if($year_index==""){
					if($result_year['budget_year']==$ed_year){
					echo "<option value=$result_year[budget_year]  selected>$result_year[budget_year]</option>"; 
					}
					else{
					echo "<option value=$result_year[budget_year]>$result_year[budget_year]</option>"; 
					}
			}
			else{
					
					if($year_index==$result_year['budget_year']){
					echo "<option value=$result_year[budget_year]  selected>$result_year[budget_year]</option>"; 
					}
					else{
					echo "<option value=$result_year[budget_year]>$result_year[budget_year]</option>"; 
					}
			}		
	}
	echo "</select>&nbsp;";
/////////////////////
	echo "<Select  name='workgroup' size='1'>";
	echo  '<option value ="" >ทุกกลุ่ม(งาน)</option>' ;
						$sql = "SELECT *  FROM   system_workgroup";
						$dbquery =DBfieldQuery($sql);
						While ($result = mysqli_fetch_array($dbquery))
							{ 
								if ($workgroup==$result['workgroup']){
								echo "<option value=$result[workgroup]  selected>$result[workgroup_desc]</option>"; 
								} 
								else{
								echo "<option value=$result[workgroup]>$result[workgroup_desc]</option>"; 
								}
							}
					echo "</select>";
					echo "&nbsp;<INPUT TYPE='button' name='smb' value='เลือก' onclick='goto_url(1)' class=entrybutton>";
echo "</td></tr></table>";
?>     
</FORM>

<?php

if(!(isset($workgroup))){
$workgroup="";
}
ShowProj($workgroup,$proj_year);
#######################################
function ShowProj($workgroup,$proj_year){
//อาเรย์บุคลากร
$sql = "SELECT *  FROM  person_main order by position_code,name";
$dbquery=DBfieldQuery($sql);
while ($result = mysqli_fetch_array($dbquery)){
$person_ar[$result['person_id']]=$result['prename'].$result['name']." ".$result['surname'];
}
/////
$ks_year=$proj_year-543;  //ปี คศ.นี้
$next_ks_year=$proj_year-543+1;  //ปีคศ. ของปีงบประมาณหน้า
$next_budget_year=$proj_year+1;  //ปีงบประมาณหน้า
$start_edyear="$ks_year"."-"."5"."-"."1";   //วันเดือนปี เริ่มปีการศึกษา  1 พ.ค.
$stop_edyear="$next_ks_year"."-"."4"."-"."30";  //วันเดือนปีสิ้นสุดปีการศึกษา 30 เม.ย
/////

 if($workgroup!=""){
$sql = "select * from  plan_proj  where (code_clus='$workgroup') and ((begin_date between '$start_edyear' and '$stop_edyear') or (finish_date between '$start_edyear' and '$stop_edyear')) order by code_proj ";
}
else{
$sql = "select * from  plan_proj  where (begin_date between '$start_edyear' and '$stop_edyear') or (finish_date between '$start_edyear' and '$stop_edyear') order by budget_year,code_proj ";
}
$dbquery =DBfieldQuery($sql);
$rd=1;
$budget_proj_sum=0; //ตัวแปรรวมเงินโครงการ
echo  "<table width='95%' border='1'  borderColor='#FF0033' align='center' style='border-collapse: collapse'>";
echo   "<Tr bgcolor='#FBD562'><Td  width='5%' valign='center'  align='center' ><font  size='2'>ที่</font></td>";
echo   "<Td  width='7%'  valign='center'  align='center' ><font  size='2'>รหัสโครงการ</font></td>";
echo   "<Td valign='center'  align='center' ><font  size='2'>ชื่อโครงการ/ชื่อกิจกรรม</font></td>";
echo   "<Td  width='12%' valign='center'  align='center' ><font  size='2'>งบประมาณ</font></td>";
echo   "<Td  width='18%' valign='center'  align='center' ><font  size='2'>แหล่งงบประมาณ</font></td>";
echo   "<Td  width='15%' valign='center'  align='center' ><font  size='2'>ระยะเวลา</font></td>";
echo   "<Td  width='15%'  valign='center'  align='center' ><font  size='2'>หัวหน้าโครงการ</font></td>";
echo   "<Td width='5%' align='center'>ไฟล์</td>";
echo   "</tr>";
While ($result = mysqli_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'];
$budget_proj_plan2=number_format($budget_proj_plan,2);
$begin_date= $result['begin_date'];
$finish_date= $result['finish_date'];
$budget_year= $result['budget_year'];

	$budget_proj_sum=$budget_proj_sum+$budget_proj_plan;  //รวมเงินโครงการ
$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);
if(isset($budget_proj)){
$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]=$owner_proj_plan;
$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;
		
///		
	
		echo "<Tr bgcolor='#CCFFFF'><Td align=center><font  size='2'>$rd</font></Td><Td align=center><font  size='2'>$code_proj_plan/$budget_year</font></Td><Td><font  size='2'>$name_proj_plan</font>";
echo "</Td><Td align=right><font  size='2'><b>$budget_proj_plan2</b></font></Td><td></td>";
echo "<td align='center'>";
if(make_time($begin_date)<make_time($start_edyear)){
echo "<img src='modules/plan/images/next.gif' border='0'>";
}
echo thai_date_3($begin_date);
echo "-";
echo thai_date_3($finish_date);
if(make_time($finish_date)>make_time($stop_edyear)){
echo "<img src='modules/plan/images/next.gif' border='0'>";
}
echo "</td>";
echo "<Td align=left><font  size='2'>";
echo $person_ar[$owner_proj_plan];
echo "</font></Td>";	
	
		if($file_detail_plan!=""){
		echo "<Td align='center'> <a href='$file_detail_plan' target='_blank' title='อ่านโครงการ'><img src='images/b_browse.png' width='16' height='16' border='0'></a></td>";	
		 }
		 else{
		 echo "<Td>&nbsp;</td>";
		}
		
echo "</tr>";
		//กิจกรรม
					$sql_acti = "select * from  plan_acti  where budget_year='$result[budget_year]' and code_proj='$result[code_proj]' order by code_acti";
					$dbquery_acti =DBfieldQuery($sql_acti);
					$re=1;
					While ($result_acti = mysqli_fetch_array($dbquery_acti))
						{
							$code_proj_acti= $result_acti['code_proj'];
							$code_acti= $result_acti['code_acti'];
							$name_acti= $result_acti['name_acti'];
							$budget_acti= $result_acti['budget_acti'];
							$code_approve= $result_acti['code_approve'];
							$begin_date_acti=$result_acti['begin_date'];
							$finish_date_acti=$result_acti['finish_date'];
							
						echo "<Tr><Td>&nbsp;</td>";
						echo "<Td>&nbsp;</td>";
						echo "<Td><font color='#0000FF'>$code_acti</font>&nbsp;$name_acti</td>";
						$budget_acti=number_format($budget_acti,2);
						echo "<Td align='right'><font color='#0000FF'>$budget_acti</font></td>";
						
						if($code_approve>0 and $code_approve<200){
						$sql_type ="select *	from budget_type where type_id='$code_approve' ";			
						$dbquery_type =DBfieldQuery($sql_type);
						$result_type = mysqli_fetch_array($dbquery_type);
						echo "<Td>$result_type[type_name]</td>";
						}
						else if($code_approve>=400){
						$bud_type_id=$code_approve-400;
						$sql_type ="select item from  budget_bud where status='1' and budget_year='$result[budget_year]' and id='$bud_type_id' ";		
						$dbquery_type =DBfieldQuery($sql_type);
						$result_type = mysqli_fetch_array($dbquery_type);
						echo "<Td>$result_type[item]</td>";
						}
						else{
						echo "<Td><font color='#FF0000'>ยังไม่ได้กำหนด</font></td>";
						}
						
						echo "<Td align='right'><font size='1'>";
						echo thai_date_3($begin_date_acti);
						echo "-";
						echo thai_date_3($finish_date_acti);
						echo "</font></td>";
						echo "<Td>&nbsp;</td>";
						echo "<Td>&nbsp;</td>";
						echo "</tr>";
						$re++;
	    				}
//		
$rd++;
}
//::: End valiable array ::://		

					$sql_acti = "select * from  plan_acti where budget_year>='$proj_year' and budget_year<='$next_budget_year' order by code_acti";
					$dbquery_acti =DBfieldQuery($sql_acti);
					$re=1;
					While ($result_acti = mysqli_fetch_array($dbquery_acti))
						{
							$code_proj_acti= $result_acti['code_proj'];
							$code_acti= $result_acti['code_acti'];
							$name_acti= $result_acti['name_acti'];
							$budget_acti= $result_acti['budget_acti'];
							$code_approve= $result_acti['code_approve'];
						$re++;
	    				}

$budget_proj_sum=number_format($budget_proj_sum,2);
echo "<Tr bgcolor='#FBD562' align='center'>";
echo "<Td colspan='3' align='center'>รวม</Td><td><b>$budget_proj_sum</b></td><Td colspan='4'>&nbsp;</Td></Tr></Table>";

return 0;
} //function
?>

<script>
function goto_url(val){
callfrm("?option=plan&task=planproject/plan_show_edplan"); 		
}

</script>

<table width="95%" align="center">
<tr><td width="50"><b>หมายเหตุ</b></td><td align="left">&nbspปีการศึกษา หมายถึง ช่วงเวลา 1 พ.ค.-30 เม.ย ปีถัดไป</td></tr>
<tr><td></td><td align="left"><img src="modules/plan/images/next.gif" border="0"> หมายถึง โครงการเริ่มจากปีการศึกษาก่อน หรือสิ้นสุดปีการศึกษาถัดไป</td></tr>
</table>
