<?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 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);
echo  "<table width='80%' border='1' align='center' style='border-collapse: collapse'>";
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;
$pay=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; 

//คงเหลือ
$net_ar[1]=0;
$net_ar[2]=0;
$net_ar[3]=0;
$net_ar[4]=0;
$net_ar[5]=0;
$net_ar[6]=0;
$net_ar[7]=0;

//% จ่าย
$percent_ar[1]="";
$percent_ar[2]="";
$percent_ar[3]="";
$percent_ar[4]="";
$percent_ar[5]="";
$percent_ar[6]="";
$percent_ar[7]="";


While ($result = mysqli_fetch_array($dbquery))
	{
		$id = $result['id'];
		$num= $result['num'];
		$num_withdraw="2_".$num;
		$item= $result['item'];
		$money= $result['money'];
		$pay_group= $result['pay_group_id']; //เพิ่มเติม 29 มิย 60
					
						//จำนวนเงินแต่ละใบงวดจำแนกตามประเภทเงิน
						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;
						}
		
		//ตัดโครงการ
		$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;    //รวมเงินคืน

		$pay=$withdraw-$return;    //จ่ายจริง
		$net=$money-$withdraw+$return;  //เหลือ
		$net_total=$net_total+$net;   //รวมเหลือ
		$money_total=$money_total+$money;	//รวมเงินแต่ละในงวด
		
							//จำนวนเงินจ่ายจำแนกตามประเภทเงิน
							if($pay_group==1){
							$group_pay[1]=$group_pay[1]+$pay;
							}
							else if($pay_group==2){
							$group_pay[2]=$group_pay[2]+$pay;
							}
							else if($pay_group==3){
							$group_pay[3]=$group_pay[3]+$pay;
							}
							else if($pay_group==4){
							$group_pay[4]=$group_pay[4]+$pay;
							}
							else if($pay_group==5){
							$group_pay[5]=$group_5[5]+$pay;
							}
							else if($pay_group==6){
							$group_pay[6]=$group_pay[6]+$pay;
							}
							else if($pay_group==7){
							$group_pay[7]+$pay;
							}
							
							//คงเหลือ
							if($pay_group==1){
							$net_ar[1]=$net_ar[1]+$net;
							}
							else if($pay_group==2){
							$net_ar[2]=$net_ar[2]+$net;
							}
							else if($pay_group==3){
							$net_ar[3]=$net_ar[3]+$net;
							}
							else if($pay_group==4){
							$net_ar[4]=$net_ar[4]+$net;
							}
							else if($pay_group==5){
							$net_ar[5]=$group_5[5]+$net;
							}
							else if($pay_group==6){
							$net_ar[6]=$net_ar[6]+$net;
							}
							else if($pay_group==7){
							$net_ar[7]=$net_ar[7]+$net;
							}
							
		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>";
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);

$net_g1=number_format($net_ar[1],2);
$net_g2=number_format($net_ar[2],2);
$net_g3=number_format($net_ar[3],2);
$net_g4=number_format($net_ar[4],2);
$net_g5=number_format($net_ar[5],2);
$net_g6=number_format($net_ar[6],2);
$net_g7=number_format($net_ar[7],2);
$net_g_total=number_format($net_ar[1]+$net_ar[2]+$net_ar[3]+$net_ar[4]+$net_ar[5]+$net_ar[6]+$net_ar[7],2);

							//เปอร์เซ็นจ่าย
									if($group[1]!=0){
									$percent_ar[1]=$group_pay[1]/$group[1]*100;
									$percent_ar[1]=number_format($percent_ar[1],2);
									}
									if($group[2]!=0){
									$percent_ar[2]=$group_pay[2]/$group[2]*100;
									$percent_ar[2]=number_format($percent_ar[2],2);
									}
									if($group[3]!=0){
									$percent_ar[3]=$group_pay[3]/$group[3]*100;
									$percent_ar[3]=number_format($percent_ar[3],2);
									}
									if($group[4]!=0){
									$percent_ar[4]=$group_pay[4]/$group[4]*100;
									$percent_ar[4]=number_format($percent_ar[4],2);
									}
									if($group[5]!=0){
									$percent_ar[5]=$group_pay[5]/$group[5]*100;
									$percent_ar[5]=number_format($percent_ar[5],2);
									}
									if($group[6]!=0){
									$percent_ar[6]=$group_pay[6]/$group[6]*100;
									$percent_ar[6]=number_format($percent_ar[6],2);
									}
									if($group[7]!=0){
									$percent_ar[7]=$group_pay[7]/$group[7]*100;
									$percent_ar[7]=number_format($percent_ar[7],2);
									}
									
									$x=$group[1]+$group[2]+$group[3]+$group[4]+$group[5]+$group[6]+$group[7];
									$percent_pay_total="";
									if($x!=0){
									$percent_pay_total=($group_pay[1]+$group_pay[2]+$group_pay[3]+$group_pay[4]+$group_pay[5]+$group_pay[6]+$group_pay[7])*100/$x;
									$percent_pay_total=number_format($percent_pay_total,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><Td>%จ่าย</Td></Tr>";
echo "<tr align='right'><td align='center'>1</td><td align='left'>งบบุคลากร</td><td>$group_1</td><td>$group_pay_1</td><td>$net_g1</td><td>$percent_ar[1]</td></tr>";
echo "<tr align='right'><td align='center'>2</td><td align='left'>งบดำเนินงาน</td><td>$group_2</td><td>$group_pay_2</td><td>$net_g2</td><td>$percent_ar[2]</td></tr>";
echo "<tr align='right'><td align='center'>3</td><td align='left'>งบลงทุน</td><td>$group_3</td><td>$group_pay_3</td><td>$net_g3</td><td>$percent_ar[3]</td></tr>";
echo "<tr align='right'><td align='center'>4</td><td align='left'>งบเงินอุดหนุน</td><td>$group_4</td><td>$group_pay_4</td><td>$net_g4</td><td>$percent_ar[4]</td></tr>";
echo "<tr align='right'><td align='center'>5</td><td align='left'>งบรายจ่ายอื่น</td><td>$group_5</td><td>$group_pay_5</td><td>$net_g5</td><td>$percent_ar[5]</td></tr>";
echo "<tr align='right'><td align='center'>6</td><td align='left'>งบกลาง</td><td>$group_6</td><td>$group_pay_6</td><td>$net_g6</td><td>$percent_ar[6]</td></tr>";
echo "<tr align='right'><td align='center'>7</td><td align='left'>งบอื่น ๆ</td><td>$group_7</td><td>$group_pay_7</td><td>$net_g7</td><td>$percent_ar[7]</td></tr>";
echo "<tr align='center' bgcolor='#ddd'><td></td><td>รวม</td><td>$group_total</td><td>$group_pay_total</td><td>$net_g_total</td><td>$percent_pay_total %</td></tr>";
echo "</Table>";
?>

