<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
	$r=$r['order']['roleid']; 
	if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');

	include(DIRCMS_ROOT.'include/member.class.php');
	$member=new member();
	if(isset($do_submit))
	{
		if(!preg_match('/^[0-9\.]{1,6}$/i',$info['number']))showmsg('充值数量必须为不超过6位的数字!');
		if(!$member->is_username($info['username']))showmsg('不存在该用户!');
		$r=$db->fetch_one("SELECT * FROM `".DB_PRE."member` WHERE `".DB_PRE."member`.`username`='{$info['username']}'");
		if(!$r)showmsg('不存在该用户!');
		$buylog=array();
		$buylog = array(
					'userid'      => $r['id'],
					'username'    => $r['username'],
					'type'		  => $info['type'],			
					'inputid'     => $_userid,
					'inputer'     => $_username,
					'ip'		  => IP,
					'note'		  => ($manageid==1?'【入款】':'【扣款】').sub_string($info['note'],80),
					'time'	      => TIME
					);
		if($info['type']=='point') ////////////  点数
		{
			if($manageid==1)
			{
				$buylog['blance']=$r['point']+intval($info['number']);
				$buylog['number']=intval($info['number']);
			}
			else
			{
				if($r['point']<intval($info['number']))showmsg('用户点数不足'.intval($info['number']).'点!');
				$buylog['blance']=$r['point']-intval($info['number']);
				$buylog['number']=intval(-$info['number']);
			}
			
			$db->update(DB_PRE.'member',array('point'=>$buylog['blance']),'id='.intval($r['id']));
			$db->update(DB_PRE.'member_cache',array('point'=>$buylog['blance']),'id='.intval($r['id']));
		}
		else
		{
			if($manageid==1)
			{
				$buylog['blance']=$r['amount']+floatval($info['number']);
				$buylog['number']=floatval($info['number']);
			}
			else
			{
				if($r['amount']<floatval($info['number']))showmsg('用户金额不足'.floatval($info['number']).'元!');
				$buylog['blance']=$r['amount']-floatval($info['number']);
				$buylog['number']=floatval(-$info['number']);
			}
			
			$db->update(DB_PRE.'member',array('amount'=>$buylog['blance']),'id='.intval($r['id']));
			$db->update(DB_PRE.'member_cache',array('amount'=>$buylog['blance']),'id='.intval($r['id']));
		}
		$db->insert(DB_PRE.'pay_exchange',$buylog);
		showmsg(($manageid==1?'入款':'扣款').'成功!');
	}
	include admin_tlp('exchange_add');
?>
