diff --git a/include/controllers/default/crunchbutton/api/user/index.php b/include/controllers/default/crunchbutton/api/user/index.php index 8bb20bb60..05501ef37 100644 --- a/include/controllers/default/crunchbutton/api/user/index.php +++ b/include/controllers/default/crunchbutton/api/user/index.php @@ -25,17 +25,20 @@ class Controller_api_user extends Crunchbutton_Controller_Rest { $out = []; $out[ 'invite_code' ] = $user->invite_code; $free_delivery = intval( $reward[ Crunchbutton_Reward::CONFIG_KEY_MAX_CAP_POINTS ] ); - $out[ 'free_delivery' ] = number_format( $free_delivery, 0, '.', ',' ); + $out[ 'free_delivery' ] = Crunchbutton_Credit::formatPoints( $free_delivery ); $out[ 'total' ] = Crunchbutton_Credit::points( $user->id_user ); if( $free_delivery > 0 && $free_delivery <= $out[ 'total' ] ){ $out[ 'show' ] = $out[ 'free_delivery' ]; + $out[ 'points_percent' ] = 100; $out[ 'free_delivery_message' ] = true; } else { $out[ 'free_delivery_message' ] = false; - $out[ 'show' ] = number_format( $out[ 'total' ], 0, '.', ',' ); - $out[ 'away_free_delivery' ] = number_format( $free_delivery - $out[ 'total' ], 0, '.', ',' ); + $out[ 'show' ] = Crunchbutton_Credit::formatPoints( $out[ 'total' ] ); + $out[ 'points_percent' ] = intval( ( $out[ 'total' ] / $free_delivery * 100 ) ); + $out[ 'away_free_delivery' ] = Crunchbutton_Credit::formatPoints( $free_delivery - $out[ 'total' ] ); } echo json_encode( $out );exit; + break; // Verify if the login was already taken case 'verify': diff --git a/include/library/Crunchbutton/Credit.php b/include/library/Crunchbutton/Credit.php index b578b52d3..8d13fbc12 100644 --- a/include/library/Crunchbutton/Credit.php +++ b/include/library/Crunchbutton/Credit.php @@ -294,12 +294,23 @@ class Crunchbutton_Credit extends Cana_Table return 0; } + public function formatPoints( $points ){ + return Util::humanReadableNumbers( $points ); + } + public function points( $id_user ){ $query = 'SELECT SUM( value ) AS points FROM credit c WHERE c.id_user = ? AND credit_type = ? AND type = ?'; $row = Cana::db()->get( $query, [$id_user, Crunchbutton_Credit::CREDIT_TYPE_POINT, Crunchbutton_Credit::TYPE_CREDIT]); if( $row->_items && $row->_items[0] ){ $row = $row->_items[0]; - $spent = ( $row->points && $row->points < 0 ) ? 0 : $row->points; + $points = ( $row->points && $row->points < 0 ) ? 0 : $row->points; + } + $spent = 0; + $query = 'SELECT SUM( value ) AS spent FROM credit c WHERE c.id_user = ? AND credit_type = ? AND type = ?'; + $row = Cana::db()->get( $query, [$id_user, Crunchbutton_Credit::CREDIT_TYPE_POINT, Crunchbutton_Credit::TYPE_DEBIT]); + if( $row->_items && $row->_items[0] ){ + $row = $row->_items[0]; + $spent = ( $row->spent && $row->spent < 0 ) ? 0 : $row->spent; } return intval( ( $points - $spent ) ); } diff --git a/include/library/Crunchbutton/Order.php b/include/library/Crunchbutton/Order.php index 93ba47de8..e2237d9e1 100644 --- a/include/library/Crunchbutton/Order.php +++ b/include/library/Crunchbutton/Order.php @@ -2207,16 +2207,15 @@ class Crunchbutton_Order extends Crunchbutton_Order_Trackchange { $reward = new Crunchbutton_Reward; $points = $reward->processOrder( $this->id_order ); $shared = $reward->orderWasAlreadyShared( $this->id_order ); - $out['reward'] = array( 'points' => number_format( $points, 0, '.', ',' ), 'shared' => $shared ); + + $out['reward'] = array( 'points' => Crunchbutton_Credit::formatPoints( $points ), 'shared' => $shared ); } } else { $reward = new Crunchbutton_Reward; $points = $reward->processOrder( $this->id_order ); - $out['reward'] = array( 'points' => number_format( $points, 0, '.', ',' ) ); + $out['reward'] = array( 'points' => Crunchbutton_Credit::formatPoints( $points ) ); } - - return $out; } diff --git a/include/library/Crunchbutton/User.php b/include/library/Crunchbutton/User.php index 14059c73c..5c6d58b9d 100644 --- a/include/library/Crunchbutton/User.php +++ b/include/library/Crunchbutton/User.php @@ -244,15 +244,17 @@ class Crunchbutton_User extends Cana_Table { $reward = $reward->loadSettings(); $free_delivery = intval( $reward[ Crunchbutton_Reward::CONFIG_KEY_MAX_CAP_POINTS ] ); $out[ 'points' ] = []; - $out[ 'points' ][ 'free_delivery' ] = number_format ( $free_delivery, 0, '.', ',' ); + $out[ 'points' ][ 'free_delivery' ] = Crunchbutton_Credit::formatPoints( $free_delivery ); $out[ 'points' ][ 'total' ] = Crunchbutton_Credit::points( $this->id_user ); if( $free_delivery > 0 && $free_delivery <= $out[ 'points' ][ 'total' ] ){ $out[ 'points' ][ 'show' ] = $out[ 'points' ][ 'free_delivery' ]; + $out[ 'points' ][ 'points_percent' ] = 100; $out[ 'points' ][ 'free_delivery_message' ] = true; } else { $out[ 'points' ][ 'free_delivery_message' ] = false; - $out[ 'points' ][ 'show' ] = number_format( $out[ 'points' ][ 'total' ], 0, '.', ',' );; - $out[ 'points' ][ 'away_free_delivery' ] = number_format( $free_delivery - $out[ 'points' ][ 'total' ], 0, '.', ',' );; + $out[ 'points' ][ 'show' ] = Crunchbutton_Credit::formatPoints( $out[ 'points' ][ 'total' ] ); + $out[ 'points' ][ 'points_percent' ] = intval( ( $out[ 'points' ][ 'total' ] / $free_delivery * 100 ) ); + $out[ 'points' ][ 'away_free_delivery' ] = Crunchbutton_Credit::formatPoints( $free_delivery - $out[ 'points' ][ 'total' ] ); } return $out; diff --git a/include/library/Crunchbutton/Util.php b/include/library/Crunchbutton/Util.php index 1a92c2a62..6951409fa 100755 --- a/include/library/Crunchbutton/Util.php +++ b/include/library/Crunchbutton/Util.php @@ -9,6 +9,18 @@ class Crunchbutton_Util extends Cana_Model { ( strpos( $_SERVER['HTTP_HOST'], 'dev.pit' ) !== false ) ) ? true : false; } + // https://gist.github.com/maggiben/9457434 + public static function humanReadableNumbers( $number ){ + if( $number < 1000 ){ + return $number; + } + $si = [ 'K', 'M', 'G', 'T', 'P', 'H' ]; + $exp = floor( log( $number ) / log( 1000 ) ); + $result = $number / pow( 1000, $exp ); + $result = ( $result % 1 > ( 1 / pow( 1000, $exp - 1 ) ) ? round( $result, 2 ) : round( $result, 0 ) ); + return $result . $si[ $exp - 1 ]; + } + public function frontendTemplates($export = false) { $files = []; diff --git a/include/views/default/crunchbutton/frontend/orders.phtml b/include/views/default/crunchbutton/frontend/orders.phtml index f56af9f36..ecc730396 100644 --- a/include/views/default/crunchbutton/frontend/orders.phtml +++ b/include/views/default/crunchbutton/frontend/orders.phtml @@ -39,7 +39,9 @@
You are {{account.user.points.away_free_delivery}} points away from a free delivery.
Earn points by ordering food, sharing your order, or referring friends.
@@ -52,11 +54,6 @@