<?php
 /**
 *      [XCECP] (C) COLNET Inc.
 *      This is NOT a freeware, use is subject to license terms
 */
if(!defined("IN_QKWEB")){exit("Access Denied");}
function smarty_block_finance($params, $content, $smarty, &$repeat)
{ 
	
  	if(!isset($params['name']) || $params['name']==""){
		$params['name'] = "item";
	}
	$dataindex = md5(__FUNCTION__ . md5(serialize($params)));
	if(!isset($smarty->block_data)){$smarty->block_data=array();}
	if(!isset($smarty->block_data[$dataindex])){
		$page = getgpc("page");
		$wheres = "";
		if(isset($params['limit']) && $params['limit']!="" && settype($params['limit'],"integer")){
			$limit = $params['limit'];
		}else{
			$limit = 10;
		}
		$pageSize = $limit;
		$smarty->assign("limit",$limit);
		$limit = "limit $limit";
		$page = getgpc("page");
		if(!$page || !settype($page,"integer")){$page=1;}
		$offset = ($page-1)*$pageSize;
		$limit = "limit $offset,$pageSize";
		if($page && settype($page,"integer")){
			$offset = ($page-1)*$pageSize;
			$limit = "limit $offset,$pageSize";
		}

		$keyTypeId = getgpc("keyTypeId");
		$key = getgpc("key");
		if($key){
				$wheres .= " and (about like '%$key%' or remark like '%$key%'  or outTradeNo = '$key' )";
		}
		if(isset($params['userId']) && $params['userId']!="" && settype($params['userId'],"integer")){
			$userId = $params['userId'];
		}else{
			$userId = getgpc('userId');
		}
		
		if($userId && is_numeric($userId)){
			$wheres .= "and userId='$userId'";
		}

		if(isset($params['typeId']) && $params['typeId']!="" && settype($params['typeId'],"integer") && intval($params['typeId'])){
			$typeId = $params['typeId'];
			$wheres .= "and typeId='$typeId'";
		}
		if(isset($params['iStatus']) && $params['iStatus']!="" && settype($params['iStatus'],"integer") && intval($params['iStatus'])){
			$iStatus = $params['iStatus'];
			$wheres .= "and iStatus='$iStatus'";
		}

		if(isset($params['email']) && $params['email']!=""){
			$sql = "select id from qk_user where email='".$params['email']."'";
			$idArr = $GLOBALS['db']->getAll($sql);
			if(is_array($idArr)){
				$i=0;
				$ids = "";
				foreach($idArr as $item){
					$ids.=$item['id'];
					$i++;
					if($i<count($idArr)){$ids.=",";}
				}
				if($ids){
					$wheres .= " and userId in ($ids)";
				}
			}
		}

		$allCount = $GLOBALS['db']->getOne("select count(id) from qk_finance where 1=1 $wheres");
		$smarty->assign("allCount",$allCount);
		
		$allAmountDeposit = 0;
		$allAmountDeposit = $GLOBALS['db']->getOne("select sum(amount) from qk_finance where amount>0 $wheres");
		$smarty->assign("allAmountDeposit",$allAmountDeposit);
		$allAmountDebit = 0;
		$allAmountDebit = $GLOBALS['db']->getOne("select sum(amount) from qk_finance where amount<0  $wheres");
		$smarty->assign("allAmountDebit",$allAmountDebit);
		$allAmount = 0;
		$allAmount = $GLOBALS['db']->getOne("select sum(amount) from qk_finance where 1=1 $wheres");
		$smarty->assign("allAmount",$allAmount);

		$res = $GLOBALS['db']->getAll("select * from qk_finance where 1=1 $wheres order by id desc $limit");
		$i = 0;
		load_class("user");
		$um = new UserModel();
		foreach($res as $k=>$v){
			$res[$i]['type'] = $GLOBALS['db']->getOne("select name from qk_finance_type where id='".$v['typeId']."'");
			$res[$i]['userInfo'] = $um->getInfo($v['userId']);
			$res[$i]['index'] = $i;
			$i++;
		}
		$smarty->block_data[$dataindex]=$res;
	}
	if(is_array($smarty->block_data[$dataindex]) && $smarty->block_data[$dataindex]){
		$item = array_shift($smarty->block_data[$dataindex]);
		$smarty->assign($params['name'], $item);
		if(count($smarty->block_data[$dataindex])==0){
			$smarty->block_data[$dataindex] = false;
		}
		$repeat= true;
	}else{
		unset($smarty->block_data[$dataindex]);
		$repeat = false;
	}
	echo $content;
}

?>