Point redemption system improvements for customer-customer referrals #4248

This commit is contained in:
Pererinha 2015-02-05 19:31:42 -02:00
parent e779bc38b5
commit 17c0a15611
10 changed files with 154 additions and 150 deletions

View File

@ -50,7 +50,7 @@ class Controller_api_config_reward extends Crunchbutton_Controller_RestAccount {
$config = Crunchbutton_Config::getConfigByKey( $key );
if( $config->key ){
$value = trim( $this->request()[ $key ] );
if( $value ){
if( is_numeric( $value ) ){
$config->set( $value );
$config->save();
}

View File

@ -1,7 +1,7 @@
<?php
class Controller_api_test extends Cana_Controller {
<<<<<<< HEAD
public function e( $request ){
if( $request->body ){
@ -13,108 +13,37 @@ class Controller_api_test extends Cana_Controller {
public function init(){
// echo '<pre>';var_dump( c::user()->id_admin );exit();
// $token = Crunchbutton_Pexcard_Token::getToken();
// echo '<pre>';var_dump( $token );exit();
// Crunchbutton_Pexcard_Transaction::loadTransactions();
// die('hard');
// $transactions = Crunchbutton_Pexcard_Transaction::transactions( '01/01/2015', '01/30/2015' );
// echo json_encode( $transactions );exit;
// Crunchbutton_Pexcard_Monitor::checkBalanceLimit();
// $pexcard = Cockpit_Admin_Pexcard::getByPexcard( 6203 );
// echo '<pre>';var_dump( $pexcard->addArbitraryFunds( -110, 'just testing 1' ) );exit();;
// $funds = Crunchbutton_Pexcard_Card::addArbitraryFunds( 6203, 10.00 );
// echo '<pre>';var_dump( $funds );exit();
// $this->e( $funds );
// $pexcard = Cockpit_Admin_Pexcard::getByPexcard( 6203 );
// echo '<pre>';var_dump( $pexcard->addArbitraryFunds( 10.00, 'testing' ) );exit();;
// $pex = new Crunchbutton_Pexcard_Resource;
// $this->e( $pex->ping() );
$word = 'WUJ517YVR';
// $business = new Crunchbutton_Pexcard_Business;
// $this->e( $business->profile() );
// $business = new Crunchbutton_Pexcard_Business;
// $this->e( $business->admin() );
// $this->e( $business->admin( 1051 ) );
// $details = new Crunchbutton_Pexcard_Details;
// $this->e( $details->account( 6202 ) );
// $this->e( $details->account() );
// foreach( $words as $word ){
$giftCardAdded = false;
Log::debug([ 'totalOrdersByPhone' => $totalOrdersByPhone ]);
// At first check if it is an user's invite code - rewards: two way gift cards #2561
$reward = new Crunchbutton_Reward;
$inviter = $reward->validateInviteCode( $word );
if( $totalOrdersByPhone <= 1 && $inviter ){
// get the value of the discount
if( $inviter[ 'id_admin' ] ){
$value = $reward->getReferredDiscountAmount();
$admin_credit = $reward->adminRefersNewUserCreditAmount();
$this->giftCardInviter = [ 'id_user' => $inviter[ 'id_user' ], 'id_admin' => $inviter[ 'id_admin' ], 'value' => $value, 'word' => $word, 'admin_credit' => $admin_credit ];
if( $value ){
$this->giftcardValue = $value;
break;
}
} elseif( $inviter[ 'id_user' ] ){
// $card = Crunchbutton_Pexcard_Details::cards( 6202 );
// $this->e( $card );
$credit = $reward->refersNewUserCreditAmount();
echo '<pre>';var_dump( $settings );exit();
}
}
// }
echo '<pre>';var_dump( Crunchbutton_Pexcard_Card::details( 6210 ) );exit();
// $status = Crunchbutton_Pexcard_Card::change_status( 6283, 'Active' );
// echo '<pre>';var_dump( $status );exit();
// echo '<pre>';var_dump( Crunchbutton_Pexcard_Card::activate_card( 6208 ) );exit();;
// echo '<pre>';var_dump( $status );exit();
// $this->e( $status );
$cards = Crunchbutton_Pexcard_Card::card_list();
foreach( $cards->body as $card ){
$_cards = Crunchbutton_Pexcard_Details::cards( $card->id );
echo '<pre>';var_dump( $_cards );exit();
}
echo '<pre>';var_dump( $cards );exit();
$card = new Crunchbutton_Pexcard_Card;
$card_create = $card->create( [ 'FirstName' => 'Daniel',
'LastName' => 'Camargo',
'DateOfBirth' => '09/05/2012',
'Phone' => '_PHONE_',
'ShippingPhone' => '_PHONE_',
'ShippingMethod' => 'Expedited',
'Email' => '_EMAIL',
'ProfileAddress' => [
'ContactName' => 'David',
'AddressLine1' =>'1120 Princeton Drive #7',
'AddressLine2' => '',
'City' => 'Marina Del Rey',
'State' =>'California',
'PostalCode' => '90292',
'Country' => 'USA'
],
'ShippingAddress' => [
'ContactName' => 'David',
'AddressLine1' =>'1120 Princeton Drive #7',
'AddressLine2' => '',
'City' => 'Marina Del Rey',
'State' =>'California',
'PostalCode' => '90292',
'Country' => 'USA'
] ] );
$this->e( $card_create );
// $start = '01/01/2015';
// $end = '01/25/2015';
// $tranactios = Crunchbutton_Pexcard_Transaction::transactions( $start, $end );
$this->e( $tranactios );
=======
public function init(){
// $cron = new Crunchbutton_Cron_Job_AutoShutDownCommunity;
// $cron->run();
// $community = Crunchbutton_Community::o( 6 );
// echo '<pre>';var_dump( $community->driverRestaurant() );exit();
>>>>>>> master
}
}

View File

@ -6,6 +6,7 @@ class Controller_api_Giftcard extends Crunchbutton_Controller_Rest {
switch ( $this->method() ) {
case 'post':
case 'get':
if ( c::admin()->id_admin ) {
@ -439,10 +440,15 @@ class Controller_api_Giftcard extends Crunchbutton_Controller_Rest {
if( !c::user()->id_user && $totalOrdersByPhone == 0 ){
$valid = $reward->validateInviteCode( $word );
if( $valid ){
$value = $reward->getReferredDiscountAmount();
if( $value ){
echo json_encode( [ 'success' => [ 'value' => $value, 'giftcard' => $word, 'message' => 'This code (' . $word . ') gives you a $' . $value . ' gift card towards your order.' ] ] );
if( $valid[ 'id_user' ] ){
echo json_encode( [ 'success' => [ 'value' => 0, 'delivery_free' => true, 'giftcard' => $word, 'message' => 'This delivery fee is on us.' ] ] );
exit;
} else {
$value = $reward->getReferredDiscountAmount();
if( $value ){
echo json_encode( [ 'success' => [ 'value' => $value, 'giftcard' => $word, 'message' => 'This code (' . $word . ') gives you a $' . $value . ' gift card towards your order.' ] ] );
exit;
}
}
}
}

View File

@ -25,7 +25,7 @@ class Controller_api_user extends Crunchbutton_Controller_Rest {
$out = [];
$out[ 'free_delivery' ] = intval( $reward[ Crunchbutton_Reward::CONFIG_KEY_MAX_CAP_POINTS ] );
$out[ 'total' ] = Crunchbutton_Credit::points( $user->id_user );
if( $out[ 'free_delivery' ] > 0 && $out[ 'free_delivery' ] < $out[ 'total' ] ){
if( $out[ 'free_delivery' ] > 0 && $out[ 'free_delivery' ] <= $out[ 'total' ] ){
$out[ 'show' ] = $out[ 'free_delivery' ];
$out[ 'free_delivery_message' ] = true;
} else {

View File

@ -329,13 +329,75 @@ class Crunchbutton_Order extends Crunchbutton_Order_Trackchange {
Log::debug([ 'inviter' => $inviter ]);
if( $totalOrdersByPhone <= 1 && $inviter ){
// get the value of the discount
$value = $reward->getReferredDiscountAmount();
$admin_credit = $reward->adminRefersNewUserCreditAmount();
$this->giftCardInviter = [ 'id_user' => $inviter[ 'id_user' ], 'id_admin' => $inviter[ 'id_admin' ], 'value' => $value, 'word' => $word, 'admin_credit' => $admin_credit ];
if( $value ){
$this->giftcardValue = $value;
break;
if( $inviter[ 'id_admin' ] ){
$value = $reward->getReferredDiscountAmount();
$admin_credit = $reward->adminRefersNewUserCreditAmount();
$this->giftCardInviter = [ 'id_user' => $inviter[ 'id_user' ], 'id_admin' => $inviter[ 'id_admin' ], 'value' => $value, 'word' => $word, 'admin_credit' => $admin_credit ];
if( $value ){
$this->giftcardValue = $value;
break;
}
} elseif( $inviter[ 'id_user' ] ){
$referral = new Crunchbutton_Referral();
$referral->id_admin_inviter = null;
$referral->id_user_inviter = $inviter[ 'id_user'];
$referral->id_user_invited = $this->id_user;
$referral->admin_credit = null;
$referral->invite_code = $word;
$referral->new_user = 1;
$referral->date = date('Y-m-d H:i:s');
$referral->save();
$settings = $reward->loadSettings();
$credits_amount = $settings[ Crunchbutton_Reward::CONFIG_KEY_GET_REFERRED_DISCOUNT_AMOUNT ];
if( $credits_amount ){
$reward->saveRewardAsCredit( [ 'id_user' => $this->id_user,
'value' => $credits_amount,
'id_order' => null,
'credit_type' => Crunchbutton_Credit::CREDIT_TYPE_CASH,
'id_referral' => $referral->id_referral,
'note' => 'Cash Invited by: ' . $inviter[ 'id_user'] . ' code: ' . $word,
] );
}
$credits_amount = $settings[ Crunchbutton_Reward::CONFIG_KEY_GET_REFERRED_VALUE ];
if( $credits_amount ){
$reward->saveRewardAsCredit( [ 'id_user' => $this->id_user,
'value' => $credits_amount,
'id_order' => null,
'credit_type' => Crunchbutton_Credit::CREDIT_TYPE_POINT,
'id_referral' => $referral->id_referral,
'note' => 'Points Invited by: ' . $inviter[ 'id_user'] . ' code: ' . $word,
] );
}
$credits_amount = $settings[ Crunchbutton_Reward::CONFIG_KEY_REFER_NEW_USER_AMOUNT ];
if( $credits_amount ){
$reward->saveRewardAsCredit( [ 'id_user' => $inviter[ 'id_user'],
'value' => $credits_amount,
'id_order' => null,
'credit_type' => Crunchbutton_Credit::CREDIT_TYPE_CASH,
'id_referral' => $referral->id_referral,
'note' => 'Cash Invited ID: ' . $this->id_user . ' code: ' . $word,
] );
}
$credits_amount = $settings[ Crunchbutton_Reward::CONFIG_KEY_REFER_NEW_USER_VALUE ];
if( $credits_amount ){
$reward->saveRewardAsCredit( [ 'id_user' => $inviter[ 'id_user'],
'value' => $credits_amount,
'id_order' => null,
'credit_type' => Crunchbutton_Credit::CREDIT_TYPE_POINT,
'id_referral' => $referral->id_referral,
'note' => 'Points Invited ID: ' . $this->id_user . ' code: ' . $word,
] );
}
}
}
}
}
@ -596,9 +658,7 @@ class Crunchbutton_Order extends Crunchbutton_Order_Trackchange {
$reward = new Crunchbutton_Reward;
// the new user earns discount
if( $this->giftCardInviter[ 'id_user'] ){
$notes = 'Inviter ID: ' . $this->giftCardInviter[ 'id_user'] . ' code: ' . $this->giftCardInviter[ 'word'];
} else if( $this->giftCardInviter[ 'id_admin'] ){
if( $this->giftCardInviter[ 'id_admin'] ){
$notes = 'Inviter ID: ' . $this->giftCardInviter[ 'id_admin'] . ' code: ' . $this->giftCardInviter[ 'word'];
}
@ -609,32 +669,10 @@ class Crunchbutton_Order extends Crunchbutton_Order_Trackchange {
'note' => $notes,
] );
if( $this->giftCardInviter[ 'id_user'] ){
$credits_amount = $reward->refersNewUserCreditAmount();
if( $credits_amount ){
// the regular user that indicates his friend earns money too - configurable option
$reward->saveRewardAsCredit( [ 'id_user' => $this->giftCardInviter[ 'id_user'],
'value' => $credits_amount,
'id_order' => null,
'id_referral' => $referral->id_referral,
'note' => 'Invited ID: ' . $this->id_user . ' code: ' . $this->giftCardInviter[ 'word'],
] );
}
} else if( $this->giftCardInviter[ 'id_admin'] ){
if( $this->giftCardInviter[ 'id_admin'] ){
$credits_amount = $reward->adminRefersNewUserCreditAmount();
Log::debug([ 'id_admin' => $this->giftCardInviter[ 'id_admin'], '$credits_amount' => $credits_amount ]);
}
// Give points to the user that invites the new user
$points = $reward->getReferNewUser();
if( floatval( $points ) > 0 ){
$reward->saveReward( [ 'id_order' => $this->id_order, 'id_user' => $this->giftCardInviter[ 'id_user'], 'points' => $points, 'note' => 'points by referrer new user O#' . $this->id_order . ' U#' . $this->id_user ] );
}
// Give points to the user that was invited
$points = $reward->getRefered();
if( floatval( $points ) > 0 ){
$reward->saveReward( [ 'id_order' => $this->id_order, 'id_user' => $this->id_user, 'points' => $points, 'note' => 'points by getting referred O#' . $this->id_order . ' U#' . $this->giftCardInviter[ 'id_user'] ] );
}
} else {
$giftcards = Crunchbutton_Promo::validateNotesField( $this->notes, $this->id_restaurant );
$giftCardAdded = false;

View File

@ -24,17 +24,20 @@ class Crunchbutton_Reward extends Cana_Table{
const CONFIG_KEY_MAX_CAP_POINTS = 'reward_points_max_cap_points';
public function validateInviteCode( $code ){
$code = trim( $code );
// at first check if it belongs to an admin
$admin = Crunchbutton_Admin::byInviteCode( $code );
if( $admin->id_admin ){
$this->code = $code;
return [ 'id_admin' => $admin->id_admin ];
}
// second check if it belongs to an user
$user = Crunchbutton_User::byInviteCode( $code );
if( $user->id_user ){
return [ 'id_user' => $user->id_user ];
$codes = explode( ' ' , $code );
foreach( $codes as $code ){
$code = trim( $code );
// at first check if it belongs to an admin
$admin = Crunchbutton_Admin::byInviteCode( $code );
if( $admin->id_admin ){
$this->code = $code;
return [ 'id_admin' => $admin->id_admin ];
}
// second check if it belongs to an user
$user = Crunchbutton_User::byInviteCode( $code );
if( $user->id_user ){
return [ 'id_user' => $user->id_user ];
}
}
return false;
}
@ -66,7 +69,7 @@ class Crunchbutton_Reward extends Cana_Table{
$credit->date = date( 'Y-m-d H:i:s' );
$credit->value = $params[ 'value' ];
$credit->id_order = $params[ 'id_order' ];
$credit->credit_type = Crunchbutton_Credit::CREDIT_TYPE_CASH;
$credit->credit_type = $params[ 'credit_type' ];
$credit->note = $params[ 'note' ];
$credit->id_referral = $params[ 'id_referral' ];
$credit->save();

View File

@ -239,14 +239,13 @@ class Crunchbutton_User extends Cana_Table {
$out['tipper'] = $this->tipper();
// Reward stuff
$reward = new Crunchbutton_Reward;
$reward = $reward->loadSettings();
$out[ 'points' ] = [];
$out[ 'points' ][ 'free_delivery' ] = intval( $reward[ Crunchbutton_Reward::CONFIG_KEY_MAX_CAP_POINTS ] );
$out[ 'points' ][ 'total' ] = Crunchbutton_Credit::points( $this->id_user );
if( $out[ 'points' ][ 'free_delivery' ] > 0 && $out[ 'points' ][ 'free_delivery' ] < $out[ 'points' ][ 'total' ] ){
if( $out[ 'points' ][ 'free_delivery' ] > 0 && $out[ 'points' ][ 'free_delivery' ] <= $out[ 'points' ][ 'total' ] ){
$out[ 'points' ][ 'show' ] = $out[ 'points' ][ 'free_delivery' ];
$out[ 'points' ][ 'free_delivery_message' ] = true;
} else {

View File

@ -40,13 +40,17 @@
<a id="credit"></a>
<div ng-if="!account.user.points.free_delivery_message && account.user.points.away_free_delivery">
<div>You are <strong>{{account.user.points.away_free_delivery}}</strong> points away from a free delivery.</div><br/>
<div>Earn points by ordering food, sharing your order, or referring friends.</div><br/>
<div>Share this family and friends code and both of you get your next delivery on us.</div><br/>
<div><strong>{{account.user.invite_code}}</strong></div>
</div>
<div ng-if="account.user.points.free_delivery_message && account.user.points.free_delivery">
You have <strong>{{account.user.points.free_delivery}}</strong> points! The next delivery fee is on us.
</div>
<br>
<div>Earn points by ordering food, sharing your order, or referring friends.</div><br/>
<div>Share this family and friends code and both of you get your next delivery on us.</div><br/>
<div><strong>{{account.user.invite_code}}</strong></div>
<div class="divider"></div>
<div class="social-button twitter-button social-button-invite" ng-click="referral.twitter();">
<i class="fa fa-twitter"></i><span class="social-button-text">Tweet</span>

View File

@ -883,6 +883,8 @@ NGApp.controller( 'RestaurantCtrl', function ($scope, $http, $routeParams, $root
restaurantPage: (App.config.ab && App.config.ab.restaurantPage == 'restaurant-page-noimage') ? ' restaurant-pic-wrapper-hidden' : ''
};
var giftcard = GiftCardService;
$scope.giftcard = { giftcards : {} };
// Event will be called when the gift card changes
@ -893,6 +895,14 @@ NGApp.controller( 'RestaurantCtrl', function ($scope, $http, $routeParams, $root
$scope.giftcard.removed = giftcard.notes_field.removed;
$scope.giftcard.hasGiftCards = giftcard.notes_field.hasGiftCards;
$scope.giftcard.justOneGiftCardError = giftcard.notes_field.justOneGiftCardError;
if( giftcard.notes_field.giftcards && giftcard.notes_field.giftcards.success && giftcard.notes_field.giftcards.success[0] && giftcard.notes_field.giftcards.success[0].delivery_free ){
order.removeDeliveryFee();
$scope.order.updateTotal();
} else {
order.restoreDeliveryFee();
$scope.order.updateTotal();
}
$scope.giftcard.hasValue = ( parseFloat( giftcard.notes_field.value ) > 0 );
$scope.$safeApply();
});
@ -902,7 +912,6 @@ NGApp.controller( 'RestaurantCtrl', function ($scope, $http, $routeParams, $root
giftcard.notes_field.lastValidation = '';
$scope.checkGiftCard();
}
console.log('$scope.user.id_user',$scope.user.id_user);
});
$scope.checkGiftCard = function(){

View File

@ -272,6 +272,9 @@ NGApp.factory( 'OrderService', function ($http, $location, $rootScope, $filter,
* @return float
*/
service._breackDownDelivery = function () {
if( service._removeDeliveryFee ){
return 0;
}
var delivery = 0;
if (service.restaurant.delivery_fee && service.form.delivery_type == 'delivery') {
delivery = parseFloat(service.restaurant.delivery_fee);
@ -282,6 +285,19 @@ NGApp.factory( 'OrderService', function ($http, $location, $rootScope, $filter,
delivery = App.ceil(delivery);
return delivery;
}
service.removeDeliveryFee = function(){
service._removeDeliveryFee = true;
service._breackDownDelivery();
}
service.restoreDeliveryFee = function(){
service._removeDeliveryFee = false;
service._breackDownDelivery();
}
/**
* Crunchbutton service
*