<?
session_start();
require_once "../init.php";
require_once "../authority.inc.php";

$UserName = $_GET['UserName'];
$key = $_POST['key'];

$backUrl = $backUrl ? $backUrl:$_SERVER["HTTP_REFERER"];
//-------------------------------------------------
//查询当前用户的余额,只生效的记录
$sql="select sum(Amount) from financial where UserName='$UserName' AND iStatus=1 AND Amount>0 $wheres";
$rem=$DB->getOne($sql); //入款总数
$sql="select sum(Amount) from financial where UserName='$UserName' AND iStatus=1 AND Amount<0 $wheres";
$rem2=$DB->getOne($sql); //支出总数

$remain=abs($rem)-abs($rem2);
//-------------------------------------------------
$keyword=trim($keyword);
if($keyword){
	$wheres .=" AND (IDFinancial like '%$keyword%' or ".
			" UserName like '%$keyword%' or ".
			" Amount like '%$keyword%' or ".
			" About like '%$keyword%' or ".
			" dtCreate like '%$keyword%' or ".
			" dtExpiry like '%$keyword%')";
}elseif($iStatus!=""){
	 $wheres .=" AND iStatus='$iStatus'";
}
//按日期搜索
if($fromYear){
   if(!$fromMonth)$fromMonth=1;
   if(!$fromDay)$fromDay=1;
   $from=$fromYear."-".$fromMonth."-".$fromDay;
   if($toYear){
	  if(!$toMonth)$toMonth=12;
	  if(!$toDay)$toDay=31;
	  $to=$toYear."-".$toMonth."-".$toDay;
   }
   if($to){
	  $wheres=" AND (dtCreate between '$from' and  '$to' OR SUBSTRING(dtCreate,1,10)='$to')";
   }elseif($from){
	  $wheres=" AND (dtCreate >='$from')";
   }
}
//查找$day天后要续贯的记录
if($op=="needrenew")$wheres=" AND iStatus=1 AND iReceive=1 AND iRefee=0 AND dtExpiry<DATE_ADD(NOW(),INTERVAL $day DAY)";
//设置搜索正数还是负数的记录
if($fType>0)$wheres.=" AND Amount>0";
elseif($fType<0)$wheres.=" AND Amount<0";
//当前条件下正数总金额
$sql="select sum(Amount) from financial where UserName='$UserName' AND Amount>0 AND iStatus=1 $wheres";
$rs=$DB->query($sql);
$totalAmountP=abs($DB->result($rs,0,0));
//当前条件下负数总金额
$sql="select sum(Amount) from financial where UserName='$UserName' AND Amount<0 AND iStatus=1 $wheres";
$rs=$DB->query($sql);
$totalAmount=abs($DB->result($rs,0,0));
//得到总记录数
$sql="select count(*) from financial where UserName='$UserName' $wheres";
$rs=$DB->query($sql);
$pageSize=30;
$totalRecord=$DB->result($rs,0,0);
$totalPage=ceil($totalRecord/$pageSize);
if(!$page)$page=1;
$offset=($page-1)*$pageSize;
//查询
$sql="select * from financial where UserName='$UserName' $wheres order by dtCreate desc limit $offset,$pageSize";
$result=$DB->query($sql);
$pageList=getPageList($_SERVER[PHP_SELF],"page","15",$totalPage,$page);
$pageAmountP=0;
$pageAmount=0;
//--------------------------------------------------------------------
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><?=$UserName?>财务管理</title>
<LINK href="../share/style.css" rel=stylesheet>
<script src='../share/global.js'></script>
</head>
<body>
<? include "../share/top.php"?>
<div id="topLink">
<div id="topInfo">
	<span class="tTitle">财务记录</span>
	<span class="sarr"> >> </span><a href="<?=$GLOBALS["parentpath"]?>">返回到管理首页</a>
</div>
</div><div class="page-container">
<!--子导航-->
<? include "financial_nav.inc.php"?>
<!--结束-->

<div id="search">
<div style="float:left;">
&nbsp;&nbsp;当前用户:<a href='../users/user_edit.php?UserName=<?=$UserName?>&key=<?=GetHashKey('users'.$UserName)?>'><?=$UserName?></a> -
<a href='<?=$_SERVER[PHP_SELF]?>?UserName=<?=$UserName?>&backUrl=<?=urlencode($backUrl)?>'>该用户财务记录</a>
 <forms method='post' action='update_remain.php'>
	<!--用户余额：<input type="text" name="iMoney" size='10' maxlength='10' class="inputbox" value="<?=$UserI[iMoney]?>">-->
	财务记录的余额为:
	<?
	  if($remain<0)echo "<font color='red'>$remain</font> (欠费)";
	  else echo "<font color='green'>$remain</font>";
	  //if($UserI[iMoney]!=$remain)echo " <font color='red'>财务状态出错</font>";
	?>
	<input type="hidden" name="UserName" value="<?=$UserName?>">
	<input type="hidden" name="backUrl" value="<?=$backUrl?>">
	<!--<input type="submit" value="修改" class="button">-->
 </forms> -
<? echo "<a href='financial_add.php?UserName=$UserName&ukey=".md5($UserName)."&backUrl=".urlencode($backUrl)."'><b style=color:Red>财务记录录入</b></a>";?>
</div>
<form method='post' action='<?=$_SERVER['PHP_SELF']?>'>
<? include "../share/cal.php"?>
<select name='op'>
   <option value="">不限</option>
   <option value="iStatus" <? if($op=="iStatus")echo "selected"?>>已激活</option>
   <option value="iReceive" <? if($op=="iReceive")echo "selected"?>>已到款</option>
</select>
<select name="fType">
   <option value="1" <? if($fType==1)echo "selected"?>>正数记录</option>
   <option value="-1" <? if($fType==-1)echo "selected"?>>负数记录</option>
</select>
<input type="text" name="keyword" class="input" size="20">
<input type="hidden" name="UserName" value="<?=$UserName?>">
<input type="hidden" name="backUrl" value="<?=$backUrl?>">
<input type="submit" value="搜索" class="bt2">
</form>
</div>
<div id="page">
<?
	$pageStr.="共 $totalRecord 条记录,共 $totalPage 页,当前第 $page 页";
	echo $pageStr;
?>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="table" class="table table-border table-bordered table-bg table-hover table-sort">
		  <tr id="tr">
			<td>编号</td>
			<td>用户</td>
			<td>说明</td>
			<td>金额</td>
			<td>已生效</td>
			<td>建立时间</td>
			<td>操作/管理</td>
		  </tr>
		  <? while($FInfo=$DB->fetch_array($result)){
		      if($FInfo[iStatus]==1){
				 if($FInfo[Amount]<0)$pageAmount+=abs($FInfo[Amount]);
				 if($FInfo[Amount]>0)$pageAmountP+=abs($FInfo[Amount]);
			  }
			  $fkey = GetHashKey('financial'.$FInfo[IDFinancial]);
			  $ukey = GetHashKey('users'.$FInfo[UserName]);
		  ?>
		  <tr id="td" onmouseout="outStyle(this)" onmouseover="inStyle(this)">
			<td align='left'><a href='financial_edit.php?IDFinancial=<?=$FInfo[IDFinancial]?>&backUrl=<?=$backUrl?>&key=<?=$fkey?>'><?=$FInfo[IDFinancial]?></a></td>
			<td align='left'><a href='../users/user_edit.php?UserName=<?=$FInfo[UserName]?>&backUrl=<?=$backUrl?>&key=<?=$ukey?>'><?=$FInfo[UserName]?></a></td>
			<td>&nbsp;<?=$FInfo[About]?>&nbsp;</td>
			<td>
		   <?
			   if($FInfo[Amount]<0)echo "<font color='red'>$FInfo[Amount]</font>";
			   else echo "$FInfo[Amount]"; 
		   ?>
			</td>
			<td><?=$FInfo[iStatus]=="1"?"<font color='green'>是</font>":"<font color='red'>否</font>"?></td>
			<td><?=$FInfo[dtCreate]?></td>
			<td>
			<a href='financial_edit.php?IDFinancial=<?=$FInfo[IDFinancial]?>&backUrl=<?=$backUrl?>&key=<?=$fkey?>'>编辑</a>
			<?
			  if($FInfo[iStatus]!=1)echo " - <a href='financial_del.php?IDFinancial[]=$FInfo[IDFinancial]&backUrl=".urlencode($backUrl)."'>删除</a>";
			?>
			</td>
		  </tr>
		  <? }?>
		</table>
<div id="page">
<?
	echo $pageStr;
?>
</div>
<center>
<?		  
echo "当前条件下入款总金额为<font color='red'> $totalAmountP </font>元,支出总金额为 <font color='red'> $totalAmount </font>元";
if($pageList) echo "$pageList[0] <br> $pageList[1]";
echo "<br><br>";
?>
</center></div>
<? 
$DB->close();
include "../share/bottom.php";
?>
</body>
</html>
