<?php
/*
copyright: copyright 2004 Php Image Host. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE
*/

require_once(dirname(__FILE__).'/includes/config.inc.php');
require_once(dirname(__FILE__).'/../classes/paypalpayments.class.php');

$payments =& new paypalpayments($ace);

$message = '';
$errors = array();
$criteria = array();

$ids = isset($_POST['ids']) ? $ace->getids($_POST['ids']) : array();
if( count($ids) > 0 ){
	if( $ace->getstr('delete', $_POST) != '' ){
		$message = $payments->deletepayments($ids)." payment(s) deleted.";
	}
}

$perpage = $ace->getint('perpage', $_REQUEST, 10);

$fromdate = $todate = '';
$from = $ace->getstr('fromdate', $_REQUEST);
if( $from != '' ){
	if( ($tm = @strtotime($from)) !== -1 ){
		$fromdate = date('Y-m-d', $tm);
	}
}
$to = $ace->getstr('todate', $_REQUEST);
if( $to != '' ){
	if( ($tm = @strtotime($to)) !== -1 ){
		$todate = date('Y-m-d', $tm);
	}
}

$username = $ace->getstr('username', $_REQUEST);
$email = $ace->getstr('email', $_REQUEST);
$paypalemail = $ace->getstr('paypalemail', $_REQUEST);
$userid = $ace->getint('userid', $_REQUEST);
$txnid = $ace->getstr('txnid', $_REQUEST);

$orderby = $ace->getstr('orderby', $_REQUEST);
$orderdir = $ace->getstr('orderdir', $_REQUEST);

if( !isset($payments->orderbys[$orderby]) ) $orderby = 'dated';
$orderdir = $ace->getstr('orderdir', $_REQUEST, 'asc');
if( $orderdir != 'asc' && $orderdir != 'desc' ) $orderdir = 'asc';

if( $username != '' ) $criteria['username'] = $username;
if( $email != '' ) $criteria['email'] = $email;
if( $paypalemail != '' ) $criteria['paypalemail'] = $paypalemail;
if( $txnid != '' ) $criteria['txnid'] = $txnid;
if( $userid != 0 ) $criteria['userid'] = $userid;

if( $todate != '' ) $criteria['to'] = $todate;
if( $fromdate != '' ) $criteria['from'] = $fromdate;

$criteria['count'] = true;

$numpayments = $payments->getpayments($criteria);

$numpages = ceil($numpayments / $perpage);

$page = $ace->getint('page', $_REQUEST, 1);
if( $page < 1 ) $page = 1;
if( $page > $numpages ) $page =$numpages;

$first = ($page-1) * $perpage;
$last = min($numpayments,(($page) * $perpage));

$criteria['count'] = false;

if( $numpayments > 0 ) $pays = $payments->getpayments($criteria, $orderby, $orderdir, $first, $perpage);

$pageurl = $_SERVER['SCRIPT_NAME']."?fromdate=$fromdate&todate=$todate&email=$email&paypalemail=$paypalemail&userid=$userid&txnid=$txnid&username=$username&perpage=$perpage&page={page}&orderby=$orderby&orderdir=$orderdir";

require_once(dirname(__FILE__).'/includes/header.inc.php');

?>
<script language="JavaScript" type="text/javascript">
<!--
function sp(pid){
	url = 'showpayment.php?p='+pid;
	win = window.open(url, '_blank', 'width=400,height=600,scrollbars=yes,toolbar=no');
	return false;
}
//-->
</script>
<div style="text-align: center;">
<b>Paypal Payments</b><br /><br />
This page lets you view and delete the payments you have received from PayPal.
Use the form on the left to filter
which payments are displayed.
<br /><br />
<table border="0" cellspacing="0" cellpadding="10" width="95%">
<tr>
<td style="text-align: center; vertical-align: top; width: 150px; color: #555555; font-weight: bold;">
<div style="padding: 5px; border: 2px solid #555555; ">
<div style="border-bottom: 2px solid #555555; font-weight: bold;text-align: center;padding: 3px;margin-bottom: 5px;">Search Payments</div>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="get">
Username<br />
<input type="text" name="username" value="<?php echo htmlspecialchars($username);?>" />
<br /><br />User Email<br />
<input type="text" name="email" value="<?php echo htmlspecialchars($email);?>" />
<br /><br />PayPal Email<br />
<input type="text" name="paypalemail" value="<?php echo htmlspecialchars($paypalemail);?>" />
<br /><br />PayPal Txn Id#<br />
<input type="text" name="txnid" value="<?php echo htmlspecialchars($txnid);?>" />
<br /><br />Dated Between<br />
<input type="text" name="fromdate" value="<?php echo htmlspecialchars($fromdate);?>" />
<br />and<br />
<input type="text" name="todate" value="<?php echo htmlspecialchars($todate);?>" />
<br />(enter dates in format yyyy-mm-dd).
<br /><br />Order By<br />
<select name="orderby">
<?php
	foreach( $payments->orderbys as $n=>$v ){
		$sel = $n == $orderby ? ' SELECTED ' : '';
		echo "<option value=\"$n\"$sel>$v</option>\n";
	}
?>
</select>
<br />
<select name="orderdir">
	<option value="asc">ascending</option>
	<option value="desc"<?php if( $orderdir == 'desc' ) echo ' SELECTED ';?>>descending</option>
</select>
<br /><br />Display<br />
<input style="width: 40px;" type="text" name="perpage" value="<?php echo $perpage;?>" />
<br />Per Page
<br /><br /><input type="submit" value="List Payments" />
</form>
</div>
</td>
<td style="text-align: center; vertical-align: top;">

<div class="errors">
<?php if( $message != '' ) echo $message."<br /><br />";?>
<?php if( count($errors) > 0 ) echo join("<br />", $errors)."<br /><br />";?>
<?php echo $numpayments;?> payment(s) matched your criteria.
</div>
<?php
if( $numpayments > 0 ){
?>
Listing PayPal Payments <?php echo ($first+1).' to '.$last.' of '.$numpayments.'.';?><br /><br />
Page <?php echo $page.' of '.$numpages.'.';?><br /><br />
View Page:
<?php
	for( $i = 1; $i <= $numpages; $i++){
?>
<a href="<?php echo str_replace('{page}', $i,$pageurl);?>"><?php echo $i;?></a>
<?php
	}
?>
<br /><br />
<form name="iform" id="iform" action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<input type="hidden" name="email" value="<?php echo htmlspecialchars($email);?>" />
<input type="hidden" name="paypalemail" value="<?php echo htmlspecialchars($paypalemail);?>" />
<input type="hidden" name="txnid" value="<?php echo htmlspecialchars($txnid);?>" />
<input type="hidden" name="username" value="<?php echo htmlspecialchars($username);?>" />
<input type="hidden" name="orderby" value="<?php echo $orderby;?>" />
<input type="hidden" name="orderdir" value="<?php echo $orderdir;?>" />
<input type="hidden" name="perpage" value="<?php echo htmlspecialchars($perpage);?>" />
<table class="formtable" cellspacing="1" cellpadding="0" border="0" align="center" style="background-color: #555555;">
<tr>
	<th>&nbsp;</th>
	<th>Transaction Id#</th>
	<th>From (PP Email)</th>
	<th>Username</th>
	<th>User Email</th>
	<th>Date</th>
	<th>Amount</th>
</tr>
<?php
	$tr = 0;
	foreach( $pays as $p ){
?>
<tr class="tr<?php echo ($tr+1);?>">
	<td>
		<input type="checkbox" name="ids[]" value="<?php echo $p->payment_id;?>" />
	</td>
	<td style="text-align: left;">
		<a onClick="return sp(<?php echo $p->payment_id;?>);" target="_blank" href="showpayment.php?p=<?php echo $p->payment_id;?>"><?php echo htmlspecialchars($p->txn_id);?></a>
	</td>
	<td style="text-align: center;"><?php echo htmlspecialchars($p->paypal_email);?></td>
	<td><a href="users.php?userid=<?php echo $p->user_id;?>"><?php echo $p->username;?></a></td>
	<td><?php echo $p->email;?></td>
	<td><?php echo date('jS F Y @ H:i', strtotime($p->dated));?></td>
	<td style="text-align: right;">$<?php echo number_format($p->amount,2);?></td>
</tr>
<?php
		$tr = 1 - $tr;
	}
?>
</table>
<br clear="all" />
<input type="button" onClick="check(this.form,true);" value="Select All" />
<input type="button" onClick="check(this.form,false);" value="Select None" />
<input type="button" onClick="invert(this.form);" value="Invert Selection" />
<noscript>
<br />The buttons above require javascript to be enabled in order to function.
</noscript>
<br /><br />
<input type="submit" name="delete" value="Delete Selected Payments" onClick="return confirm('Are you sure you want to delete all the selected payments from the database? NB. This will NOT refund the payments from your PayPal account.');" />
</form>
<br />
View Page:
<?php
	for( $i = 1; $i <= $numpages; $i++){
?>
<a href="<?php echo str_replace('{page}', $i,$pageurl);?>"><?php echo $i;?></a>
<?php
	}
?>
<br /><br />
Page <?php echo $page.' of '.$numpages.'.';?><br /><br />
Listing Payments <?php echo ($first+1).' to '.$last.' of '.$numpayments.'.';?>
<?php
}
?>
</td>
</tr>
</table>
</div>
<?php


require_once(dirname(__FILE__).'/includes/footer.inc.php');

?>
