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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / models / WithdrawModel.php
tutor / models Last commit date
CourseModel.php 2 years ago LessonModel.php 2 years ago QuizModel.php 2 years ago WithdrawModel.php 2 years ago
WithdrawModel.php
221 lines
1 <?php
2 /**
3 * Withdraw Model
4 *
5 * @package Tutor\Models
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 2.0.7
9 */
10
11 namespace Tutor\Models;
12
13 /**
14 * WithdrawModel Class
15 *
16 * @since 2.0.7
17 */
18 class WithdrawModel {
19 /**
20 * All withdraw status
21 */
22 const STATUS_PENDING = 'pending';
23 const STATUS_APPROVED = 'approved';
24 const STATUS_REJECTED = 'rejected';
25
26 /**
27 * Get withdraw summary info for an user
28 *
29 * @since 2.0.7
30 *
31 * @param int $instructor_id instructor id.
32 * @return array|object|null|void
33 */
34 public static function get_withdraw_summary( $instructor_id ) {
35 global $wpdb;
36
37 $maturity_days = tutor_utils()->get_option( 'minimum_days_for_balance_to_be_available' );
38
39 //phpcs:disable WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder
40 $data = $wpdb->get_row(
41 $wpdb->prepare(
42 "SELECT ID, display_name,
43 total_income,
44 total_withdraw,
45 (total_income-total_withdraw) current_balance,
46 total_matured,
47 total_pending,
48 greatest(0, total_matured - total_withdraw) available_for_withdraw
49
50 FROM (
51 SELECT ID,display_name,
52 COALESCE((SELECT SUM(instructor_amount) FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s' GROUP BY user_id HAVING user_id=u.ID),0) total_income,
53
54 COALESCE((
55 SELECT sum(amount) total_withdraw FROM {$wpdb->prefix}tutor_withdraws
56 WHERE status='%s'
57 GROUP BY user_id
58 HAVING user_id=u.ID
59 ),0) total_withdraw,
60
61 COALESCE((
62 SELECT sum(amount) total_pending FROM {$wpdb->prefix}tutor_withdraws
63 WHERE status='pending'
64 GROUP BY user_id
65 HAVING user_id=u.ID
66 ),0) total_pending,
67
68 COALESCE((
69 SELECT SUM(instructor_amount) FROM(
70 SELECT user_id, instructor_amount, created_at, DATEDIFF(NOW(),created_at) AS days_old FROM {$wpdb->prefix}tutor_earnings WHERE order_status='%s'
71 ) a
72 WHERE days_old >= %d
73 GROUP BY user_id
74 HAVING user_id = u.ID
75 ),0) total_matured
76
77 FROM {$wpdb->prefix}users u WHERE u.ID=%d
78
79 ) a",
80 'completed',
81 self::STATUS_APPROVED,
82 'completed',
83 $maturity_days,
84 $instructor_id
85 )
86 );
87
88 //phpcs:enable WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder
89
90 return $data;
91 }
92
93 /**
94 * Get withdrawal history
95 *
96 * @since 1.0.0
97 *
98 * @param int $user_id | optional.
99 * @param array $filter | ex: array('status' => '','date' => '', 'order' => '', 'start' => 10, 'per_page' => 10,'search' => '').
100 * @param int $start start.
101 * @param int $limit limit.
102 *
103 * @return object
104 */
105 public static function get_withdrawals_history( $user_id = 0, $filter = array(), $start = 0, $limit = 20 ) {
106 global $wpdb;
107
108 $filter = (array) $filter;
109 extract( $filter ); //phpcs:ignore WordPress.PHP.DontExtract.extract_extract
110
111 $query_by_status_sql = '';
112 $query_by_user_sql = '';
113
114 if ( ! empty( $status ) ) {
115 $status = (array) $status;
116 $status = "'" . implode( "','", $status ) . "'";
117
118 $query_by_status_sql = " AND status IN({$status}) ";
119 }
120
121 if ( $user_id ) {
122 $query_by_user_sql = " AND user_id = {$user_id} ";
123 }
124
125 // Order query @since 2.0.0.
126 $order_query = '';
127 if ( isset( $order ) && '' !== $order ) {
128 $order_query = "ORDER BY created_at {$order}";
129 } else {
130 $order_query = 'ORDER BY created_at DESC';
131 }
132
133 // Date query @since 2.0.0.
134 $date_query = '';
135 if ( isset( $date ) && '' !== $date ) {
136 $date_query = "AND DATE(created_at) = CAST( '$date' AS DATE )";
137 }
138
139 // Search query @since 2.0.0.
140 $search_term_raw = empty( $search ) ? '' : $search;
141 $search_query = '%%';
142 if ( ! empty( $search_term_raw ) ) {
143 $search_query = '%' . $wpdb->esc_like( $search_term_raw ) . '%';
144 }
145
146 //phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
147 $count = (int) $wpdb->get_var(
148 $wpdb->prepare(
149 "SELECT COUNT(withdraw_id)
150 FROM {$wpdb->prefix}tutor_withdraws withdraw_tbl
151 INNER JOIN {$wpdb->users} user_tbl
152 ON withdraw_tbl.user_id = user_tbl.ID
153 WHERE 1 = 1
154 {$query_by_user_sql}
155 {$query_by_status_sql}
156 {$date_query}
157 AND (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)
158 ",
159 $search_query,
160 $search_query,
161 $search_query,
162 $search_term_raw
163 )
164 );
165
166 $results = $wpdb->get_results(
167 $wpdb->prepare(
168 "SELECT withdraw_tbl.*,
169 user_tbl.display_name AS user_name,
170 user_tbl.user_email
171 FROM {$wpdb->prefix}tutor_withdraws withdraw_tbl
172 INNER JOIN {$wpdb->users} user_tbl
173 ON withdraw_tbl.user_id = user_tbl.ID
174 WHERE 1 = 1
175 {$query_by_user_sql}
176 {$query_by_status_sql}
177 {$date_query}
178
179 AND (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)
180 {$order_query}
181 LIMIT %d, %d
182 ",
183 $search_query,
184 $search_query,
185 $search_query,
186 $search_term_raw,
187 $start,
188 $limit
189 )
190 );
191
192 //phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
193
194 $withdraw_history = array(
195 'count' => $count ? $count : 0,
196 'results' => is_array( $results ) ? $results : array(),
197 );
198
199 return (object) $withdraw_history;
200 }
201
202 /**
203 * Get withdraw method for a specific
204 *
205 * @since 1.0.0
206 *
207 * @param int $user_id user id.
208 * @return bool|mixed
209 */
210 public static function get_user_withdraw_method( $user_id = 0 ) {
211 $user_id = tutor_utils()->get_user_id( $user_id );
212 $account = get_user_meta( $user_id, '_tutor_withdraw_method_data', true );
213
214 if ( $account ) {
215 return maybe_unserialize( $account );
216 }
217
218 return false;
219 }
220 }
221