BaseModel.php
10 months ago
BillingModel.php
1 year ago
CartItemModel.php
10 months ago
CartModel.php
19 hours ago
CouponModel.php
19 hours ago
CourseModel.php
19 hours ago
EnrollmentModel.php
19 hours ago
LessonModel.php
9 months ago
OrderActivitiesModel.php
1 year ago
OrderItemMetaModel.php
10 months ago
OrderItemModel.php
10 months ago
OrderMetaModel.php
1 year ago
OrderModel.php
19 hours ago
QuizModel.php
19 hours ago
UserModel.php
1 year ago
WithdrawModel.php
19 hours ago
WithdrawModel.php
310 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 | use Tutor\Helpers\QueryHelper; |
| 14 | use Tutor\Helpers\UrlHelper; |
| 15 | use TUTOR\Input; |
| 16 | |
| 17 | /** |
| 18 | * WithdrawModel Class |
| 19 | * |
| 20 | * @since 2.0.7 |
| 21 | */ |
| 22 | class WithdrawModel { |
| 23 | /** |
| 24 | * All withdraw status |
| 25 | */ |
| 26 | const STATUS_PENDING = 'pending'; |
| 27 | const STATUS_APPROVED = 'approved'; |
| 28 | const STATUS_REJECTED = 'rejected'; |
| 29 | |
| 30 | /** |
| 31 | * Withdrawal method keys |
| 32 | */ |
| 33 | const METHOD_BANK_TRANSFER_WITHDRAW = 'bank_transfer_withdraw'; |
| 34 | const METHOD_PAYPAL_WITHDRAW = 'paypal_withdraw'; |
| 35 | const METHOD_ECHECK_WITHDRAW = 'echeck_withdraw'; |
| 36 | |
| 37 | /** |
| 38 | * Get withdrawal status list |
| 39 | * |
| 40 | * @since 4.0.0 |
| 41 | * |
| 42 | * @return array |
| 43 | */ |
| 44 | public static function get_withdrawal_status_list() { |
| 45 | return array( |
| 46 | self::STATUS_PENDING => __( 'Pending', 'tutor' ), |
| 47 | self::STATUS_APPROVED => __( 'Approved', 'tutor' ), |
| 48 | self::STATUS_REJECTED => __( 'Rejected', 'tutor' ), |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get withdrawal method list |
| 54 | * |
| 55 | * @since 4.0.0 |
| 56 | * |
| 57 | * @return array |
| 58 | */ |
| 59 | public static function get_withdrawal_method_list() { |
| 60 | return array( |
| 61 | self::METHOD_BANK_TRANSFER_WITHDRAW => __( 'Bank Transfer', 'tutor' ), |
| 62 | self::METHOD_PAYPAL_WITHDRAW => __( 'PayPal', 'tutor' ), |
| 63 | self::METHOD_ECHECK_WITHDRAW => __( 'E-Check', 'tutor' ), |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get withdrawal method icons |
| 69 | * |
| 70 | * @since 4.0.0 |
| 71 | * |
| 72 | * @return array |
| 73 | */ |
| 74 | public static function get_method_icons() { |
| 75 | return array( |
| 76 | self::METHOD_BANK_TRANSFER_WITHDRAW => UrlHelper::asset( 'images/icon-bank.svg' ), |
| 77 | self::METHOD_PAYPAL_WITHDRAW => UrlHelper::asset( 'images/icon-paypal.svg' ), |
| 78 | self::METHOD_ECHECK_WITHDRAW => UrlHelper::asset( 'images/icon-echeck.svg' ), |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get withdrawal count |
| 84 | * |
| 85 | * @since 4.0.0 |
| 86 | * |
| 87 | * @param array $where where. |
| 88 | * |
| 89 | * @return int |
| 90 | */ |
| 91 | public static function get_withdrawal_count( $where = array() ) { |
| 92 | return QueryHelper::get_count( 'tutor_withdraws', $where, array(), 'withdraw_id' ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get status count |
| 97 | * |
| 98 | * @since 4.0.0 |
| 99 | */ |
| 100 | public static function get_status_filter_options() { |
| 101 | $url = get_pagenum_link(); |
| 102 | $where = array( |
| 103 | 'user_id' => get_current_user_id(), |
| 104 | ); |
| 105 | |
| 106 | $start_date = Input::get( 'start_date' ); |
| 107 | $end_date = Input::get( 'end_date' ); |
| 108 | |
| 109 | if ( ! empty( $start_date ) && ! empty( $end_date ) ) { |
| 110 | $where['DATE(created_at)'] = array( 'BETWEEN', array( $start_date, $end_date ) ); |
| 111 | } |
| 112 | |
| 113 | $tabs [] = array( |
| 114 | 'key' => '', |
| 115 | 'title' => __( 'All', 'tutor' ), |
| 116 | 'value' => self::get_withdrawal_count( $where ), |
| 117 | 'url' => UrlHelper::add_query_params( $url, array( 'data' => 'all' ) ), |
| 118 | ); |
| 119 | |
| 120 | foreach ( self::get_withdrawal_status_list() as $status => $title ) { |
| 121 | $where['status'] = $status; |
| 122 | |
| 123 | $tabs[] = array( |
| 124 | 'key' => $status, |
| 125 | 'title' => $title, |
| 126 | 'value' => self::get_withdrawal_count( $where ), |
| 127 | 'url' => UrlHelper::add_query_params( $url, array( 'data' => $status ) ), |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | return $tabs; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get withdraw summary info for an user |
| 136 | * |
| 137 | * @since 2.0.7 |
| 138 | * @since 4.0.0 $args parameter added. |
| 139 | * |
| 140 | * @param int $instructor_id instructor id. |
| 141 | * @param array $args Optional additional WHERE conditions. |
| 142 | * @return array|object|null|void |
| 143 | */ |
| 144 | public static function get_withdraw_summary( $instructor_id, $args = array() ) { |
| 145 | global $wpdb; |
| 146 | |
| 147 | $args = tutor_utils()->sanitize_array( $args ); |
| 148 | $date_clause = ''; |
| 149 | |
| 150 | if ( ! empty( $args['from'] ) && ! empty( $args['to'] ) ) { |
| 151 | $from = Input::sanitize( $args['from'] ); |
| 152 | $to = Input::sanitize( $args['to'] ); |
| 153 | |
| 154 | $where['created_at'] = array( 'BETWEEN', array( $from, $to ) ); |
| 155 | $date_clause = ' AND ' . QueryHelper::prepare_where_clause( $where ); |
| 156 | } |
| 157 | |
| 158 | $maturity_days = tutor_utils()->get_option( 'minimum_days_for_balance_to_be_available' ); |
| 159 | |
| 160 | //phpcs:disable |
| 161 | $data = $wpdb->get_row( |
| 162 | $wpdb->prepare( |
| 163 | "SELECT ID, display_name, |
| 164 | total_income, |
| 165 | total_withdraw, |
| 166 | (total_income-total_withdraw) current_balance, |
| 167 | total_matured, |
| 168 | total_pending, |
| 169 | greatest(0, total_matured - total_withdraw) available_for_withdraw |
| 170 | |
| 171 | FROM ( |
| 172 | SELECT ID,display_name, |
| 173 | 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, |
| 174 | |
| 175 | COALESCE(( |
| 176 | SELECT sum(amount) total_withdraw FROM {$wpdb->prefix}tutor_withdraws |
| 177 | WHERE status='%s' {$date_clause} |
| 178 | GROUP BY user_id |
| 179 | HAVING user_id=u.ID |
| 180 | ),0) total_withdraw, |
| 181 | |
| 182 | COALESCE(( |
| 183 | SELECT sum(amount) total_pending FROM {$wpdb->prefix}tutor_withdraws |
| 184 | WHERE status='pending' {$date_clause} |
| 185 | GROUP BY user_id |
| 186 | HAVING user_id=u.ID |
| 187 | ),0) total_pending, |
| 188 | |
| 189 | COALESCE(( |
| 190 | SELECT SUM(instructor_amount) FROM( |
| 191 | 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} |
| 192 | ) a |
| 193 | WHERE days_old >= %d |
| 194 | GROUP BY user_id |
| 195 | HAVING user_id = u.ID |
| 196 | ),0) total_matured |
| 197 | |
| 198 | FROM {$wpdb->prefix}users u WHERE u.ID=%d |
| 199 | |
| 200 | ) a", |
| 201 | 'completed', |
| 202 | self::STATUS_APPROVED, |
| 203 | 'completed', |
| 204 | $maturity_days, |
| 205 | $instructor_id |
| 206 | ) |
| 207 | ); |
| 208 | |
| 209 | //phpcs:enable |
| 210 | |
| 211 | return $data; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Get withdrawal history |
| 216 | * |
| 217 | * @since 1.0.0 |
| 218 | * |
| 219 | * @param int $user_id | optional. |
| 220 | * @param array $filter | ex: array('status' => '','date' => '', 'order' => '', 'start' => 10, 'per_page' => 10,'search' => ''). |
| 221 | * @param int $start start. |
| 222 | * @param int $limit limit. |
| 223 | * |
| 224 | * @return object |
| 225 | */ |
| 226 | public static function get_withdrawals_history( $user_id = 0, $filter = array(), $start = 0, $limit = 20 ) { |
| 227 | global $wpdb; |
| 228 | |
| 229 | $filter = (array) $filter; |
| 230 | |
| 231 | $where = array(); |
| 232 | if ( $user_id ) { |
| 233 | $where['withdraw_tbl.user_id'] = $user_id; |
| 234 | } |
| 235 | |
| 236 | if ( isset( $filter['status'] ) && ! empty( $filter['status'] ) ) { |
| 237 | $where['withdraw_tbl.status'] = (array) $filter['status']; |
| 238 | } |
| 239 | |
| 240 | if ( isset( $filter['date'] ) && ! empty( $filter['date'] ) ) { |
| 241 | $where['DATE(withdraw_tbl.created_at) = %s'] = array( 'RAW', array( $filter['date'] ) ); |
| 242 | } |
| 243 | |
| 244 | if ( isset( $filter['start_date'], $filter['end_date'] ) ) { |
| 245 | $where['DATE(withdraw_tbl.created_at)'] = array( 'BETWEEN', array( $filter['start_date'], $filter['end_date'] ) ); |
| 246 | } |
| 247 | |
| 248 | if ( isset( $filter['search'] ) && ! empty( $filter['search'] ) ) { |
| 249 | $term = $filter['search']; |
| 250 | $like = '%' . $wpdb->esc_like( $term ) . '%'; |
| 251 | |
| 252 | $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( |
| 253 | 'RAW', |
| 254 | array( $like, $like, $like, $term ), |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | $orderby = 'withdraw_tbl.created_at'; |
| 259 | $order = isset( $filter['order'] ) ? QueryHelper::get_valid_sort_order( $filter['order'] ) : 'DESC'; |
| 260 | |
| 261 | $args = array( |
| 262 | 'select' => array( 'withdraw_tbl.*', 'user_tbl.display_name AS user_name', 'user_tbl.user_email' ), |
| 263 | 'alias' => 'withdraw_tbl', |
| 264 | 'joins' => array( |
| 265 | array( |
| 266 | 'type' => 'INNER', |
| 267 | 'table' => $wpdb->users . ' AS user_tbl', |
| 268 | 'on' => 'withdraw_tbl.user_id = user_tbl.ID', |
| 269 | ), |
| 270 | ), |
| 271 | 'where' => $where, |
| 272 | 'orderby' => $orderby, |
| 273 | 'order' => $order, |
| 274 | 'limit' => (int) $limit, |
| 275 | 'offset' => (int) $start, |
| 276 | ); |
| 277 | |
| 278 | $results = QueryHelper::query( 'tutor_withdraws', $args ); |
| 279 | |
| 280 | $args['count'] = true; |
| 281 | $count = QueryHelper::query( 'tutor_withdraws', $args ); |
| 282 | |
| 283 | $withdraw_history = array( |
| 284 | 'count' => $count ? $count : 0, |
| 285 | 'results' => is_array( $results ) ? $results : array(), |
| 286 | ); |
| 287 | |
| 288 | return (object) $withdraw_history; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get withdraw method for a specific |
| 293 | * |
| 294 | * @since 1.0.0 |
| 295 | * |
| 296 | * @param int $user_id user id. |
| 297 | * @return bool|mixed |
| 298 | */ |
| 299 | public static function get_user_withdraw_method( $user_id = 0 ) { |
| 300 | $user_id = tutor_utils()->get_user_id( $user_id ); |
| 301 | $account = get_user_meta( $user_id, '_tutor_withdraw_method_data', true ); |
| 302 | |
| 303 | if ( $account ) { |
| 304 | return maybe_unserialize( $account ); |
| 305 | } |
| 306 | |
| 307 | return false; |
| 308 | } |
| 309 | } |
| 310 |