-
Notifications
You must be signed in to change notification settings - Fork 0
/
trades_pending.php
557 lines (370 loc) · 18.6 KB
/
trades_pending.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?
include_once("includes/inc.global.php");
include_once("classes/class.datetime.php");
include("classes/class.trade.php");
/*
An explanation of different member_decisions statuses in the trades_pending database...
1 = Member hasn't made a decision regarding this trade - either it is Open or it has been Fulfilled (see 'status' column)
2 = Member has removed this trade from his own records
3 = Member has rejected this trade
4 = Member has accepted that this trade has been rejected
*/
$p->site_section = EXCHANGES;
$p->page_title = "Exchanges Pending";
$cUser->MustBeLoggedOn();
$pending = new cTradesPending($_SESSION["user_login"]);
$list = "<em>NOTE that only transactions currently pending approval from one member or the other are displayed here. To view your complete
Exchange History please <a href=trade_history.php?mode=self>click here</a>.</em><p><A HREF=trades_pending.php><FONT SIZE=2>Summary</FONT></A>
| <A HREF=trades_pending.php?action=incoming><FONT SIZE=2>Payments to Confirm (".$pending->numToConfirm.")</FONT></A>";
if (MEMBERS_CAN_INVOICE==true) // No point displaying invoice stats if invoicing has been disabled
$list .= " | <A HREF=trades_pending.php?action=outgoing><FONT SIZE=2>Invoices to Pay (".$pending->numToPay.")</FONT></A>";
$list .= " | <A HREF=trades_pending.php?action=payments_sent><FONT SIZE=2>Sent Payments (".$pending->numToHaveConfirmed.")</FONT></A>";
if (MEMBERS_CAN_INVOICE==true) // ditto
$list .= "| <A HREF=trades_pending.php?action=invoices_sent><FONT SIZE=2>Sent Invoices (".$pending->numToBePayed.")</FONT></A><p>";
function initTradeTable() {
$output = "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=3 WIDTH=\"100%\"><TR BGCOLOR=\"#d8dbea\"><TD><FONT SIZE=2><B>Date</B></FONT></TD><TD><FONT SIZE=2><B>From</B></FONT></TD><TD><FONT SIZE=2><B>To</B></FONT></TD><TD ALIGN=RIGHT><FONT SIZE=2><B>". UNITS ." </B></FONT></TD><TD><FONT SIZE=2><B> Description</B></FONT></TD><td><font size=2><b>Action</td></font></td></TR>";
return $output;
}
function closeTradeTable() {
return "</table>";
}
function displayTrade($t,$typ) {
global $cDB;
$fcolor = "#554f4f";
//$fcolor = '#FFFFFF';
if ($t["status"]=='O') {
$bgcolor = "white";
if ($typ=='P')
//$actionTxt = '';
$actionTxt = "<a href=trades_pending.php?action=confirm&tid=".$t["id"].">Accept Payment</a> | <a href=trades_pending.php?action=reject&tid=".$t["id"].">Reject</a>";
else if ($typ=='I')
// $actionTxt = '';
$actionTxt = "<a href=trades_pending.php?action=confirm&tid=".$t["id"].">Pay Invoice</a> |
<a href=trades_pending.php?action=reject&tid=".$t["id"].">Reject</a>";
else if ($typ=='TBC') {
if ($t["member_to_decision"]==3) {
$bgcolor = 'red';
$actionTxt = "<font size=2 color=\"".$fcolor."\">".$t["member_id_to"]." has rejected this transaction. <br>
<a href=trades_pending.php?action=resend&tid=".$t["id"].">[ Resend Payment ]</a> |
<a href=trades_pending.php?action=accept_rejection&tid=".$t["id"].">[ Remove this Notice ]</a></font>";
}
else
$actionTxt = "<font size=2 color=\"".$fcolor."\">Awaiting Confirmation by ".$t["member_id_to"]."...</font>";
}
else if ($typ=='TBP') {
if ($t["member_to_decision"]==3) {
$bgcolor = 'red';
$actionTxt = "<font size=2 color=\"".$fcolor."\">".$t["member_id_to"]." has rejected this transaction. <br>
<a href=trades_pending.php?action=resend&tid=".$t["id"].">[ Resend Invoice ]</a> |
<a href=trades_pending.php?action=accept_rejection&tid=".$t["id"].">[ Remove this Notice ]</a></font>";
}
else
$actionTxt = "<font size=2 color=\"".$fcolor."\">Awaiting Payment from ".$t["member_id_to"]."...</font>";
}
}
else {
$bgcolor = "green";
$fcolor = "#ffffff";
if ($typ=='P')
$actionTxt = "<font size=2 color=\"".$fcolor."\">Payment Accepted!</font>";
else if ($typ=='I')
$actionTxt = "<font size=2 color=\"".$fcolor."\">Invoice Paid!</font>";
else if ($typ=='TBC')
$actionTxt = "<font size=2 color=\"".$fcolor."\">".$t["member_id_to"]." has confirmed!</font>";
else if ($typ=='TBP')
$actionTxt = "<font size=2 color=\"".$fcolor."\">".$t["member_id_to"]." has paid this invoice!</font>";
$actionTxt .= " <font color=\"".$fcolor."\">--</font> <a href=trades_pending.php?action=remove&tid=".$t["id"]."><font size=2 color=\"".$fcolor."\">[ Remove this Notice ]</font></a>";
}
if ($typ=='P')
$output .= "<TR VALIGN=TOP BGCOLOR=". $bgcolor ."><TD><FONT SIZE=2 COLOR=".$fcolor.">". $t["trade_date"]."</FONT></TD><TD><FONT SIZE=2 COLOR=".$fcolor.">". $t["member_id_from"] ."</FONT></TD><TD><FONT SIZE=2 COLOR=".$fcolor.">".$t["member_id_to"]."</FONT></TD><TD ALIGN=RIGHT><FONT SIZE=2 COLOR=".$fcolor.">". $t["amount"] ." </FONT></TD><TD><FONT SIZE=2 COLOR=".$fcolor.">". $cDB->UnEscTxt($t["description"]) ."</FONT></TD>
<td>$actionTxt</td>
</TR>";
else
$output .= "<TR VALIGN=TOP BGCOLOR=". $bgcolor ."><TD><FONT SIZE=2 COLOR=".$fcolor.">". $t["trade_date"]."</FONT></TD><TD><FONT SIZE=2 COLOR=".$fcolor.">". $t["member_id_from"] ."</FONT></TD><TD><FONT SIZE=2 COLOR=".$fcolor.">".$t["member_id_to"]."</FONT></TD><TD ALIGN=RIGHT><FONT SIZE=2 COLOR=".$fcolor.">". $t["amount"] ." </FONT></TD><TD><FONT SIZE=2 COLOR=".$fcolor.">". $cDB->UnEscTxt($t["description"]) ."</FONT></TD>
<td>$actionTxt</td>
</TR>";
return $output;
}
function doTrade($t) {
$member_to = new cMember;
if ($t["typ"]=='T')
$member_to->LoadMember($_SESSION["user_login"]);
else
$member_to->LoadMember($t["member_id_from"]);
$member = new cMember;
if ($t["typ"]=='T')
$member->LoadMember($t["member_id_from"]);
else
$member->LoadMember($_SESSION["user_login"]);
$trade = new cTrade($member, $member_to, htmlspecialchars($t['amount']), htmlspecialchars($t['category']), htmlspecialchars($t['description']),
"T");
$status = $trade->MakeTrade();
if(!$status)
return false;
else {
// Has the recipient got an income tie set-up? If so, we need to transfer a percentage of this elsewhere...
$recipTie = cIncomeTies::getTie($member_to->member_id);
if ($recipTie) {
$theAmount = round(($t['amount']*$recipTie->percent)/100);
$charity_to = new cMember;
$charity_to->LoadMember($recipTie->tie_id);
$trade = new cTrade($member_to, $charity_to, htmlspecialchars($theAmount), htmlspecialchars(12), htmlspecialchars("Donation from ".$member_to->member_id.""), 'T');
$status = $trade->MakeTrade();
}
return true;
}
}
switch($_REQUEST["action"]) {
case("resend"):
$q = "SELECT * FROM trades_pending where id=".$cDB->EscTxt($_GET["tid"])." limit 0,1";
$result = $cDB->Query($q);
if ($result && mysql_num_rows($result)>0) { // Trade Exists
$row = mysql_fetch_array($result);
// Do we have permission to act on this trade?
if ($row["member_id_from"]!=$_SESSION["user_login"]) {
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
}
// Check this is not a 'still Open' trade
if ($row["status"]!='O') {
$list .= "<em>Sorry, only Open trades can be rejected or resent.</em>";
break;
}
if ($row["member_to_decision"]!=3) {
$list .= "<em>This member hasn't rejected this transaction!</em>";
break;
}
$q = "UPDATE trades_pending set member_to_decision=1 where id=".$cDB->EscTxt($row["id"])."";
if ($cDB->Query($q))
$list .= "Transaction re-submitted successfully.";
else
$list .= "<em>Error updating the database.</em>";
}
else
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
case("accept_rejection"):
$q = "SELECT * FROM trades_pending where id=".$cDB->EscTxt($_GET["tid"])." limit 0,1";
$result = $cDB->Query($q);
if ($result && mysql_num_rows($result)>0) { // Trade Exists
$row = mysql_fetch_array($result);
// Do we have permission to act on this trade?
if ($row["member_id_from"]!=$_SESSION["user_login"]) {
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
}
// Check this is not a 'still Open' trade
if ($row["status"]!='O') {
$list .= "<em>Sorry, only Open trades can be rejected.</em>";
break;
}
if ($row["member_to_decision"]!=3) {
$list .= "<em>This member hasn't rejected this transaction!</em>";
break;
}
$q = "UPDATE trades_pending set member_from_decision=4 where id=".$cDB->EscTxt($row["id"])."";
if ($cDB->Query($q))
$list .= "Transaction removed successfully.";
else
$list .= "<em>Error updating the database.</em>";
}
else
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
case("reject"):
$q = "SELECT * FROM trades_pending where id=".$cDB->EscTxt($_GET["tid"])." limit 0,1";
$result = $cDB->Query($q);
if ($result && mysql_num_rows($result)>0) { // Trade Exists
$row = mysql_fetch_array($result);
// Do we have permission to act on this trade?
if ($row["member_id_to"]!=$_SESSION["user_login"]) {
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
}
// Check this is not a 'still Open' trade
if ($row["status"]!='O') {
$list .= "<em>This trade is no longer Open and therefore cannot be rejected.</em>";
break;
}
if ($row["typ"]=='T' && $row["member_id_to"]==$_SESSION["user_login"]) { // We want to reject the payment!
$q = "UPDATE trades_pending set member_to_decision=3 where id=".$cDB->EscTxt($row["id"])."";
}
else if ($row["typ"]=='I' && $row["member_id_to"]==$_SESSION["user_login"]) { // We don't want to pay this invoice!
$q = "UPDATE trades_pending set member_to_decision=3 where id=".$cDB->EscTxt($row["id"])."";
}
if ($cDB->Query($q))
$list .= "Member ".$row["member_id_from"]." has been informed that you have rejected this transaction.";
else
$list .= "<em>Error updating the database.</em>";
}
else
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
case("remove"):
$q = "SELECT * FROM trades_pending where id=".$cDB->EscTxt($_GET["tid"])." limit 0,1";
$result = $cDB->Query($q);
if ($result && mysql_num_rows($result)>0) { // Trade Exists
$row = mysql_fetch_array($result);
// Do we have permission to act on this trade?
if ($row["member_id_from"]!=$_SESSION["user_login"] && $row["member_id_to"]!=$_SESSION["user_login"]) {
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
}
// Check this is not a 'still Open' trade
if ($row["status"]=='O') {
$list .= "<em>This trade is currently marked as Open and thus cannot be removed until the required action has been taken.</em>";
break;
}
if ($row["typ"]=='T' && $row["member_id_from"]==$_SESSION["user_login"]) { // Our sent payment has finally been confirmed!
$q = "UPDATE trades_pending set member_from_decision=2 where id=".$cDB->EscTxt($row["id"])."";
}
else if ($row["typ"]=='T' && $row["member_id_to"]==$_SESSION["user_login"]) { // We have confirmed receipt of a payment!
$q = "UPDATE trades_pending set member_to_decision=2 where id=".$cDB->EscTxt($row["id"])."";
}
else if ($row["typ"]=='I' && $row["member_id_from"]==$_SESSION["user_login"]) { // Our invoice has finally been paid!
$q = "UPDATE trades_pending set member_from_decision=2 where id=".$cDB->EscTxt($row["id"])."";
}
else if ($row["typ"]=='I' && $row["member_id_to"]==$_SESSION["user_login"]) { // We have now paid this invoice!
$q = "UPDATE trades_pending set member_to_decision=2 where id=".$cDB->EscTxt($row["id"])."";
}
if ($cDB->Query($q))
$list .= "Transaction removed successfully.";
else
$list .= "<em>Error updating the database.</em>";
}
else
$list .= "<em>This trade does not exist or you do not have permission to act on it.</em>";
break;
case("confirm"):
$q = "SELECT * FROM trades_pending where id=".$cDB->EscTxt($_GET["tid"])." limit 0,1";
$result = $cDB->Query($q);
if ($result && mysql_num_rows($result)>0) { // Trade Exists
$row = mysql_fetch_array($result);
if ($row["status"]!='O') {
$list .= "<em>This trade has already been confirmed and is now closed.</em>";
break;
}
/* What is the nature of the trade - Payment or Invoice? */
if ($row["typ"]=='T') { // Payment - we are confirming receipt of incoming
// Check we are the intended recipient
if ($row["member_id_to"]!=$_SESSION["user_login"])
$list .= "<em>You do not have permission to confirm this trade.</em>";
else { // Action the trade
if (!doTrade($row))
$list .= "<font color=red>Error confirming payment.</font>";
else {
$cDB->Query("UPDATE trades_pending set status=".$cDB->EscTxt('F')." where id=".$cDB->EscTxt($_GET["tid"])."");
$list .= "<em>You have accepted a payment of ".$row["amount"]." ".UNITS." from ".$row["member_id_from"]."</em>";
}
}
}
else if ($row["typ"]=='I') { // Invoice - we are sending a payment
// Check we are the intended recipient of the invoice
if ($row["member_id_to"]!=$_SESSION["user_login"])
$list .= "<em>You do not have permission to confirm this trade.</em>";
else { // Action the trade
/*
$goingFrom = $_SESSION["user_login"];
$goingTo = $row["member_id_from"];
$row["member_id_to"] = $goingTo;
$row["member_id_from"] = $goingFrom;
*/
if (!doTrade($row)) {
$member = new cMember;
$member->LoadMember($_SESSION["user_login"]);
if ($member->restriction==1) {
$list .= LEECH_NOTICE;
}
else
$list .= "<font color=red>Error sending payment.</font>";
}
else {
$cDB->Query("UPDATE trades_pending set status=".$cDB->EscTxt('F')." where id=".$cDB->EscTxt($_GET["tid"])."");
$list .= "<em>You have sent a payment of ".$row["amount"]." ".UNITS." to ".$row["member_id_from"]."</em>";
}
}
}
}
else // This trade doesn't exist in the database!
$list .= "<em>You have elected to confirm a non-existant trade!</em>";
break;
case("incoming"):
$list .= "<b>The following Incoming Payments require your confirmation...</b><p>";
/*
$cDB->Query("INSERT INTO trades_pending (trade_date, member_id_from, member_id_to, amount, category, description, typ) VALUES (now(), ". $cDB->EscTxt($member->member_id) .", ". $cDB->EscTxt($member_to_id) .", ". $cDB->EscTxt($values["units"]) .", ". $cDB->EscTxt($values["category"]) .", ". $cDB->EscTxt($values["description"]) .", \"T\");");
*/
$q = "SELECT * FROM trades_pending where member_id_to=".$cDB->EscTxt($_SESSION["user_login"])." and typ='T' and member_to_decision = 1";
$result = $cDB->Query($q);
if ($result) {
$list .= initTradeTable();
for($i=0;$i<mysql_num_rows($result);$i++) {
$row = mysql_fetch_array($result);
$list .= displayTrade($row,'P');
}
$list .= closeTradeTable();
}
else
$list .= "<em>None found!</em>";
break;
case("outgoing"):
$list .= "<b>The following Invoices need paying...</b><p>";
$q = "SELECT * FROM trades_pending where member_id_to=".$cDB->EscTxt($_SESSION["user_login"])." and typ='I' and member_to_decision = 1";
$result = $cDB->Query($q);
if ($result) {
$list .= initTradeTable();
for($i=0;$i<mysql_num_rows($result);$i++) {
$row = mysql_fetch_array($result);
$list .= displayTrade($row,'I');
}
$list .= closeTradeTable();
}
else
$list .= "<em>None found!</em>";
break;
case("payments_sent"):
$list .= "<b>You are awaiting confirmation of the following Payments...</b><p>";
/*
$cDB->Query("INSERT INTO trades_pending (trade_date, member_id_from, member_id_to, amount, category, description, typ) VALUES (now(), ". $cDB->EscTxt($member->member_id) .", ". $cDB->EscTxt($member_to_id) .", ". $cDB->EscTxt($values["units"]) .", ". $cDB->EscTxt($values["category"]) .", ". $cDB->EscTxt($values["description"]) .", \"T\");");
*/
$q = "SELECT * FROM trades_pending where member_id_from=".$cDB->EscTxt($_SESSION["user_login"])." and typ='T' and member_from_decision = 1";
$result = $cDB->Query($q);
if ($result) {
$list .= initTradeTable();
for($i=0;$i<mysql_num_rows($result);$i++) {
$row = mysql_fetch_array($result);
$list .= displayTrade($row,'TBC');
}
$list .= closeTradeTable();
}
else
$list .= "<em>None found!</em>";
break;
case("invoices_sent"):
$list .= "<b>You are awaiting payment for the following Invoices...</b><p>";
/*
$cDB->Query("INSERT INTO trades_pending (trade_date, member_id_from, member_id_to, amount, category, description, typ) VALUES (now(), ". $cDB->EscTxt($member->member_id) .", ". $cDB->EscTxt($member_to_id) .", ". $cDB->EscTxt($values["units"]) .", ". $cDB->EscTxt($values["category"]) .", ". $cDB->EscTxt($values["description"]) .", \"T\");");
*/
$q = "SELECT * FROM trades_pending where member_id_from=".$cDB->EscTxt($_SESSION["user_login"])." and typ='I' and member_from_decision = 1";
$result = $cDB->Query($q);
if ($result) {
$list .= initTradeTable();
for($i=0;$i<mysql_num_rows($result);$i++) {
$row = mysql_fetch_array($result);
$list .= displayTrade($row,'TBP');
}
$list .= closeTradeTable();
}
else
$list .= "<em>None found!</em>";
break;
default:
if (MEMBERS_CAN_INVOICE==true)
$list .= "I need to pay ".$pending->numToPay." Invoices<br>";
$list .= "I need to confirm ".$pending->numToConfirm." Incoming Payments<p>";
if (MEMBERS_CAN_INVOICE==true)
$list .= "I am awaiting payment for ".$pending->numToBePayed." Invoices<br>";
$list .= "I am awaiting confirmation of ".$pending->numToHaveConfirmed." Outgoing Payments<p>";
break;
}
$p->DisplayPage($list);
?>