<?php
/*********************************************************      
 程序说明：         
    功能说明：每日报表显示
    调用关系：
    变量说明：

    作者：    
      
修改注释：      
NO.1      
姓名：            
时间：      
修改说明：      
*********************************************************/
require("../inc/include.inc");
$DEBUG = 0;
?>
<script language="javascript">
function selectDate(){
	var url,tmpDate;
	tmpDate=document.selectForm.date.options[document.selectForm.date.selectedIndex].value;
	url="./dailyReport.php?date="+tmpDate;
	window.location.href=url;
}
</script>
<body topmargin="0">
<br>
<form method="POST" action="$PHP_SELF" name="selectForm">
<table border="1" width="640" bordercolordark="#FFFFFF" bordercolor="#808080" cellspacing="0">
  <tr bgcolor="#E6E6E6">
    <td width="90%">Date:       
    <select size="1" name="date" onChange="javascript:selectDate()">
<?php
for($i=365;$i >= 0;$i--){
	$dayStr=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-$i,date("Y")));
	if($date == $dayStr){
		echo "<option value=\"$dayStr\" selected>$dayStr</option> ";
	}elseif($i == 0 && !isset($date)){
		echo "<option value=\"$dayStr\" selected>$dayStr</option> ";
		$date=$dayStr;
	}else{
		echo "<option value=\"$dayStr\">$dayStr</option> ";
	}
}
?>
    </select>
    </td>
<td align="right">
<nobr>
<div onClick="window.print();" style="cursor:hand">
&nbsp;<img border="0" src="../images/printer.gif" >&nbsp; Print &nbsp;
</div>
</nobr>
</td>
  </tr>      
</table>
<br>
<?php
//删掉本目录中早期产生的图片
$directory="./";
$mydir=dir($directory); 
$delTimestamp=mktime()-600;//十分钟前
while($file=$mydir->read()){ 
	//echo "<br> $file  ";
	//echo filectime($file);
	if(strstr($file,'.png') 
		&& filectime($file) < $delTimestamp){
		if($DEBUG) echo "<br>DELETE : $file";
		@unlink($file);
	}		    
} 
$mydir->close();

//产生随机熟用于做文件名
mt_srand((double)microtime()*1000000);
$randval = mt_rand();

if(!isset($date)){
	if($DEBUG) echo "<br>Default is Today";
	$date=date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")));
}
if($DEBUG) echo "<br>date=$date";
$dateArr	= explode('-',$date);

//========================================================================
//						CDR Daily Report
//========================================================================
$db='95DB_CDR';
$tableName = "95dbCdr_{$dateArr[0]}_{$dateArr[1]}_{$dateArr[2]}";
$sumSql	= "SELECT operation,count(charge) as conCharge,sum(charge) as sumCharge
			FROM $tableName GROUP BY operation ORDER BY operation";
if($DEBUG) echo "<br>sumSql=$sumSql";
$sumResult	= @mysqli_query($pubConn,$sumSql);

$errorStr	= mysqli_error($pubConn); 
if(strstr($errorStr,"doesn't exist")){
	echo "<br>This day's record doesn't exist";
	exit(0);
}

$sumChargeArr=array(0,0,0);
$conChargeArr=array(0,0,0);
while($sumRows	= @mysqli_fetch_array($sumResult)){
	$i=$sumRows[operation];
	$sumChargeArr[$i]=$sumRows[sumCharge];
	$conChargeArr[$i]=$sumRows[conCharge];
}
echo "<table border=\"1\" width=\"640\" bordercolordark=\"#FFFFFF\" bordercolor=\"#808080\" cellspacing=\"0\">";
echo "  <tr bgcolor=\"#E6E6E6\" align=\"left\">";
echo "  <td colspan=\"3\">Charge</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td width=\"40%\" align=\"center\">Operation Type</td>";
echo "    <td width=\"30%\" align=\"center\">Total Times</td>";
echo "    <td width=\"30%\" align=\"center\">Total Charge</td>";
echo "  </tr>";
for($i=0;$i<sizeof($subsOperDefArr);$i++){
	echo "  <tr>";
	echo "    <td align=\"left\">$subsOperDefArr[$i]&nbsp;</td>";
	echo "    <td align=\"right\">$conChargeArr[$i]&nbsp;</td>";
	echo "    <td align=\"right\">$sumChargeArr[$i]&nbsp;</td>";
	echo "  </tr>";
}
echo "</table>";


//========================================================================
//						CSTA Daily Report
//========================================================================
$db='95DB_CSTA';

$tableName = "95dbCsta_{$dateArr[0]}_{$dateArr[1]}_{$dateArr[2]}";
$sumSql	= "SELECT sum(active) as sumActive,sum(webVisit) as sumWebVisit,
			sum(dnRequ) as sumDnRequ,
			sum(dnSucc) as sumDnSucc,
			sum(dnFailNoCharge) as sumDnFailNoCharge,
			sum(dnFailNetFail) as sumDnFailNetFail,
			sum(dnFailFileExists) as sumDnFailFileExists,
			sum(dnFailAuthFail) as sumDnFailAuthFail,
			sum(dnFailOther) as sumDnFailOther,
			sum(upRequ) as sumUpRequ,
			sum(upSucc) as sumUpSucc,
			sum(upFailDataError) as sumUpFailDataError,
			sum(upFailNetFail) as sumUpFailNetFail,
			sum(upFailAuthFail) as sumUpFailAuthFail,
			sum(upFailOther) as sumUpFailOther
			FROM $tableName ";
if($DEBUG) echo "<br>sumSql=$sumSql";
$sumResult	= @mysqli_query($pubConn,$sumSql);
$sumRows	= @mysqli_fetch_array($sumResult);
$errorStr	= mysqli_error($pubConn); 
if(strstr($errorStr,"doesn't exist")){
	echo "<br>This day's record doesn't exist";
	exit(0);
}
//坐标转换函数，将数值转换成坐标
function getNewXY($pointArr)
{
	global	$xBgn,$yBgn,$xEnd,$yEnd,$maxYNum,$unitXPix;
	$DEBUG=0;
	for($i=0;$i<sizeof($pointArr);$i=$i+2){
		$pointArr[$i]	= $xBgn + $pointArr[$i] * $unitXPix;
		if($DEBUG) echo "<br>pointArr[$i]={$pointArr[$i]}";
		$pointArr[$i+1]	= $yEnd - round(($yEnd - $yBgn) * ($pointArr[$i+1] / $maxYNum)) - 1;
		if($DEBUG) echo "<br>pointArr[$i+1]={$pointArr[$i+1]}";
	}
	$pointArr[$i]	= $pointArr[$i-2];
	if($DEBUG) echo "<br>pointArr[$i]={$pointArr[$i]}";
	$pointArr[$i+1]	= $yEnd-1;
	if($DEBUG) echo "<br>pointArr[$i+1]={$pointArr[$i+1]}";
	$pointArr[$i+2]	= $pointArr[0] * $unitXPix;
	$pointArr[$i+3]	= $yEnd;
	return($pointArr);
}

echo "<br><br>";
$sql	= "SELECT * FROM $tableName ORDER BY timeNo";
if($DEBUG) echo "<br>sql=$sql";

//=============================User=======================================
$result	= @mysqli_query($pubConn,$sql);
require("./cstaImgUser.php");

echo "<table border=\"1\" width=\"640\" bordercolordark=\"#FFFFFF\" bordercolor=\"#808080\" cellspacing=\"0\">";
echo "  <tr bgcolor=\"#E6E6E6\" align=\"left\">";
echo "  <td colspan=\"4\">User</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td align=\"center\" rowspan=\"10\" width=\"40%\"><img src=\"$imgFileName\"></td>";
echo "    <td align=\"center\">&nbsp;</td>";
echo "    <td align=\"center\">Total</td>";
echo "    <td align=\"center\">%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Web Visit</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumWebVisit]</td>";
echo "    <td align=\"right\">&nbsp;100%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "</table>";
echo "<br><br>";

//=============================Download=====================================
$result	= @mysqli_query($pubConn,$sql);
require("./cstaImgDownload.php");

echo "<table border=\"1\" width=\"640\" bordercolordark=\"#FFFFFF\" bordercolor=\"#808080\" cellspacing=\"0\">";
echo "  <tr bgcolor=\"#E6E6E6\" align=\"left\">";
echo "  <td colspan=\"4\">Download</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td align=\"center\" rowspan=\"10\" width=\"40%\"><img src=\"$imgFileName\"></td>";
echo "    <td align=\"center\">&nbsp;</td>";
echo "    <td align=\"center\">Total</td>";
echo "    <td align=\"center\">%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Request</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumDnRequ]</td>";
echo "    <td align=\"right\">&nbsp;100%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Success</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumDnSucc]</td>";
$tmpPercent = getPercent($sumRows[sumDnRequ],$sumRows[sumDnSucc]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>No Charge</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumDnFailNoCharge]</td>";
$tmpPercent = getPercent($sumRows[sumDnRequ],$sumRows[sumDnFailNoCharge]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Net Failure</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumDnFailNetFail]</td>";
$tmpPercent = getPercent($sumRows[sumDnRequ],$sumRows[sumDnFailNetFail]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>File Exist</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumDnFailFileExists]</td>";
$tmpPercent = getPercent($sumRows[sumDnRequ],$sumRows[sumDnFailFileExists]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Auth. Failure</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumDnFailAuthFail]</td>";
$tmpPercent = getPercent($sumRows[sumDnRequ],$sumRows[sumDnFailAuthFail]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Other Failure</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumDnFailOther]</td>";
$tmpPercent = getPercent($sumRows[sumDnRequ],$sumRows[sumDnFailOther]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "</table>";

echo "<br><br>";

//=============================Upload=======================================
$result	= @mysqli_query($pubConn,$sql);
require("./cstaImgUpload.php");

echo "<table border=\"1\" width=\"640\" bordercolordark=\"#FFFFFF\" bordercolor=\"#808080\" cellspacing=\"0\">";
echo "  <tr bgcolor=\"#E6E6E6\" align=\"left\">";
echo "  <td colspan=\"4\">Upload</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td align=\"center\" rowspan=\"10\" width=\"40%\"><img src=\"$imgFileName\"></td>";
echo "    <td align=\"center\">&nbsp;</td>";
echo "    <td align=\"center\">Total</td>";
echo "    <td align=\"center\">%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Request</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumUpRequ]</td>";
$tmpPercent = getPercent($sumRows[sumUpRequ],$sumRows[sumUpRequ]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Success</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumUpSucc]</td>";
$tmpPercent = getPercent($sumRows[sumUpRequ],$sumRows[sumUpSucc]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Data Error</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumUpFailDataError]</td>";
$tmpPercent = getPercent($sumRows[sumUpRequ],$sumRows[sumUpFailDataError]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Net Failure</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumUpFailNetFail]</td>";
$tmpPercent = getPercent($sumRows[sumUpRequ],$sumRows[sumUpFailNetFail]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Auth. Failure</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumUpFailAuthFail]</td>";
$tmpPercent = getPercent($sumRows[sumUpRequ],$sumRows[sumUpFailAuthFail]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>Other Failure</td>";
echo "    <td align=\"right\">&nbsp;$sumRows[sumUpFailOther]</td>";
$tmpPercent = getPercent($sumRows[sumUpRequ],$sumRows[sumUpFailOther]);
echo "    <td align=\"right\">&nbsp;$tmpPercent%</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "  <tr>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "    <td>&nbsp;</td>";
echo "  </tr>";
echo "</table>";
//==========================================================================
$dayStr=date("Y-m-d H:i:s",mktime());
echo "<br>&nbsp;&nbsp;&nbsp;&nbsp;Print Time:&nbsp;$dayStr ";
echo "&nbsp;&nbsp;&nbsp;&nbsp;Print User:&nbsp;$userInfo[ID] <br><br>";
echo "</body>";
?>