BaseModel.php
9 months ago
BillingModel.php
1 year ago
CartItemModel.php
9 months ago
CartModel.php
5 months ago
CouponModel.php
4 months ago
CourseModel.php
3 months ago
LessonModel.php
9 months ago
OrderActivitiesModel.php
1 year ago
OrderItemMetaModel.php
9 months ago
OrderItemModel.php
9 months ago
OrderMetaModel.php
1 year ago
OrderModel.php
2 months ago
QuizModel.php
3 months ago
UserModel.php
1 year ago
WithdrawModel.php
3 weeks ago
WithdrawModel.php
224 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 ) && in_array( $status, array( self::STATUS_PENDING, self::STATUS_APPROVED, self::STATUS_REJECTED ), true ) ) { |
| 115 | $status = (array) $status; |
| 116 | $placeholder = implode( ',', array_fill( 0, count( $status ), '%s' ) ); |
| 117 | |
| 118 | $query_by_status_sql = $wpdb->prepare( "AND status IN($placeholder)", ...$status ); //phpcs:ignore |
| 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 | $is_valid_sql = sanitize_sql_orderby( $order ); |
| 129 | if ( $is_valid_sql ) { |
| 130 | $order_query = "ORDER BY created_at {$order}"; |
| 131 | } |
| 132 | } else { |
| 133 | $order_query = 'ORDER BY created_at DESC'; |
| 134 | } |
| 135 | |
| 136 | // Date query @since 2.0.0. |
| 137 | $date_query = ''; |
| 138 | if ( isset( $date ) && '' !== $date ) { |
| 139 | $date_query = "AND DATE(created_at) = CAST( '$date' AS DATE )"; |
| 140 | } |
| 141 | |
| 142 | // Search query @since 2.0.0. |
| 143 | $search_term_raw = empty( $search ) ? '' : $search; |
| 144 | $search_query = '%%'; |
| 145 | if ( ! empty( $search_term_raw ) ) { |
| 146 | $search_query = '%' . $wpdb->esc_like( $search_term_raw ) . '%'; |
| 147 | } |
| 148 | |
| 149 | //phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 150 | $count = (int) $wpdb->get_var( |
| 151 | $wpdb->prepare( |
| 152 | "SELECT COUNT(withdraw_id) |
| 153 | FROM {$wpdb->prefix}tutor_withdraws withdraw_tbl |
| 154 | INNER JOIN {$wpdb->users} user_tbl |
| 155 | ON withdraw_tbl.user_id = user_tbl.ID |
| 156 | WHERE 1 = 1 |
| 157 | {$query_by_user_sql} |
| 158 | {$query_by_status_sql} |
| 159 | {$date_query} |
| 160 | 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) |
| 161 | ", |
| 162 | $search_query, |
| 163 | $search_query, |
| 164 | $search_query, |
| 165 | $search_term_raw |
| 166 | ) |
| 167 | ); |
| 168 | |
| 169 | $results = $wpdb->get_results( |
| 170 | $wpdb->prepare( |
| 171 | "SELECT withdraw_tbl.*, |
| 172 | user_tbl.display_name AS user_name, |
| 173 | user_tbl.user_email |
| 174 | FROM {$wpdb->prefix}tutor_withdraws withdraw_tbl |
| 175 | INNER JOIN {$wpdb->users} user_tbl |
| 176 | ON withdraw_tbl.user_id = user_tbl.ID |
| 177 | WHERE 1 = 1 |
| 178 | {$query_by_user_sql} |
| 179 | {$query_by_status_sql} |
| 180 | {$date_query} |
| 181 | |
| 182 | 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) |
| 183 | {$order_query} |
| 184 | LIMIT %d, %d |
| 185 | ", |
| 186 | $search_query, |
| 187 | $search_query, |
| 188 | $search_query, |
| 189 | $search_term_raw, |
| 190 | $start, |
| 191 | $limit |
| 192 | ) |
| 193 | ); |
| 194 | |
| 195 | //phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 196 | |
| 197 | $withdraw_history = array( |
| 198 | 'count' => $count ? $count : 0, |
| 199 | 'results' => is_array( $results ) ? $results : array(), |
| 200 | ); |
| 201 | |
| 202 | return (object) $withdraw_history; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Get withdraw method for a specific |
| 207 | * |
| 208 | * @since 1.0.0 |
| 209 | * |
| 210 | * @param int $user_id user id. |
| 211 | * @return bool|mixed |
| 212 | */ |
| 213 | public static function get_user_withdraw_method( $user_id = 0 ) { |
| 214 | $user_id = tutor_utils()->get_user_id( $user_id ); |
| 215 | $account = get_user_meta( $user_id, '_tutor_withdraw_method_data', true ); |
| 216 | |
| 217 | if ( $account ) { |
| 218 | return maybe_unserialize( $account ); |
| 219 | } |
| 220 | |
| 221 | return false; |
| 222 | } |
| 223 | } |
| 224 |