1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Smr\Pages\Player\Bank; |
4
|
|
|
|
5
|
|
|
use AbstractSmrPlayer; |
6
|
|
|
use Menu; |
7
|
|
|
use Smr\Database; |
8
|
|
|
use Smr\Page\PlayerPage; |
9
|
|
|
use Smr\Template; |
10
|
|
|
|
11
|
|
|
class AllianceBankReport extends PlayerPage { |
12
|
|
|
|
13
|
|
|
public string $file = 'bank_report.php'; |
14
|
|
|
|
15
|
|
|
private const WITHDRAW = 0; |
16
|
|
|
private const DEPOSIT = 1; |
17
|
|
|
|
18
|
|
|
public function __construct( |
19
|
|
|
private readonly int $allianceID, |
20
|
|
|
private readonly bool $reportSent = false |
21
|
|
|
) {} |
22
|
|
|
|
23
|
|
|
public function build(AbstractSmrPlayer $player, Template $template): void { |
24
|
|
|
$alliance_id = $this->allianceID; |
25
|
|
|
|
26
|
|
|
//get all transactions |
27
|
|
|
$db = Database::getInstance(); |
28
|
|
|
$dbResult = $db->read('SELECT * FROM alliance_bank_transactions WHERE alliance_id = ' . $db->escapeNumber($alliance_id) . ' AND game_id = ' . $db->escapeNumber($player->getGameID())); |
29
|
|
|
if (!$dbResult->hasRecord()) { |
30
|
|
|
create_error('Your alliance has no recorded transactions.'); |
31
|
|
|
} |
32
|
|
|
$trans = []; |
33
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
34
|
|
|
$transType = ($dbRecord->getString('transaction') == 'Payment') ? self::WITHDRAW : self::DEPOSIT; |
35
|
|
|
$payeeId = ($dbRecord->getInt('exempt')) ? 0 : $dbRecord->getInt('payee_id'); |
36
|
|
|
// initialize payee if necessary |
37
|
|
|
if (!isset($trans[$payeeId])) { |
38
|
|
|
$trans[$payeeId] = [self::WITHDRAW => 0, self::DEPOSIT => 0]; |
39
|
|
|
} |
40
|
|
|
$trans[$payeeId][$transType] += $dbRecord->getInt('amount'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
//ordering |
44
|
|
|
$playerIDs = array_keys($trans); |
45
|
|
|
$totals = []; |
46
|
|
|
foreach ($trans as $accId => $transArray) { |
47
|
|
|
$totals[$accId] = $transArray[self::DEPOSIT] - $transArray[self::WITHDRAW]; |
48
|
|
|
} |
49
|
|
|
arsort($totals, SORT_NUMERIC); |
50
|
|
|
$dbResult = $db->read('SELECT * FROM player WHERE account_id IN (' . $db->escapeArray($playerIDs) . ') AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' ORDER BY player_name'); |
51
|
|
|
$players = [0 => 'Alliance Funds']; |
52
|
|
|
foreach ($dbResult->records() as $dbRecord) { |
53
|
|
|
$players[$dbRecord->getInt('account_id')] = htmlentities($dbRecord->getString('player_name')); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
//format it this way so its easy to send to the alliance MB if requested. |
57
|
|
|
$text = '<table class="nobord centered" cellspacing="1">'; |
58
|
|
|
$text .= '<tr><th>Player</th><th>Deposits</th><th>Withdrawals</th><th>Total</th></tr>'; |
59
|
|
|
$balance = 0; |
60
|
|
|
foreach ($totals as $accId => $total) { |
61
|
|
|
$balance += $total; |
62
|
|
|
$text .= '<tr>'; |
63
|
|
|
$text .= '<td><span class="yellow">' . $players[$accId] . '</span></td>'; |
64
|
|
|
$text .= '<td class="right">' . number_format($trans[$accId][self::DEPOSIT]) . '</td>'; |
65
|
|
|
$text .= '<td class="right">-' . number_format($trans[$accId][self::WITHDRAW]) . '</td>'; |
66
|
|
|
$text .= '<td class="right"><span class="'; |
67
|
|
|
if ($total < 0) { |
68
|
|
|
$text .= 'red bold'; |
69
|
|
|
} else { |
70
|
|
|
$text .= 'bold'; |
71
|
|
|
} |
72
|
|
|
$text .= '">' . number_format($total) . '</span></td>'; |
73
|
|
|
$text .= '</tr>'; |
74
|
|
|
} |
75
|
|
|
$text .= '</table>'; |
76
|
|
|
$text = '<div class="center"><br />Ending Balance: ' . number_format($balance) . '</div><br />' . $text; |
77
|
|
|
$template->assign('BankReport', $text); |
78
|
|
|
|
79
|
|
|
if (!$this->reportSent) { |
80
|
|
|
$container = new AllianceBankReportProcessor($alliance_id, $text); |
81
|
|
|
$template->assign('SendReportHREF', $container->href()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$template->assign('PageTopic', 'Alliance Bank Report'); |
85
|
|
|
Menu::bank(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|