/** * 获取优惠券类型 * @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; }
/** * 使用优惠券时 (绑定优惠券、计算优惠券金额)【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)); }
<?php echo Yii::t('CouponModule.coupon', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('CouponModule.coupon', 'are required'); ?> </div> <?php echo $form->errorSummary($model); ?> <div class="row"> <div class="col-sm-3"> <?php echo $form->dropDownListGroup($model, 'type', ['widgetOptions' => ['data' => CouponType::all()]]); ?> </div> <div class="col-sm-3"> <?php echo $form->dropDownListGroup($model, 'status', ['widgetOptions' => ['data' => CouponStatus::all()]]); ?> </div> </div> <div class="row"> <div class="col-sm-7"> <?php echo $form->textFieldGroup($model, 'name'); ?> </div>
/** * 优惠券列表. */ 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); }
/** * 获取优惠券信息并检查有效信息(有效信息检查-购物车检查优惠券时用) * @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; }
/** * @return array validation rules for model attributes. */ public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return [['name, code, status, type', 'required'], ['name, code', 'filter', 'filter' => 'trim'], ['name, code', 'length', 'max' => 255], ['status', 'in', 'range' => CouponStatus::keys()], ['type', 'in', 'range' => CouponType::keys()], ['value, min_order_price', 'numerical'], ['quantity, quantity_per_user', 'numerical', 'integerOnly' => true], ['registered_user, free_shipping', 'in', 'range' => [0, 1]], ['start_time, end_time', 'date', 'format' => 'yyyy-MM-dd'], ['code', 'unique'], ['id, name, code, type, value, min_order_price, registered_user, free_shipping, start_time, end_time, quantity, quantity_per_user, status', 'safe', 'on' => 'search']]; }
<?php /** * @var $model Coupon */ $this->breadcrumbs = [Yii::t('CouponModule.coupon', 'Coupons') => ['/coupon/couponBackend/index'], $model->code]; $this->pageTitle = Yii::t('CouponModule.coupon', 'Coupons - view'); $this->menu = [['icon' => 'fa fa-fw fa-list-alt', 'label' => Yii::t('CouponModule.coupon', 'Manage coupons'), 'url' => ['/coupon/couponBackend/index']], ['icon' => 'fa fa-fw fa-plus-square', 'label' => Yii::t('CouponModule.coupon', 'Create coupon'), 'url' => ['/coupon/couponBackend/create']], ['label' => Yii::t('CouponModule.coupon', 'Coupon') . ' «' . mb_substr($model->code, 0, 32) . '»'], ['icon' => 'fa fa-fw fa-pencil', 'label' => Yii::t('CouponModule.coupon', 'Update coupon'), 'url' => ['/coupon/couponBackend/update', 'id' => $model->id]], ['icon' => 'fa fa-fw fa-eye', 'label' => Yii::t('CouponModule.coupon', 'View coupon'), 'url' => ['/coupon/couponBackend/view', 'id' => $model->id]], ['icon' => 'fa fa-fw fa-trash-o', 'label' => Yii::t('CouponModule.coupon', 'Delete coupon'), 'url' => '#', 'linkOptions' => ['submit' => ['/coupon/couponBackend/delete', 'id' => $model->id], 'confirm' => Yii::t('CouponModule.coupon', 'Do you really want to remove this coupon?'), 'params' => [Yii::app()->getRequest()->csrfTokenName => Yii::app()->getRequest()->csrfToken], 'csrf' => true]]]; ?> <div class="page-header"> <h1> <?php echo Yii::t('CouponModule.coupon', 'Viewing coupon'); ?> <br/> <small>«<?php echo $model->code; ?> »</small> </h1> </div> <?php $this->widget('bootstrap.widgets.TbDetailView', ['data' => $model, 'attributes' => ['id', 'name', 'code', ['name' => 'type', 'value' => CouponType::title($model->type)], 'value', 'min_order_price', 'registered_user:boolean', 'free_shipping:boolean', 'start_time', 'end_time', 'quantity', 'quantity_per_user', ['name' => 'status', 'type' => 'raw', 'value' => CouponStatus::coloredLabel($model->status)]]]);
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); }