Last active
May 11, 2020 10:55
-
-
Save kreamweb/b2c377ef2acd26b002b24ba05e1aed5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( isset( $_GET['ywpar_fix_points8'] ) ) { | |
add_action( 'init', 'start_the_script7' ); | |
function start_the_script7() { | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'yith_ywpar_points_log'; | |
$limit = 10; | |
$users = $wpdb->get_col( "select distinct point.user_id from $table_name as point WHERE NOT EXISTS ( | |
SELECT * FROM " . $wpdb->base_prefix . "usermeta as um | |
WHERE `meta_key` = '_ywpar_fixed1' | |
AND um.user_id = point.user_id | |
) LIMIT $limit" ); | |
if ( $users ) { | |
foreach ( $users as $user ) { | |
error_log("==== UTENTE ".$user."====="); | |
ywpar_recalculation( $user ); | |
error_log("==== END ".$user."====="); | |
delete_user_meta( $user,'_ywpar_fixed8'); | |
delete_user_meta( $user,'_ywpar_fixed7'); | |
break; | |
} | |
wp_safe_redirect( add_query_arg( array( 'users_done' => count( $users ) ) ) ); | |
exit(); | |
} | |
} | |
} | |
//New calculation | |
function ywpar_recalculation( $user_id ){ | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'yith_ywpar_points_log'; | |
update_user_meta( $user_id,'_ywpar_rewarded_points',''); | |
update_user_meta( $user_id,'_ywpar_used_points',''); | |
$q= $wpdb->prepare( "UPDATE $table_name set cancelled =%s WHERE user_id = %d", '0000-00-00 00:00:00', $user_id ); | |
$wpdb->query($q); | |
$wpdb->query( "DELETE from $table_name where action IN ('expired_points') and user_id={$user_id}" ); | |
$point_earned = array(); | |
$status_earned = array( 'order_completed','reviews_exp','amount_spent_exp','registration_exp','order_refund','order_cancelled', 'admin_action', 'points_exp', 'num_of_orders_exp'); | |
$status_redeemed = array( 'redeemed_points', 'admin_action', 'order_refund', 'order_cancelled'); | |
$total_points = 0; //total user points | |
$used_point = 0; //total used point | |
$redeemed_points = 0; //total redeemed points | |
$query = "SELECT * FROM $table_name as ywpar_points where user_id = $user_id"; | |
$res = $wpdb->get_results( $query ); | |
if ( $res ) { | |
foreach ( $res as $entry ) { | |
if ( in_array( $entry->action, $status_earned ) && $entry->amount > 0 ) { | |
$point_earned[ $entry->id ] = array( | |
'amount' => $entry->amount, | |
'start_amount' => $entry->amount, | |
'date_earning' => $entry->date_earning | |
); | |
$total_points += $entry->amount; | |
continue; | |
} | |
if ( in_array( $entry->action, $status_redeemed ) && $entry->amount < 0 ) { | |
$redeemed_points += absint( $entry->amount ); | |
if ( $point_earned ) { | |
$to_redeemed = absint( $entry->amount ); | |
error_log(print_r($point_earned,1)); | |
foreach ( $point_earned as $key => $row ) { | |
if ( $to_redeemed == 0 ) { | |
continue; | |
} | |
//check we have saturated this row | |
if ( isset( $row['cancelled'] ) || $row['amount'] == 0 ) { | |
continue; | |
} | |
//check expired | |
if ( ywpar_are_expired( $row['date_earning'], $entry->date_earning ) ) { | |
$total_points -= $row['amount']; | |
$point_earned[ $key ]['cancelled'] = $entry->date_earning; | |
$point_earned[ $key ]['expired'] = ywpar_get_expired_date( $row['date_earning'] ); | |
continue; | |
} | |
$difference = $row['amount'] - absint( $to_redeemed ); | |
if ( $difference <= 0 ) { | |
// error_log('usati tutti e '.$row['amount']); | |
$point_earned[ $key ]['cancelled'] = $entry->date_earning; | |
$used_point += $row['amount']; | |
$point_earned[ $key ]['amount'] = 0; | |
$total_points -= $row['amount']; | |
$to_redeemed -= $row['amount']; | |
} else { | |
//error_log('usati solo '. $to_redeemed ); | |
$used_point += absint( $to_redeemed ); | |
$point_earned[ $key ]['amount'] = $difference; | |
$total_points -= absint( $to_redeemed ); | |
$to_redeemed = 0; | |
} | |
} | |
error_log(print_r($point_earned,1)); | |
error_log('used_point: '. $used_point); | |
} | |
} | |
} | |
} | |
if ( $point_earned ) { | |
foreach ( $point_earned as $id => $row ) { | |
if ( ! isset( $row['cancelled'] ) && ! isset( $row['expired'] ) ) { | |
if ( ywpar_are_expired( $row['date_earning'], date( 'Y-m-d H:i:s' ) ) ) { | |
$total_points -= $row['amount']; | |
$row['cancelled'] = ywpar_get_expired_date( $row['date_earning'] ); | |
$row['expired'] = ywpar_get_expired_date( $row['date_earning'] ); | |
} | |
} | |
error_log(print_r($row,1)); | |
if ( isset( $row['cancelled'] ) && ! isset( $row['expired'] ) ) { | |
$q = $wpdb->prepare( "UPDATE $table_name set cancelled=%s where id=%d", $row['cancelled'], $id ); | |
$wpdb->query( $q ); | |
} | |
if ( isset( $row['expired'] ) ) { | |
$q = $wpdb->prepare( "INSERT INTO $table_name (`user_id`, `action`, `order_id`, `amount`, `date_earning` , `cancelled` , `description` ) VALUES | |
( %d, 'expired_points', 0, %d, %s, '0000-00-00 00:00:00','recalculated expiration points')", $user_id, - $row['amount'], $row['expired'] ); | |
$wpdb->query( $q ); | |
$q = $wpdb->prepare( "UPDATE $table_name set cancelled=%s where id=%d", $row['expired'], $id ); | |
$updated = $wpdb->query( $q ); | |
} | |
} | |
} | |
error_log('_ywpar_used_points '.$used_point ); | |
error_log('_ywpar_rewarded_points '.$redeemed_points ); | |
error_log('_ywpar_user_total_points '.$total_points ); | |
update_user_meta( $user_id,'_ywpar_used_points',$used_point); | |
update_user_meta( $user_id,'_ywpar_rewarded_points',$redeemed_points); | |
update_user_meta( $user_id,'_ywpar_user_total_points',$total_points); | |
update_user_meta( $user_id, '_ywpar_fixed1', 1 ); | |
} | |
function ywpar_are_expired( $data_earning, $data_check ) { | |
$is_expired = false; | |
if ( get_option( 'ywpar_enable_expiration_point' ) ) { | |
$num_days = get_option( 'ywpar_days_before_expiration' ); | |
$date1 = new DateTime( $data_earning ); | |
$date2 = new DateTime( $data_check ); | |
$interval = $date1->diff( $date2 ); | |
$is_expired = ( $interval->days >= $num_days ); | |
} | |
return $is_expired; | |
} | |
function ywpar_get_expired_date( $data_earning ) { | |
$expire_date = $data_earning; | |
if ( get_option( 'ywpar_enable_expiration_point' ) ) { | |
$num_days = get_option( 'ywpar_days_before_expiration' ); | |
$date1 = new DateTime( $data_earning ); | |
$date1->modify('+'.$num_days.' days'); | |
$expire_date = $date1->format('Y-m-d H:i:s'); | |
} | |
return $expire_date; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment