Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix user lifetime donation stat #716

Merged
merged 3 commits into from
Jul 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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