<?php 
ob_start();
session_start();

 require("Connection.php");
 
 $thang = @$_POST["thang"];
 $nam = @$_POST["nam"];
 $truongmn = @$_POST["truongmn"];
 
 
		$sqltrg="select * from truongmn where matruongmn='"  . $truongmn . "'";
		$resulttrg=mysql_query($sqltrg,$link);
		$rowtrg=mysql_fetch_array($resulttrg);
    	$tentruong=$rowtrg["tentruongmn"];
 
 	$sql10="delete FROM solieugt";
	$result10=mysql_query($sql10,$link);
	
	

	$sql1="SELECT * FROM dexuatgiaotrinh where matruongmn='"  . $truongmn . "' and month(ngaydexuat)=" . $thang . " and year(ngaydexuat)=" . $nam;



	$result1=mysql_query($sql1,$link);
	$tongrec1= mysql_num_rows($result1);
	if ($tongrec1>0)
	{
	while ($row1=mysql_fetch_array($result1))
			{		
				$matruongmn=$row1["matruongmn"];
				$malopmn=$row1["malopmn"];
				$soluongbe=$row1["soluongbe"];
				$noidungdx=$row1["noidungdx"];
				$ngaydexuat=$row1["ngaydexuat"];
				for ($i = 0; $i < strlen($noidungdx); $i++) 
					{
        				$kytu=substr($noidungdx,$i,1);
						
						if(strtoupper($kytu)=="K")
							{
								$mabai=substr($noidungdx,$i,6);
								$sql2="insert into solieugt(matruongmn,malopmn,magt,solg,ngaydx)";
						$sql2 .=" values('".$matruongmn."',".$malopmn.",'".$mabai."',".$soluongbe.",'".$ngaydexuat."')";	
						$result2=mysql_query($sql2,$link);
						$i =$i +6;
							}
						
						$kytu=substr($noidungdx,$i,1);
						if(strtoupper($kytu)=="L" || strtoupper($kytu)=="D" || strtoupper($kytu)=="N")
							{
								$mabai=substr($noidungdx,$i,4);
								$sql2="insert into solieugt(matruongmn,malopmn,magt,solg,ngaydx)";
						$sql2 .=" values('".$matruongmn."',".$malopmn.",'".$mabai."',".$soluongbe.",'".$ngaydexuat."')";	
						$result2=mysql_query($sql2,$link);
						$i =$i +4;
							}
    				}
				
			  }

	}
				
		

//header("location:dexuatgiaotrinhnew.php#xemgt");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<HTML>
	<HEAD>
		<Title>Admin</Title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	    <style type="text/css">
<!--
.style3 {
	font-size: 14pt;
	font-family: Arial, Helvetica, sans-serif;
	color: #990000;
	font-weight: bold;
}
-->
        </style>
	</HEAD>
	<body>
	  <form name=f1  method=POST >

        <div align="center">
          <font color="#000000" size="+2"><b><br />
                BẢNG KÊ CHI PHÍ HÀNG THÁNG</b></font>
           <table width="80%" cellspacing="0" border="0">
        <tr>
          <td width="80%" align = "left"><p align="center"><font color="#000000" face="Arial" size="3"><b>
                       Tháng : <?php echo $thang; ?>/<?php echo $nam; ?><br />
                       <?php echo $tentruong; ?></font></p></td>
        <tr>
                     
                     <table>
                   
                   <?php
                  $sql6="select count(malopmn) tslop, sum(sohs) tshs, sum(solgmagt) tsmagt, sum(soluonggt) tsgt  from (SELECT malopmn, count(malopmn) solgmagt, sum(solg) soluonggt, round(sum(solg)/count(malopmn),0) sohs FROM solieugt where matruongmn='"  . $truongmn	. "' and month(ngaydx)=" . $thang . " and year(ngaydx)=" . $nam ." group by malopmn order by soluonggt desc) tongso ";
//echo $sql6;

	$result6=mysql_query($sql6,$link);
	$tongrec6= mysql_num_rows($result6);
	if ($tongrec6>0)
	{
	while ($row6=mysql_fetch_array($result6))
			{
                        $tongsolggt=$row6["tsgt"];
						?>
               
                    <?php
                        }
                    }
                    ?>
                 
          <table width="80%" cellspacing="0" border="1">
               
                    <tr>
                      <td width="12%" align = "center"><p align="center"><font color="#000000" face="Arial" size="3"><b>LỚP</font></p></td>
                      <td width="12%" align = "center"><p align="center"><font color="#000000" face="Arial" size="3"><b>SỐ HS</font></p></td>
                      <td width="18%" align = "center"><p align="center"><font color="#000000" size="3" face="Arial"><b>MÃ GIÁO TRÌNH</font></p></td>
                      <td width="18%" align = "center"><p align="center"><font color="#000000" size="3" face="Arial"><b>SỐ LG GIÁO TRÌNH</font></p></td>
                      <td width="20%" align = "center"><p align="center"><font color="#000000" size="3" face="Arial"><b>THÀNH TIỀN</font></p></td>
                    </tr>
                    <?php
                  $sql5="select tenlopmn,solgmagt,soluonggt,sohs,(soluonggt*5950) thanhtien from( SELECT malopmn, count(malopmn) solgmagt, sum(solg) soluonggt , round(sum(solg)/count(malopmn),0) sohs FROM solieugt where matruongmn='"  . $truongmn	. "' and month(ngaydx)=" . $thang . " and year(ngaydx)=" . $nam ." group by malopmn order by soluonggt desc) tk,lopmn where lopmn.malopmn=tk.malopmn order by tenlopmn";

//echo $sql5;
	$result5=mysql_query($sql5,$link);
	$tongrec5= mysql_num_rows($result5);
	if ($tongrec5>0)
	{
	while ($row5=mysql_fetch_array($result5))
			{
                        ?>
                    <tr>
                      <td align = "center"><div align="center"><font  face="arial"  size="3"><?php echo $row5["tenlopmn"]; ?></font>&nbsp;</div></td>
                      <td align = "center"><div align="center"><font  face="arial"  size="3"><?php echo $row5["sohs"]; ?></font>&nbsp;</div></td>
                      <td align = "center"><div align="center"><font  face="arial"  size="3"><?php echo $row5["solgmagt"]; ?></font>&nbsp;</div></td>
                         <td align = "center"><div align="center"><font  face="arial"  size="3"><?php echo $row5["soluonggt"]; ?></font>&nbsp;</div></td>
                         <td align = "center"><div align="right"><font  face="arial"  size="3"><?php echo number_format($row5["thanhtien"]); ?></font>&nbsp;</div></td>
                    </tr>
    
                    <?php
                        }
                    }
                    ?>
                                         <tr>
    <td colspan="3" align = "right"><font  face="arial"  size="3"><b>&nbsp;Tổng cộng</td>
    <td  align = "center">&nbsp;<font  face="arial"  size="3"><b><?php echo number_format($tongsolggt); ?></td>
    <td  align = "right">&nbsp;<font  face="arial"  size="3"><b><?php echo number_format($tongsolggt*5950); ?></td>
    <tr>
          </table>
          <br />
           <table width="80%" cellspacing="0" border="0">
           <tr>
           <td align="left"><strong><em><a href="thongketruong.php">Lưu ý:</a></em></strong><br />
            Chí phí trên chưa bao gồm các chi phí khấu hao thiết bị, chi phí bảo trì, sửa chữa thay thế linh kiện máy tính máy chiếu….vv hàng tháng.<br />
          </td></tr>
          </table>

        </div>
</form>
	</body>
</HTML>
<script language="javascript">

    function kt()

 {
		if (document.f1.thang.value=="")
           {
		   alert("Sao khong chon thang?!");
            return false;
		   }
		   if (document.f1.nam.value=="")
           {
		   alert("Chon nam chua?!");
            return false;
		   }
		    

            document.f1.action = "thongketruong.php";
            document.f1.submit();
       

    
}

</script>






</script>

