<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：QQ877873666
 邮箱：877873666@qq.com

 **** 版权所有 **** */



//用户检测
$MB->Open(',MB_realname','login',2);


switch ($mudi){
	case 'deal':
		$menuFileID = 639;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		Deal();
		break;

	case 'moreDel':
		$menuFileID = 640;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		MoreDel();
		break;

	default:
		die('err');
}

$MB->Close();
$DB->Close();





// 操作处理
function Deal(){
	global $DB,$MB,$dataType,$dataTypeCN;

	$mudi2			= OT::GetStr('mudi2');
	$mode			= OT::GetStr('mode');
	$backURL		= OT::GetStr('backURL');
	$theme			= OT::GetStr('theme');
	$dataID			= OT::GetInt('dataID');
	$skipEndTime	= OT::GetInt('skipEndTime');
	$skipState		= OT::GetInt('skipState');
		if ($dataID <= 0){
			JS::AlertEnd('该记录ID错误.');
		}

	$row = $DB->GetRow('select TX_type,TX_type2,TX_dataID,TX_payMode,TX_price,TX_cost,TX_startTime,TX_endTime,TX_state from '. OT_dbPref .'tixing where TX_ID='. $dataID);
	if (! $row){
		JS::AlertEnd('该记录不存在。');
	}
	if ($row['TX_state'] != 0){
		JS::AlertEnd('该记录状态不是待确认['. $row['TX_state'] .']，不允许操作。');
	}
	$backMoney = floatval($row['TX_price']);
	$type = $row['TX_type'];

	switch ($type){
		case 'domain':
			$typeCN		= '域名';
			$part		= 'DD_';
			$tabName	= 'domainData';
			$MB->IsSecMenuRight('alert',395,$dataType);
			break;

		case 'host':
			$typeCN		= '虚拟主机';
			$part		= 'HU_';
			$tabName	= 'hostUsers';
			$MB->IsSecMenuRight('alert',329,$dataType);
			break;

		case 'vpsApi':
			$typeCN		= '云服务器';
			$part		= 'VAU_';
			$tabName	= 'vpsApiUsers';
			$MB->IsSecMenuRight('alert',359,$dataType);
			break;

		case 'dialing':
			$typeCN		= '拨号VPS';
			$part		= 'DU_';
			$tabName	= 'dialingUsers';
			$MB->IsSecMenuRight('alert',450,$dataType);
			break;

		case 'serv':
			$typeCN		= '服务器租用';
			$part		= 'SU_';
			$tabName	= 'servUsers';
			$MB->IsSecMenuRight('alert',374,$dataType);
			break;

		case 'ssl':
			$typeCN		= 'SSL证书';
			$part		= 'SD_';
			$tabName	= 'sslUsers';
			$MB->IsSecMenuRight('alert',522,$dataType);
			break;

		default :
			JS::AlertEnd('该记录类型未知['. $type .']，不支持操作。');
			break;
	}
	$chkexe = $DB->query('select '. $part .'userID,'. $part .'username,'. $part .'apiID,'. $part .'proDataName,'. $part .'payMode,'. $part .'price,'. $part .'cost,'. $part .'vpsName,'. $part .'time,'. $part .'endTime,'. $part .'vpsEndTime,'. $part .'xufeiNum,'. $part .'state from '. OT_dbPref . $tabName .' where '. $part .'ID='. $row['TX_dataID']);
	if (! $rs = $chkexe->fetch()){
		JS::AlertEnd('该机器记录不存在。');
	}
	$userID = $rs[$part .'userID'];

	$judKaitong = false;
	if ($mode == 'yes'){
		$recomId = 0;
		$proArr = $listArr = array();
		$modeStr = '同意';
		if ($row['TX_type2'] == '开通'){
			if ($rs[$part .'apiID'] != 0){
				JS::AlertEnd('提醒开通只有无接口机器才能使用，有接口机器请手动开通。');
			}
			if ($skipEndTime == 0 && strtotime($row['TX_endTime']) != strtotime($rs[$part .'endTime'])){
				JS::AlertEnd('原到期时间（'. $row['TX_endTime'] .'）与机器当前到期时间（'. $rs[$part .'endTime'] .'）不匹配，不允许操作。');
			}
			if ($skipState == 0 && $rs[$part .'state'] != 5){ JS::AlertEnd('该机器状态不是“开通中”，禁止操作。'); }

			$judKaitong = true;

			$record = array();
			$record[$part .'state']	= 10;

			// 如有推荐人，推荐人推广佣金处理
			$vpsSysArr = Cache::PhpFile('vpsSys');
			if (strpos($vpsSysArr['VS_commArea'],'|'. $type .'KaiAdmin|') !== false){
				$urs = $DB->GetRow('select UE_ID,UE_username,UE_recomId,UE_recomUser,UE_money from '. OT_dbPref .'users where UE_ID='. $rs[$part .'userID']);
				$userID		= $urs['UE_ID'];
				$username	= $urs['UE_username'];
				$recomId	= $urs['UE_recomId'];
				$recomUser	= $urs['UE_recomUser'];
				$userMoney	= floatval($urs['UE_money']);
				unset($urs);

				if ($recomId > 0){
					$urs = $DB->GetRow('select UE_makeMoney,UE_makingMoney from '. OT_dbPref .'users where UE_ID='. $recomId);
					$makeMoney	= floatval($urs['UE_makeMoney']);
					$makingMoney= floatval($urs['UE_makingMoney']);
					unset($urs);

					$userPrice	= $rs[$part .'price'];
					$proDataName= $rs[$part .'proDataName'];
					$vpsName	= $rs[$part .'vpsName'];
					$endTime	= $rs[$part .'endTime'];
					$proPrice	= abs($userPrice);
					$money = $proPrice * $vpsSysArr['VS_'. $type .'KaiRate'];
					if ($money > $vpsSysArr['VS_'. $type .'KaiMax']){ $money = $vpsSysArr['VS_'. $type .'KaiMax']; }
					$proArr = array(
						'GI_source'			=> '前台',
						'GI_userID'			=> $recomId,
						'GI_username'		=> $recomUser,
						'GI_proUserID'		=> $userID,
						'GI_proUsername'	=> $username,
						'GI_proType'		=> $type,
						'GI_proID'			=> $row['TX_dataID'],
						'GI_proName'		=> $proDataName,
						'GI_proPrice'		=> $proPrice,
						'GI_vpsName'		=> $vpsName,
						'GI_endTime'		=> $endTime,
						'GI_mode'			=> '开通',
						'GI_rate'			=> $vpsSysArr['VS_'. $type .'KaiRate'],
						'GI_money'			=> $money,
						);
					$listArr = array(
						'GH_makingMoney'	=> $makingMoney + $money,
						'GH_makeMoney'		=> $makeMoney,
						);
				}
			}

		}elseif ($row['TX_type2'] == '续费'){
			if ($rs[$part .'apiID'] != 0){
				JS::AlertEnd('提醒续费只有无接口机器才能使用，有接口机器请手动续费。');
			}
			if ($skipEndTime == 0 && strtotime($row['TX_startTime']) != strtotime($rs[$part .'endTime'])){
				JS::AlertEnd('原到期时间（'. $row['TX_startTime'] .'）与机器当前到期时间（'. $rs[$part .'endTime'] .'）不匹配，不允许操作。');
			}
			if ($skipState == 0 && $rs[$part .'state'] != 15){ JS::AlertEnd('该机器状态不是“续费中”，禁止操作。'); }

			$vpsEndTime = IdcArea::NewEndTime($row['TX_payMode'], $rs[$part .'vpsEndTime']);

			$record = array();
			$record[$part .'payMode']		= $row['TX_payMode'];
			$record[$part .'price']			= $row['TX_price'];
			$record[$part .'cost']			= $row['TX_cost'];
			$record[$part .'endTime']		= $row['TX_endTime'];
			$record[$part .'vpsEndTime']	= $vpsEndTime;
			$record[$part .'xufeiNum']		= $part .'xufeiNum+1';
			$record[$part .'state']			= 10;

			// 如有推荐人，推荐人推广佣金处理
			$vpsSysArr = Cache::PhpFile('vpsSys');
			if (strpos($vpsSysArr['VS_commArea'],'|'. $type .'XuAdmin|') !== false && $rs[$part .'xufeiNum'] < $vpsSysArr['VS_'. $type .'XuNum']){
				$urs = $DB->GetRow('select UE_ID,UE_username,UE_recomId,UE_recomUser,UE_money from '. OT_dbPref .'users where UE_ID='. $rs[$part .'userID']);
				$userID		= $urs['UE_ID'];
				$username	= $urs['UE_username'];
				$recomId	= $urs['UE_recomId'];
				$recomUser	= $urs['UE_recomUser'];
				$userMoney	= floatval($urs['UE_money']);
				unset($urs);

				if ($recomId > 0){
					$urs = $DB->GetRow('select UE_makeMoney,UE_makingMoney from '. OT_dbPref .'users where UE_ID='. $recomId);
					$makeMoney	= floatval($urs['UE_makeMoney']);
					$makingMoney= floatval($urs['UE_makingMoney']);
					unset($urs);

					$userPrice	= $rs[$part .'price'];
					$proDataName= $rs[$part .'proDataName'];
					$vpsName	= $rs[$part .'vpsName'];
					$endTime	= $rs[$part .'endTime'];
					$proPrice	= abs($userPrice);
					$money = $proPrice * $vpsSysArr['VS_'. $type .'XuRate'];
					if ($money > $vpsSysArr['VS_'. $type .'XuMax']){ $money = $vpsSysArr['VS_'. $type .'XuMax']; }
					$proArr = array(
						'GI_source'			=> '前台',
						'GI_userID'			=> $recomId,
						'GI_username'		=> $recomUser,
						'GI_proUserID'		=> $userID,
						'GI_proUsername'	=> $username,
						'GI_proType'		=> $type,
						'GI_proID'			=> $row['TX_dataID'],
						'GI_proName'		=> $proDataName,
						'GI_proPrice'		=> $proPrice,
						'GI_vpsName'		=> $vpsName,
						'GI_endTime'		=> $endTime,
						'GI_mode'			=> '续费',
						'GI_rate'			=> $vpsSysArr['VS_'. $type .'XuRate'],
						'GI_money'			=> $money,
						);
					$listArr = array(
						'GH_makingMoney'	=> $makingMoney + $money,
						'GH_makeMoney'		=> $makeMoney,
						);
				}
			}

		}elseif ($row['TX_type2'] == '退款'){
			if ($skipState == 0 && $rs[$part .'state'] != 8){ JS::AlertEnd('该机器状态不是“退款中”，禁止操作。'); }
			if (OT::FloatCmp($backMoney,$rs[$part .'price']) != 0){
				JS::AlertEnd('提醒退款金额：'. $backMoney .'元与机器记录的价格：'. $rs[$part .'price'] .'元不一致，操作停止。');
			}

			// 如有推荐人，推荐人推广佣金处理
			$chkrs = $DB->GetRow('select * from '. OT_dbPref .'gainItem where GI_proID='. $row['TX_dataID'] .' and GI_proType='. $DB->ForStr($type) .' order by GI_ID DESC limit 1');
			if ($chkrs){
				if ($chkrs['GI_money'] > 0 && strtotime($chkrs['GI_endTime']) > time()){
					$urs = $DB->GetRow('select UE_makeMoney,UE_makingMoney from '. OT_dbPref .'users where UE_ID='. $chkrs['GI_userID']);
					$makeMoney	= floatval($urs['UE_makeMoney']);
					$makingMoney= floatval($urs['UE_makingMoney']);
					unset($urs);

					$proArr = array(
						'GI_ID'				=> $chkrs['GI_ID'],
						'GI_source'			=> '前台',
						'GI_money'			=> 0,
						'GI_isPay'			=> 0,
						'GI_state'			=> 9
						);
					$listArr = array(
						'GH_userID'		=> $chkrs['GI_userID'],
						'GH_username'		=> $chkrs['GI_username'],
						'GH_proUserID'		=> $chkrs['GI_proUserID'],
						'GH_proUsername'	=> $chkrs['GI_proUsername'],
						'GH_proType'		=> $chkrs['GI_proType'],
						'GH_proID'			=> $chkrs['GI_proID'],
						'GH_proName'		=> $chkrs['GI_proName'],
						'GH_proPrice'		=> $chkrs['GI_proPrice'],
						'GH_vpsName'		=> $chkrs['GI_vpsName'],
						'GH_endTime'		=> $row['TX_endTime'],
						'GH_mode'			=> '退款',
						'GH_money'			=> $chkrs['GI_money']*(-1),
						'GH_makingMoney'	=> $makingMoney - $chkrs['GI_money'],
						'GH_makeMoney'		=> $makeMoney,
						);
					AppGain::RevItemData($proArr, $listArr, '退款');
				}
			}
			unset($chkrs);

			$record = array();
			$record[$part .'state']		= 9;
			$record[$part .'isUserPay']	= 0;

		}else{
			JS::AlertEnd('TX_type2 信息不对。');
		}

		$result = $DB->UpdateParam($tabName, $record, $part .'ID='. $row['TX_dataID']);
		if ($result){
			$alertRes='成功';
			$dataArr = array();
			$dataArr['TX_state']		= 1;
			$dataArr['TX_adminID']		= $MB->mMbRow['MB_ID'];
			$dataArr['TX_adminName']	= $MB->mMbRow['MB_realname'];
			$dataArr['TX_adminTime']	= TimeDate::Get();
			$DB->UpdateParam('tixing', $dataArr, 'TX_ID='. $dataID);

			if (in_array($row['TX_type2'], array('开通','续费'))){
				if ($judKaitong){
					$DB->UpdateParam('users',array('UE_'. $type .'Num'=>'UE_'. $type .'Num+1'),'UE_ID='. $userID);
				}

				// 如有推荐人，推荐人推广佣金处理
				if ($recomId > 0 && count($proArr) > 0 && count($listArr) > 0){
					AppGain::AddItemData($proArr, $listArr);
				}
			}
		}else{
			$alertRes='失败';
		}

		$userMoney = floatval($DB->GetOne('select UE_money from '. OT_dbPref .'users where UE_ID='. $rs[$part .'userID']));

		$alertStr = '';
		$scoreArr = array();
		if (in_array($row['TX_type2'], array('开通','续费'))){
			$userGain = $backMoney - $row['TX_cost'];
			if ($userGain < 0){ $userGain = 0; }
			$scoreArr['MR_money']		= 0;
			$scoreArr['MR_remMoney']	= -9;
			$scoreArr['MR_cost']		= $row['TX_cost'];
			$scoreArr['MR_bank']		= '';
			$scoreArr['MR_note']		= $row['TX_type2'] .'审核通过！产品：'. $rs[$part .'proDataName'] .'；机器编码：'. $rs[$part .'vpsName'] .'；';
		}else{
			$userGain = $backMoney - $rs[$part .'cost'];
			if ($userGain > 0){ $userGain = 0; }
			$scoreArr['MR_money']		= $backMoney;
			$scoreArr['MR_remMoney']	= $userMoney + $backMoney;
			$scoreArr['MR_cost']		= $rs[$part .'cost'];
			$scoreArr['MR_bank']		= '账户余额';
			$scoreArr['MR_note']		= $row['TX_type2'] .'审核通过！产品：'. $rs[$part .'proDataName'] .'；机器编码：'. $rs[$part .'vpsName'] .'；退款：'. $backMoney .'元；';
		}
		$scoreArr['MR_userID']		= $rs[$part .'userID'];
		$scoreArr['MR_username']	= $rs[$part .'username'];
		$scoreArr['MR_dataID']		= $row['TX_dataID'];
		$scoreArr['MR_dataName']	= $rs[$part .'vpsName'];
		$scoreArr['MR_type']		= $row['TX_type'];
		$scoreArr['MR_type2']		= $row['TX_type2'];
		$scoreArr['MR_gain']		= $userGain;
		$scoreArr['MR_adminID']		= $MB->mMbRow['MB_ID'];
		$scoreArr['MR_adminName']	= $MB->mMbRow['MB_realname'];
		$mrRes = AppMoneyRecord::AddData($scoreArr);
			if (! $mrRes){
				$alertStr .= '\n财务信息记录失败（需要“财务基础包”插件支持）！';
			}elseif ($row['TX_type2'] == '退款'){
				$result = $DB->UpdateParam('users',array('UE_money'=>'UE_money+'. $backMoney,'UE_payMoney'=>'UE_payMoney-'. $backMoney),'UE_ID='. $rs[$part .'userID']);
				if (! $result){
					JS::AlertBackEnd('用户退款处理失败，请刷新页面重试下。');
					$alertStr .= '\n用户原金额 '. $userMoney .' 元，退款 '. $backMoney .' 元，现金额 '. OT::NumFormat($userMoney + $backMoney) .' 元';
				}
			}

	}elseif ($mode == 'no'){
		$modeStr = '拒绝';
		if ($row['TX_type2'] == '开通'){
			if ($skipState == 0 && $rs[$part .'state'] != 5){ JS::AlertEnd('该机器状态不是“开通中”，禁止操作。'); }

			$record = array();
			$record[$part .'endTime']	= $rs[$part .'time'];
			$record[$part .'isUserPay']= 0;
			$record[$part .'state']	= 6;

		}elseif ($row['TX_type2'] == '续费'){
			if ($skipState == 0 && $rs[$part .'state'] != 15){ JS::AlertEnd('该机器状态不是“续费中”，禁止操作。'); }

			$record = array();
			$record[$part .'state']	= 10;

		}elseif ($row['TX_type2'] == '退款'){
			if ($skipState == 0 && $rs[$part .'state'] != 8){ JS::AlertEnd('该机器状态不是“退款中”，禁止操作。'); }
			if (OT::FloatCmp($backMoney,$rs[$part .'price']) != 0){
				JS::AlertEnd('提醒退款金额：'. $backMoney .'元与机器记录的价格：'. $rs[$part .'price'] .'元不一致，操作停止。');
			}

			$record = array();
			$record[$part .'state']	= 10;

		}else{
			JS::AlertEnd('TX_type2 信息不对。');
		}

		$result = $DB->UpdateParam($tabName, $record, $part .'ID='. $row['TX_dataID']);
		if ($result){
			$alertRes='成功';
			$dataArr = array();
			$dataArr['TX_state']		= 2;
			$dataArr['TX_adminID']		= $MB->mMbRow['MB_ID'];
			$dataArr['TX_adminName']	= $MB->mMbRow['MB_realname'];
			$dataArr['TX_adminTime']	= TimeDate::Get();
			$DB->UpdateParam('tixing', $dataArr, 'TX_ID='. $dataID);
		}else{
			$alertRes='失败';
		}

		$alertStr = '';
		$scoreArr = array();
		if (in_array($row['TX_type2'], array('开通','续费'))){
			$scoreArr['MR_money']		= $row['TX_price'];
			$scoreArr['MR_remMoney']	= $userMoney + floatval($row['TX_price']);
			$scoreArr['MR_note']		= $row['TX_type2'] .'被拒绝！产品：'. $rs[$part .'proDataName'] .'；机器编码：'. $rs[$part .'vpsName'] .'；';
		}else{
			$scoreArr['MR_money']		= 0;
			$scoreArr['MR_remMoney']	= -9;
			$scoreArr['MR_note']		= $row['TX_type2'] .'被拒绝！产品：'. $rs[$part .'proDataName'] .'；机器编码：'. $rs[$part .'vpsName'] .'；退款：'. $backMoney .'元；';
		}
		$scoreArr['MR_userID']		= $rs[$part .'userID'];
		$scoreArr['MR_username']	= $rs[$part .'username'];
		$scoreArr['MR_dataID']		= $row['TX_dataID'];
		$scoreArr['MR_dataName']	= $rs[$part .'vpsName'];
		$scoreArr['MR_type']		= $row['TX_type'];
		$scoreArr['MR_type2']		= $row['TX_type2'];
		$scoreArr['MR_bank']		= '账户余额';
		$scoreArr['MR_adminID']		= $MB->mMbRow['MB_ID'];
		$scoreArr['MR_adminName']	= $MB->mMbRow['MB_realname'];
		$mrRes = AppMoneyRecord::AddData($scoreArr);
			if (! $mrRes){
				$alertStr .= '\n财务信息记录失败（需要“财务基础包”插件支持）！';
			}elseif (in_array($row['TX_type2'], array('开通','续费'))){
				$result = $DB->UpdateParam('users',array('UE_money'=>'UE_money+'. $row['TX_price'],'UE_payMoney'=>'UE_payMoney-'. $row['TX_price']),'UE_ID='. $rs[$part .'userID']);
				if (! $result){ $alertStr .= '\n用户余额扣款失败。'; }
			}

	}else{
		JS::AlertEnd('mode参数不对。');

	}

	echo('
	<script language="javascript" type="text/javascript">
	alert("'. $modeStr . $row['TX_type2'] .'处理'. $alertRes .'！'. $alertStr .'");
	');
	if ($mudi2 == 'det'){
		echo('parent.document.location.reload();');
	}else{
		echo('parent.$id("tixingData'. $dataID .'").style.display="none";');
	}
	echo('
	</script>
	');
}



// 批量删除
function MoreDel(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$systemArr;

	$backURL	= OT::PostStr('backURL');
	$dataType	= OT::PostStr('dataType');
	$dataTypeCN	= OT::PostStr('dataTypeCN');
	$selDataID	= OT::Post('selDataID');

	$selCount = OT::NewCount($selDataID);
	if ($selCount <= 0){
		JS::AlertBackEnd('请先选择要删除的记录.');
	}

	$whereStr = '';
	for ($i=0; $i<$selCount; $i++){
		$whereStr .= ','. intval($selDataID[$i]);
	}
	if ($whereStr == ''){
		JS::AlertBackEnd('请先选择要删除的记录.');
	}

	$todayDate = TimeDate::Get('date');

	$alertStr = '';
	$dataNum = $failNum = $succNum = 0;
	$delIdArr = array();
	$delrec=$DB->query('select VAU_ID,VAU_proDataName,VAU_ipStr,VAU_endTime,VAU_state from '. OT_dbPref .'vpsApiUsers where VAU_ID in (0'. $whereStr .')');
	while ($row = $delrec->fetch()){
		$dataNum ++;
		$dayDiff = TimeDate::Diff('d', $row['VAU_endTime'], $todayDate);
		if ($dayDiff >= 8 || in_array($row['VAU_state'],array(0,3,6,9))){
			$succNum ++;
			$delIdArr[] = $row['VAU_ID'];

			AdmIdc::AddDelUsers('vpsApi', $row['VAU_ID']);

		}elseif ($dayDiff < 8){
			$failNum ++;
			if ($dayDiff < 0){ $endStr='还有'. abs($dayDiff) .'天才到期'; }else{ $endStr='只有'. $dayDiff .'天，不到8天'; }
			$alertStr .= '\n'. $failNum .'、'. $row['VAU_proDataName'] .'['. $row['VAU_ipStr'] .'] 到期时间（'. TimeDate::Get('date',$row['VAU_endTime']) .'）'. $endStr .'，不允许删除';

		}else{
			$failNum ++;
			$alertStr .= '\n'. $failNum .'、'. $row['VAU_proDataName'] .'['. $row['VAU_ipStr'] .'] 状态为 '. IdcInfo::State($row['VAU_state'],'cn') .'，不予许删除！';

		}
	}
	unset($delrec);

	if (count($delIdArr) > 0){
		$idStr = implode(',',$delIdArr);
		$DB->query('delete from '. OT_dbPref .'vpsApiUsers where VAU_ID in ('. $idStr .')');
		$DB->query('delete from '. OT_dbPref .'tixing where TX_dataID in ('. $idStr .')');
	}

	Adm::AddLog(array(
		'note'		=> '【'. $dataTypeCN .'】批量删除(共'. $dataNum .'条，成功'. $succNum .'条，失败'. $failNum .'条)！',
		));

	JS::AlertHrefEnd('批量删除(共'. $dataNum .'条，成功'. $succNum .'条，失败'. $failNum .'条).'. $alertStr,$backURL);

}

?>