EDD::add_earning_data( $course_ids,  $product_id,  $order_id )


Source

File: classes/EDD.php

	public function add_earning_data( $course_ids, $product_id, $order_id ) {
		global $wpdb;

		$total_price = edd_get_download_price( $product_id );
		$payment     = new \EDD_Payment( $order_id );

		$payment_status = 'pending';
		if ( $payment ) {
			$payment_status = $payment->status;

			if ( $payment->status === 'publish' ) {
				$payment_status = 'completed';
			}
		}

		if ( dozent_count( $course_ids ) ) {

			$total_courses    = count( $course_ids );
			$per_course_price = $total_price;

			/**
			 * Divide Split course price into all courses
			 * so each attached course will get the earning based on equal amount
			 */

			if ( $per_course_price > 0 && $total_courses > 1 ) {
				$per_course_price = $total_price / $total_courses;
			}

			$enable_fees_deducting = (bool) dozent_get_option( 'dozent_earning_fees.enable_fees_deducting' );
			$fees_name             = dozent_get_option( 'dozent_earning_fees.fees_name' );
			$deduct_fees_rate      = dozent_get_option( 'dozent_earning_fees.fees_amount' );
			$fees_type             = dozent_get_option( 'dozent_earning_fees.fees_type' );

			$fees_deduct_data = array();

			if ( $enable_fees_deducting ) {
				$fees_amount = $deduct_fees_rate;

				if ( $deduct_fees_rate > 0 ) {
					if ( $fees_type === 'percent' ) {
						$fees_amount = ( $total_price * $deduct_fees_rate ) / 100;
					}

					if ( $fees_amount > 0 && $total_courses > 1 ) {
						$fees_amount = $fees_amount / $total_courses;
					}

					//If fis amount fixed, it will deduct direct $fees_amount

					$per_course_price = $per_course_price - $fees_amount;
				}

				$fees_deduct_data = array(
					'deduct_fees_rate'   => $deduct_fees_rate,
					'deduct_fees_amount' => $fees_amount,
					'deduct_fees_name'   => $fees_name,
					'deduct_fees_type'   => $fees_type,
				);
			}

			$instructor_rate = dozent_get_option( 'earning_instructor_commission' );
			$admin_rate      = dozent_get_option( 'earning_admin_commission' );

			$instructor_amount = 0;
			if ( $instructor_rate > 0 ) {
				$instructor_amount = ( $per_course_price * $instructor_rate ) / 100;
			}

			$admin_amount = 0;
			if ( $admin_rate > 0 ) {
				$admin_amount = ( $per_course_price * $admin_rate ) / 100;
			}

			foreach ( $course_ids as $course_id ) {
				$user_id = $wpdb->get_var( "SELECT post_author FROM {$wpdb->posts} WHERE ID = {$course_id} " );

				$earning_data = array(
					'user_id'                   => $user_id,
					'course_id'                 => $course_id,
					'order_id'                  => $order_id,
					'order_status'              => $payment_status,
					'total_courses_under_order' => $total_courses,
					'course_price_total'        => $total_price,
					'per_course_price'          => $per_course_price,
					'instructor_amount'         => $instructor_amount,
					'instructor_rate'           => $instructor_rate,
					'admin_amount'              => $admin_amount,
					'admin_rate'                => $admin_rate,
					'commission_type'           => 'percent',
					'process_by'                => 'EDD',
					'created_at'                => date( 'Y-m-d H:i:s', dozent_time() ),
				);

				$earning_data['raw_data'] = json_encode( $earning_data );

				$earning_data = apply_filters( 'dozent_new_earning_data', array_merge( $earning_data, $fees_deduct_data ) );

				$wpdb->insert( $wpdb->prefix . 'dozent_earnings', $earning_data );
			}
		}

	}