<?php

if(!defined('IN_PHPMPS'))
{
	die('Access Denied');
}

function gold_add($userid, $number, $note = '')
{
	global $db,$table;
	$userid = addslashes($userid);
	$number = intval($number);
	if($number < 0)	show('数量不能小于0');
	$note = addslashes($note);
	$userid = htmlspecialchars($userid);
	$db->query("UPDATE {$table}member SET gold=gold+$number WHERE userid='$userid'");
	if($db->affected_rows() == 0) show('操作失败');
	$time = time();
	$ip = get_ip();
	$db->query("INSERT INTO {$table}pay_exchange  (`userid`,`type`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','gold','$number','$note','$time','$ip')");
}

function gold_diff($userid, $number, $note = '', $authid = '')
{
	global $db, $table;
	$userid = addslashes($userid);
	$number = intval($number);
	if($number < 0)	show('数量不能小于0');
	$note = addslashes($note);
	$r = member_info($userid,'2');
	if(!$r) show('不存在此用户');
	extract($r);
	$time = time();
	$ip = get_ip();
	if($number > $gold) show('您的金额不够支付');
	$db->query("UPDATE {$table}member SET gold=gold-$number WHERE userid='$userid'");
	$db->query("INSERT INTO {$table}pay_exchange  (`userid`,`type`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','gold','-".$number."','$note','$time','$ip')");
	
	if($member['ispointdiffemail']) {
		//$data = tpl_data('member','pointmailtpl');
		sendmail($email, '确认会员金币变动邮件'.'('.$CFG['sitename'].')', stripslashes($data));
	}
	return TRUE;
}

function credit_add($userid, $number, $note = '')
{
	global $db, $table;
	$userid = addslashes($userid);
	$number = intval($number);
	if($number < 0)	show('数量不能小于0');
	$db->query("UPDATE {$table}member SET credit=credit+$number WHERE userid='$userid'");
	$note = addslashes($note);
	$time = time();
	$ip = get_ip();
	if($db->affected_rows() == 0) show('添加失败');
	$db->query("INSERT INTO {$table}pay_exchange  (`userid`,`type`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','credit','$number','$note','$time','$ip')");
}

function credit_diff($userid, $number, $note = '')
{
	global $db, $table;
	$userid = addslashes($userid);
	$number = intval($number);
	if($number < 0)	show($LANG['illegal_parameters']);
	$note = addslashes($note);
	$r = member_info($userid,'2');
	if(!$r) show('不存在此用户');
	extract($r);
	$time = time();
	$ip = get_ip();
	if($chargetype == 0)
	{
		if($number > $credit) show('您的积分不足以支付');
        $db->query("UPDATE {$table}member SET credit=credit-$number WHERE userid='$userid'");
	    $db->query("INSERT INTO {$table}pay_exchange  (`userid`,`type`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','credit','-".$number."','$note','$time','$ip')");
	}
	return TRUE;
}

function money_add($userid, $number, $note = '', $payid='')
{
	global $db, $table;
	$userid = intval($userid);
	$number = round(floatval($number) ,2);
	if($number < 0) show('不能小于0元');
	$note = addslashes($note);
	$r = member_info($userid);
	if(!$r) show('不存在此用户');
	extract($r);
	$userid = addslashes($userid);
	$money = $money + $number;
	$db->query("UPDATE {$table}member SET money=$money WHERE userid='$userid'");
	if($db->affected_rows() == 0) show('操作失败');
	$time = time();
	$year = date('Y', $time);
	$month = date('m', $time);
	$date = date('Y-m-d', $time);
	$ip = get_ip();
	
	//资金变动记录
	$db->query("INSERT INTO {$table}pay (typeid,note,paytype,amount,balance,userid,year,month,date,inputtime,inputer,ip) VALUES('1','$note','入款','$number','$money','$userid','$year','$month','$date','$time','system','$ip')");

	$db->query("INSERT INTO {$table}pay_add (`userid`,`payid`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','$payid','$number','$note','$time','$ip')");
}

function money_diff($userid, $number, $note = '')
{
	global $db, $table;

	$number = round(floatval($number) ,2);
	if($number == 0) return true;
	if($number < 0) show('不能小于0元');
	$note = addslashes($note);
	$r = member_info($userid,'2');
	if(!$r) show('不存在此用户');
	extract($r);
	$userid = addslashes($userid);
	if($number > $money) show('帐户资金不够，请先入款！');
	$money = $money - $number;
	$db->query("UPDATE {$table}member SET money=$money WHERE userid='$userid'");
	
	$time = time();
	$year = date('Y', $time);
	$month = date('m', $time);
	$date = date('Y-m-d', $time);
	$ip = get_ip();
	
	//资金变动记录
	$db->query("INSERT INTO {$table}pay (typeid,note,paytype,amount,balance,userid,year,month,date,inputtime,inputer,ip) VALUES('2','$note','扣款','$number','$money','$userid','$year','$month','$date','$time','system','$ip')");

	$db->query("INSERT INTO {$table}pay_exchange (`userid`,`type`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','money','-".$number."','$note','$time','$ip')");

	return true;
}

//新增加
function pay_money_add($userid, $number, $note = '')
{
	global $db, $table;
	$userid = intval($userid);
	$number = round(floatval($number) ,2);
	if($number < 0) show('不能小于0元');
	$note = addslashes($note);
	$r = member_info($userid);
	if(!$r) show('不存在此用户');
	extract($r);
	$userid = addslashes($userid);
	$money = $money + $number;
	$db->query("UPDATE {$table}member SET money=$money WHERE userid='$userid'");
	if($db->affected_rows() == 0) show('操作失败');
	$time = time();
	$year = date('Y', $time);
	$month = date('m', $time);
	$date = date('Y-m-d', $time);
	$ip = get_ip();
	
	//资金变动记录
	$db->query("INSERT INTO {$table}pay (typeid,note,paytype,amount,balance,userid,year,month,date,inputtime,inputer,ip) VALUES('1','$note','入款','$number','$money','$userid','$year','$month','$date','$time','system','$ip')");

	$db->query("INSERT INTO {$table}pay_add (`userid`,`payid`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','11','$number','$note','$time','$ip')");
}

function pay_money_diff($userid, $number, $note = '')
{
	global $db, $table;

	$number = round(floatval($number) ,2);
	if($number == 0) return true;
	if($number < 0) show('不能小于0元');
	$note = addslashes($note);
	$r = member_info($userid);
	if(!$r) show('不存在此用户');
	extract($r);
	$userid = addslashes($userid);
	if($number > $money) show('帐户资金不够，请先入款！');
	$money = $money - $number;
	$db->query("UPDATE {$table}member SET money=$money WHERE userid='$userid'");
	$time = time();
	$year = date('Y', $time);
	$month = date('m', $time);
	$date = date('Y-m-d', $time);
	$ip = get_ip();
	
	//资金变动记录
	$db->query("INSERT INTO {$table}pay (typeid,note,paytype,amount,balance,userid,year,month,date,inputtime,inputer,ip) VALUES('2','$note','扣款','$number','$money','$userid','$year','$month','$date','$time','system','$ip')");

	$db->query("INSERT INTO {$table}pay_add (`userid`,`payid`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','12','-$number','$note','$time','$ip')");

	return true;
}

function pay_credit_add($userid, $number, $note = '')
{
	global $db, $table;
	$userid = addslashes($userid);
	$number = intval($number);
	if($number < 0)	show('数值不能小于0');
	$r = member_info($userid);
	if(!$r) show('不存在此用户');
	$db->query("UPDATE {$table}member SET credit=credit+$number WHERE userid='$userid'");
	$note = addslashes($note);
	$time = time();
	$ip = get_ip();
	if($db->affected_rows() == 0) show('添加失败');
	$db->query("INSERT INTO {$table}pay (typeid,note,paytype,amount,balance,userid,year,month,date,inputtime,inputer,ip) VALUES('1','$note','入款','$number','$money','$userid','$year','$month','$date','$time','system','$ip')");
	$db->query("INSERT INTO {$table}pay_credit (`userid`,`tid`,`cnum`,`note`,`addtime`,`ip`) VALUES('$userid','1','$number','$note','$time','$ip')");
}

function pay_credit_diff($userid, $number, $note = '')
{
	global $db, $table;
	$userid = addslashes($userid);
	$number = intval($number);
	if($number < 0)	show('数值不能小于0');
	$note = addslashes($note);
	$r = member_info($userid);
	if(!$r) show('不存在此用户');
	extract($r);
	$time = time();
	$ip = get_ip();
	if($number > $credit) show('积分不足');
       $db->query("UPDATE {$table}member SET credit=credit-$number WHERE userid='$userid'");
	   $db->query("INSERT INTO {$table}pay (typeid,note,paytype,amount,balance,userid,year,month,date,inputtime,inputer,ip) VALUES('1','$note','扣款','$number','','$userid','$year','$month','$date','$time','system','$ip')");
	   $db->query("INSERT INTO {$table}pay_credit (`userid`,`tid`,`cnum`,`note`,`addtime`,`ip`) VALUES('$userid','2','-$number','$note','$time','$ip')");

	return TRUE;
}
?>