Repository navigation
Expand file tree
/
Copy pathWithdrawModel.php
More file actions
368 lines (313 loc) · 9.92 KB
/
Copy pathWithdrawModel.php
File metadata and controls
368 lines (313 loc) · 9.92 KB
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
<?php
/**
* Withdraw Model
*
* @package Tutor\Models
* @author Themeum <support@themeum.com>
* @link https://themeum.com
* @since 2.0.7
*/
namespace Tutor\Models;
use Tutor\Helpers\QueryHelper;
use Tutor\Helpers\UrlHelper;
use TUTOR\Input;
/**
* WithdrawModel Class
*
* @since 2.0.7
*/
class WithdrawModel {
/**
* All withdraw status
*/
const STATUS_PENDING = 'pending';
const STATUS_APPROVED = 'approved';
const STATUS_REJECTED = 'rejected';
/**
* Withdrawal method keys
*/
const METHOD_BANK_TRANSFER_WITHDRAW = 'bank_transfer_withdraw';
const METHOD_PAYPAL_WITHDRAW = 'paypal_withdraw';
const METHOD_ECHECK_WITHDRAW = 'echeck_withdraw';
/**
* Get withdrawal status list
*
* @since 4.0.0
*
* @return array
*/
public static function get_withdrawal_status_list() {
return array(
self::STATUS_PENDING => __( 'Pending', 'tutor' ),
self::STATUS_APPROVED => __( 'Approved', 'tutor' ),
self::STATUS_REJECTED => __( 'Rejected', 'tutor' ),
);
}
/**
* Get withdrawal method list
*
* @since 4.0.0
*
* @return array
*/
public static function get_withdrawal_method_list() {
return array(
self::METHOD_BANK_TRANSFER_WITHDRAW => __( 'Bank Transfer', 'tutor' ),
self::METHOD_PAYPAL_WITHDRAW => __( 'PayPal', 'tutor' ),
self::METHOD_ECHECK_WITHDRAW => __( 'E-Check', 'tutor' ),
);
}
/**
* Get withdrawal method icons
*
* @since 4.0.0
*
* @return array
*/
public static function get_method_icons() {
return array(
self::METHOD_BANK_TRANSFER_WITHDRAW => UrlHelper::asset( 'images/icon-bank.svg' ),
self::METHOD_PAYPAL_WITHDRAW => UrlHelper::asset( 'images/icon-paypal.svg' ),
self::METHOD_ECHECK_WITHDRAW => UrlHelper::asset( 'images/icon-echeck.svg' ),
);
}
/**
* Get withdrawal count
*
* @since 4.0.0
*
* @param array $where where.
*
* @return int
*/
public static function get_withdrawal_count( $where = array() ) {
return QueryHelper::get_count( 'tutor_withdraws', $where, array(), 'withdraw_id' );
}
/**
* Get status count
*
* @since 4.0.0
*/
public static function get_status_filter_options() {
$url = get_pagenum_link();
$where = array(
'user_id' => get_current_user_id(),
);
$start_date = Input::get( 'start_date' );
$end_date = Input::get( 'end_date' );
if ( ! empty( $start_date ) && ! empty( $end_date ) ) {
$where['DATE(created_at)'] = array( 'BETWEEN', array( $start_date, $end_date ) );
}
$tabs [] = array(
'key' => '',
'title' => __( 'All', 'tutor' ),
'value' => self::get_withdrawal_count( $where ),
'url' => UrlHelper::add_query_params( $url, array( 'data' => 'all' ) ),
);
foreach ( self::get_withdrawal_status_list() as $status => $title ) {
$where['status'] = $status;
$tabs[] = array(
'key' => $status,
'title' => $title,
'value' => self::get_withdrawal_count( $where ),
'url' => UrlHelper::add_query_params( $url, array( 'data' => $status ) ),
);
}
return $tabs;
}
/**
* Get withdraw summary info for an user
*
* @since 2.0.7
* @since 4.0.0 $args parameter added.
*
* @param int $instructor_id instructor id.
* @param array $args Optional additional WHERE conditions.
* @return array|object|null|void
*/
public static function get_withdraw_summary( $instructor_id, $args = array() ) {
global $wpdb;
$args = tutor_utils()->sanitize_array( $args );
$date_clause = '';
if ( ! empty( $args['from'] ) && ! empty( $args['to'] ) ) {
$from = Input::sanitize( $args['from'] );
$to = Input::sanitize( $args['to'] );
$where['created_at'] = array( 'BETWEEN', array( $from, $to ) );
$date_clause = ' AND ' . QueryHelper::prepare_where_clause( $where );
}
$maturity_days = tutor_utils()->get_option( 'minimum_days_for_balance_to_be_available' );
//phpcs:disable
$data = $wpdb->get_row(
$wpdb->prepare(
"SELECT ID, display_name,
total_income,
total_withdraw,
(total_income-total_withdraw) current_balance,
total_matured,
total_pending,
greatest(0, total_matured - total_withdraw) available_for_withdraw
FROM (
SELECT ID,display_name,
COALESCE((SELECT SUM(instructor_amount) FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s' {$date_clause} GROUP BY user_id HAVING user_id=u.ID),0) total_income,
COALESCE((
SELECT sum(amount) total_withdraw FROM {$wpdb->prefix}tutor_withdraws
WHERE status='%s' {$date_clause}
GROUP BY user_id
HAVING user_id=u.ID
),0) total_withdraw,
COALESCE((
SELECT sum(amount) total_pending FROM {$wpdb->prefix}tutor_withdraws
WHERE status='pending' {$date_clause}
GROUP BY user_id
HAVING user_id=u.ID
),0) total_pending,
COALESCE((
SELECT SUM(instructor_amount) FROM(
SELECT user_id, instructor_amount, created_at, DATEDIFF(NOW(),created_at) AS days_old FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s' {$date_clause}
) a
WHERE days_old >= %d
GROUP BY user_id
HAVING user_id = u.ID
),0) total_matured
FROM {$wpdb->prefix}users u WHERE u.ID=%d
) a",
'completed',
self::STATUS_APPROVED,
'completed',
$maturity_days,
$instructor_id
)
);
//phpcs:enable
return $data;
}
/**
* Get withdrawal history
*
* @since 1.0.0
*
* @param int $user_id | optional.
* @param array $filter | ex: array('status' => '','date' => '', 'order' => '', 'start' => 10, 'per_page' => 10,'search' => '').
* @param int $start start.
* @param int $limit limit.
*
* @return object
*/
public static function get_withdrawals_history( $user_id = 0, $filter = array(), $start = 0, $limit = 20 ) {
global $wpdb;
$filter = (array) $filter;
$where = array();
if ( $user_id ) {
$where['withdraw_tbl.user_id'] = $user_id;
}
if ( isset( $filter['status'] ) && ! empty( $filter['status'] ) ) {
$where['withdraw_tbl.status'] = (array) $filter['status'];
}
if ( isset( $filter['date'] ) && ! empty( $filter['date'] ) ) {
$where['DATE(withdraw_tbl.created_at) = %s'] = array( 'RAW', array( $filter['date'] ) );
}
if ( isset( $filter['start_date'], $filter['end_date'] ) ) {
$where['DATE(withdraw_tbl.created_at)'] = array( 'BETWEEN', array( $filter['start_date'], $filter['end_date'] ) );
}
if ( isset( $filter['search'] ) && ! empty( $filter['search'] ) ) {
$term = $filter['search'];
$like = '%' . $wpdb->esc_like( $term ) . '%';
$where['(user_tbl.display_name LIKE %s OR user_tbl.user_login LIKE %s OR user_tbl.user_nicename LIKE %s OR user_tbl.user_email = %s)'] = array(
'RAW',
array( $like, $like, $like, $term ),
);
}
$orderby = 'withdraw_tbl.created_at';
$order = isset( $filter['order'] ) ? QueryHelper::get_valid_sort_order( $filter['order'] ) : 'DESC';
$args = array(
'select' => array( 'withdraw_tbl.*', 'user_tbl.display_name AS user_name', 'user_tbl.user_email' ),
'alias' => 'withdraw_tbl',
'joins' => array(
array(
'type' => 'INNER',
'table' => $wpdb->users . ' AS user_tbl',
'on' => 'withdraw_tbl.user_id = user_tbl.ID',
),
),
'where' => $where,
'orderby' => $orderby,
'order' => $order,
'limit' => (int) $limit,
'offset' => (int) $start,
);
$results = QueryHelper::query( 'tutor_withdraws', $args );
$args['count'] = true;
$count = QueryHelper::query( 'tutor_withdraws', $args );
$withdraw_history = array(
'count' => $count ? $count : 0,
'results' => is_array( $results ) ? $results : array(),
);
return (object) $withdraw_history;
}
/**
* Get withdraw method for a specific
*
* @since 1.0.0
*
* @param int $user_id user id.
*
* @return bool|mixed
*/
public static function get_user_withdraw_method( $user_id = 0 ) {
$user_id = tutor_utils()->get_user_id( $user_id );
$account = self::get_user_withdraw_method_meta( $user_id, '_tutor_withdraw_method_data' );
return $account ? $account : false;
}
/**
* Get a withdraw method meta blob as a plain array.
*
* Reads the raw usermeta value and unserializes with objects disabled so
* poisoned serialized payloads cannot materialize PHP objects.
*
* @since 4.0.8
*
* @param int $user_id User ID.
* @param string $meta_key Meta key.
*
* @return array
*/
public static function get_user_withdraw_method_meta( $user_id, $meta_key ) {
global $wpdb;
$user_id = absint( $user_id );
$meta_key = sanitize_key( $meta_key );
if ( ! $user_id || ! $meta_key ) {
return array();
}
$raw = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = %s LIMIT 1",
$user_id,
$meta_key
)
);
return self::safe_unserialize_array( $raw );
}
/**
* Unserialize withdraw method data as an array only.
*
* @since 4.0.8
*
* @param mixed $data Serialized string or already-decoded array.
*
* @return array
*/
public static function safe_unserialize_array( $data ) {
if ( is_array( $data ) ) {
return $data;
}
if ( ! is_string( $data ) || '' === $data ) {
return array();
}
// Reject object payloads before calling unserialize().
if ( preg_match( '/(^|;|{|})O:\+?[0-9]+:"/', $data ) ) {
return array();
}
$unserialized = unserialize( $data, array( 'allowed_classes' => false ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
return is_array( $unserialized ) ? $unserialized : array();
}
}