示例#1
0
 /**
  * 获取优惠券类型
  * @param $coupon_type_ids
  */
 public function getCouponTypeInfoByIds($coupon_type_ids)
 {
     if (empty($coupon_type_ids)) {
         return false;
     }
     $list = CouponType::model()->findAllByAttributes(array('id' => $coupon_type_ids));
     $newList = array();
     if (!empty($list)) {
         foreach ($list as $row) {
             $newList[$row->id] = $row->getAttributes();
         }
     }
     return $newList;
 }
示例#2
0
 /**
  * 使用优惠券时 (绑定优惠券、计算优惠券金额)【ckeckout页面绑定优惠券、并检查优惠券的使用】
  */
 public function actionCheckCoupon()
 {
     $res = array('status' => 1, 'msg' => '');
     $sn = addslashes(trim($_REQUEST['sn']));
     $userId = $this->user_id;
     try {
         if (empty($userId)) {
             throw new Exception("用户还没有登陆,请登录!", 2);
         }
         $map = array();
         $map['coupon_sn'] = $sn;
         $info = Coupon::model()->findByAttributes($map);
         $type = 'B';
         if (empty($info)) {
             $map = array();
             $map['coupon_sn'] = $sn;
             $info = CouponType::model()->findByAttributes($map);
             $type = 'A';
             if (empty($info)) {
                 throw new Exception("优惠券错误,该券号不存在!", 0);
             }
         } else {
             if ($info['end_time'] < time()) {
                 throw new Exception("优惠券已过期!", 0);
             } elseif (empty($info['user_id']) && empty($info['order_id'])) {
                 $info->user_id = $userId;
                 $info->band_time = time();
                 $flag = $info->save();
                 if (empty($flag)) {
                     throw new Exception("优惠券绑定失败,请重试!", 0);
                 }
             } elseif ($info['user_id'] != $userId || !empty($info['order_id'])) {
                 throw new Exception("优惠券已使用或者异常!", 0);
             } elseif ($info['start_time'] > time()) {
                 throw new Exception("优惠券还未到使用日期!", 0);
             }
         }
         $list = Cart::model()->getCartList($userId);
         if (empty($list['list'])) {
             throw new Exception("购物车不能为空!", 0);
         }
         $productAmount = $list['total']['product_amount'];
         if (!empty($productAmount) && $productAmount >= $info['satisfied_amount'] || empty($info['satisfied_amount'])) {
             $finialAmount = $list['total']['product_amount'] + $list['total']['shipping_fee'] - $info['coupon_amount'];
             $res['couponsn'] = $info['coupon_sn'];
             $res['couponAmount'] = $info['coupon_amount'];
             $res['shipingFee'] = $list['total']['shipping_fee'];
             $res['finalAmount'] = $finialAmount < 0 ? 0 : $finialAmount;
         } elseif ($productAmount < $info['satisfied_amount']) {
             throw new Exception("优惠券不满足使用条件!", 0);
         }
         $couponlist = Coupon::model()->getUserUsingCouponList($userId);
         if ($type == 'A') {
             array_push($couponlist, $info->getAttributes());
         }
         $res['couponlist'] = $couponlist;
     } catch (exception $e) {
         $res['status'] = $e->getCode();
         $res['msg'] = $e->getMessage();
     }
     exit(json_encode($res));
 }
示例#3
0
 /**
  * 获取优惠券信息并检查有效信息(有效信息检查-购物车检查优惠券时用)
  * @param $request
  * @return bool
  * @throws exception
  */
 public function getAndCheckCouponInfo($userId, $coupon_sn)
 {
     if (empty($coupon_sn) || empty($userId)) {
         return false;
     }
     #先检查时否是优惠券
     $map = array();
     $map['coupon_sn'] = $coupon_sn;
     $map['user_id'] = $userId;
     $couponInfo = Coupon::model()->findByAttributes($map);
     if (!empty($couponInfo)) {
         $couponInfo = $couponInfo->getAttributes();
         if (!empty($couponInfo['order_id']) || !empty($couponInfo['use_time'])) {
             throw new exception('该优惠券已经使用过!');
         } elseif ($couponInfo['start_time'] > time()) {
             throw new exception('优惠券还未到使用日期!');
         } elseif ($couponInfo['end_time'] < time()) {
             throw new exception('该优惠券已经过期!');
         }
         $couponInfo['type'] = 'B';
         return $couponInfo;
     }
     //如果A类券不存在就检查下B类券,就返回错误信息了
     $map = array();
     $map['coupon_sn'] = $coupon_sn;
     $couponInfo = CouponType::model()->findByAttributes($map);
     if (!empty($couponInfo)) {
         $couponInfo = $couponInfo->getAttributes();
         if ($couponInfo['start_time'] > time()) {
             throw new exception('优惠券还未到使用日期!');
         } elseif ($couponInfo['end_time'] < time()) {
             throw new exception('该优惠券已经过期!');
         }
         $couponInfo['type'] = 'A';
     } else {
         throw new exception('该优惠券不存在!');
     }
     return $couponInfo;
 }
示例#4
0
 /**
  * 优惠券列表.
  */
 public function actionCoupon()
 {
     $userId = $this->user_id;
     if (empty($userId)) {
         $this->redirect('/user/login');
     }
     $request = $_REQUEST;
     $result = Coupon::model()->getUserCouponListPage($userId, $request);
     $count = $result['count'];
     $list = $result['list'];
     $coupon_type_ids = $result['coupon_type_ids'];
     $couponTypeList = CouponType::model()->getCouponTypeInfoByIds($coupon_type_ids);
     $pages = '';
     if ($count > 0) {
         $pages = Common::instance()->get_page_list($count, $result['p'], $result['page_size'], '', 'coupon');
     }
     $viewData = array();
     $viewData['pages'] = $pages;
     $viewData['list'] = $list;
     $viewData['count'] = $count;
     $viewData['couponTypeList'] = $couponTypeList;
     $this->render('/user/coupon', $viewData);
 }
示例#5
0
 public function actionTypeDel()
 {
     $res = array('statusCode' => 200, 'message' => '删除成功!');
     try {
         if (empty($_REQUEST['id'])) {
             throw new Exception("数据错误,id不能为空!", 1);
         }
         $flag = CouponType::model()->deleteByPk($_REQUEST['id']);
         if (!$flag) {
             throw new exception('删除失败');
         }
     } catch (Exception $e) {
         $res['statusCode'] = 300;
         $res['message'] = '删除失败【' . $e->getMessage() . '】';
     }
     $res['callbackType'] = 'reloadTab';
     $res['forwardUrl'] = '/manage/coupon/typeIndex';
     $this->ajaxDwzReturn($res);
 }