<?php
defined( '_VALID_' ) or die( 'Direct Access to this location is not allowed.' );

//ปีงบประมาณ
$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="80%" border="0" align="center">
  <tr>
    <td><div align="center">
        <p><font color="#006666" size="3"><strong>รายงานการใช้จ่ายในโครงการ(ปกติ)  ปีงบประมาณ <?php echo $year_active_result['budget_year'] ?></strong></font></p>
      </div></td>
  </tr>
</table>
<?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  budget_receive where budget_year='$year_active_result[budget_year]' order by num ";
$dbquery = mysqli_query($connect,$sql);
echo  "<table width='80%' border='0' align='center'>";
echo "<Tr bgcolor='#FFCCCC' align='center'><Td width='90'>เลขที่ใบงวด</Td><Td>รายการ</Td><Td width='120'>จำนวนเงิน</Td><Td width='110'>ตัดจ่ายในโครงการ</Td><Td width='110'>คืนเงินโครงการ</Td><Td width='110'>คงเหลือ</Td><Td width='70'>%จ่าย</Td></Tr>";
$N=1;
$M=1;

$money_total=0;
$withdraw_total=0;
$return_total=0;
$net_total=0;

While ($result = mysqli_fetch_array($dbquery))
	{
		$id = $result['id'];
		$num= $result['num'];
		$num_withdraw="2_".$num;
		$item= $result['item'];
		$money= $result['money'];
		
		//ตัดโครงการ
		$sql_withdraw = "select  sum(budget_withdraw.money) as money from  budget_withdraw left join plan_acti on budget_withdraw.pj_activity=plan_acti.code_acti where budget_withdraw.budget_year='$year_active_result[budget_year]'  and plan_acti.budget_year='$year_active_result[budget_year]'  and  plan_acti.code_approve='$num_withdraw' ";
		$dbquery_withdraw = mysqli_query($connect,$sql_withdraw);
		$result_withdraw = mysqli_fetch_array($dbquery_withdraw);
		$withdraw=$result_withdraw['money'];
		$withdraw_total=$withdraw_total+$withdraw;
		//คืนเงินโครงการ
		$sql_return = "select  sum(budget_money_return.money) as money_return  from  budget_money_return left join plan_acti on budget_money_return.pj_activity=plan_acti.code_acti where budget_money_return.budget_year='$year_active_result[budget_year]'  and plan_acti.budget_year='$year_active_result[budget_year]'  and  plan_acti.code_approve='$num_withdraw' ";
		$dbquery_return = mysqli_query($connect,$sql_return);
		$result_return = mysqli_fetch_array($dbquery_return);
		$return=$result_return['money_return'];
		$return_total=$return_total+$return;

		$net=$money-$withdraw+$return;
		$net_total=$net_total+$net;
		$money_total=$money_total+$money;	
					
		if($money>0){
		$percent=(($money-$net)/$money)*100;
		$percent=number_format($percent,2);
		}
		$money1=number_format($money,2);
		$withdraw1=number_format($withdraw,2);
		$return1=number_format($return,2);
		$net1=number_format($net,2);
		
			if(($M%2) == 0)
			$color="#FFFFC";
			else  	$color="#FFFFFF";
		echo "<Tr bgcolor=$color align='center'><Td>$num</Td><Td align='left'>$item</Td><Td align='right'>$money1</Td><Td align='right'>$withdraw1</Td><Td align='right'>$return1</Td><Td align='right'>$net1</Td><Td align='right'>$percent</Td>";
		echo "</tr>";
	
$M++;
$N++;
}
		if($money_total<>0){
		$percent_total=(($money_total-$net_total)/$money_total)*100;
		}
		else{
		$percent_total=0;
		}
		
		$money_total=number_format($money_total,2);
		$withdraw_total=number_format($withdraw_total,2);
		$return_total=number_format($return_total,2);
		$net_total=number_format($net_total,2);
		$percent_total=number_format($percent_total,2);

		echo "<Tr bgcolor='#FFCCCC' align='center'><Td></Td><Td align='center'>รวม</Td><Td align='center'>$money_total</Td><Td align='center'>$withdraw_total</Td><Td>$return_total</Td><Td align='center'>$net_total</Td><Td>$percent_total</Td></Tr>";
echo "</Table>";
?>

