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

$iActive = $_GET['iActive'];
$sDate = $_POST['sDate'];
$tDate = $_POST['tDate'];
$iStatus = $_POST['iStatus'];

$backUrl = GetUrl();
?>
 <?
	$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%')";
				//" dtExpiry like '%$keyword%')";
	}
	if($iActive!=""){
		 $wheres=" AND iStatus='$iActive'";
	}
	
	if($iStatus!=""){
		 $wheres=" AND iStatus='$iStatus'";
	}
	if($fUserName){
		 $wheres=" AND UserName='$fUserName'";
	}
	//按日期搜索
	   if($to){
		  $wheres=" AND (dtCreate between '$from' and  '$to' OR SUBSTRING(dtCreate,1,10)='$to')";
	   }
	   if($from){
		  $wheres=" AND (dtCreate >='$from')";
	   }

	//查找$day天后要续贯的记录
	if($op=="needrenew")$wheres=" AND Amount>0 AND iStatus=1 AND iReceive=1 AND iRefee=0 AND iPActive>1 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 Amount>0 $wheres";
	$rs=$DB->query($sql);
	$totalPAmount=abs($DB->result($rs,0,0));
	//当前条件下负数总金额
	$sql="select sum(Amount) from financial where Amount<0 $wheres";
	$rs=$DB->query($sql);
	$totalAmount=abs($DB->result($rs,0,0));
	//得到总记录数
	$sql="select count(*) from financial where 1 $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 1 $wheres order by IDFinancial DESC limit $offset,$pageSize";
	$result=$DB->query($sql);
	$pageList=getPageList($_SERVER[PHP_SELF],"page","15",$totalPage,$page);
	$pageAmount=0;
	$pagePAmount=0;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>财务管理</title>
<LINK href="../share/style.css" rel=stylesheet>
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="/admin/lib/Hui-iconfont/1.0.7/iconfont.css" />
<link rel="stylesheet" type="text/css" href="/admin/lib/icheck/icheck.css" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/style.css" />
<script src="../share/global.js"></script>
<script src="../share/cal.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">
<form method='post' action='<?=$_SERVER['PHP_SELF']?>'>
<? include "../share/cal.php"?>
<select name='iStatus'>
   <option value="">不限</option>
   <option value="0" <? if($iStatus=="0")echo "selected"?>>未生效</option>
   <option value="1" <? if($iStatus=="1")echo "selected"?>>已生效</option>
</select>
<select name="fType">
   <option value="">不限</option>
   <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">
<input type="submit" value="搜索" class="bt2">
</form>
</div>

<form method='post' action='financial_del.php' onSubmit="return confirm('删除后不可恢复,确认吗')">
<div id="page">
<?
	$pageStr='<div id="dels"><input type="submit" value="删除选中" class="bt2"></div>';
	$pageStr.="共 $totalRecord 条记录,共 $totalPage 页,当前第 $page 页";
	echo $pageStr;
?>
</div>
<input type="hidden" name="backUrl" value="<?=GetUrl()?>">
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="table table-border table-bordered table-bg table-hover table-sort">
  <tr id="tr">
	<td><input type="checkbox" value="1" onClick="selectAll(this.form,this,'IDFinancial[]')" class='checkbox'></td>
	<td>编号</td>
	<td>用户</td>
	<td>描述</td>
	<td>金额</td>
	<td>已生效</td>
	<td>建立时间</td>
	<td>操作/管理</td>
  </tr>
  <? while($FInfo=$DB->fetch_array($result)){
	 if($FInfo[Amount]>0)$pagePAmount+=abs($FInfo[Amount]);
	 elseif($FInfo[Amount]<0)$pageAmount+=abs($FInfo[Amount]);
	 $fkey = GetHashKey('financial'.$FInfo[IDFinancial]);
	 $ukey = GetHashKey('users'.$FInfo[UserName]);
  ?>
  <tr id="td" onMouseOut="outStyle(this)" onMouseOver="inStyle(this)">
	<td>
	  <?
		 if($FInfo[iStatus]==0){
			echo "<input type='checkbox' name='IDFinancial[]' value='$FInfo[IDFinancial]' class='checkbox'>";
		 }else{
			echo "&nbsp;";
		 }
	  ?>
	 </td>
	<td>
	<a href='financial_edit.php?IDFinancial=<?=$FInfo[IDFinancial]?>&backUrl=<?=$backUrl?>&key=<?=$fkey?>'><?=$FInfo[IDFinancial]?></a>
	</td>
	<td>
	<a href='../users/user_edit.php?UserName=<?=$FInfo[UserName]?>&backUrl=<?=$backUrl?>&key=<?=$ukey?>'><?=$FInfo[UserName]?></a>
	</td>
	<td style="text-align:left"><?=$FInfo[About]?>&nbsp;</td>
	<td><?=$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"){?>|
	<a href='financial_del.php?IDFinancial=<?=$FInfo[IDFinancial]?>&backUrl=<?=$backUrl?>&key=<?=$fkey?>' onClick="return confirm('确定删除吗?')">删除</a><? }?>
	</td>
  </tr>
  <? }?>
</table>
<div id="page">
<?
	echo $pageStr;
?>
</div>
<center>
<?		  
echo "当前条件下入帐总金额为<font color='red'> $totalPAmount </font>元,出帐为<font color='red'> $totalAmount </font>元 | ";
echo "当前页入帐共<font color='red'> $pagePAmount </font>元，出帐为<font color='red'> $pageAmount </font>元";
if($pageList) echo "$pageList[0] <br> $pageList[1]";
?>
</center>
</form>
</div>
<? 
$DB->close();
include "../share/bottom.php";
?>
</body>
</html>
