public function redeem_discount($cart_id = null)
 {
     if (!$cart_id) {
         $cart_id = $this->GetData('id');
     }
     $cart_id = mysql_real_escape_string($cart_id);
     $user_id = mysql_real_escape_string(Site::CurrentUser()->id);
     $cart = Cart::find("carts.id = '{$cart_id}' and carts.user_id = '{$user_id}'");
     if (!$cart) {
         throw new Error404();
     }
     $url = "bookings/pay/{$cart->id}";
     if ($this->post) {
         $code = $this->PostData('code');
         if (!$code) {
             Site::Flash('error', 'No discount code entered');
             RedirectBack($url);
         }
         $discount = DiscountCode::find_by_code($code);
         if (!$discount) {
             Site::Flash('error', 'The discount code could not be found');
             RedirectBack($url);
         }
         try {
             $discount->redeem($cart);
         } catch (Error500 $e) {
             Site::Flash('error', $e->getMessage());
             RedirectBack($url);
         }
         Site::Flash('notice', 'The discount has been applied');
     }
     RedirectBack($url);
 }
 public function create($discount_id = null, $item = null)
 {
     if (!$discount_id) {
         $discount_id = $this->GetData('discount_id');
     }
     $discount = DiscountCode::find_by_id($discount_id);
     if (!$discount) {
         throw new Error404('Unable to find DiscountCode');
     }
     if (!$item) {
         $item = $this->GetData('item');
     }
     $items = array('ticket' => 'EventTicket', 'service' => 'Service');
     if (!isset($items[$item])) {
         $item = 'ticket';
     }
     $class = $items[$item];
     $ditem = new DiscountItem();
     $ditem->discount_id = $discount->id;
     $ditem->item_class = $class;
     if ($this->post) {
         $ditem->item_id = $this->PostData('item_id');
         if ($ditem->save()) {
             Site::Flash('notice', 'The discount item has been added');
             Redirect("admin/discounts/{$discount->id}");
         }
     }
     $allItems = call_user_func(array($class, 'find_all'));
     $allItems = array_reverse($allItems);
     $items = array();
     foreach ($allItems as $obj) {
         $items[$obj->id] = "[{$obj}] {$obj->event->name} - {$obj->name}";
     }
     $this->assign('ditem', $ditem);
     $this->assign('item', $item);
     $this->assign('discount', $discount);
     $this->assign('items', $items);
     $this->title = "Discounts :: {$discount->code} :: Items";
     $this->render('discount_item/create.tpl');
 }
Example #3
0
 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 static function get_fields()
 {
     return self::select_fields() . ", " . User::select_fields() . ", " . DiscountCode::select_fields();
 }
 protected function load_discount($id = null)
 {
     if ($this->GetData('id')) {
         $id = $this->GetData('id');
     }
     $discount = DiscountCode::find_by_id($id);
     if ($discount) {
         return $discount;
     } else {
         throw new Error404();
     }
 }
 protected static function get_fields()
 {
     return implode(', ', array(self::select_fields(), DiscountCode::select_fields(), Cart::select_fields(), CartItem::select_fields(), EventSignup::select_fields(), EventService::select_fields(), User::select_fields()));
 }
Example #7
0
 public function actionEditsend()
 {
     //exit;
     $disid = $_GET['id'];
     $userID = Commonmodel::getOrganID();
     $model = DiscountCode::model()->findAll('DiscountID=:disid', array(':disid' => $disid));
     foreach ($model as $key => $val) {
         $ownID = $val['OwnerID'];
         $carID = $val['CarID'];
         $sql = "select b.ID,b.Name,b.Phone,a.LicensePlate,a.ID as CarID,b.NickName from tbl_car_info a,tbl_car_owner b \n\t\t\t\t  where a.ID not in(select  CarID from tbl_discount_code where DiscountID='{$disid}') and b.ID=a.OwnerID\n\t\t\t\t\tand a.UserID='{$userID}'";
         $result = Yii::app()->db->createCommand($sql)->queryAll();
     }
     foreach ($result as $key => $value) {
         $data[$key]['ID'] = $value['ID'];
         $data[$key]['CarID'] = $value['CarID'];
         $data[$key]['NickName'] = $value['NickName'];
         $data[$key]['OwnerName'] = $value['Name'];
         $data[$key]['LicensePlate'] = $value['LicensePlate'];
         $data[$key]['OwnerPhone'] = $value['Phone'];
     }
     $rs = array('total' => 1, 'rows' => $data ? $data : array());
     echo json_encode($rs);
 }
 protected static function get_fields()
 {
     return self::select_fields() . ", " . DiscountCode::select_fields() . ", " . Service::select_fields() . ", " . EventTicket::select_fields();
 }