Skip to content

Commit

Permalink
Merge pull request #716 from ravinderk/issues/714
Browse files Browse the repository at this point in the history
Fix user lifetime donation stat
  • Loading branch information
Devin Walker committed Jul 14, 2016
2 parents 9dc290d + 2f29661 commit 2277dcc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
15 changes: 9 additions & 6 deletions includes/admin/payments/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,21 @@ function give_update_payment_details( $data ) {
$previous_customer->remove_payment( $payment_id, false );
$customer->attach_payment( $payment_id, false );

// If purchase was completed and not ever refunded, adjust stats of customers
if ( 'revoked' == $status || 'publish' == $status ) {

$previous_customer->decrease_purchase_count();
$previous_customer->decrease_value( $curr_total );
// Reduce previous user donation count and amoount.
$previous_customer->decrease_purchase_count();
$previous_customer->decrease_value( $curr_total );

// If purchase was completed and not ever refunded, adjust stats of new customers.
if ( 'revoked' == $status || 'publish' == $status ) {
$customer->increase_purchase_count();
$customer->increase_value( $new_total );
}

$payment->customer_id = $customer->id;
}
} else{
// Update user donation stat.
$customer->update_donation_value( $curr_total, $new_total );
}

// Set new meta values
$payment->user_id = $customer->user_id;
Expand Down
40 changes: 40 additions & 0 deletions includes/class-give-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,46 @@ public function decrease_value( $value = 0.00 ) {
return $this->purchase_value;
}

/**
* Decrease/Increase a customer's lifetime value
*
* This function will update donation stat on basis of current amount and new amount donation difference.
* Difference value can positive or negative. Negative value will decrease user donation stat while positive value increase donation stat.
*
*
* @access public
* @since 1.0
*
* @param float $curr_amount Current Donation amount
* @param float $new_amount New ( changed ) Donation amount
*
* @return mixed If successful, the new donation stat value, otherwise false
*/
public function update_donation_value( $curr_amount, $new_amount ) {
/**
* Payment total difference value can be:
* zero (in case amount not change)
* or -ve (in case amount decrease)
* or +ve (in case amount increase)
*/
$payment_total_diff = $new_amount - $curr_amount;

// We do not need to update donation stat if donation did not change.
if( ! $payment_total_diff ) {
return false;
}


if( $payment_total_diff > 0 ) {
$this->increase_value( $payment_total_diff );
}else{
// Pass payment total difference as +ve value to decrease amount from user lifetime stat.
$this->decrease_value( -$payment_total_diff );
}

return $this->purchase_value;
}

/**
* Get the parsed notes for a customer as an array
*
Expand Down

0 comments on commit 2277dcc

Please sign in to comment.