<?php
define('THIS_SCRIPT', 'plus_tools_pay');
require_once 'global.php';
$lang = load_lang($lang, 'plus_tools_pay' );
$stats = $lang["Repl_HtmlTitle_{$action}"];
$istoptable = 0;
head();

if( $userid <= 0 ){
	showmsg($lang['MustLogin']);
	exit;
}
if( $fsetting['plus_tools'] == 0 && empty($_SESSION['flag']) ){
	showmsg($lang['Error.strings1'],0,'',false);
	exit;
}
if( !is_numeric($toolsid) || $toolsid <= 0 ){
	showmsg($lang['Error.strings2'],0,'',false);
	exit;
}

$buyCount = 1;
$saleUserName = $lang['SaleUserName'];
$inputDisable = ' disabled ';

$toolsInfo = false;
if( $query = $db->query("SELECT id,toolsname,toolsinfo,isstar,sysstock,userstock,usermoney,userpost,userwealth,userep,usercp,usergroupid,boardid,userticket,buytype,toolsimg,toolssetting FROM {$dv}plus_tools_info WHERE id='{$toolsid}'") ){
	$toolsInfo =& $db->fetch_array($query);
}
if( $toolsInfo === false ){
	showmsg($lang['Error.strings2'],0,'',false);
	exit;
} else{
	$toolsSetting = explode(',',$toolsInfo['toolssetting']);
}

//若是用户购买或转让，更改道具价格为用户自定义价格
$bussInfo = false;
if( is_numeric($bussid) ){
	if( $query = $db->query("SELECT toolscount,salecount,salemoney,saleticket,userid,username,toolsid FROM {$dv}plus_tools_buss WHERE id='{$bussid}'") ){
		$bussInfo =& $db->fetch_array($query);
	}
	if( $bussInfo === false ){
		showmsg($lang['Error.strings2'],0,'',false);
		exit;
	} else{
		$saleUserName = iHtmlencode($bussInfo['username']);
	}
}

if( $action === 'SellTools' ){
	$inputDisable = '';
}

require printout('plus_tools_pay');

$db->close();

function &getCountFromBuss($ToolsID){
	global $dv,$db,$toolsid;
	$countInfo = false;
	if( $query = $db->query("SELECT toolscount,salecount FROM {$dv}plus_tools_buss WHERE userid='{$GLOBALS['userid']}' AND toolsid='{$GLOBALS['toolsid']}'") ){
		$countInfo =& $db->fetch_array($query);
	}
	return $countInfo;
}

function &getUserGroupsBy($grpIDList){
	global $dv,$db;
	if( !checkIDList($grpIDList) ){
		return false;
	} else{
		return $db->getResultSet("SELECT usergroupid,usertitle FROM {$dv}usergroups WHERE usergroupid IN({$grpIDList}) ORDER BY usergroupid");
	}
}

function ChkUserGroup(){
	global$lang,$fsetting,$toolsid,$bussid,$toolsInfo,$userinfo;
	if( $toolsInfo['isstar'] == 0 ){
		showmsg($lang['Error.strings5'],0,'',false);
		exit;
	}
	if( $toolsInfo['usergroupid'] == '' || strpos(",{$toolsInfo['usergroupid']},",",{$GLOBALS['usergroupid']},") === false ){
		showmsg($lang['Error.strings3'],0,'',false);
		exit;
	}
}

function SaveBuyTools(){//保存道具购买（与系统交易）
	global $dv,$db,$lang,$fsetting,$toolsid,$bussid,$toolsInfo,$userinfo;
	if( ChkPost() === false ){
		showmsg($lang['Error.strings0'],0,'',false);
		exit;
	}
	if( $toolsInfo['sysstock'] <= 0 ){
		showmsg($lang['Error.strings3'],0,'',false);
		exit;
	}
	ChkUserGroup();
	$toolsSum = trim($GLOBALS['toolssum']);
	if( !is_numeric($toolsSum) || $toolsSum < 0 ){
		$toolsSum = 0;
	}
	if( $toolsSum > 10 ){
		showmsg($lang['Error.strings4'],0,'',false);
		exit;
	}
	$buyType = trim($GLOBALS['buytype']);
	$buyCount = $toolsSum;
	//Start 检查用户购买道具权限： bType 数字型，为用户选取的购买类型
	if( !is_numeric($buyType) ){
		$buyType = -1;
	}
	if( $toolsInfo['sysstock'] == 0 || $buyCount > $toolsInfo['sysstock'] || $buyCount == 0 ){//库存不足
		showmsg($lang['Error.strings6'],0,'',false);
		exit;
	}
	$canbuyTools = false;
	switch(intval($toolsInfo['buytype'])){
		case 0://只需金币
			if( php_cCur($userinfo['usermoney']) >= ($toolsInfo['usermoney']*$buyCount) && $buyType == 0 ){
				$canbuyTools = true;
			}
			break;
		case 1://只需点券
			if( php_cCur($userinfo['userticket']) >= ($toolsInfo['userticket']*$buyCount) && $buyType == 1 ){
				$canbuyTools = true;
			}
			break;
		case 2://金币+点券
			if( php_cCur($userinfo['usermoney']) < ($toolsInfo['usermoney']*$buyCount) || php_cCur($userinfo['userticket']) < ($toolsInfo['userticket']*$buyCount) ){
				$canbuyTools = false;
			} else{
				$canbuyTools = true;
			}
			break;
		case 3://金币或点券
			if( $buyType == 0 ){
				if( php_cCur($userinfo['usermoney']) >= ($toolsInfo['usermoney']*$buyCount) ){
					$canbuyTools = true;
				}
			} elseif( $buyType == 1 ){
				if( php_cCur($userinfo['userticket']) >= ($toolsInfo['userticket']*$buyCount) ){
					$canbuyTools = true;
				}
			} else{
				$canbuyTools = false;
			}
			break;
		default:
			showmsg($lang['Error.strings7'],0,'',false);
			exit;
			break;
	}
	if( $canbuyTools === false ){
		showmsg($lang['Error.strings8'],0,'',false);
		exit;
	}
	//End 检查用户购买道具权限： bType 数字型，为用户选取的购买类型
	//Start 保存购买道具
	$bussCount = $db->scalar("SELECT COUNT(1) FROM {$dv}plus_tools_buss WHERE userid='{$GLOBALS['userid']}' AND toolsid='{$GLOBALS['toolsid']}'");
	if( $bussCount <= 0 ){
		$db->query("INSERT INTO {$dv}plus_tools_buss(username,toolsname,userid,toolsid,toolscount) VALUES('{$GLOBALS['membername']}','{$toolsInfo['toolsname']}','{$GLOBALS['userid']}','{$GLOBALS['toolsid']}','{$toolsSum}')");
	} else{
		$db->query("UPDATE {$dv}plus_tools_buss SET toolscount=toolscount+'{$toolsSum}' WHERE userid='{$GLOBALS['userid']}' AND toolsid='{$GLOBALS['toolsid']}'");
	}
	//End 保存购买道具
	//Start 减少系统库存和增加用户库存
	$db->query("UPDATE {$dv}plus_tools_info SET sysstock=sysstock-'{$toolsSum}',userstock=userstock+'{$toolsSum}' WHERE id='{$GLOBALS['toolsid']}'");
	//End 减少系统库存和增加用户库存
	//Start 更新用户当前信息
	$toolsMoney = $toolsInfo['usermoney'] * $toolsSum;
	if( $toolsMoney < 0 ){
		$toolsMoney = 0;
	}
	$toolsTicket = $toolsInfo['userticket'] * $toolsSum;
	if( $toolsTicket < 0 ){
		$toolsTicket = 0;
	}
	if( $toolsInfo['buytype'] == 3 ){
		if( $buyType == 0 ){
			$toolsTicket = 0;
			$userinfo['usermoney'] -=  $toolsMoney;
		} elseif( $buyType == 1 ){
			$toolsMoney = 0;
			$userinfo['userticket'] -=  $toolsTicket;
		} else{
			$userinfo['usermoney'] -=  $toolsMoney;
			$userinfo['userticket'] -=  $toolsTicket;
		}
	} else{
			$userinfo['usermoney'] -=  $toolsMoney;
			$userinfo['userticket'] -=  $toolsTicket;
	}
	UpdateUserInfoById($GLOBALS['userid'],array('usermoney'=>$userinfo['usermoney'],'userticket'=>$userinfo['userticket']));
	//End 更新用户当前信息
	eval('$SucMsg = "'.addslashes($lang['template.strings1']).'";');
	//Start 插入事件记录
	ToolsLog($GLOBALS['toolsid'],$toolsSum,$toolsMoney,$toolsTicket,4,$SucMsg,"{$userinfo['usermoney']}|{$userinfo['userticket']}");
	//End 插入事件记录
	showmsg($SucMsg.$lang['template.strings1_1'],1,'',false);
	//End 更新用户当前信息
}

function SaveSellTools(){//保存道具出售(转让)
	global $dv,$db,$lang,$fsetting,$toolsid,$bussid,$toolsInfo,$bussInfo,$userinfo;
	if( ChkPost() === false ){
		showmsg($lang['Error.strings0'],0,'',false);
		exit;
	}
	ChkUserGroup();
	$toolsSum = trim($GLOBALS['toolssum']);
	if( $toolsSum < 0 ){
		$toolsSum = 0;
	}
	$toolsMoney = trim($GLOBALS['toolsmoney']);
	if( $toolsMoney < 0 ){
		$toolsMoney = 0;
	}
	$toolsTicket = trim($GLOBALS['toolsticket']);
	if( $toolsTicket < 0 ){
		$toolsTicket = 0;
	}
	if( $toolsMoney == 0 && $toolsTicket == 0 ){
		showmsg($lang['Error.strings9'],0,'',false);
		exit;
	}
	echo "<br><br>".$bussInfo['salecount'];
	exit;
	if( $toolsSum == 0 || $bussInfo['salecount'] < $toolsSum ){
		showmsg($lang['Error.strings10'],0,'',false);
		exit;
	}
	if( $toolsInfo['sysstock'] > 0 ){
		if( $toolsInfo['sysstock'] < $toolsSum ){
			$upToolsCount = $bussInfo['toolscount'] - ( $toolsSum - $toolsInfo['sysstock'] );
		} else{
			$upToolsCount = $bussInfo['toolscount'] + ( $toolsInfo['sysstock'] - $toolsSum );
		}
		$upSaleCount = $toolsSum;
	} else{
		$upToolsCount = $bussInfo['toolscount'] - $toolsSum;
		$upSaleCount = $toolsInfo['sysstock'] + $toolsSum;
	}
	$db->query("UPDATE {$dv}plus_tools_buss SET toolscount='{$upToolsCount}',salecount='{$upSaleCount}',salemoney='{$toolsMoney}',saleticket='{$toolsTicket}' WHERE id='{$GLOBALS['bussid']}'");
	//Start 插入事件记录
	eval('$SucMsg = "'.addslashes($lang['template.strings2']).'";');
	ToolsLog($GLOBALS['toolsid'],$toolsSum,$toolsMoney,$toolsTicket,2,$SucMsg,"{$userinfo['usermoney']}|{$userinfo['userticket']}");
	//End 插入事件记录
	showmsg($SucMsg.$lang['template.strings2_1'],1,'',false);
}

function SaveBuyUserTools(){//保存道具购买（用户间交易）
	global $dv,$db,$lang,$fsetting,$toolsid,$bussid,$toolsInfo,$bussInfo,$userinfo;
	if( ChkPost() === false ){
		showmsg($lang['Error.strings0'],0,'',false);
		exit;
	}
	ChkUserGroup();
	$toolsSum = trim($GLOBALS['toolssum']);
	if( !is_numeric($toolsSum) || $toolsSum < 0 ){
		$toolsSum = 0;
	}
	$buyType = trim($GLOBALS['buytype']);
	if( !is_numeric($buyType) || $buyType < 0 ){
		$buyType = 0;
	}
	if( $toolsInfo['sysstock'] == 0 || $toolsSum > $toolsInfo['sysstock'] || $toolsSum == 0 ){
		showmsg($lang['Error.strings6'],0,'',false);
		exit;
	}
	$toolsMoney = $toolsInfo['usermoney'] * $toolsSum;
	$toolsTicket = $toolsInfo['userticket'] * $toolsSum;
	if( $toolsMoney <= 0 && $toolsTicket <= 0 ){
		showmsg($lang['Error.strings8'],0,'',false);
		exit;
	}
	//Start 判断用户是否具有购买权限
	if( $bussInfo['userid'] != $GLOBALS['userid'] ){
		if( php_cCur($userinfo['usermoney']) < $toolsMoney || php_cCur($userinfo['userticket']) < $toolsTicket ){
			showmsg($lang['Error.strings8'],0,'',false);
			exit;
		}
	} else{
		$db->query("UPDATE {$dv}plus_tools_buss SET toolscount=toolscount+'{$toolsSum}',salecount=salecount-'{$toolsSum}' WHERE id='{$GLOBALS['bussid']}'");
		//Start 插入事件记录
		eval('$SucMsg = "'.addslashes($lang['template.strings3']).'";');
		ToolsLog($GLOBALS['toolsid'],$toolsSum,$toolsMoney,$toolsTicket,4,$SucMsg,"{$userinfo['usermoney']}|{$userinfo['userticket']}");
		//End 插入事件记录
		showmsg($SucMsg.$lang['template.strings3_1'],1,'',false);
		return true;
	}
	//End 判断用户是否具有购买权限
	//Start 更新卖方数据(减少售出数量)
	$db->query("UPDATE {$dv}plus_tools_buss SET salecount=salecount-'{$toolsSum}' WHERE id='{$GLOBALS['bussid']}'");
	UpdateUserInfoById($bussInfo['userid'],array('usermoney'=>$toolsMoney,'userticket'=>$toolsTicket));
	//End 更新卖方数据(减少售出数量)
	//更新买方数据(减少售出数量)
	//保存购买道具(若未找到道具添加新的记录，已有道具只需更新个人库存)
	$bussCount = $db->scalar("SELECT COUNT(1) FROM {$dv}plus_tools_buss WHERE userid='{$GLOBALS['userid']}' AND toolsid='{$GLOBALS['toolsid']}'");
	if( $bussCount <= 0 ){
		$db->query("INSERT INTO {$dv}plus_tools_buss(username,toolsname,userid,toolsid,toolscount) VALUES('{$GLOBALS['membername']}','{$toolsInfo['toolsname']}','{$GLOBALS['userid']}','{$GLOBALS['toolsid']}','{$toolsSum}')");
	} else{
		$db->query("UPDATE {$dv}plus_tools_buss SET toolscount=toolscount+'{$toolsSum}' WHERE userid='{$GLOBALS['userid']}' AND toolsid='{$GLOBALS['toolsid']}'");
	}

	$userinfo['usermoney'] -= $toolsMoney;
	$userinfo['userticket'] -= $toolsTicket;
	UpdateUserInfoById($GLOBALS['userid'],array('usermoney'=>$userinfo['usermoney'],'userticket'=>$userinfo['userticket']));
	//Start 插入事件记录
	$SaleUserName = iHtmlencode($bussInfo['username']);
	eval('$SucMsg = "'.addslashes($lang['template.strings4']).'";');
	ToolsLog($GLOBALS['toolsid'],$toolsSum,$toolsMoney,$toolsTicket,4,$SucMsg,"{$userinfo['usermoney']}|{$userinfo['userticket']}");
	//End 插入事件记录
	showmsg($SucMsg.$lang['template.strings4_1'],1,'',false);
}

?>