public function index()
 {
     $page = 1;
     if ($this->GetData('page')) {
         $page = $this->GetData('page');
     }
     $user = Site::CurrentUser();
     $id = mysql_real_escape_string($user->id);
     $awards = LoyaltyAward::paginate("users.id = '{$id}'", "loyalty_awards.created_at DESC, loyalty_awards.id DESC", $page, 50);
     $this->assign('user', $user);
     $this->assign('awards', $awards);
     $this->title = 'Loyalty Points';
     $this->render('loyalty_award/index.tpl');
 }
 public function convert_loyalty()
 {
     $balance = $this->loyalty_balance();
     global $config;
     $expiry = strtotime($config['loyalty']['expiry'], time());
     while ($balance > 0) {
         $voucher = $balance;
         if ($voucher > 1000) {
             $voucher = 1000;
         }
         $balance -= $voucher;
         $value = $voucher * $config['loyalty']['cash_value'];
         // Let's do this inside a transaction
         mysql_query('BEGIN');
         $discount = new DiscountCode();
         $discount->code = strtoupper(md5(uniqid() . mt_rand()));
         $discount->description = "Loyalty Discount for {$this->nickname}: {$voucher}pts";
         $discount->type = "monetary";
         $discount->quantity = 1;
         $discount->active = true;
         $discount->value = $value;
         $discount->expires = $expiry;
         $discount->full_cart_discount = true;
         $discount->loyalty = true;
         if (!$discount->save()) {
             mysql_query('ROLLBACK');
             throw new Error500('Unable to save discount', $discount);
         }
         $ld = new LoyaltyDiscount();
         $ld->discount_id = $discount->id;
         $ld->user_id = $this->id;
         $ld->discount_id = $discount->id;
         $ld->points_cash_value = $value;
         $ld->points = $voucher;
         if (!$ld->save()) {
             mysql_query('ROLLBACK');
             throw new Error500('Unable to save LoyaltyDiscount', $discount);
         }
     }
     // Mark all points as redeemed
     $id = mysql_real_escape_string($this->id);
     $awards = LoyaltyAward::find_all("users.id = '{$id}' AND loyalty_awards.redeemed = 0");
     foreach ($awards as $award) {
         $award->redeemed = true;
         if (!$award->save()) {
             mysql_query('ROLLBACK');
             throw new Error500('Unable to save LoyaltyAward', $award);
         }
     }
     mysql_query('COMMIT');
 }
 protected function load_award($id = null)
 {
     if (!$id) {
         $id = $this->GetData('id');
     }
     $award = LoyaltyAward::find_by_id($id);
     if ($award) {
         return $award;
     } else {
         Error404();
     }
 }
 public static function award_loyalty($to, $awarded_by = null, $points = 0, $justification = "Automated award", $redeemed = false)
 {
     if (is_a($awarded_by, 'User')) {
         $awarded_by = $awarded_by->id;
     }
     if (is_a($to, 'User')) {
         $to = $to->id;
     }
     if (!$awarded_by) {
         $awarded_by = User::find_by_nickname('Administrator')->id;
     }
     $award = new LoyaltyAward();
     $award->user_id = $to;
     $award->awarded_by_id = $awarded_by;
     $award->justification = $justification;
     $award->points = $points;
     $award->redeemed = $redeemed;
     if ($award->save()) {
         return true;
     } else {
         return false;
     }
 }