Skip to content

Commit

Permalink
Report: Storing order earnings item wise
Browse files Browse the repository at this point in the history
Previously multiple earnings getting inserted but price was showing only one record and rest were showing 0 amount
  • Loading branch information
shewa12 committed Nov 7, 2024
1 parent 039a75b commit d57f4ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
34 changes: 28 additions & 6 deletions classes/Earnings.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function prepare_order_earnings( int $order_id ) {
$course_id = apply_filters( 'tutor_subscription_course_by_plan', $item->id, $order_details );
}

$this->earning_data = $this->prepare_earning_data( $total_price, $course_id, $order_id, $order_details->order_status );
$this->earning_data[] = $this->prepare_earning_data( $total_price, $course_id, $order_id, $order_details->order_status );
}
}
}
Expand Down Expand Up @@ -202,8 +202,11 @@ public function store_earnings() {
throw new \Exception( self::INVALID_DATA_MSG );
}

$inserted_id = 0;
try {
$inserted_id = QueryHelper::insert( $this->earning_table, $this->earning_data );
foreach ( $this->earning_data as $earning ) {
$inserted_id = QueryHelper::insert( $this->earning_table, $earning );
}
} catch ( \Throwable $th ) {
throw new \Exception( $th->getMessage() );
}
Expand Down Expand Up @@ -273,6 +276,22 @@ public function delete_earning( $earning_id ) {
);
}

/**
* Delete earning by order id
*
* @since 3.0.0
*
* @param int $order_id Order id.
*
* @return bool true|false
*/
public function delete_earning_by_order( $order_id ) {
return QueryHelper::delete(
$this->earning_table,
array( 'order_id' => $order_id )
);
}

/**
* Before storing earning this method will check if
* earning exist for the given order id. If found it will
Expand All @@ -289,12 +308,15 @@ public function remove_before_store_earnings() {
throw new \Exception( self::INVALID_DATA_MSG );
}

$earning = $this->is_exist_order_earning( $this->order_id );
if ( $earning ) {
$this->delete_earning( $earning->earning_id );
if ( $this->is_exist_order_earning( $this->order_id ) ) {
$this->delete_earning( $this->order_id );
}

return $this->store_earnings();
try {
return $this->store_earnings();
} catch ( \Throwable $th ) {
tutor_log( $th );
}
}

}
8 changes: 4 additions & 4 deletions ecommerce/HooksHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@ function( $enroll_data ) {
}
}
}

// Update earnings.
$earnings->prepare_order_earnings( $order_id );
$earnings->remove_before_store_earnings();
}

// Update earnings.
$earnings->prepare_order_earnings( $order_id );
$earnings->remove_before_store_earnings();
}

}
Expand Down

0 comments on commit d57f4ed

Please sign in to comment.