/
balances.php
342 lines (282 loc) · 7.37 KB
/
balances.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
<?php
/**
* Helper functions that calculate balances.
*
* @since 6.0.0 Introduced.
* @author Alexandros Georgiou <info@dashed-slug.net>
*/
namespace DSWallets;
defined( 'ABSPATH' ) || die( -1 );
function get_balance_for_user_and_currency_id( int $user_id, int $currency_id ): int {
return get_all_balances_assoc_for_user( $user_id )[ $currency_id ] ?? 0;
}
/**
* Get all balances for a user or for all users.
*
* Balances are the sum of all transactions with "done" state.
* Balances for all enabled currencies are returned.
*
* @param int $user_id The ID for the user, or 0 to retrieve balance sums over all users.
* @return array Assoc array of currency_ids to balance amounts, in decimal form (float).
*/
function get_all_balances_assoc_for_user( int $user_id = 0 ): array {
static $memoize = [];
if ( $user_id && ! ds_user_can( $user_id, 'has_wallets' ) ) {
throw new \Exception( __( 'Not allowed', 'wallets' ) );
};
if ( ! isset( $memoize[ $user_id ] ) ) {
global $wpdb;
maybe_switch_blog();
$sql =
"SELECT
tmc.meta_value currency_id,
SUM(
IF (
tmv.meta_value >= 0,
tmv.meta_value,
tmv.meta_value + tmf.meta_value
)
) amount
FROM
{$wpdb->posts} t
JOIN
{$wpdb->postmeta} tmu ON (
tmu.post_id = t.id
AND tmu.meta_key = 'wallets_user'
)
JOIN
{$wpdb->postmeta} tmc ON (
tmc.post_id = t.id
AND tmc.meta_key = 'wallets_currency_id'
)
JOIN
{$wpdb->postmeta} tmv ON (
tmv.post_id = t.id
AND tmv.meta_key = 'wallets_amount'
)
JOIN
{$wpdb->postmeta} tmf ON (
tmf.post_id = t.id
AND tmf.meta_key = 'wallets_fee'
)
JOIN
{$wpdb->posts} c ON (
c.id = tmc.meta_value
AND c.post_status = 'publish'
)
";
if ( $user_id ) {
$sql .= $wpdb->prepare(
"
WHERE
t.post_type = 'wallets_tx'
AND tmu.meta_value = %d
AND t.post_status = 'publish'
GROUP BY
tmc.meta_value;
",
$user_id
);
} else {
$sql .=
"WHERE
t.post_type = 'wallets_tx'
AND t.post_status = 'publish'
GROUP BY
tmc.meta_value;
";
}
$result = $wpdb->get_results( $sql, 'OBJECT_K' );
if ( $wpdb->last_error ) {
error_log(
sprintf(
'%s(), line %d, SQL error: %s',
__FUNCTION__,
__LINE__,
$wpdb->last_error
)
);
$result = [];
}
maybe_restore_blog();
$balances = [];
foreach ( $result as $currency_id => $row ) {
$balances[ $currency_id ] = intval( $row->amount );
}
/**
* Allows extensions to modify the balances.
*
* This should be used CAREFULLY!
*
* @since 6.0.0 Introduced.
* @param array $balances An associative array of currency_ids to balances.
* @param int $user_id The ID of the user whose balances are being retrieved.
*
*/
$balances = apply_filters( 'wallets_balances', $balances, $user_id );
$memoize[ $user_id ] = $balances;
}
return $memoize[ $user_id ];
}
function get_available_balance_for_user_and_currency_id( int $user_id, int $currency_id ): int {
return get_all_available_balances_assoc_for_user( $user_id )[ $currency_id ] ?? 0;
}
/**
* Get all available balances for a user or for all users.
*
* Available balances are the sum of all transactions in "done" state
* and any debiting balances (withdrawals, negative moves) that are in a "pending" state.
* Balances for all enabled currencies are returned.
*
* @param int $user_id The ID for the user. If zero, then return total balances for all users.
* @return array Assoc array of currency_ids to balance amounts, in integer form.
*/
function get_all_available_balances_assoc_for_user( int $user_id = 0 ): array {
static $memoize = [];
if ( $user_id && ! ds_user_can( $user_id, 'has_wallets' ) ) {
throw new \Exception( __( 'Not allowed', 'wallets' ) );
};
if ( ! isset( $memoize[ $user_id ] ) ) {
global $wpdb;
$wpdb->flush();
maybe_switch_blog();
$sql = "
SELECT
tmc.meta_value currency_id,
SUM(
IF (
tmv.meta_value >= 0,
tmv.meta_value,
tmv.meta_value + tmf.meta_value
)
) amount
FROM
{$wpdb->posts} t
JOIN
{$wpdb->postmeta} tmc ON (
tmc.post_id = t.id
AND tmc.meta_key = 'wallets_currency_id'
)
JOIN
{$wpdb->postmeta} tmv ON (
tmv.post_id = t.id
AND tmv.meta_key = 'wallets_amount'
)
JOIN
{$wpdb->postmeta} tmf ON (
tmf.post_id = t.id
AND tmf.meta_key = 'wallets_fee'
)
JOIN
{$wpdb->posts} c ON (
c.id = tmc.meta_value
AND c.post_status = 'publish'
)
";
if ( $user_id ) {
$sql .=
"JOIN
{$wpdb->postmeta} tmu ON (
tmu.post_id = t.id
AND tmu.meta_key = 'wallets_user'
)";
$sql .= $wpdb->prepare(
"WHERE
t.post_type = 'wallets_tx'
AND tmu.meta_value = %d
AND (
( tmv.meta_value > 0 AND t.post_status='publish' )
OR ( tmv.meta_value <= 0 AND t.post_status IN ( 'pending', 'publish' ) )
)
GROUP BY
tmc.meta_value;
",
$user_id
);
} else {
$sql .=
"WHERE
t.post_type = 'wallets_tx'
AND (
( tmv.meta_value > 0 AND t.post_status='publish' )
OR ( tmv.meta_value <= 0 AND t.post_status IN ( 'pending', 'publish' ) )
)
GROUP BY
tmc.meta_value;
";
}
$result = $wpdb->get_results( $sql, 'OBJECT_K' );
if ( $wpdb->last_error ) {
error_log(
sprintf(
'%s(), line %d, SQL error: %s',
__FUNCTION__,
__LINE__,
$wpdb->last_error
)
);
$result = [];
}
maybe_restore_blog();
$balances = [];
foreach ( $result as $currency_id => $row ) {
$balances[ $currency_id ] = intval( $row->amount );
}
/**
* Allows extensions to modify the available balances.
*
* For example, the exchange extension uses this to "reserve" available balance amounts
* that are locked in open orders.
*
* @since 6.0.0 Introduced.
* @param array $balances An associative array of currency_ids to available balances.
* @param int $user_id The ID of the user whose available balances are being retrieved.
*
*/
$memoize[ $user_id ] = apply_filters( 'wallets_available_balances', $balances, $user_id );
}
return $memoize[ $user_id ];
}
/**
* Sums transactions into balances.
*
* Gets the transaction objects from the passed array and sums them into an assoc array
* of currency_id to balance sum.
* Applies fees to debiting transactions (withdrawals, negative moves).
*
* @param array $txs The array of Transaction objects.
* @return number[] The assoc array of currency_ids to balances.
*/
function sum_transactions( array $txs ) {
$sums = [];
foreach ( $txs as $tx ) {
if ( !$tx ) {
continue;
}
$currency_id = $tx->currency->post_id;
if ( ! isset( $sums[ $currency_id ] ) ) {
$sums[ $currency_id ] = 0;
}
switch ( $tx->category ) {
case 'deposit':
// amount is credited to user (incoming)
// fee is purely informational, has no effect towards balances
$sums[ $currency_id ] += $tx->amount;
break;
case 'withdrawal':
// amount and fee is debited to user (outgoing)
$sums[ $currency_id ] += $tx->amount + $tx->fee;
break;
case 'move':
if ( $tx->amount >= 0 ) {
// amount is credited to user (incoming)
$sums[ $currency_id ] += $tx->amount;
} else {
// amount and fee is debited to user (outgoing)
$sums[ $currency_id ] += ( $tx->amount + $tx->fee );
}
break;
}
}
return $sums;
}