<?php
require_once(dirname(__FILE__).'/../../config.php');
require_once(DIR_SYSTEM.'/users/class.session.user.php');

$USER_ID	=	$_SESSION[USER_SESSION]['0']['user_id'];

$t	=	$prepare->sanitize($_GET['t'],'input');

if ( $t == 'withdrawal' ) {
$value	=	$prepare->sanitize($_POST['value'],'input');

//*** publishers_withdrawals ***//

$pub_withdrawal_id		=	$prepare->sanitize( $_POST['id'], 'input' );
$pub_withdrawal_user_id	=	$USER_ID;
$pub_withdrawal_type	=	'1';
$pub_withdrawal_email	=	'';
$pub_withdrawal_amount	=	$prepare->sanitize( $_POST['value'], 'input' );
$pub_withdrawal_details	=	'-';
$pub_withdrawal_date	=	strtotime( "now" );
$pub_withdrawal_status	=	'0';



$checkMoney	=	$db->Query("SELECT `pub_profile_money` FROM publishers_profiles where pub_profile_user_id=?","i",$USER_ID);


if ( ( $checkMoney['0']['pub_profile_money'] > WITHDRAWAL_MINIMUM ) && ( $pub_withdrawal_amount > WITHDRAWAL_MINIMUM ) ) {

$insert = $db->NoQuery("INSERT INTO publishers_withdrawals VALUES(NULL,?,?,?,?,?,?,?)","sssssss",$pub_withdrawal_user_id,$pub_withdrawal_type,$pub_withdrawal_email,$pub_withdrawal_amount,$pub_withdrawal_details,$pub_withdrawal_date,$pub_withdrawal_status);

$db->NoQuery("UPDATE publishers_profiles SET pub_profile_money=pub_profile_money-".$pub_withdrawal_amount." WHERE pub_profile_user_id=?","i",$USER_ID);


	if ( $insert ) {
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_PUB_MONEY_REQUEST_ADDED"].'</div>';
	} else {
		echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_PUB_MONEY_ERROR"].'</div>';
	}

} else {
		echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_PUB_MONEY_NOT_ENOUGHT"].'</div>';
}

}




if ( $t == 'status' ) {
	$id		=	$prepare->sanitize($_POST['id'],'input');
	$status	=	$prepare->sanitize($_POST['m'],'input');

//*** GET ACTUAL STATUS STATUS ***//
$checkStatus	=	$db->Query("SELECT `pub_withdrawal_status` FROM publishers_withdrawals where pub_withdrawal_user_id=? and pub_withdrawal_id=?","ii",$USER_ID,$id);

	if ( $status == 'cancel' ) {//*** START THE CANCEL STATUS ***//

	if ( $checkStatus['0']['pub_withdrawal_status'] == 0 ) {//*** ALLOW USER TO CANCEL THE PAYMENT ***//

	$checkMoney	=	$db->Query("SELECT `pub_withdrawal_amount` FROM publishers_withdrawals where pub_withdrawal_user_id=? and pub_withdrawal_id=?","ii",$USER_ID,$id);



		$cancelPayment = $db->NoQuery("UPDATE publishers_withdrawals SET pub_withdrawal_status=? WHERE pub_withdrawal_user_id=? and pub_withdrawal_id=?","iii",3,$USER_ID,$id);
	if ( $cancelPayment ) { 
			echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_MONEY_HISTORY_CANCEL_MSJ_OK"].'</div>';

	$db->NoQuery("UPDATE publishers_profiles SET pub_profile_money=pub_profile_money+".$checkMoney['0']['pub_withdrawal_amount']." WHERE pub_profile_user_id=?","i",$USER_ID);

		} else {
			echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_MONEY_HISTORY_CANCEL_MSJ_ERROR_RETRY"].'</div>';
		}
	}

	if ( $checkStatus['0']['pub_withdrawal_status'] == 4 ) {//*** CHECK IF ADMIN IS OPENING THIS REQUEST ***//
			echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_MONEY_HISTORY_CANCEL_MSJ_ERROR_ADMIN_CHECKING"].'</div>';
	}

	if ( $checkStatus['0']['pub_withdrawal_status'] == 1 ) {//*** ALREADY APPROVED ***//
			echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_MONEY_HISTORY_CANCEL_MSJ_APPROVED"].'</div>';
	}

	if ( $checkStatus['0']['pub_withdrawal_status'] == 2 ) {//*** ALREADY REJECTED ***//
			echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_MONEY_HISTORY_CANCEL_MSJ_REJECTED"].'</div>';
	}

	if ( $checkStatus['0']['pub_withdrawal_status'] == 3 ) {//*** ALREADY CANCELED ***//
			echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_MONEY_HISTORY_CANCEL_MSJ_CANCELED"].'</div>';
	}





	}//*** END START THE CANCEL STATUS ***//
}




if ( $t == 'payment-profile' ) {


//*** publisher_payment_paypal ***//
$ppp_profile_user_id	=	$USER_ID;
$ppp_first_name			=	$prepare->sanitize( $_POST['fname'], 'input' );
$ppp_last_name			=	$prepare->sanitize( $_POST['lname'], 'input' );
$ppp_paypal_id			=	$prepare->sanitize( $_POST['email'], 'input' );

$vpp	=	$db->Query("SELECT `ppp_profile_user_id` FROM publisher_payment_paypal where ppp_profile_user_id=?","i",$USER_ID);

	if ( empty ( $vpp ) ) { //*** CREATE PROFILE ***//

	$create = $db->NoQuery("INSERT INTO publisher_payment_paypal VALUES(?,?,?,?)","ssss",$ppp_profile_user_id,$ppp_first_name,$ppp_last_name,$ppp_paypal_id);

	if ( $create ) {
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_MONEY_SETTINGS_CREATED"].'</div>';
	} else {
		echo '<div id="mode">2</div><div id="message">'.$LANG["WBS_MONEY_SETTINGS_ERROR"].'</div>';
	}
	} else { //*** JUST UPDATE PROFILE ***//

	$updateProfile = $db->NoQuery("UPDATE publisher_payment_paypal SET ppp_first_name=?, ppp_last_name=?, ppp_paypal_id=? WHERE ppp_profile_user_id=?","sssi",$ppp_first_name,$ppp_last_name,$ppp_paypal_id,$ppp_profile_user_id);

	if ( $updateProfile ) {
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_MONEY_SETTINGS_UPDATED"].'</div>';
	} else {
		echo '<div id="mode">1</div><div id="message">'.$LANG["WBS_MONEY_SETTINGS_ERROR"].'</div>';
	}
	}




}





















?>