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

function gold_add($userid, $number, $note = '')
{
	global $db,$table;
	$userid = intval($userid);
	$number = intval($number);
	if($number < 0)	showmsg('数量不能小于0');
	$note = addslashes($note);
	$db->query("UPDATE {$table}member SET gold=gold+$number WHERE userid='$userid'");
	if($db->affected_rows() == 0) showmsg('操作失败');
	$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 card_gold_add($card,$siso,$shop_card_fc,$comid)
{
	global $db,$table;
	$comid = intval($comid);
	$r = com_info($comid,'','2');
	$db->query("UPDATE {$table}member SET gold=gold+$shop_card_fc WHERE userid='$r'");
	$time = time();
	$db->query("INSERT INTO {$table}card_fc (`cardnum`,`siso`,`fc`,`userid`,`comid`,`addtime`) VALUES('$card','$siso','$shop_card_fc','$r','$comid','$time')");
}

function gold_diff($userid, $number, $note = '', $authid = '')
{
	global $db, $table;
	$userid = intval($userid);
	$number = intval($number);
	if($number < 0)	showmsg('数量不能小于0');
	$note = addslashes($note);
	$r = member_info($userid,'2');
	if(!$r) showmsg('不存在此用户');
	extract($r);
	$userid = intval($userid);
	$time = time();
	$ip = get_ip();
	if($number > $gold) showmsg('您的余额不足');
	$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 gold_com_diff($userid, $number, $note = '', $authid = '')
{
	global $db, $table;
	$userid = intval($userid);
	$number = intval($number);
	if($number < 0)	showmsg('数量不能小于0');
	$note = addslashes($note);
	$r = member_info($userid,'2');
	if(!$r) showmsg('不存在此用户');
	extract($r);
	$userid = intval($userid);
	$time = time();
	$ip = get_ip();
	if($number > $gold) showmsg('您的余额不足');
	$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 = intval($userid);
	$number = intval($number);
	if($number < 0)	showmsg('数量不能小于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) showmsg('添加失败');
	$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 = '', $moneys='',$tid='')
{
	global $db, $table;
	$userid = intval($userid);
	$number = intval($number);
	if($number < 0)	showmsg("积分数量不能小于0");
	$note = addslashes($note);
	$r = member_info($userid);
	if(!$r) showmsg('不存在此用户');
	extract($r);
	$userid = intval($userid);
	$time = time();
	$ip = get_ip();
	if($chargetype == 0) {
		if($number > $credit) showmsg('您的积分不足');
        $db->query("UPDATE {$table}member SET credit=credit-$number WHERE userid='$userid'");
		$db->query("UPDATE {$table}member SET money=money+$moneys WHERE userid='$userid'");
	    $db->query("INSERT INTO {$table}pay_credit (`userid`,`tid`,`value`,`note`,`cnum`,`addtime`,`ip`) VALUES('$userid','$tid','$moneys','$note','$number','$time','$ip')");
	}
	return TRUE;
}

function money_add($userid, $number, $note = '')
{
	global $db,$table;
	$userid = intval($userid);
	$number = round(floatval($number) ,2);
	if($number < 0) showmsg('不能小于0元');
	$note = addslashes($note);
	$r = member_info($userid);
	if(!$r) showmsg('不存在此用户');
	extract($r);
	$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_exchange (`userid`,`type`,`value`,`note`,`addtime`,`ip`) VALUES('$userid','money','$number','$note','$time','$ip')");
}

function money_diff($userid, $number, $note = '',$zekou='',$yjia='',$soj='',$lej='',$jifen='',$fc='',$comid='',$cardnum='',$orderid='')
{
	global $db, $table;
	//$userid = intval($userid);
	$number = number_format($number,2);
	if($number == 0) return true;
	if($number < 0) showmsg('不能小于0元');
	$note = addslashes($note);
    $userid =  htmlspecialchars($userid,ENT_QUOTES) ;
	$r = member_info($userid,'1');
	if(!$r)showmsg('不存在此用户');
	extract($r);
	$userid = intval($userid);
	if($number > $money) showmsg('帐户资金不够，请先入款！');
	$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`,`orderid`,`cardnum`,`type`,`value`,`zekou`,`yjia`,`soj`,`lej`,`jifen`,`fc`,`comid`,`note`,`addtime`,`ip`) VALUES('$userid','$orderid','$cardnum','money','$number','$zekou','$yjia','$soj','$lej','$jifen','$fc','$comid','$note','$time','$ip')");

	return true;
}

//新增加
function pay_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 pay_money_diff($userid, $number, $note = '', $payid='')
{
	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','$payid','-$number','$note','$time','$ip')");

	return true;
}

//券
function pay_juan($ssid,$usid='',$money='')
{
	global $db, $table ,$CFG;
	$times = time();
	if($money>0){
		$sql = " and '$money'>=smoney";	
	}
	$userinfo = member_info("userid,cardnum","3");
	$userid = $userinfo['userid'] ? $userinfo['userid'] : $usid ;
	$juans = array();
	$result = $db->query("SELECT * FROM {$table}juan WHERE ssid='$ssid' and nums>0 $sql");
	while($r = $db->fetchrow($result)) {
		$juans[] = $r;
	}

if($juans){
	foreach($juans AS $j){
     if($db->getOne("SELECT COUNT(*) FROM {$table}juan_pay where userid='$userid' and jid=".$j['id']) < $j['klc'] || $j['klc']==0){
	  if($j['mid']==1){
		$db->query("INSERT INTO {$table}juan_pay (`jid`,`userid`,`money`,`smoney`,`ssid`,`paytime`,`yxday`,`xfmy`,`comid`) VALUES('$j[id]','$userid','$j[money]','$j[smoney]','$j[ssid]','$times','$j[yxday]','$j[xfmy]','$j[comid]')");
		$db->query("UPDATE {$table}juan SET nums=nums-1 where id=".$j['id']);
	  }elseif($j['mid']==2){
		if(!empty($userinfo['cardnum'])){
		 $db->query("INSERT INTO {$table}juan_pay (`jid`,`userid`,`money`,`smoney`,`ssid`,`paytime`,`yxday`,`xfmy`,`comid`) VALUES('$j[id]','$userid','$j[money]','$j[smoney]','$j[ssid]','$times','$j[yxday]','$j[xfmy]','$j[comid]')");
		  $db->query("UPDATE {$table}juan SET nums=nums-1 where id=".$j['id']);
		}
	  }elseif($j['mid']==3){
		if(empty($userinfo['cardnum'])){
		  $db->query("INSERT INTO {$table}juan_pay (`jid`,`userid`,`money`,`smoney`,`ssid`,`paytime`,`yxday`,`xfmy`,`comid`) VALUES('$j[id]','$userid','$j[money]','$j[smoney]','$j[ssid]','$times','$j[yxday]','$j[xfmy]','$j[comid]')");
		  $db->query("UPDATE {$table}juan SET nums=nums-1 where id=".$j['id']); 
		}
	  }
		if($money>$j['smoney']){
	       $_SESSION['sqltxt'] = $j['money'].'元'.$CFG['lejuan'];
		}else{
		   unset($_SESSION['sqltxt']); 
		}
	 }else{
		unset($_SESSION['sqltxt']); 
	 }
	}
  }else{
	  unset($_SESSION['sqltxt']); 
}

}

function dopay($orderid, $amount, $bank)
{
	global $db,$table;
	$r = $db->getRow("SELECT * FROM {$table}pay_online WHERE `orderid`='$orderid'");
	if(!$r)showmsg('订单信息不存在');
	if($r['amount']+$r['trade_fee'] != $amount) showmsg('支付失败！支付金额不对！');
	if($r['status'] > 0) showmsg('请不要刷新');
	$db->query("UPDATE {$table}pay_online SET status=1, receivetime='".time()."' WHERE `orderid`='$orderid'");
	
	pay_money_add($r['userid'] , $amount, '在线支付入款'.':'.$orderid,'13');
	if($r['trade_fee']) money_diff($r['userid'], $r['trade_fee'], '扣除手续费'.':'.$orderid);
	return $r;
}
?>