<?php 

include_once './include/common.inc.php';

if(!$_userid)
{
	showmessage($LANG['login_website'], $MODULE['member']['linkurl'].'login.php?forward='.urlencode($PHP_URL));
}
$dt = md5(date('Y-m-d',$PHP_TIME));
$uc = md5($_username.'HARDYSOFT');
$newsessionid = base64_encode($dt.$uc);
if($sessionid!=$newsessionid)
{
 showmessage($LANG['illegal_action'],$PHP_REFERER);	
}else{

	$head['title'] = $LANG['generate_order'];
	$head['keywords'] = $MOD['seo_keywords'];
	$head['description'] = $MOD['seo_description'];
	$query = 'SELECT truename,telephone,qq,email,money FROM '.TABLE_MEMBER_INFO.' mi,'.TABLE_MEMBER.' m WHERE m.userid='.$_userid.' AND mi.userid='.$_userid;
	@extract($db->get_one($query),EXTR_OVERWRITE);
	
  $query = "SELECT cart.pdt_No,cart.itembind,cart.itemcolor,cart.idserver,cart.idfso,cart.fsoname,pdt.price FROM ".TABLE_PRODUCT_CART." cart,".TABLE_PRODUCT." pdt WHERE cart.odr_id =0  AND pdt.productid=cart.productid AND cart.user_id='$_userid'";
	$p = $db->get_one($query);
	if($money<$p['price']) showmessage("余额不足，请充值","/pay");

/************************连接远程数据库*******************************************/
	$rs = $db->get_one("SELECT * FROM ".TABLE_GAMESERVER." WHERE id='".$p['idserver']."'");
	@extract($rs);

  $msyqlco = @mysql_connect($serverip.':'.$port, $loginname, $loginpass,$datafso);
	if(!$msyqlco) showmessage("不能连接到游戏服务器，请与管理员联系,支付失败！！","/");
		
	$sqldb = new db_mysql;
	$sqldb->connect($serverip.':'.$port, $loginname, $loginpass, $datafso);
	$sqldb->query("SET NAMES DEFAULT");
/************************连接远程数据库结束*******************************************/

	$micro = explode(" ",microtime());
	$odr_No = date('ymd',$micro[1]).substr($micro[1],6,4).substr($micro[0],2,6);
	$query = "INSERT INTO ".TABLE_PRODUCT_ORDER." ( `odr_No` , `memberid` , `truename` , `zipcode` , `email` , `telephone` , `qq`, `isverify` , `verifytime` , `isship` , `shiptime` , `ispay` , `paytime` ,`paytype`,`disabled`,`addtime`,`note`,`idserver`,`idfso`,`fsoname`) 
			  VALUES ( '$odr_No', '$_userid', '$truename','$postid', '$email', '$telephone', '$qq', '0', '0', '0', '0', '0', '0','','0','$PHP_TIME', '$note','".$p['idserver']."','".$p['idfso']."','".$p['fsoname']."')";
	$db->query($query);
	$odr_id = $db->insert_id();
	$query = "UPDATE ".TABLE_PRODUCT_CART." SET odr_id='$odr_id'  where user_id=$_userid and odr_id=0 ";
	$db->query($query);

  include_once HS_ROOT.'/pay/include/pay.func.php';
  
  $query = "SELECT odr_No FROM ".TABLE_PRODUCT_ORDER." WHERE odr_id=".$odr_id;
	$r = $db->get_one($query);
	
	$note = $MOD['name'].$LANG['order_number'].":".$r['odr_No'];
	if(money_diff($_username, $p['price'], $note, $r['odr_No'])) 
	{
	  
		$db->query("UPDATE ".TABLE_PRODUCT_ORDER." SET ispay=1,paytime='$PHP_TIME' WHERE odr_id='$odr_id'");	
		$query ="SELECT odr_id,productid FROM ".TABLE_PRODUCT_CART." WHERE odr_id=".$odr_id;
		$r = $db->get_one($query);
		$db->query("UPDATE ".TABLE_PRODUCT." SET sales=sales+1 WHERE productid=".$r['productid']);

		$buytime =date("Y-m-d H:i:s",$PHP_TIME);
    $gift = $sqldb->query("INSERT INTO t_gift (`charID`,`level`,`type`,`attribute`,`addTime`,`limitTime`,`msg`) VALUES ('$p[idfso]',0,1,'ver=1;count=1;|id=$p[pdt_No];color=$p[itemcolor];lev=0;bind=$p[itembind];|','$buytime','','网站购买道具')");
	  if($sqldb->affected_rows())
	  {
	   $shipstatus = 1;
	  }else{
	   $shipstatus = 0;
	  }
	  
		$query = "UPDATE ".TABLE_PRODUCT_ORDER." SET isship='$shipstatus',shiptime='$PHP_TIME' WHERE odr_id='$odr_id'";
		$db->query($query);	

		showmessage('购买成功，请在游戏中 <font color=green>道具码管理员</font> 处领取您所购买的道具', $MOD['linkurl'].'myorder.php');	
	}
}
?>