<?php
/** ensure this file is being included by a parent file */
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );

// ตรวจสอบปีงบประมาณ
if($_SESSION["budget_year"]==""){
echo "<br>";
echo "<div align='center'>";
echo "ตรวจสอบการกำหนดปีงบประมาณให้ถูกต้องก่อนค่ะ";
echo "</div>";
exit();
}

if(!(isset($code_proj))){
$code_proj="";
}

require_once("plan_calendar.php");
require_once("dbconfig.inc.php");


$space="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
$proj_year=$_SESSION["budget_year"];

if($index==1){
?>
<script type="text/javascript" src="./css/js/calendarDateInput.js"></script> 
<Center>
<b>
<Font Size=4 color='#000099'><BR>:::  บันทึกข้อมูล งาน/โครงการ ปีงบประมาณ <?php echo $_SESSION['budget_year']?> :::</Font></b>
<Br><BR><BR>
<form id='frm1' name='frm1'>
<TABLE width="80%"  border="0" align="center" cellpadding="2" cellspacing="2" height="50">
<TR>
<TD  width="50%"  valign="top"> 
<TABLE width="100%" border="0" borderColor=#FF0033 cellpadding="0" cellspacing="0">
	<tr> 
            <td align="right"  width="40%"><b><font  class="textfrm">กลุ่ม(งาน) :</font></b></td>
                    <td align='left' >
                 <?php   
					$sql = "SELECT *  FROM   system_workgroup order by workgroup";
					$dbquery =DBfieldQuery($sql);
					$num_rows = mysqli_num_rows($dbquery);
					echo "<Select name='vcode_clus' size='1'>";
					echo "<Option value=''>--- เลือกกลุ่ม(งาน) ---</option>";
					$i=0;
					while ($i < $num_rows)
					{
					$result = mysqli_fetch_array($dbquery);
					$code_clus = $result['workgroup'];
					$name_clus = $result['workgroup_desc'];
					$txtshows = $code_clus." ".$name_clus;
					echo "<Option value='$code_clus'>$code_clus $name_clus$space</option>";
					$i++;
					}
					echo "</Select>";
					?>
					</td></tr>
			<tr> 
            <td align="right" ><b><font class="textfrm">กลยุทธ์ :</font></b></td>
                    <td align='left' >
					<?php
						$sql = "SELECT *  FROM  plan_stregic where budget_year='$_SESSION[budget_year]' order by id_tegic";
						$dbquery=DBfieldQuery($sql);
					echo "<Select name='vcode_tegy' size='1'>";
					echo "<Option value=''>--- เลือกกลยุทธ์  ---&nbsp;&nbsp;&nbsp;&nbsp; </option>";
					while ($result = mysqli_fetch_array($dbquery))
					{
					$id_tegic = $result['id_tegic'];
					$strategic = $result['strategic'];
					$strategic=substr($strategic,0,100);
					echo "<Option value='$id_tegic'>$id_tegic $strategic$space</option>";
					}
					echo "</Select>";
					?>
                    </td></tr>
				 <tr>
                    <td align="right"><b><font  class="textfrm">รหัสโครงการ :</font></b></td>
                    <td align='left' ><input  size="4" type=text name="vcode_proj" maxlength="3" value=<?php echo $code_proj?>> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font  class="textfrm">ปีงบประมาณ :</font></b></td>
                    <td align='left' > <input  type="text"  readonly="readonly" name="vbudget_year" size=4 maxlength=4  value=<?php echo $proj_year?>> 
					</td></tr>	
					<tr> 
                    <td align="right"><b><font  class="textfrm">ชื่อโครงการ :</font></b></td>
                    <td align='left' > <textarea name='vname_proj' rows = '3' cols='50'></textarea>
                    </td></tr>
				<tr> 
                    <td align="right"><b><font  class="textfrm">วันเริ่มต้นโครงการ :</font></b></td>
                    <td align='left' > <script>
								var Y_date=<?php echo $Ynum?>  
								var m_date=<?php echo $mnum?>  
								var d_date=<?php echo $dnum?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('mybeginday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font  class="textfrm">วันสิ้นสุดโครงการ :</font></b></td>
                    <td align='left' > <script>
								var Y_date=<?php echo $Ybudget?>  
								var m_date=<?php echo $mbudget?>  
								var d_date=<?php echo $dbudget?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('myfinishday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font  class="textfrm">จำนวนเงินที่จัดสรร :</font></b></td>
                    <td align='left' > <input  size="9" type="text"  name="vbudget_proj" maxlength=9> 
					</td></tr>	
				<tr> 
                    <td align="right"><b><font  class="textfrm">หัวหน้าโครงการ :</font></b></td>
                    <td align='left' >
					<?php
					$sql = "SELECT *  FROM  person_main where status='0' order by position_code,name";
					$dbquery=DBfieldQuery($sql);
					echo "<Select name='vowner_proj' size='1'>";
					echo "<Option value=''>--- เลือกบุคลากร  ---&nbsp;&nbsp;&nbsp;&nbsp; </option>";
					while ($result = mysqli_fetch_array($dbquery))
					{
					echo "<Option value=$result[person_id]>$result[name]&nbsp;$result[surname]</option>";
					$person_ar[$result['person_id']]=$result['prename'].$result['name']." ".$result['surname'];
					}
					echo "</Select>";
					?>

                    </td></tr>
               </table>
		</TD>
</table>

 <?php
echo "<p align='center'>";

		if($_SESSION["mpms_add"]=='1')
		{
		echo "<INPUT TYPE='button' name='smb' value='บันทึก' onclick='goto_url_update(1)'>&nbsp;<INPUT TYPE='button' name='smb' value='ยกเลิก' onclick='goto_url_update(0)'>";
		}
		else{
		echo "<div align='center'><font size='3' color='#FF0033'>คุณไม่ได้รับสิทธิ์บันทึกข้อมูล</font></div>";
		}
} //end if  index==1

//ส่วนยืนยันการลบข้อมูล
if($index==2) {
echo "<br>";
echo "<table width='500' border='0' align='center'>";
echo "<tr><td align='center'><font color='#990000' size='4'>โปรดยืนยันความต้องการลบข้อมูลอีกครั้ง</font><br></td></tr>";
echo "<tr><td align=center>";
echo "<INPUT TYPE='button' name='smb' value='ยืนยัน' onclick='location.href=\"?option=plan&task=planproject/plan_in_proj_2&index=3&vcode_proj=$_GET[vcode_proj]&page=$_GET[page]\"'>
		&nbsp;&nbsp;<INPUT TYPE='button' name='back' value='ยกเลิก' onclick='location.href=\"?option=plan&task=planproject/plan_in_proj_2&page=$_GET[page]\"'";
echo "</td></tr></table>";
}

//ส่วนลบข้อมูล
if($index==3){
$sql_acti = "SELECT  *  FROM  plan_acti_2 where  budget_year='$proj_year'  and  code_proj='$vcode_proj' ";
$dbquery_acti =DBfieldQuery($sql_acti);
while ($result_acti = mysqli_fetch_array($dbquery_acti)){
		$sql =  "delete  FROM  plan_acti_3  where budget_year='$proj_year'  and  code_acti= '$result_acti[code_acti]' ";
		$dbquery = DBfieldQuery($sql);
		$sql =  "delete  FROM  plan_acti_2  where budget_year='$proj_year'  and  code_acti=  '$result_acti[code_acti]' ";
		$dbquery = DBfieldQuery($sql);
}
$sql =  "delete FROM  plan_proj_2  where  budget_year='$proj_year' and  code_proj='$vcode_proj' " ;
$dbquery = DBfieldQuery($sql);
}

if($index==4){
$dayseri =date("Y-m-d H:i:s");
$vcode_proj=$vcode_proj+1000;
$sql = "SELECT *  FROM  plan_proj_2  where budget_year='$proj_year' and  code_proj = '$vcode_proj' ";
$result0 =DBfieldQuery($sql);
$fetyear = mysqli_fetch_array($result0);
$num=mysqli_num_rows($result0);
		if ($num==0){
		$sql = "insert into plan_proj_2 (code_clus,code_tegy,code_proj,budget_year,budget_proj,name_proj,owner_proj,begin_date,finish_date,dayrec,budget_approve,file_detail,eval_tegy,eval_activity,eval_result,eval_obstacle,eval_particular,allow_edit) values ('$vcode_clus','$vcode_tegy','$vcode_proj','$vbudget_year','$vbudget_proj_2','$vname_proj','$vowner_proj','$mybeginday','$myfinishday','$dayseri','0','','','','','','','')";
		$result0  =  DBfieldQuery($sql);
		}else{
?>
		<script>
			alert(" <?php echo $vcode_proj?>  คุณใส่รหัสโครงการซ้ำ\nกรุณาบันทึกใหม่");
		</script>
		<?php
		}
}	// end if index==4	

if($index==5){
$sql = "SELECT *  FROM  plan_proj_2  where budget_year='$proj_year' and id='$_GET[plan_proj_id]' ";
$dbquery =DBfieldQuery($sql);
$result = mysqli_fetch_array($dbquery);
	$id =$result['id'];
	$budget_year =$result['budget_year'];
	$w_code_clus =$result['code_clus'];
	$w_code_tegy =$result['code_tegy'];
	$w_code_proj =$result['code_proj'];
	$w_name_proj = $result['name_proj'];
	$w_budget_proj =$result['budget_proj'];
	$w_owner_proj =$result['owner_proj'];
	$begin_date =$result['begin_date'];	
list($begin_year,$begin_month,$begin_day) = explode("-",$begin_date);	
	$finish_date =$result['finish_date'];	
list($finish_year,$finish_month,$finish_day) = explode("-",$finish_date);	
	
?>
<script type="text/javascript" src="./css/js/calendarDateInput.js"></script> 
<p align="center"><font color="#FFCC00"></font><Center><b>
<Font face="Tahoma" Size=4 color='#000099'><BR>::: แก้ไขโครงการเพิ่มเติมจากเงินเหลือจ่าย ปีงบประมาณ <?php echo $_SESSION['budget_year']?> :::</Font></b>
<Br><BR><BR>
<form id='frm1' name='frm1'>
<TABLE width="100%"  border="0" align="center" cellpadding="2" cellspacing="2" height="50">
<TR>
<TD  width="50%"  valign="top"> 
<TABLE width="100%" border="0" borderColor=#FF0033 cellpadding="0" cellspacing="0">
	<tr> 
            <td align="right"  width="40%"><b><font size="3" face="MS Sans Serif" color="#009900">กลุ่ม(งาน) :</font></b></td>
                    <td align='left' >
                 <?php   
					$sql = "SELECT *  FROM   system_workgroup";
					$dbquery =DBfieldQuery($sql);
					$num_rows = mysqli_num_rows($dbquery);
					echo "<Select name='vcode_clus' size='1'>";
					echo "<Option value=''>--- เลือกกลุ่ม(งาน) ---</option>";
					$i=0;
					while ($i < $num_rows)
					{
					$result = mysqli_fetch_array($dbquery);
					$code_clus = $result['workgroup'];
					$name_clus = $result['workgroup_desc'];
					$txtshows = $code_clus." ".$name_clus;
					if ($code_clus==$w_code_clus){
					echo "<Option value='$code_clus' selected>$code_clus $name_clus$space</option>";
					}else{echo "<Option value='$code_clus'>$code_clus $name_clus$space</option>";
					}
					$i++;
					}
					echo "</Select>";
					?>
					</td></tr>
			<tr> 
            <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">กลยุทธ์ :</font></b></td>
                    <td align='left' >
					<?php
					$sql = "SELECT *  FROM  plan_stregic where budget_year='$_SESSION[budget_year]' order by id_tegic";
					$dbquery=DBfieldQuery($sql);
					echo "<Select name='vcode_tegy' size='1'> ";
					echo "<Option value=''>--- เลือกกลยุทธ์  ---&nbsp;&nbsp;&nbsp;&nbsp; </option>";
					while ($result = mysqli_fetch_array($dbquery))
					{
					$id_tegic = $result['id_tegic'];
					$strategic = $result['strategic'];
					$strategic=substr($strategic,0,100);
					if($id_tegic==$w_code_tegy){
					echo "<Option value='$id_tegic' selected>$id_tegic $strategic$space</option>";
					}
					else{
					echo "<Option value='$id_tegic'>$id_tegic $strategic$space</option>";
					}
					}
					echo "</Select>";
					?>
                    </td></tr>
				 <tr>
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">รหัสโครงการ :</font></b></td>
                    <td align='left' > <input  size="4" type=text readonly  name="vcode_proj" maxlength=3 value=<?php echo $w_code_proj?>> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">ปีงบประมาณ :</font></b></td>
                    <td align='left'> <input  type=text readonly name="vbudget_year" size="4" maxlength="4"  value=<?php echo $proj_year?> 
					</td></tr>	
					<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">ชื่อโครงการ :</font></b></td>
                    <td align='left' > <textarea name='vname_proj' rows = '3' cols='50'><?php echo $w_name_proj?></textarea>
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">วันเริ่มต้นโครงการ :</font></b></td>
                    <td align='left' > <script>
								var Y_date=<?php echo $begin_year?>  
								var m_date=<?php echo $begin_month?>  
								var d_date=<?php echo $begin_day?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('mybeginday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">วันสิ้นสุดโครงการ :</font></b></td>
                    <td align='left' > <script>
								var Y_date=<?php echo $finish_year?>  
								var m_date=<?php echo $finish_month?>  
								var d_date=<?php echo $finish_day?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('myfinishday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">จำนวนเงินที่จัดสรร :</font></b></td>
                    <td align='left' > <input  size="9" type=text  name="vbudget_proj"  value=<?php echo $w_budget_proj?> maxlength="9"> 
					</td></tr>	
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">หัวหน้าโครงการ :</font></b></td><td align="left">
					<?php
					$sql = "SELECT *  FROM  person_main order by position_code,name";
					$dbquery=DBfieldQuery($sql);
					echo "<Select name='vowner_proj' size='1'>";
					echo "<Option value=''>--- เลือกบุคลากร  ---&nbsp;&nbsp;&nbsp;&nbsp; </option>";
					while ($result = mysqli_fetch_array($dbquery))
					{
						if($w_owner_proj==$result['person_id']){
						echo "<Option value=$result[person_id] selected>$result[name]&nbsp;$result[surname]</option>";
						}
						else{
						echo "<Option value=$result[person_id]>$result[name]&nbsp;$result[surname]</option>";
						}
					}
					echo "</Select>";
					?>					
                    </td></tr>
               </table>
		</TD>
</table>

 <?php
echo "<INPUT TYPE='hidden' name='plan_proj_id' value='$id'>";
echo "<input type='hidden' name='page' value='$_GET[page]'>";
echo "<p align='center'>";
echo "<INPUT TYPE='button' name='smb' value='ตกลง' onclick='goto_url_update_proj(1)'>&nbsp;<INPUT TYPE='button' name='smb' value='ย้อนกลับ' onclick='goto_url_update_proj(0)'>";
echo "</p>";

} //end if index==5

if($index==6){
//ตรวจการซ้ำของรหัสโครงการ
$sql = "SELECT *  FROM  plan_proj_2  where budget_year='$_SESSION[budget_year]' and  code_proj = '$vcode_proj' and id!='$_POST[plan_proj_id]' ";
$result0 =DBfieldQuery($sql);
$fetyear = mysqli_fetch_array($result0);
$num=mysqli_num_rows($result0);

		if ($num==0){
		$sql =  "update plan_proj_2  set  code_clus='$vcode_clus',code_tegy='$vcode_tegy', code_proj='$vcode_proj', budget_proj= '$vbudget_proj_2',name_proj = '$vname_proj' ,owner_proj ='$vowner_proj' ,begin_date='$mybeginday',finish_date='$myfinishday' where id='$_POST[plan_proj_id]' " ;
		$dbquery  =  DBfieldQuery($sql);
		}
		else {
		echo "<br>";
		echo "<div align='center'>";
		echo "รหัสโครงการซ่ำ โปรดตรวจสอบค่ะ";
		echo "</div>";
		exit();
		}

} //end if index==6

if($index==7){
$sql = "SELECT *  FROM  plan_proj_2  where id='$_GET[plan_proj_id]' ";
$dbquery =DBfieldQuery($sql);
$result = mysqli_fetch_array($dbquery);
	$id =$result['id'];
	$budget_year =$result['budget_year'];
	$w_code_clus =$result['code_clus'];
	$w_code_tegy =$result['code_tegy'];
	$w_code_proj =$result['code_proj'];
	$w_name_proj = $result['name_proj'];
	$w_budget_proj =$result['budget_proj'];
	$w_owner_proj =$result['owner_proj'];
	$begin_date =$result['begin_date'];	
list($begin_year,$begin_month,$begin_day) = explode("-",$begin_date);	
	$finish_date =$result['finish_date'];	
list($finish_year,$finish_month,$finish_day) = explode("-",$finish_date);	
	
?>
<script type="text/javascript" src="./css/js/calendarDateInput.js"></script> 
<p align="center"><font color="#FFCC00"></font><Center><b>
<Font face="Tahoma" Size=4 color='#000099'><BR>::: รายละเอียดโครงการเพิ่มเติมจากเงินเหลือจ่าย ปีงบประมาณ <?php echo $_SESSION['budget_year']?> :::</Font></b>
<Br><BR><BR>
<form id='edit_form' name='frm1'>
<TABLE width="100%"  border="0" align="center" cellpadding="2" cellspacing="2" height="50">
<TR>
<TD  width="50%"  valign="top"> 
<TABLE width="100%" border="0" borderColor=#FF0033 cellpadding="0" cellspacing="0">
	<tr> 
            <td align="right"  width="50%"><b><font size="3" face="MS Sans Serif" color="#009900">กลุ่ม(งาน) :</font></b></td>
                    <td align='left' >
                 <?php   
					$sql = "SELECT *  FROM   system_workgroup";
					$dbquery =DBfieldQuery($sql);
					$num_rows = mysqli_num_rows($dbquery);
					echo "<Select name='vcode_clus' size='1'>";
					echo "<Option value=''>--- เลือกกลุ่ม(งาน) ---</option>";
					$i=0;
					while ($i < $num_rows)
					{
					$result = mysqli_fetch_array($dbquery);
					$code_clus = $result['workgroup'];
					$name_clus = $result['workgroup_desc'];
					$txtshows = $code_clus." ".$name_clus;
					if ($code_clus==$w_code_clus){
					echo "<Option value='$code_clus' selected>$code_clus $name_clus$space</option>";
					}else{echo "<Option value='$code_clus'>$code_clus $name_clus$space</option>";
					}
					$i++;
					}
					echo "</Select>";
					?>
					</td></tr>
			<tr> 
            <td align="right"  width="50%"><b><font size="3" face="MS Sans Serif" color="#009900">กลยุทธ์ :</font></b></td>
                    <td align='left' >
					<?php
					$sql = "SELECT *  FROM  plan_stregic where budget_year='$_SESSION[budget_year]' order by id_tegic";
					$dbquery=DBfieldQuery($sql);
					echo "<Select name='vcode_tegy' size='1'> ";
					echo "<Option value=''>--- เลือกกลยุทธ์  ---&nbsp;&nbsp;&nbsp;&nbsp; </option>";
					while ($result = mysqli_fetch_array($dbquery))
					{
					$id_tegic = $result['id_tegic'];
					$strategic = $result['strategic'];
					$strategic=substr($strategic,0,80);
					if($id_tegic==$w_code_tegy){
					echo "<Option value='$id_tegic' selected>$id_tegic $strategic$space</option>";
					}
					else{
					echo "<Option value='$id_tegic'>$id_tegic $strategic$space</option>";
					}
					}
					echo "</Select>";
					?>
                    </td></tr>
				 <tr>
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">รหัสโครงการ :</font></b></td>
                    <td align='left' > <input  size="4" type readonly name="vcode_proj" maxlength=3 value=<?php echo $w_code_proj?>> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">ปีงบประมาณ :</font></b></td>
                    <td align='left'> <input  type=text readonly name="vbudget_year" size="4" maxlength="4"  value=<?php echo $proj_year ?> 
					</td></tr>	
					<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">ชื่อโครงการ :</font></b></td>
                    <td align='left' > <textarea name='vname_proj' rows = '3' cols='50' readonly="readonly"><?php echo $w_name_proj?></textarea>
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">วันเริ่มต้นโครงการ :</font></b></td>
                    <td align='left' > <script>
								var Y_date=<?php echo $begin_year?>  
								var m_date=<?php echo $begin_month?>  
								var d_date=<?php echo $begin_day?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('mybeginday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">วันสิ้นสุดโครงการ :</font></b></td>
                    <td align='left' > <script>
								var Y_date=<?php echo $finish_year?>  
								var m_date=<?php echo $finish_month?>  
								var d_date=<?php echo $finish_day?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('myfinishday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">จำนวนเงินที่จัดสรร :</font></b></td>
				<?php $w_budget_proj=number_format($w_budget_proj,2)	?>
                    <td align='left' > <input  size="9" type=text  name="vbudget_proj"  value=<?php echo $w_budget_proj?> maxlength="9" readonly="readonly"> 
					</td></tr>	
				<tr> 
                    <td align="right"><b><font size="3" face="MS Sans Serif" color="#009900">หัวหน้าโครงการ :</font></b></td><td align="left">
					<?php
					$sql = "SELECT *  FROM  person_main order by position_code,name";
					$dbquery=DBfieldQuery($sql);
					echo "<Select name='vowner_proj' size='1'>";
					while ($result = mysqli_fetch_array($dbquery))
					{
						if($w_owner_proj==$result['person_id']){
						echo "<Option value=$result[person_id] selected>$result[name]&nbsp;$result[surname]</option>";
						}
					}
					echo "</Select>";
					?>					
                    </td></tr>
               </table>
		</TD>
</table>

 <?php
echo "<form id='frm1' name='frm1'>";
echo "<input type='hidden' name='page' value='$_GET[page]'>";
echo "<p align='center'>";
echo "<INPUT TYPE='button' name='smb' value='ย้อนกลับ' onclick='goto_url(0)'>";
echo "</p>";
echo "</form>";
} // end if index==7

if($index==11){
$sql = "SELECT *  FROM  plan_proj_2  where id='$_GET[plan_proj_id]'";
$dbquery=DBfieldQuery($sql);
$result_proj = mysqli_fetch_array($dbquery);
?>
<script type="text/javascript" src="./css/js/calendarDateInput.js"></script> 
<form id='frm1' name='frm1'>
<br />
<p align="center"><Font Size='3' color='#000099'><B>กำหนดกิจกรรมของโครงการ ปีงบประมาณ <?php echo $_SESSION['budget_year']?></B></Font></p>
<p align="center"><Font Size='3' color='#000099'><B>โครงการ <?php echo $result_proj['name_proj']?></B></Font></p>

<TABLE width="80%"  border="0" align="center" cellpadding="2" cellspacing="2" height="50">
<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วัน- เดือน-ปี : </font></b></td>
                    <td align="left" width="60%"> <script>
								var Y_date=<?php echo $Ynum?>  
								var m_date=<?php echo $mnum?>  
								var d_date=<?php echo $dnum?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('rec_date', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
					 <tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">รหัสกิจกรรม :</font></b></td>
                    <td align="left"> <input size=4 type=text name="vcode_acti" maxlength=3 value=''> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">กิจกรรม :</font></b></td>
                    <td align="left"> <textarea name='vname_acti' rows = '3' cols='40' ></textarea>
                    </td></tr>
<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วันเริ่มต้นกิจกรรม :</font></b></td>
                    <td align="left"> <script>
								var Y_date=<?php echo $Ynum?>  
								var m_date=<?php echo $mnum?>  
								var d_date=<?php echo $dnum?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('mybeginday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วันสิ้นสุดกิจกรรม :</font></b></td>
                    <td align="left"> <script>
								var Y_date=<?php echo $Ybudget?>  
								var m_date=<?php echo $mbudget?>  
								var d_date=<?php echo $dbudget?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('myfinishday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
                 <tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">แหล่งเงิน :</font></b></td>
                    <td align="left">
<?php
echo "<br />";
		
		echo "1 <Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
		echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9'>";
		echo "</td></tr>";
		//รายการที่ 2
		echo "<tr><td></td><td>2 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
		echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9'>";
		echo "</td></tr>";
		//รายการที่ 3
		echo "<tr><td></td><td>3 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
		echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9'>";
		echo "</td></tr>";
		//รายการที่ 4
		echo "<tr><td></td><td>4 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
		echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9'>";
		echo "</td></tr>";
		//รายการที่ 5
		echo "<tr><td></td><td>5 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
		echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9'>";
		echo "</td></tr>";
		echo "<INPUT TYPE='hidden' name='vcode_proj' value='$result_proj[code_proj]'>";
		echo "<INPUT TYPE='hidden' name='page' value='$_GET[page]'>";
		echo "<tr><td colspan='2' align='center'><INPUT TYPE='button' name='smb' value='บันทึก' onclick='goto_insert_acti(1)'>&nbsp;<INPUT TYPE='button' name='smb' value='ยกเลิก' onclick='goto_insert_acti(0)'></td></tr>";
		
?>	
				
<table>
</form>
<?php
} //end if index==11

if($index==13){
$sql =  "delete  FROM  plan_acti_3  where budget_year='$proj_year'  and  code_acti= '$_GET[code_acti]' ";
$dbquery = DBfieldQuery($sql);
$sql =  "delete  FROM  plan_acti_2  where budget_year='$proj_year'  and  code_acti= '$_GET[code_acti]' ";
$dbquery = DBfieldQuery($sql);
} //end if index==13

if($index==14){
$rec_date =date("Y-m-d H:i:s");
$vcode_acti=$_POST['vcode_proj'].$_POST['vcode_acti'];
$sql = "SELECT *  FROM  plan_acti_2  where budget_year='$proj_year' and  code_acti = '$vcode_acti' ";
$result0 =DBfieldQuery($sql);
$fetyear = mysqli_fetch_array($result0);
$num=mysqli_num_rows($result0);
		if ($num==0){
		$sql = "insert into plan_acti_2 (budget_year,code_proj,code_acti,name_acti,rec_date,begin_date,finish_date,officer) values ('$proj_year','$_POST[vcode_proj]','$vcode_acti','$_POST[vname_acti]','$rec_date','$_POST[mybeginday]','$_POST[myfinishday]','$_SESSION[login_user_id]')";
		$result0  =  DBfieldQuery($sql);
		}else{
?>
		<script>
			alert(" คุณใส่รหัสกิจกรรมซ้ำ\nกรุณาบันทึกใหม่");
		</script>
		<?php
		echo "<script>document.location.href='?option=plan&task=planproject/plan_in_proj_2';</script>\n";
		exit();
		}
		
		for($i=0;$i<count($_POST['vcode_approve']);$i++){
		$budget_acti=$_POST['vbudget_acti'][$i];
		$code_approve=$_POST['vcode_approve'][$i];
				if(($_POST['vcode_approve'][$i]!="")  and  ($_POST['vbudget_acti'][$i]!="")){
				$sql = "insert into plan_acti_3 (budget_year,code_acti,budget_acti,code_approve) values ('$proj_year','$vcode_acti','$budget_acti','$code_approve')";
				$result0  =  DBfieldQuery($sql);
				}
		}
} //end if index==14

if($index==15){
$sql = "SELECT *,plan_acti_2.begin_date,plan_acti_2.finish_date FROM  plan_acti_2 left join plan_proj_2 on  plan_acti_2.code_proj=plan_proj_2.code_proj  where plan_acti_2.budget_year='$proj_year'  and  plan_proj_2.budget_year='$proj_year'  and  plan_acti_2.id='$_GET[acti_id]'";
$dbquery=DBfieldQuery($sql);
$result_acti = mysqli_fetch_array($dbquery);
$begin_date =$result_acti['begin_date'];	
list($begin_year,$begin_month,$begin_day) = explode("-",$begin_date);	
$finish_date =$result_acti['finish_date'];	
list($finish_year,$finish_month,$finish_day) = explode("-",$finish_date);	
?>
<script type="text/javascript" src="./css/js/calendarDateInput.js"></script> 
<form id='frm1' name='frm1'>
<br />
<p align="center"><Font Size='3' color='#000099'><B>แก้ไขกิจกรรมโครงการ<?php echo $result_acti['name_proj']?> ปีงบประมาณ <?php echo $_SESSION['budget_year']?></B></Font></p>

<TABLE width="80%"  border="0" align="center" cellpadding="2" cellspacing="2" height="50">
<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วัน- เดือน-ปี : </font></b></td>
                    <td align="left" width="60%"> <script>
								var Y_date=<?php echo $Ynum?>  
								var m_date=<?php echo $mnum?>  
								var d_date=<?php echo $dnum?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('rec_date', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
					 <tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">รหัสกิจกรรม :</font></b></td>
                    <td align="left"> <input size=4 type=text name="vcode_acti" maxlength=3 value='<?php echo $result_acti['code_acti']?>' readonly> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">กิจกรรม :</font></b></td>
                    <td align="left"> <textarea name='vname_acti' rows = '3' cols='40' ><?php echo $result_acti['name_acti']?></textarea>
                    </td></tr>
<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วันเริ่มต้นกิจกรรม :</font></b></td>
                    <td align="left"> <script>
								var Y_date=<?php echo $begin_year?>  
								var m_date=<?php echo $begin_month?>  
								var d_date=<?php echo $begin_day?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('mybeginday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
				<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วันสิ้นสุดกิจกรรม :</font></b></td>
                    <td align="left"> <script>
								var Y_date=<?php echo $finish_year?>  
								var m_date=<?php echo $finish_month?>  
								var d_date=<?php echo $finish_day?>  
								Y_date= Y_date+'/'+m_date+'/'+d_date
								DateInput('myfinishday', true, 'YYYY-MM-DD', Y_date)</script> 
                    </td></tr>
                 <tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">แหล่งเงิน :</font></b></td>
                    <td align="left">
<?php
echo "<br />";
		//แสดงแหล่งเงิน
		$sql_budget = "SELECT *  FROM  plan_acti_3  where budget_year='$proj_year'  and  code_acti = '$result_acti[code_acti]' ";
		$query_budget =DBfieldQuery($sql_budget);
		$N=1;
		$X=0;
		$budget[1]=0; $budget[2]=0; $budget[3]=0; $budget[4]=0; $budget[5]=0;
		$approve[1]=""; $approve[2]=""; $approve[3]=""; $approve[4]=""; $approve[5]="";
		while($result_budget= mysqli_fetch_array($query_budget)){
		$budget[$N]=$result_budget['budget_acti'];
		$approve[$N]=$result_budget['code_approve'];
		$N++;
		}
		echo "1 <Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$num2="2_$num";
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
					if($num2==$approve[1]){
					echo  "<option value ='2_$num' selected>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
					else{
					echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9' value='$budget[1]'>";
		echo "</td></tr>";
		//รายการที่ 2
		echo "<tr><td></td><td>2 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$num2="2_$num";
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
					if($num2==$approve[2]){
					echo  "<option value ='2_$num' selected>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
					else{
					echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9' value='$budget[2]'>";
		echo "</td></tr>";
		//รายการที่ 3
		echo "<tr><td></td><td>3 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$num2="2_$num";
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
					if($num2==$approve[3]){
					echo  "<option value ='2_$num' selected>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
					else{
					echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9' value='$budget[3]'>";
		echo "</td></tr>";
		//รายการที่ 4
		echo "<tr><td></td><td>4 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$num2="2_$num";
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
					if($num2==$approve[4]){
					echo  "<option value ='2_$num' selected>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
					else{
					echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9' value='$budget[4]'>";
		echo "</td></tr>";
		//รายการที่ 5
		echo "<tr><td></td><td>5 ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$num2="2_$num";
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
					if($num2==$approve[5]){
					echo  "<option value ='2_$num' selected>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
					else{
					echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b><input  size='9' type='text'  name='vbudget_acti[]' maxlength='9' value='$budget[5]'>";
		echo "</td></tr>";
		echo "<INPUT TYPE='hidden' name='acti_id' value='$_GET[acti_id]'>";
		echo "<INPUT TYPE='hidden' name='code_acti' value='$result_acti[code_acti]'>";
		echo "<INPUT TYPE='hidden' name='page' value='$_GET[page]'>";

		echo "<tr><td colspan='2' align='center'><INPUT TYPE='button' name='smb' value='บันทึก' onclick='goto_update_acti(1)'>&nbsp;<INPUT TYPE='button' name='smb' value='ยกเลิก' onclick='goto_update_acti(0)'></td></tr>";
		
?>	
<table>
</form>
<?php
}//end if index==15

if($index==16){
$rec_date =date("Y-m-d H:i:s");
		$sql =  "update  plan_acti_2  set  name_acti='$_POST[vname_acti]', begin_date='$_POST[mybeginday]', finish_date='$_POST[myfinishday]', rec_date= '$rec_date', officer = '$_SESSION[login_user_id]'  where budget_year='$proj_year'  and  id = '$_POST[acti_id]' ";
		$dbquery  =  DBfieldQuery($sql);
		
		$sql =  "delete  FROM  plan_acti_3  where budget_year='$proj_year'  and  code_acti= '$_POST[code_acti]' ";
		$dbquery = DBfieldQuery($sql);
	
		for($i=0;$i<count($_POST['vcode_approve']);$i++){
		$budget_acti=$_POST['vbudget_acti'][$i];
		$code_approve=$_POST['vcode_approve'][$i];
				if(($_POST['vcode_approve'][$i]!="")  and  ($_POST['vbudget_acti'][$i]!="")){
				$sql = "insert into plan_acti_3 (budget_year,code_acti,budget_acti,code_approve) values ('$proj_year','$vcode_acti','$budget_acti','$code_approve')";
				$result0  =  DBfieldQuery($sql);
				}
		}
} //end if index==16

if($index==17){
$sql_ref = "SELECT *  FROM  plan_acti_2  where budget_year='$proj_year'  and  id = '$_GET[acti_id]' ";
$query_ref =DBfieldQuery($sql_ref);
$result_ref = mysqli_fetch_array($query_ref);

function thai_date_2($date){
		if(!isset($date)){
		return;
		}
$thai_month_arr=array(
	"01"=>"มกราคม",
	"02"=>"กุมภาพันธ์",
	"03"=>"มีนาคม",
	"04"=>"เมษายน",
	"05"=>"พฤษภาคม",
	"06"=>"มิถุนายน",	
	"07"=>"กรกฎาคม",
	"08"=>"สิงหาคม",
	"09"=>"กันยายน",
	"10"=>"ตุลาคม",
	"11"=>"พฤศจิกายน",
	"12"=>"ธันวาคม"					
);
	$thai_date_return="";
	$f_date=explode("-", $date);
	$f_date[2]=intval($f_date[2]);
	$thai_date_return.=	"วันที่ ".$f_date[2];
	$thai_date_return.=" เดือน".$thai_month_arr[$f_date[1]];
	$thai_date_return.=	" พ.ศ.".($f_date[0]+543);
	if($date!=""){
	return $thai_date_return;
	}
	else{
	$thai_date_return="";
	return $thai_date_return;
	}
}

?>
<script type="text/javascript" src="./css/js/calendarDateInput.js"></script> 

<br />
<p align="center"><Font Size='3' color='#000099'><B>รายละเอียดกิจกรรม</B></Font></p>
<TABLE width="80%"  border="0" align="center" cellpadding="2" cellspacing="2" height="50" Bgcolor="#Fcf9d8">
<?php
echo "<Tr ><Td colspan='2' align='right'><INPUT TYPE='button' name='smb' value='<<กลับหน้าก่อน' onclick='location.href=\"?option=plan&task=planproject/plan_in_proj_2&page=$_GET[page]\"'></Td></Tr>";
?>
<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วัน- เดือน-ปี : </font></b></td>
                    <td align="left" width="60%"> 
					<?php
					echo thai_date_2($result_ref['rec_date']);
					?>
                    </td></tr>
					 <tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">รหัสกิจกรรม :</font></b></td>
                    <td align="left">
					<?php
					echo $result_ref['code_acti'];
					?>
                    </td></tr>
				<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">กิจกรรม :</font></b></td>
                    <td align="left">
					<?php
					echo $result_ref['name_acti'];
					?>
                    </td></tr>
<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วันเริ่มต้นกิจกรรม :</font></b></td>
                    <td align="left"> 
					<?php
					 echo thai_date_2($result_ref['begin_date']);
					?>
                    </td></tr>
				<tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">วันสิ้นสุดกิจกรรม :</font></b></td>
                    <td align="left">
					<?php
					echo thai_date_2($result_ref['finish_date']);
					?>
                    </td></tr>
                 <tr> 
                    <td align="right"><b><font color="#003333" size="2" face="MS Sans Serif">แหล่งเงิน :</font></b></td>
                    <td align="left"></td></tr>
<?php
		
		//แสดงแหล่งเงิน
		$sql_budget = "SELECT *  FROM  plan_acti_3  where budget_year='$proj_year'  and  code_acti = '$result_ref[code_acti]' ";
		$query_budget =DBfieldQuery($sql_budget);
		$N=1;
		while($result_budget= mysqli_fetch_array($query_budget)){
		echo "<tr><td></td><td>$N ";
		echo "<Select  name='vcode_approve[]' size='1'>";
		$sql = "select  num, item from  budget_receive where  budget_year='$proj_year' order by num";
		$dbquery =DBfieldQuery($sql);
		echo  "<option  value = ''>เลือก</option>" ;
		echo "<optgroup label='งบประมาณ'>";
		While ($result = mysqli_fetch_array($dbquery))
		   {
		$num = $result['num'];
		$num2="2_$num";
		echo $num2;
		$item_name = $result['item'];
		$item_name=substr($item_name,0,100);
		
					if($num2==$result_budget['code_approve']){
					echo  "<option value ='2_$num' selected>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
					else{
					echo  "<option value ='2_$num'>งวดที่&nbsp;$num&nbsp;$item_name</option>";
					}
			}
		echo  "</optgroup>";
		echo "</select>";	
		echo "<br><b><font color='#003333' size='2' face='MS Sans Serif'> จำนวนเงิน </font></b>";
		echo number_format($result_budget['budget_acti'],2);
		echo " บาท";
		echo "</td></tr>";
		$N++;
		} //while
		echo "<tr><td>&nbsp;</td><td></td></tr>";	
?>	
				
<table>
<?php


} //end if index==17


?>

<!-- Part2 -->
<?php 
if(!($index==1 or $index==2 or $index==5 or $index==7 or $index==11 or $index==15 or $index==17)){
echo "<br>";
echo "<div align='center'><Font Size='4' color='#000099'>โครงการเพิ่มเติมจากเงินเหลือจ่าย ปีงบประมาณ  $_SESSION[budget_year]</Font></div>";

$sql_page = "SELECT *  FROM  plan_proj_2  where budget_year='$proj_year' ";
$dbquery_page = mysqli_query($connect,$sql_page);
$num_rows=mysqli_num_rows($dbquery_page);
$pagelen=10; // กำหนดแถวต่อหน้า
$url_link="option=plan&task=planproject/plan_in_proj_2";
$totalpages=ceil($num_rows/$pagelen);

if(!(isset($_REQUEST['page']))){
$_REQUEST['page']="";
}

if($_REQUEST['page']==""){
$page=$totalpages;
		if($page<2){
		$page=1;
		}
}
else{
		if($totalpages<$_REQUEST['page']){
		$page=$totalpages;
					if($page<1){
					$page=1;
					}
		}
		else{
		$page=$_REQUEST['page'];
		}
}

$start=($page-1)*$pagelen;

if(($totalpages>1) and ($totalpages<16)){
echo "<div align=center>";
echo "หน้า	";
			for($i=1; $i<=$totalpages; $i++)	{
					if($i==$page){
					echo "[<b><font size=+1 color=#990000>$i</font></b>]";
					}
					else {
					echo "<a href=$PHP_SELF?$url_link&page=$i>[$i]</a>";
					}
			}
echo "</div>";
}			
if($totalpages>15){
			if($page <=8){
			$e_page=15;
			$s_page=1;
			}
			if($page>8){
					if($totalpages-$page>=7){
					$e_page=$page+7;
					$s_page=$page-7;
					}
					else{
					$e_page=$totalpages;
					$s_page=$totalpages-15;
					}
			}
			echo "<div align=center>";
			if($page!=1){
			$f_page1=$page-1;
			echo "<<a href=$PHP_SELF?$url_link&page=1>หน้าแรก </a>";
			echo "<<<a href=$PHP_SELF?$url_link&page=$f_page1>หน้าก่อน </a>";
			}
			else {
			echo "หน้า	";
			}					
			for($i=$s_page; $i<=$e_page; $i++){
					if($i==$page){
					echo "[<b><font size=+1 color=#990000>$i</font></b>]";
					}
					else {
					echo "<a href=$PHP_SELF?$url_link&page=$i>[$i]</a>";
					}
			}
			if($page<$totalpages)	{
			$f_page2=$page+1;
			echo "<a href=$PHP_SELF?$url_link&page=$f_page2> หน้าถัดไป</a>>>";
			echo "<a href=$PHP_SELF?$url_link&page=$totalpages> หน้าสุดท้าย</a>>";
			}
			echo " <select onchange=\"location.href=this.options[this.selectedIndex].value;\" size=\"1\" name=\"select\">";
			echo "<option  value=\"\">หน้า</option>";
				for($p=1;$p<=$totalpages;$p++){
				echo "<option  value=\"?$url_link&page=$p\">$p</option>";
				}
			echo "</select>";
echo "</div>";  
}					
//จบแยกหน้า
?>
<BR><BR>
<?php
echo "<table width='90%' align='center' border='0'>";
echo "<Tr><Td align='left'><INPUT TYPE='button' name='smb' value='เพิ่มข้อมูล' onclick='location.href=\"?option=plan&task=planproject/plan_in_proj_2&index=1\"'></Td>";
echo "</table>";
?>
<Table width="90%" Border="1" borderColor=#990000  text="#FFFFFF"  align="center" style="border-collapse: collapse">
<?php
echo "</Tr>";
echo   "<Tr bgcolor='#FFEAFF'>";
echo   "<Th  width='7%'>รหัสโครงการ</th>";
echo   "<Th>โครงการ/กิจกรรม</th>";
echo   "<Th  width='15%'>งบประมาณ</th>";
echo   "<Th  width='18%'>หัวหน้าโครงการ</th>";
echo   "<Th  width='4%'>รายละเอียด</th>";
echo   "<Th  width='4%'>แก้ไข</th>";
echo   "<Th width='4%'>ลบ</th>";
echo   "<Th width='4%'>เพิ่ม<br>กิจกรรม</th>";

$sql = "SELECT  *,plan_proj_2.id  FROM  plan_proj_2 left join person_main on plan_proj_2.owner_proj=person_main.person_id  where budget_year='$proj_year' ORDER BY code_proj  limit $start,$pagelen";
$dbquery =DBfieldQuery($sql);
$i=0;
$N=(($page-1)*$pagelen)+1;  //*เกี่ยวข้องกับการแยกหน้า
$M=1;
$sum_budget_proj=0;
while ($result = mysqli_fetch_array($dbquery))
	{
	$id =$result['id'];
	$budget_year =$result['budget_year'];
	$code_clus =$result['code_clus'];
	$code_tegy =$result['code_tegy'];
	$code_proj =$result['code_proj'];
	$name_proj = $result['name_proj'];
	$budget_proj =$result['budget_proj'];
	$sum_budget_proj=$sum_budget_proj+$budget_proj; 
	$owner_proj =$result['owner_proj'];
	if  (empty($code_tegy)) 
	{   $code_tegy= chr(149);   }
	$mcode_tegy =$code_tegy.'-'.$code_proj;
	$len=strlen($owner_proj);
	$point=strpos($owner_proj,' ');
	$long=$len-$point;
	$fname=substr($owner_proj,0,$point);
	$sname=substr($owner_proj,-$long,$long);
	$cname=trim($sname);
	$Fcredit1=number_format($budget_proj,2);
	echo   "<Tr Bgcolor='#F8E874'>";
	echo   "<Td align='center'>$code_proj</td>";
	echo   "<Td align='left'>&nbsp;$name_proj</td>";
	echo   "<Td align='center'>$Fcredit1&nbsp;&nbsp;&nbsp;</td>";
	echo   "<Td align='left'>$result[prename]$result[name] $result[surname]</td>";
	echo   "<Td><div align=center><a href=?option=plan&task=planproject/plan_in_proj_2&index=7&plan_proj_id=$id&page=$page><img src=\"./images/b_browse.png\" WIDTH='16' HEIGHT='16' BORDER=0 ALT='รายละเอียดโครงการ'></a></div></Td>";

		if($_SESSION["mpms_edit"]=='1')
		{
			echo   "<Td><div align='center'><a href=?option=plan&task=planproject/plan_in_proj_2&index=5&plan_proj_id=$id&page=$page><img src=\"./images/b_edit.png\" WIDTH='16' HEIGHT='16' BORDER=0 ALT='แก้ไขโครงการ'></a></div></Td>";
		}
		else{
		echo "<td></td>";
		}
		if($_SESSION["mpms_dele"]=='1')
		{
			echo   "<Td><div align=center><a href=?option=plan&task=planproject/plan_in_proj_2&vcode_proj=$code_proj&index=2&page=$page><img src=\"./images/b_drop.png\" WIDTH='16' HEIGHT='16' BORDER=0 ALT='ลบโครงการ'></a></div></Td>";
		}
		else{
		echo "<td></td>";
		}
		echo   "<Td><div align='center'><a href=?option=plan&task=planproject/plan_in_proj_2&index=11&plan_proj_id=$id&page=$page><img src=\"./images/b_edit.png\" WIDTH='16' HEIGHT='16' BORDER=0 ALT='เพิ่มกิจกรรม'></a></div></Td>";
		echo "</tr>";
		
		$sql_acti = "SELECT  *  FROM  plan_acti_2 where  budget_year='$proj_year'  and  code_proj='$code_proj' order by code_acti";
		$dbquery_acti =DBfieldQuery($sql_acti);
		while ($result_acti = mysqli_fetch_array($dbquery_acti)){
				//จำนวนเงิน
				$sql_budget = "SELECT  sum(budget_acti) as budget_acti  FROM  plan_acti_3 where  budget_year='$proj_year'  and  code_acti='$result_acti[code_acti]' ";
				$dbquery_budget =DBfieldQuery($sql_budget);
				$result_budget = mysqli_fetch_array($dbquery_budget);
				$budget_acti=$result_budget['budget_acti'];
				$budget_acti=number_format($budget_acti,2);
				//
		echo "<tr><td></td><td>";
		echo $result_acti['code_acti']; 
		echo " ";
		echo $result_acti['name_acti']; 
		echo "</td><td align='right'>$budget_acti</td><td></td>";
		echo   "<Td><div align=center><a href=?option=plan&task=planproject/plan_in_proj_2&index=17&acti_id=$result_acti[id]&page=$page><img src=\"./images/b_browse.png\" WIDTH='16' HEIGHT='16' BORDER=0 ALT='รายละเอียดกิจกรรม'></a></div></Td>";
		echo   "<Td><div align='center'><a href=?option=plan&task=planproject/plan_in_proj_2&index=15&acti_id=$result_acti[id]&page=$page><img src=\"./images/b_edit.png\" WIDTH='16' HEIGHT='16' BORDER=0 ALT='แก้ไขกิจกรรม'></a></div></Td>";
		echo   "<Td><div align=center><a href=?option=plan&task=planproject/plan_in_proj_2&index=13&code_acti=$result_acti[code_acti]&page=$page><img src=\"./images/b_drop.png\" WIDTH='16' HEIGHT='16' BORDER=0 ALT='ลบกิจกรรม'></a></div></Td>";
		echo "<td></td></tr>";
		}
$i++;
$M++;
$N++;
}
$sum_budget_proj=number_format($sum_budget_proj,2);
echo "<tr bgcolor='#FFEAFF'><td></td><td align='center'><b>รวม</b></td><td align='center'><b>$sum_budget_proj</b></td><td colspan='5'></td></tr>";
} //end if index
?>

</Table>
</Center>
</form>
<script>
function goto_url(val){
	if(val==0){
		callfrm("?option=plan&task=planproject/plan_in_proj_2");   
	}
} 

function goto_url_update(val){
	if(val==0){
		callfrm("?option=plan&task=planproject/plan_in_proj_2");   
	}else if(val==1){
								var v1 = document.frm1.vcode_clus.value;
								var v2 = document.frm1.vcode_proj.value;
								var v3 = document.frm1.vname_proj.value;
								var v4 = document.frm1.vbudget_proj.value;
								var v5 = document.frm1.vowner_proj.value;
								 if (v1.length=="0")
									{
										alert("กรุณาเลือกกลุ่มงาน");
										document.frm1.vcode_clus.focus();           
										return false;
									 }
									else if (v2.length!=3) 
									{
										alert("กรุณาใส่รหัสโครงการ 3 หลัก เช่น 001 , 002 เป็นต้น");
										document.frm1.vcode_proj.focus();           
										return false;
									}
									else if (v3.length==0) 
									{
										alert("กรุณาใส่ชื่อโครงการ");
										document.frm1.vname_proj.focus();           
										return false;
									 }
									else if (v4.length==0)
									{
										alert("กรุณาใส่งบประมาณที่ได้รับจัดสรร") ;
										document.frm1.vbudget_proj.focus();           
										return false;
									}
									else if (v5.length==0) 
									{
										alert("กรุณาใส่ชื่อหัวหน้าโครงการ");
										document.frm1.vowner_proj.focus();           
										return false;
									}
									else{
										callfrm("?option=plan&task=planproject/plan_in_proj_2&index=4"); }  //page ประมวลผล
									}
					} // if(val==1)
					
function goto_url_update_proj(val){
	if(val==0){
		callfrm("?option=plan&task=planproject/plan_in_proj_2");   
	}else if(val==1){
								var v1 = document.frm1.vcode_clus.value;
								var v2 = document.frm1.vcode_proj.value;
								var v3 = document.frm1.vname_proj.value;
								var v4 = document.frm1.vbudget_proj.value;
								var v5 = document.frm1.vowner_proj.value;
								 if (v1.length=="0")
									{
										alert("กรุณาเลือกกลุ่มงาน");
										document.frm1.vcode_clus.focus();           
										return false;
									 }
									else if (v2.length!=4) 
									{
										alert("กรุณาใส่รหัสโครงการ 4 หลัก เช่น 1001 , 1002 เป็นต้น");
										document.frm1.vcode_proj.focus();           
										return false;
									}
									else if (v3.length==0) 
									{
										alert("กรุณาใส่ชื่อโครงการ");
										document.frm1.vname_proj.focus();           
										return false;
									 }
									else if (v4.length==0)
									{
										alert("กรุณาใส่งบประมาณที่ได้รับจัดสรร") ;
										document.frm1.vbudget_proj.focus();           
										return false;
									}
									else if (v5.length==0) 
									{
										alert("กรุณาใส่ชื่อหัวหน้าโครงการ");
										document.frm1.vowner_proj.focus();           
										return false;
									}
									else{
										callfrm("?option=plan&task=planproject/plan_in_proj_2&index=6"); }  
									}
					} // if(val==1)
					
function goto_insert_acti(val){
	if(val==0)
	{ 
	callfrm("?option=plan&task=planproject/plan_in_proj_2");  
	}
	if(val==1)
	{ 
			 if(frm1.vcode_acti.value == ""){
			alert("กรุณาป้อน	รหัสกิจกรรม \n 001,002,...");
			document.frm1.vcode_acti.focus();
			}
			else if (frm1.vcode_acti.value.length!=3) {
			alert("กรุณาใส่รหัสกิจกรรม 3 หลัก เช่น 001 , 002 เป็นต้น");
			document.frm1.vcode_acti.focus();
			}
			else if(frm1.vname_acti.value == ""){
			alert("กรุณาป้อน กิจกรรม");
			document.frm1.vname_acti.focus();
			}
			else	{ 
				callfrm("?option=plan&task=planproject/plan_in_proj_2&index=14");  
			}	
	} 
} 

function goto_update_acti(val){
			if(val==0)
			{ 
			callfrm("?option=plan&task=planproject/plan_in_proj_2");  
			}	
			if(val==1)
			{ 
					if(frm1.vname_acti.value == ""){
					alert("กรุณาป้อน กิจกรรม");
					document.frm1.vname_acti.focus();
					}
					else	{ 
					callfrm("?option=plan&task=planproject/plan_in_proj_2&index=16");  
					}	
			}	
}					
</script>
