dozent_get_earning_sum( int $user_id, array $date_filter = array() )

Get earning summery of an instructor


Description

This function returns data like below

 $earning_sum = (object) [
'per_course_price'   => 0,
'course_price_total' => 0,
'instructor_amount'  => 0,
'withdraws_amount'   => 0,
'balance'            => 0,
'admin_amount'       => 0,
'deduct_fees_amount' => 0,
]; 

See also


Parameters

$user_id

(int) (Required) User ID (Instructor ID)

$date_filter

(array) (Optional) Filtering data args

Default value: array()


Return

(array|object|void|null)


Source

File: includes/earning-functions.php

	function dozent_get_earning_sum( $user_id = 0, $date_filter = [] ) {
		global $wpdb;

		$user_id    = dozent_get_user_id( $user_id );
		$date_query = '';

		if ( dozent_count( $date_filter ) ) {
			extract( $date_filter );

			if ( ! empty( $dataFor ) ) {
				if ( $dataFor === 'yearly' ) {
					if ( empty( $year ) ) {
						$year = date( 'Y' );
					}
					$date_query = "AND YEAR(created_at) = {$year} ";
				}
			} else {
				$date_query = " AND (created_at BETWEEN '{$start_date}' AND '{$end_date}') ";
			}
		}

		$complete_status = dozent_get_earnings_completed_statuses();
		$complete_status = "'" . implode( "','", $complete_status ) . "'";

		$earning_sum = $wpdb->get_row( "SELECT SUM(per_course_price) as per_course_price, 
                    SUM(course_price_total) as course_price_total, 
                    SUM(instructor_amount) as instructor_amount, 
                    (SELECT SUM(amount) FROM {$wpdb->dozent_withdrawal} WHERE user_id = {$user_id} AND status != 'rejected' ) as 
                    withdraws_amount,
                    SUM(admin_amount) as admin_amount, 
                    SUM(deduct_fees_amount)  as deduct_fees_amount
                    FROM {$wpdb->dozent_earnings} 
                    WHERE user_id = {$user_id} AND order_status IN({$complete_status}) {$date_query} " );

		if ( $earning_sum->per_course_price ) {
			$earning_sum->balance = $earning_sum->instructor_amount - $earning_sum->withdraws_amount;
		} else {

			$earning_sum = (object) [
				'per_course_price'   => 0,
				'course_price_total' => 0,
				'instructor_amount'  => 0,
				'withdraws_amount'   => 0,
				'balance'            => 0,
				'admin_amount'       => 0,
				'deduct_fees_amount' => 0,
			];
		}

		return $earning_sum;
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.