<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：QQ877873666
 邮箱：877873666@qq.com

 **** 版权所有 **** */


//用户检测
$MB->Open(',MB_realname','login',10);


switch($mudi){
	case 'addOrRev':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		AddOrRev();
		break;

	case 'send':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		send();
		break;

	case 'del':
//		del();
		break;

	case 'addMoney':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		AddMoney();
		break;

	case 'delRecord':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		DelRecord();
		break;

	case 'revData':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		RevData();
		break;

	case 'moneyTimeAdd':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		MoneyTimeAdd();
		break;

	case 'moreSet':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		MoreSetDeal();
		break;

	case 'export':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		ExportDeal();
		break;

	case 'stati':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		stati();
		break;

	case 'statiDay':
		$menuFileID = 265;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		StatiDay();
		break;

	default:
		die('err');
}

$MB->Close();
$DB->Close();





// 添加/修改进出账
function AddOrRev(){
	global $DB,$MB,$mudi,$menuFileID,$menuTreeID,$memberexe;

	$backURL	= OT::PostStr('backURL');
	$dataMudi	= OT::PostStr('dataMudi');
	$dataType	= OT::PostStr('dataType');
	$dataTypeCN	= OT::PostStr('dataTypeCN');

	$dataID		= OT::PostInt('dataID');
	$userID		= OT::PostInt('userID');
	$username	= OT::PostStr('username');

	$mode		= OT::PostInt('mode');
	$money		= OT::PostInt('money',-1);
	$isXiaofei	= OT::PostInt('isXiaofei');
	$isGive		= OT::PostInt('isGive');
	$note		= Str::MoreReplace(rtrim(OT::Post('note')),'html');

	if ($mode==0 || $money<0){
		JS::AlertBackEnd('金额填写错误（未填或填写数据非数值型）.');
	}

	if ($note == ''){
		JS::AlertBackEnd('备注都不能为空.');
	}

	if ($mode == 2){ $money = $money*(-1); }

	if ($dataMudi == 'add'){
		$menuFileID = 77;
//		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);

		$userArr=array();
		$userrec=$DB->query('select UE_money from '. OT_dbPref .'users where UE_ID='. $userID);
			if (! $row = $userrec->fetch()){
				JS::AlertBackEnd('指定用户ID错误.');
			}
			$UE_money	= floatval($row['UE_money']);
		unset($userrec);
		$remMoney = $UE_money + $money;
		if ($money >= 0){
			$userArr['UE_money'] = 'UE_money+'. $money;
			if ($isGive == 1){ $userArr['UE_moneyGive'] = 'UE_moneyGive+'. $money; }
		}else{
			$userArr['UE_money']= 'UE_money'. $money;
			if ($isXiaofei == 1){ $userArr['UE_payMoney'] = 'UE_payMoney+'. $money*(-1); }
		}

		$record=array();
		$record['MR_time']			= TimeDate::Get();
		$record['MR_type']			= 'diy';
		$record['MR_type2']			= '';
		$record['MR_userID']		= $userID;
		$record['MR_username']		= $username;
		$record['MR_money']			= $money;
		$record['MR_remMoney']		= $remMoney;
		$record['MR_isGive']		= $isGive;
		$record['MR_cost']			= 0;
		$record['MR_gain']			= $isGive == 1 ? 0 : $money;
		$record['MR_note']			= $note;
		$record['MR_bank']			= '后台';
		$record['MR_adminID']		= $MB->mMbRow['MB_ID'];
		$record['MR_adminName']		= $MB->mMbRow['MB_realname'];
		$record['MR_state']		= 1;
		$judRes = $DB->InsertParam('moneyRecord',$record);
		if ($judRes==true){
			$alertMode = '添加成功.';
			if (! $DB->UpdateParam('users',$userArr,'UE_ID='. $userID)){
				$alertMode = '添加失败.用户信息处理失败.';
				$DB->Delete('moneyRecord','MR_userID='. $userID .' and MR_adminID='. $record['MR_adminID'] .' and MR_time='. $record['MR_time']);
			}
		}else{
			$alertMode = '添加失败.';
		}
	}else{
		$menuFileID = 78;
//		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);

		$judRes = $DB->UpdateParam('moneyRecord',array('MR_isGive' => $isGive, 'MR_note' => $note),'MR_ID='. $dataID);
			if ($judRes){
				$alertMode = '修改成功.';
			}else{
				$alertMode = '修改失败.';
			}
	}

	Adm::AddLog(array(
		'theme'	=> $username .'，ID号：'. $userID,
		'note'	=> '【'. $dataTypeCN .'】'. $alertMode .'！',
		));

	JS::AlertHrefEnd('进出账'. $alertMode .'',$backURL);
}



// 发送进出账信息
function send(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$dataID = OT::GetInt2( OT::ParaArr('id') );

	$checkexe=$DB->query('select * from '. OT_dbPref .'moneyRecord where MR_ID='. $dataID);
	if ($row = $checkexe->fetch()){
		if ($row['MR_adminID']==0){
			JS::AlertEnd('该记录是由'. $row['MR_adminName'] .'自动生成的,禁止修改.');
		}
		echo('
		<script language="javascript" type="text/javascript">
		parent.$id("money").readOnly=true;
		parent.$id("dataMudi").value="rev";
		parent.$id("dataID").value="'. $row['MR_ID'] .'";
		parent.$id("userID").value="'. $row['MR_userID'] .'";
		parent.$id("username").value="'. $row['MR_username'] .'";
			try {
		');
		if ($row['MR_money'] > 0){
			echo('
			parent.$name("mode")[0].disabled=true;
			parent.$name("mode")[1].disabled=false;
			parent.$name("mode")[2].disabled=true;
			parent.$name("mode")[1].checked=true;
			parent.$id("moneyK").style.display="";
			');
		}elseif ($row['MR_money'] < 0){
			echo('
			parent.$name("mode")[0].disabled=true;
			parent.$name("mode")[1].disabled=true;
			parent.$name("mode")[2].disabled=false;
			parent.$name("mode")[2].checked=true;
			parent.$id("moneyK").style.display="";
			');
		}else{
			echo('
			parent.$name("mode")[0].disabled=false;
			parent.$name("mode")[1].disabled=true;
			parent.$name("mode")[2].disabled=true;
			parent.$name("mode")[0].checked=true;
			parent.$id("moneyK").style.display="none";
			');
		}
		echo('
			}catch (e){}

		parent.$id("money").value="'. abs($row['MR_money']) .'";
		parent.$id("money").focus();
		parent.$id("isGive'. $row['MR_isGive'] .'").checked=true;
		parent.$id("note").value="'. Str::MoreReplace(Str::MoreReplace($row['MR_note'],'deHtml'),'js') .'";
		parent.$id("subButton").src="'. AdmTpl::ImgSrc('button_rev') .'";
		parent.$id("alertStr").innerHTML="提示：修改状态仅允许修改[汇款信息]和[备注]；<br />&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;如添加了错误记录，则可以通过添加一条相反记录进行冲销；";
		</script>
		');
	}
	unset($checkexe);
}



// 删除进出账信息
function del(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$dataID		= OT::GetInt2( OT::ParaArr('id') );
	$userID		= OT::GetInt('userID');
	$theme		= OT::GetStr('theme');

	if ($dataID<=0 || $userID<=0){
		JS::AlertEnd('指定ID错误.');
	}

	$judRes = $DB->query('delete from '. OT_dbPref .'moneyRecord where MR_ID='. $dataID .' and MR_userID='. $userID);
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'theme'	=> $theme,
		'note'		=> '【'. $dataTypeCN .'】删除'. $alertRes .'！',
		));
	
	JS::ParentHiddenEnd('data'. $dataID);
}



// 不平衡纠正
function AddMoney(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$user_ID,$user_realname;

	$backURL	= OT::PostStr('backURL');
	$dataType	= OT::PostStr('dataType');
	$dataTypeCN	= OT::PostStr('dataTypeCN');

	$money		= OT::PostFloat('money');
	$endTime	= OT::PostStr('endTime');
	$note		= OT::PostStr('note');
		if (! strtotime($endTime)){
			JS::AlertBackEnd('末尾时间不是时间格式.');
		}

	$judRes = $DB->InsertParam("moneyRecord",array(
		'MR_userID'		=> 0,
		'MR_username'	=> '',
		'MR_time'		=> $endTime,
		'MR_orderId'	=> '',
		'MR_money'		=> $money,
		'MR_remMoney'	=> 0,
		'MR_bank'		=> '对账更正',
		'MR_note'		=> $note .'【更正时间：'. TimeDate::Get() .'】',
		'MR_adminID'	=> $user_ID,
		'MR_adminName'	=> $user_realname,
		'MR_state'		=> 1
		));

	if ($judRes){
		$alertRes = '成功';
		/* $DB->InsertParam('moneyTime',array(
			'MT_time'		=> TimeDate::Get(),
			'MT_date'		=> $endTime,
			'MT_remMoney'	=> -9,
			'MT_payMoney'	=> 0
			)); */
	}else{
		$alertRes = '失败';
	}

	Adm::AddLog(array(
		'theme'	=> '对账更正金额：'. $money,
		'note'		=> '【'. $dataTypeCN .'】对账更正记录添加'. $alertRes .'！',
		));

	JS::AlertHrefEnd('对账更正记录添加'. $alertRes,$backURL);
}



// 删除至少3个月前记录
function DelRecord(){
	global $DB,$mudi,$menuFileID,$menuTreeID;

	$backURL	= OT::PostStr('backURL');
	$dataType	= OT::PostStr('dataType');
	$dataTypeCN	= OT::PostStr('dataTypeCN');
	$refDate	= OT::PostStr('refDate');
		if (! strtotime($refDate)){ $refDate=''; }

	if ($refDate==''){
		JS::AlertBackEnd('删除日期不能没填.');
	}
	if (TimeDate::Add('m',3,$refDate) > TimeDate::Get('date')){
		JS::AlertBackEnd('记录删除仅允许是3个月前的记录.');
	}

	$judRes = $DB->Delete('moneyRecord','MR_time<'. $DB->ForTime($refDate));
		if ($judRes){
			$alertRes = '成功';
			// $DB->Delete('moneyTime','MT_time<'. $DB->ForTime($refDate));
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'theme'	=> ''. $refDate .'之前的记录',
		'note'	=> '【'. $dataTypeCN .'】定日期删除'. $alertRes .'！',
		));

	JS::AlertHrefEnd('删除'. $alertRes,$backURL);
}



// 修改详情数据
function RevData(){
	global $DB,$MB,$mudi,$menuFileID,$menuTreeID,$memberexe;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataID			= OT::PostInt('dataID');
	$cost			= OT::PostFloat('cost');
	$gain			= OT::PostFloat('gain');

	if ($dataID<=0){
		JS::AlertBackEnd('表单信息不全.');
	}

	$record=array();
	$record['MR_cost']	= $cost;
	$record['MR_gain']	= $gain;

	$judRes = $DB->UpdateParam('moneyRecord', $record, 'MR_ID='. $dataID);
		if ($judRes){
			$alertMode = '成功.';
		}else{
			$alertMode = '失败.';
		}

	Adm::AddLog(array(
		'note'	=> '【'. $dataTypeCN .'】修改成本/利润'. $alertMode .'！',
		));

	// JS::AlertHrefEnd('修改成本/利润'. $alertMode .'',$backURL);
	JS::AlertCloseEnd('修改成本/利润'. $alertMode);
}



// 添加财务时间金额信息
function MoneyTimeAdd(){
	global $DB,$MB,$mudi,$menuFileID,$menuTreeID,$memberexe;

	$backURL	= OT::GetStr('backURL');
	$dataType	= OT::GetStr('dataType');
	$dataTypeCN	= '进出账统计与导出';

	$remUserMoney	= OT::GetFloat('remUserMoney');
	$remMoney		= OT::GetFloat('remMoney');
	$remMoney0		= OT::GetFloat('remMoney0');
	$refDate1		= OT::GetStr('refDate1');

	if (! strtotime($refDate1)){
		JS::AlertBackEnd('时间格式错误.');
	}
	if (OT::FloatCmp($remUserMoney, $remMoney+$remMoney0) != 0){
		JS::AlertBackEnd('对账不平衡，不能标记平衡.');
	}
	$umRem1 = floatval($DB->GetOne('select MT_remMoney from '. OT_dbPref .'moneyTime where MT_time='. $DB->ForTime($refDate1)));
	$umRem2 = floatval($DB->GetOne('select sum(UE_money) from '. OT_dbPref .'users'));
	if (OT::FloatCmp($remUserMoney, $umRem2-$umRem1) != 0){
		JS::AlertBackEnd('会员余额错误，请刷新页面重新操作.');
	}

	$chkexe = $DB->query('select MT_remMoney from '. OT_dbPref .'moneyTime order by MT_ID DESC limit 1');
	if ($row = $chkexe->fetch()){
		if ($row['MT_remMoney'] == -8){
			JS::AlertBackEnd('已标记平衡，不要重复标记.');
		}
	}
	unset($chkexe);

	$record=array();
	$record['MT_time']		= TimeDate::Get();
	$record['MT_date']		= TimeDate::Get('date');
	$record['MT_remMoney']	= -8;
	$record['MT_payMoney']	= 0;

	$judRes = $DB->InsertParam('moneyTime', $record);
		if ($judRes){
			$alertMode = '成功.';
		}else{
			$alertMode = '失败.';
		}

	Adm::AddLog(array(
		'note'	=> '【'. $dataTypeCN .'】标记平衡'. $alertMode .'！',
		));

	JS::AlertHrefEnd('标记平衡'. $alertMode .'',$backURL);
}



// 批量设置
function MoreSetDeal(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN,$systemArr;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$moreSetTo		= OT::PostRegExpStr('moreSetTo','sql');
	$moreSetToCN	= OT::PostRegExpStr('moreSetToCN','sql');
	$selDataID		= OT::Post('selDataID');

	$selCount = OT::NewCount($selDataID);
	if ($selCount <= 0){
		JS::AlertBackEnd('请先选择要设置的记录.');
	}
	if ($moreSetTo==''){
		JS::AlertBackEnd('请选择批量设置的操作.');
	}

	$whereStr = '';
	for ($i=0; $i<$selCount; $i++){
		$whereStr .= ','. intval($selDataID[$i]);
	}
	if ($whereStr == ''){
		JS::AlertBackEnd('请先选择要设置的记录.');
	}

	switch ($moreSetTo){
		case 'state1':		$dealStr = 'MR_state=1';		break;
		case 'state0':		$dealStr = 'MR_state=0';		break;
		case 'userState1':	$dealStr = 'MR_userState=1';	break;
		case 'userState0':	$dealStr = 'MR_userState=0';	break;
		default :
			JS::AlertBackEnd('目的不明确.');
			break;
	}

	$judRes = $DB->query('update '. OT_dbPref .'moneyRecord set '. $dealStr .' where MR_ID in (0'. $whereStr .')');
	if ($judRes){
		$alertRes = '成功';
	}else{
		$alertRes = '失败';
	}

	Adm::AddLog(array(
		'note'		=> '【'. $dataTypeCN .'】批量设置成【'. $moreSetToCN .'】'. $alertRes .'！',
		));

	JS::AlertHref('批量设置成【'. $moreSetToCN .'】'. $alertRes .'.',$backURL);
}



// 后台导出 根据条件
function ExportDeal(){
	global $DB,$mudi,$menuFileID,$menuTreeID;

	$refType		= OT::PostRegExpStr('refType','sql');
	$refType2		= OT::PostRegExpStr('refType2','sql');
	$refUsername	= OT::PostRegExpStr('refUsername','sql');
	$refRealname	= OT::PostRegExpStr('refRealname','sql');
	$refState		= OT::PostInt('refState',-1);
	$refNote		= OT::PostRegExpStr('refNote','sql');
	$refBank		= OT::PostRegExpStr('refBank','sql');
	$refAdminName	= OT::PostRegExpStr('refAdminName','sql');
	$refMoney1		= OT::PostFloat('refMoney1');
	$refMoney2		= OT::PostFloat('refMoney2');
	$refDate1		= OT::PostStr('refDate1');
		if (! strtotime($refDate1)){ $refDate1=''; }
	$refDate2		= OT::PostStr('refDate2');
		if (! strtotime($refDate2)){ $refDate2=''; }	// else{ $refDate2=TimeDate::Add('d',1,$refDate2); }

	$SQLstr = 'select MR_ID,MR_adminID,MR_adminName,MR_isGive,MR_time,MR_type,MR_type2,MR_money,MR_remMoney,MR_cost,MR_gain,MR_userID,MR_username,MR_bank,MR_note,MR_state,MR_username from '. OT_dbPref .'moneyRecord where (1=1)';

	$refItemStr = '';
	if ($refType != ''){ 
		$SQLstr .= ' and MR_type='. $DB->ForStr($refType);
		$refItemStr .= '类型：'. PayInfo::RecordTypeCN($refType) .'；';
	}
	if ($refType2 != ''){ 
		$SQLstr .= ' and MR_type2 like "%'. $refType2 .'%"';
		$refItemStr .= '类型2：'. $refType2 .'；';
	}
	if ($refUsername != ''){ 
		$SQLstr .= ' and MR_username like "%'. $refUsername .'%"';
		$refItemStr .= '用户名：'. $refUsername .'；';
	}
	/* if ($refRealname != ''){ 
		$SQLstr .= ' and MR_realname like "%'. $refRealname .'%"';
		$refItemStr .= '真实姓名：'. $refRealname .'；';
	} */
	if ($refState >= 0){
		$SQLstr .= ' and MR_state='. $refState;
		$refItemStr .= '状态：'. ($refState == 1 ? '有效' : '作废') .'；';
	}
	if ($refMoney1 > 0){
		$SQLstr .= ' and MR_money>='. $refMoney1;
		$refItemStr .= '金额开始：'. $refMoney1 .'；';
	}
	if ($refMoney2 > 0){
		$SQLstr .= ' and MR_money<='. $refMoney2;
		$refItemStr .= '金额结束：'. $refMoney2 .'；';
	}
	if ($refNote != ''){ 
		$SQLstr .= ' and MR_note like "%'. $refNote .'%"';
		$refItemStr .= '备注：'. $refNote .'；';
	}
	if ($refBank != ''){ 
		$SQLstr .= ' and MR_bank like "%'. $refBank .'%"';
		$refItemStr .= '来源：'. $refBank .'；';
	}
	if ($refAdminName != ''){ 
		$SQLstr .= ' and MR_adminNote like "%'. $refAdminName .'%"';
		$refItemStr .= '操作员：'. $refAdminName .'；';
	}
	if ($refDate1 != ''){ 
		$SQLstr .= ' and MR_time>='. $DB->ForTime($refDate1);
		$refItemStr .= '发生时间开始：'. $refDate1 .'；';
	}
	if ($refDate2 != ''){ 
		$SQLstr .= ' and MR_time<='. $DB->ForTime($refDate2);
		$refItemStr .= '发生时间结束：'. $refDate2 .'；';
	}

	$retStr = '';
	$recordNum = 0;
	$loadexe = $DB->query($SQLstr .' order by MR_time DESC,MR_ID DESC');
		if (! $row = $loadexe->fetch()){
			JS::AlertCloseEnd('没有搜索到记录，无需导出.');
		}

	$filename = '财务数据（进出账）_'. TimeDate::Get('datetimeStr') .'.xls';
	$ua = $_SERVER['HTTP_USER_AGENT'];
	$encoded_filename = urlencode($filename);
	$encoded_filename = str_replace('+', '%20', $encoded_filename);
	if (preg_match('/MSIE/', $ua)) {
		header('Content-Disposition: attachment; filename="'. $encoded_filename .'"');
	} else if (preg_match("/Firefox/", $ua)) {
		header('Content-Disposition: attachment; filename*="utf8\'\''. $filename .'"');
	} else {
		header('Content-Disposition: attachment; filename="'. $filename .'"');
	}

	header('Content-Type: application/vnd.ms-excel; name="excel"');
	header('Content-type: application/octet-stream');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Pragma: no-cache');
	header('Expires: 0');
										
	$retStr = '
		<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
		<head>
			<meta http-equiv=Content-Type content="text/html; charset=utf-8">
		</head>
		<body style="font-size:14px; font-family:simsun,"Microsoft YaHei";">
		<table border=1><head>'. $filename .'</head>
		<tr>
			<td>编号</td><td>用户名</td><td>类型</td><td>类型2</td><td>交易金额</td><td>用户余额</td><td>成本</td><td>利润</td><td>来源</td><td>备注</td><td>发生时间</td><td>状态</td><td>操作员</td>
		</tr>
		'; 

	do {
		$recordNum ++;
		$retStr .= '
			<tr>
				<td>'. $recordNum .'</td>
				<td style="vnd.ms-excel.numberformat:@">'. $row['MR_username'] .'</td>
				<td>'. PayInfo::RecordTypeCN($row['MR_type']) .'</td>
				<td>'. $row['MR_type2'] .'</td>
				<td style="vnd.ms-excel.numberformat:#,##0.00">'. $row['MR_money'] .'</td>
				<td style="vnd.ms-excel.numberformat:#,##0.00">'. $row['MR_remMoney'] .'</td>
				<td style="vnd.ms-excel.numberformat:#,##0.00">'. $row['MR_cost'] .'</td>
				<td style="vnd.ms-excel.numberformat:#,##0.00">'. $row['MR_gain'] .'</td>
				<td>'. $row['MR_bank'] .'</td>
				<td>'. $row['MR_note'] .'</td>
				<td>'. $row['MR_time'] .'</td>
				<td>'. ($row['MR_state'] == 1 ? '有效' : '作废') .'</td>
				<td>'. $row['MR_adminName'] .'</td>
			</tr>';
	}while ($row = $loadexe->fetch());
	/*
	1） 文本：vnd.ms-excel.numberformat:@
	2） 日期：vnd.ms-excel.numberformat:yyyy/mm/dd
	3） 数字：vnd.ms-excel.numberformat:#,##0.00
	4） 货币：vnd.ms-excel.numberformat:￥#,##0.00
	5） 百分比：vnd.ms-excel.numberformat: #0.00%
	*/

	$retStr .= '
		</table>
		<div>共导出'. $recordNum .'条（查询条件 '. $refItemStr .'）</div>
		</body>
		</html>
		';

	echo($retStr);
}



// 历年月金额统计
function stati(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN,$pageCount,$recordCount;

	$num = OT::GetInt('num');
		if ($num <= 0){ $num = 3; }

	$minTime = $DB->GetOne('select min(MR_time) from '. OT_dbPref .'moneyRecord');
		if (! strtotime(''. $minTime)){
			die('无记录');
		}

	$yearNum	= TimeDate::Get('Y');
	$yearDiff	= $yearNum - TimeDate::Get('Y',$minTime) + 1;
	if ($yearDiff > $num){ $yearDiff = $num; }
	for ($y=0; $y<$yearDiff; $y++){
		echo('
		<table cellpadding="0" cellspacing="0" class="tabList12">
		<tr><th colspan="14">'. ($yearNum - $y) .'年每月收入情况</th></tr>
		<tr>
			<th align="center" style="text-align:center;">类型</th>
			');
		for ($i=1; $i<=12; $i++){
			echo('
			<th align="center" style="text-align:center;">'. $i .'</th>
			');
		}
			echo('
			<th align="center" style="text-align:center;">总计</th>
		</tr>
		<tr>
			<td align="right" style="text-align:right;">
				<div style="color:green;" title="进账">进账</div>
				<div style="color:red;" title="出账">出账</div>
				<div style="color:#999;" title="成本">成本</div>
				<div style="color:blue;" title="利润">利润</div>
			</td>
			');
		$addTotal = $cutTotal = $costTotal = $gainTotal = 0;
		for ($i=1; $i<=12; $i++){
			$whereStr = 'MR_time>='. $DB->ForTime(TimeDate::Add('m',$i-1,$yearNum - $y .'-1-1')) .' and MR_time<'. $DB->ForTime(TimeDate::Add('m',$i,$yearNum - $y .'-1-1')) .' and ';
			$addMoney = floatval($DB->GetOne('select sum(MR_money) from '. OT_dbPref .'moneyRecord where '. $whereStr .'MR_money>=0 and MR_state=1'));
			$cutMoney = floatval($DB->GetOne('select sum(MR_money) from '. OT_dbPref .'moneyRecord where '. $whereStr .'MR_money<=0 and MR_state=1'));
			$rs = $DB->GetRow('select sum(MR_cost) as cost,sum(MR_gain) as gain from '. OT_dbPref .'moneyRecord where '. $whereStr .'MR_state=1');
			$addTotal += $addMoney;
			$cutTotal += $cutMoney;
			$costTotal += $rs['cost'];
			$gainTotal += $rs['gain'];
			echo('
			<td align="right" style="text-align:right;">
				<div style="color:green;" title="进账">'. floatval($addMoney) .'</div>
				<div style="color:red;" title="出账">'. floatval($cutMoney) .'</div>
				<div style="color:#999;" title="成本">'. floatval($rs['cost']) .'</div>
				<div style="color:blue;" title="利润">'. floatval($rs['gain']) .'</div>
			</td>
			');
		}
			echo('
			<td align="right" style="text-align:right;color:red;">
				<div style="color:green;" title="进账">'. floatval($addTotal) .'</div>
				<div style="color:red;" title="出账">'. floatval($cutTotal) .'</div>
				<div style="color:#999;" title="成本">'. floatval($costTotal) .'</div>
				<div style="color:blue;" title="利润">'. floatval($gainTotal) .'</div>
			</td>
		</tr>
		</table>
		');
	}

}


// 历日金额统计
function StatiDay(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN,$pageCount,$recordCount;

	$num		= OT::GetInt('num');
		if ($num <= 0){ $num = 30; }

	$dataNum = 0;
	$stepNum = 15;
	$todayDate = TimeDate::Get('date');
	echo('
	<table cellpadding="0" cellspacing="0" class="tabList12">
	<tr><th colspan="'. ($stepNum + 1) .'">近'. $num .'天收入情况</th></tr>
	');
	for ($i=0; $i<$num; $i++){
		if ($dataNum % $stepNum == 0){
			echo('
			<tr>
				<td align="right" style="text-align:right;">
					<b>类型</b>
					<div style="color:green;" title="进账">进账</div>
					<div style="color:red;" title="出账">出账</div>
					<div style="color:#999;" title="成本">成本</div>
					<div style="color:blue;" title="利润">利润</div>
				</td>
				');
		}

		$dealDate = TimeDate::Add('d', $i*(-1), $todayDate);
		$whereStr = 'MR_time>='. $DB->ForTime($dealDate .' 00:00:00') .' and MR_time<='. $DB->ForTime($dealDate .' 23:59:59') .' and ';
		$addMoney = $DB->GetOne('select sum(MR_money) from '. OT_dbPref .'moneyRecord where '. $whereStr .'MR_money>=0 and MR_state=1');
		$cutMoney = $DB->GetOne('select sum(MR_money) from '. OT_dbPref .'moneyRecord where '. $whereStr .'MR_money<=0 and MR_state=1');
		$rs = $DB->GetRow('select sum(MR_cost) as cost,sum(MR_gain) as gain from '. OT_dbPref .'moneyRecord where '. $whereStr .'MR_state=1');
		echo('
		<td align="right" style="text-align:right;">
			<b title="'. $dealDate .'">'. TimeDate::Get('m.d',$dealDate) .'</b>
			<div style="color:green;" title="进账">'. floatval($addMoney) .'</div>
			<div style="color:red;" title="出账">'. floatval($cutMoney) .'</div>
			<div style="color:#999;" title="成本">'. floatval($rs['cost']) .'</div>
			<div style="color:blue;" title="利润">'. floatval($rs['gain']) .'</div>
		</td>
		');

		$dataNum ++;
	}
	$dayDiff = $stepNum - ($dataNum % $stepNum);
	if ($dayDiff < $stepNum){
		for ($i=1; $i<=$dayDiff; $i++){
			echo('<td></td>');
		}
	}
}
?>