<?php
if(!(($_SESSION['login_status']<=4) and ($_SESSION["mpms_add"]==1 or $_SESSION["mpms_edit"] ==1 or $_SESSION["mpms_dele"]==1))){	
exit();
}

//ปีงบประมาณ
$sql = "select * from  plan_year where year_active='1' order by budget_year desc limit 1";
$dbquery = mysqli_query($connect,$sql);
$year_active_result = mysqli_fetch_array($dbquery);
if($year_active_result['budget_year']==""){
echo "<br />";
echo "<div align='center'>ยังไม่ได้กำหนดทำงานในปีงบประมาณใด ๆ  กรุณาไปที่เมนูตั้งค่าระบบ เพื่อกำหนดปีงบประมาณ</div>";
exit();
}
?>

<br />
<table width="90%" border="0" align="center">
  <tr>
    <td><div align="center">
        <font color="#006666" size="3"><strong>ตรวจสอบการจัดสรรงบประมาณ ปีงบประมาณ <?php echo $year_active_result['budget_year']?></strong></font>
      </div></td>
  </tr>
</table>
<br>
<?php
$money_total=0;
$acti_total=0;
$net_total=0;

//ที่ได้รับ
$group[1]=0;  //งบบุคลากร
$group[2]=0; //งบดำเนินงาน
$group[3]=0; //งบลงทุน
$group[4]=0; //งบเงินอุดหนุน
$group[5]=0; //งบรายจ่ายอื่น
$group[6]=0; //งบกลาง
$group[7]=0; //งบอื่น ๆ

//ที่จัดสรร
$group_pay[1]=0;  
$group_pay[2]=0; 
$group_pay[3]=0; 
$group_pay[4]=0; 
$group_pay[5]=0; 
$group_pay[6]=0; 
$group_pay[7]=0; 

					$sql = "select * from  budget_receive left join budget_pay_type on budget_receive.m_pay=budget_pay_type.pay_type_id  where budget_receive.budget_year='$year_active_result[budget_year]' order by  num";
					$dbquery = mysqli_query($connect,$sql);
					$rd=1;
					While ($result = mysqli_fetch_array($dbquery))
						{
						$id = $result['id'];
						$num= $result['num'];
						$item= $result['item'];
						$money= $result['money'];
						$pay_group= $result['pay_group_id']; //เพิ่มเติม 29 มิย 60
						$money_total=$money_total+$money;
						
						$num_ar[$rd]=$num;
						$item_ar[$rd]=$item;
						$money_ar[$rd]=$money;
						$pay_group_ar[$rd]=$pay_group;
						
						if($pay_group==1){
						$group[1]=$group[1]+$money;
						}
						else if($pay_group==2){
						$group[2]=$group[2]+$money;
						}
						else if($pay_group==3){
						$group[3]=$group[3]+$money;
						}
						else if($pay_group==4){
						$group[4]=$group[4]+$money;
						}
						else if($pay_group==5){
						$group[5]=$group[5]+$money;
						}
						else if($pay_group==6){
						$group[6]=$group[6]+$money;
						}
						else if($pay_group==7){
						$group[7]=$group[7]+$money;
						}
						
						$rd++;
	    				}

for($i=1;$i<$rd;$i++)
{
$num_index="2_$num_ar[$i]";
					$sql = "select * from  plan_acti  where code_approve='$num_index' and budget_year='$year_active_result[budget_year]'";
					$dbquery = mysqli_query($connect,$sql);
					$sum_budget=0;
					While ($result = mysqli_fetch_array($dbquery))
						{
							$code_proj_acti= $result['code_proj'];
							$budget_acti= $result['budget_acti'];
							$sum_budget=$sum_budget+$budget_acti;
							$acti_total=$acti_total+$budget_acti;
							
							//ประเภทงบต่าง ๆ ที่จัดสรร
							if($pay_group_ar[$i]==1){
							$group_pay[1]=$group_pay[1]+$budget_acti;
							}
							else if($pay_group_ar[$i]==2){
							$group_pay[2]=$group_pay[2]+$budget_acti;
							}
							else if($pay_group_ar[$i]==3){
							$group_pay[3]=$group_pay[3]+$budget_acti;
							}
							else if($pay_group_ar[$i]==4){
							$group_pay[4]=$group_pay[4]+$budget_acti;
							}
							else if($pay_group_ar[$i]==5){
							$group_pay[5]=$group_5[5]+$budget_acti;
							}
							else if($pay_group_ar[$i]==6){
							$group_pay[6]=$group_pay[6]+$budget_acti;
							}
							else if($pay_group_ar[$i]==7){
							$group_pay[7]=$group_pay[7]+$budget_acti;
							}
	    				}
						$sum_budget_ar[$i]=$sum_budget;
}


echo  "<table width='70%' border='1' align='center' style='border-collapse: collapse'>";
echo "<Tr bgcolor='#FFCCCC' align='center'><Td width='100'>ใบงวด</Td><Td>รายการ</Td><Td width='150'>จำนวนเงินตามใบงวด</Td><Td width='150'>จัดสรรกิจกรรมในโครงการ</Td><Td align=center width='150'>คงเหลือ</Td></Tr>";
for($x=1;$x<$rd;$x++)
{
	$net=$money_ar[$x]-$sum_budget_ar[$x];
	$net_total=$net_total+$net;
	if($net==0)
	$net1="ครบ";
	else
	$net1=number_format($net,2);
	$money_rec=number_format($money_ar[$x],2);
	$sum_budget=number_format($sum_budget_ar[$x],2);
			if(($x%2) == 0)
			$color="#FFFFC";
			else  	$color="#FFFFFF";
		echo "<Tr bgcolor=$color align='center'><Td>$num_ar[$x]</Td><Td align=left>$item_ar[$x]</Td><Td align=right>$money_rec</Td><Td align=right>$sum_budget</Td><Td align='right'>$net1</Td></Tr>";
}
		$money_total=number_format($money_total,2);
		$acti_total=number_format($acti_total,2);
		$net_total=number_format($net_total,2);
		echo "<Tr bgcolor='#FFCCCC' align='center'><Td colspan='2'>รวม</Td><Td align='right'>$money_total</Td><Td align='right'>$acti_total</Td><Td align='right'>$net_total</Td></Tr>";
echo "</Table>";
echo "<br>";

$group_1=number_format($group[1],2);
$group_2=number_format($group[2],2);
$group_3=number_format($group[3],2);
$group_4=number_format($group[4],2);
$group_5=number_format($group[5],2);
$group_6=number_format($group[6],2);
$group_7=number_format($group[7],2);
$group_total=number_format($group[1]+$group[2]+$group[3]+$group[4]+$group[5]+$group[6]+$group[7],2);

$group_pay_1=number_format($group_pay[1],2);
$group_pay_2=number_format($group_pay[2],2);
$group_pay_3=number_format($group_pay[3],2);
$group_pay_4=number_format($group_pay[4],2);
$group_pay_5=number_format($group_pay[5],2);
$group_pay_6=number_format($group_pay[6],2);
$group_pay_7=number_format($group_pay[7],2);
$group_pay_total=number_format($group_pay[1]+$group_pay[2]+$group_pay[3]+$group_pay[4]+$group_pay[5]+$group_pay[6]+$group_pay[7],2);

$group_remain_1=number_format($group[1]-$group_pay[1],2);
$group_remain_2=number_format($group[2]-$group_pay[2],2);
$group_remain_3=number_format($group[3]-$group_pay[3],2);
$group_remain_4=number_format($group[4]-$group_pay[4],2);
$group_remain_5=number_format($group[5]-$group_pay[5],2);
$group_remain_6=number_format($group[6]-$group_pay[6],2);
$group_remain_7=number_format($group[7]-$group_pay[7],2);

$remain_total=number_format($group[1]+$group[2]+$group[3]+$group[4]+$group[5]+$group[6]+$group[7]-$group_pay[1]-$group_pay[2]-$group_pay[3]-$group_pay[4]-$group_pay[5]-$group_pay[6]-$group_pay[7],2);

echo  "<table width='60%' border='1' align='center' style='border-collapse: collapse'>";
echo "<Tr bgcolor='#ddd' align='center'><Td width='60'>ที่</Td><Td>ประเภท</Td><Td>จำนวนเงิน</Td><Td>จัดสรรแล้ว</Td><Td>คงเหลือ</Td></Tr>";
echo "<Tr align='center'><Td>1</Td><Td align='left'>งบบุคลากร</Td><Td align='right'>$group_1</Td><Td align='right'>$group_pay_1</Td><Td align='right'>$group_remain_1</Td></Tr>";
echo "<Tr align='center'><Td>2</Td><Td align='left'>งบดำเนินงาน</Td><Td align='right'>$group_2</Td><Td align='right'>$group_pay_2</Td><Td align='right'>$group_remain_2</Td></Tr>";
echo "<Tr align='center'><Td>3</Td><Td align='left'>งบลงทุน</Td><Td align='right'>$group_3</Td><Td align='right'>$group_pay_3</Td><Td align='right'>$group_remain_3</Td></Tr>";
echo "<Tr align='center'><Td>4</Td><Td align='left'>งบเงินอุดหนุน</Td><Td align='right'>$group_4</Td><Td align='right'>$group_pay_4</Td><Td align='right'>$group_remain_4</Td></Tr>";
echo "<Tr align='center'><Td>5</Td><Td align='left'>งบรายจ่ายอื่น</Td><Td align='right'>$group_5</Td><Td align='right'>$group_pay_5</Td><Td align='right'>$group_remain_5</Td></Tr>";
echo "<Tr align='center'><Td>6</Td><Td align='left'>งบกลาง</Td><Td align='right'>$group_6</Td><Td align='right'>$group_pay_6</Td><Td align='right'>$group_remain_6</Td></Tr>";
echo "<Tr align='center'><Td>7</Td><Td align='left'>งบอื่น ๆ</Td><Td align='right'>$group_7</Td><Td align='right'>$group_pay_7</Td><Td align='right'>$group_remain_7</Td></Tr>";
echo "<Tr bgcolor='#ddd' align='center'><Td></Td><Td>รวม</Td><Td>$group_total</Td><Td>$group_pay_total</Td><Td>$remain_total</Td></Tr>";
echo "</Table>";
?>
