Esempio n. 1
0
 private function issueBirthdayCoupon($accountId, $birthdayCoupon, $memberId)
 {
     //check coupon
     $couponId = $birthdayCoupon->couponId;
     $suitMemberIds = $this->getRewardMemberIds($accountId, $memberId, $birthdayCoupon);
     if (!empty($suitMemberIds)) {
         $members = Member::findAll(['_id' => ['$in' => $suitMemberIds]]);
         $coupon = Coupon::findByPk($couponId);
         if (empty($coupon)) {
             LogUtil::error(['message' => 'can not find the coupon', 'couponId' => (string) $couponId], 'member');
             return false;
         }
         $total = count($members);
         $result = Coupon::updateAll(['$inc' => ['total' => 0 - $total]], ['total' => ['$gt' => $total], '_id' => $couponId]);
         if ($result) {
             foreach ($members as $member) {
                 //issue membershipdiscount
                 $membershipDiscount = MembershipDiscount::transformMembershipDiscount($coupon, $member, ScoreRule::NAME_BIRTHDAY);
                 if (false === $membershipDiscount->save()) {
                     Coupon::updateAll(['$inc' => ['total' => 1]], ['_id' => $couponId]);
                     LogUtil::error(['message' => 'add membershipdiscount fail', 'memberId' => (string) $member->_id, 'couponId' => (string) $coupon->_id], 'member');
                 } else {
                     LogUtil::info(['message' => 'issue coupon successful', 'couponId' => (string) $couponId, 'memberId' => (string) $member->_id], 'member');
                 }
             }
         } else {
             LogUtil::error(['message' => 'coupon is not enough when give member birthday reward'], 'member');
             return false;
         }
     }
 }
Esempio n. 2
0
 /**
  * update store info in coupon
  * @param $store,object
  */
 private function _updateCouponStroeInfo($store)
 {
     $location = ['province', 'city', 'district', 'detail'];
     $address = '';
     $storeLocation = $store->location;
     foreach ($location as $key) {
         if (isset($storeLocation[$key])) {
             $address .= $storeLocation[$key];
         }
     }
     $address = $store->location['province'] . $store->location['city'] . $store->location['district'] . $store->location['detail'];
     $storeData = ['stores.$.name' => $store->name, 'stores.$.branchName' => $store->branchName, 'stores.$.address' => $address, 'stores.$.phone' => $store->telephone];
     Coupon::updateAll($storeData, ['stores.id' => $store->_id]);
 }
 public function actionStatsTotalCoupon()
 {
     $params = $this->getQuery();
     if (empty($params['id'])) {
         throw new BadRequestHttpException(Yii::t('common', 'parameters_missing'));
     }
     $id = new MongoId($params['id']);
     $coupon = Coupon::findOne(["_id" => $id]);
     if (empty($coupon)) {
         throw new BadRequestHttpException(Yii::t('product', 'membershipDiscount_is_deleted'));
     }
     $couponTotalInfo = StatsCouponLogDaily::getCouponLogTotalStats($id);
     $item = empty($couponTotalInfo[0]) ? [] : $couponTotalInfo[0];
     return $item;
 }
Esempio n. 4
0
 public function actionUpdate($id)
 {
     $params = $this->getParams();
     if (empty($params['url']) && empty($params['total']) && empty($params['time'])) {
         throw new InvalidParameterException(Yii::t('product', 'invalide_params'));
     }
     $where = ['_id' => new MongoId($id)];
     $coupon = Coupon::findOne($where);
     if (empty($coupon)) {
         throw new InvalidParameterException(Yii::t('product', 'invalide_params'));
     }
     if (!empty($params['time'])) {
         $params = Coupon::converCouponTime($params);
     }
     $data = [];
     if (!empty($params['total'])) {
         if ($coupon->total + $params['total'] < 0) {
             throw new InvalidParameterException(Yii::t('product', 'coupon_total_overflow'));
         } else {
             $data['$inc'] = ['total' => $params['total']];
             //add a condition to check the total
             if ($params['total'] < 0) {
                 $where['total'] = ['$gte' => intval($params['total'])];
             }
         }
     }
     $saveKeys = ['url', 'time'];
     foreach ($saveKeys as $saveKey) {
         if (isset($params[$saveKey])) {
             $data['$set'][$saveKey] = $params[$saveKey];
         }
     }
     Coupon::updateAll($data, $where);
     return ['result' => 'Ok', 'message' => 'update coupon successful'];
 }
Esempio n. 5
0
 /**
  * get coupon about store from mobile
  * note: mobile only get some special modules
  */
 public function actionCouponStore()
 {
     $params = $this->getQuery();
     if (empty($params['couponId'])) {
         throw new InvalidParameterException(Yii::t('common', 'parameters_missing'));
     }
     $couponId = new MongoId($params['couponId']);
     $coupon = Coupon::findByPk($couponId);
     if (!empty($coupon)) {
         if ($coupon->storeType == Coupon::COUPON_ALL_STORE) {
             $stores = Store::find()->where(['accountId' => $coupon->accountId, 'isDeleted' => Coupon::NOT_DELETED])->orderBy(['_id' => SORT_DESC])->all();
             $coupon->stores = Coupon::conver2couponStore($stores);
         } else {
             if (!empty($coupon->stores)) {
                 foreach ($stores = $coupon->stores as &$store) {
                     $store['id'] = (string) $store['id'];
                 }
                 ArrayHelper::multisort($stores, 'id', SORT_DESC);
                 $coupon->stores = $stores;
             }
         }
     }
     return $coupon;
 }
Esempio n. 6
0
 public static function rewardByScoreRule($ruleName, $memberId, $accountId)
 {
     //get score rule
     $rule = ScoreRule::getByName($ruleName, $accountId);
     if (!empty($rule) && $rule->isEnabled) {
         //if check the property, the property is not filled,not need to give reward
         if (!self::_checkRulePropertiesFilled($rule, $memberId)) {
             return true;
         }
         //get reward type
         if ($rule->rewardType == ScoreRule::REWARD_SCORE_TYPE) {
             // member rewarded
             $rewardHistory = ScoreHistory::getByRuleName($ruleName, $memberId, $accountId);
             if (!empty($rewardHistory)) {
                 return true;
             }
             //reward score
             $memberList = Member::giveScoreByIds($rule->score, [$memberId]);
             //update history
             $scoreHistory = new ScoreHistory();
             $scoreHistory->assigner = ScoreHistory::ASSIGNER_RULE;
             $scoreHistory->increment = $rule->score + 0;
             $scoreHistory->memberId = $memberId;
             $scoreHistory->brief = ScoreHistory::ASSIGNER_RULE;
             $scoreHistory->description = $ruleName;
             $scoreHistory->channel = ['origin' => ScoreHistory::PORTAL];
             $scoreHistory->accountId = $accountId;
             $scoreHistory->save();
         } else {
             if ($rule->rewardType == ScoreRule::REWARD_COUPON_TYPE && !empty($rule->couponId)) {
                 $rewardCouponLog = CouponLog::getLogByRuleName($ruleName, $memberId, $accountId);
                 if (!empty($rewardCouponLog)) {
                     return true;
                 }
                 $coupon = Coupon::findByPk($rule->couponId);
                 if (empty($coupon)) {
                     LogUtil::error(['message' => 'can not find the coupon when give member birthday reward', 'couponId' => (string) $couponId], 'member');
                     return true;
                 }
                 if (Coupon::updateAll(['$inc' => ['total' => -1]], ['total' => ['$gt' => 0], '_id' => $rule->couponId])) {
                     //issue membershipdiscount
                     $member = Member::findByPk($memberId);
                     $membershipDiscount = MembershipDiscount::transformMembershipDiscount($coupon, $member, $ruleName);
                     if (false == $membershipDiscount->save()) {
                         Coupon::updateAll(['$inc' => ['total' => 1]], ['_id' => $rule->couponId]);
                         LogUtil::error(['message' => 'add membershipdiscount fail', 'memberId' => (string) $member->_id, 'couponId' => (string) $coupon->_id], 'member');
                     }
                 } else {
                     LogUtil::error(['message' => 'coupon is not enough when give member birthday reward', 'memberId' => (string) $member->_id, 'couponId' => (string) $coupon->_id], 'member');
                 }
             }
         }
     }
 }
Esempio n. 7
0
 /**
  * search coupon
  */
 public static function search($params)
 {
     $query = Coupon::find();
     $condition = ['accountId' => $params['accountId'], 'isDeleted' => self::NOT_DELETED];
     if (!empty($params['title'])) {
         $key = $params['title'];
         $key = StringUtil::regStrFormat(trim($key));
         $keyReg = new MongoRegex("/{$key}/i");
         $search = ['title' => $keyReg];
         $condition = array_merge($condition, $search);
         unset($search);
     }
     if (!empty($params['unexpired'])) {
         $time = new MongoDate(strtotime(TimeUtil::msTime2String($params['unexpired'], 'Y-m-d')));
         $search = ['$or' => [['time.type' => self::COUPON_ABSOLUTE_TIME, 'time.endTime' => ['$gte' => $time]], ['time.type' => self::COUPON_RELATIVE_TIME]]];
         $condition = array_merge($condition, $search);
         unset($search, $time);
     }
     $query->orderBy(self::normalizeOrderBy($params));
     $query->where($condition);
     $unlimited = Yii::$app->request->get('unlimited', false);
     if ($unlimited) {
         return ['items' => $query->all()];
     }
     return new ActiveDataProvider(['query' => $query]);
 }
 /**
  * receive copon through oauth recall this api
  */
 public function actionReceivedCoupon()
 {
     $params = $this->getQuery();
     $defaultId = -1;
     $message = '';
     if (empty($params['couponId']) || empty($params['memberId']) || empty($params['channelId'])) {
         LogUtil::error(['message' => 'missing params when receive coupon', 'params' => $params], 'product');
         exit;
     }
     $number = !empty($params['number']) && intval($params['number']) > 1 ? intval($params['number']) : 1;
     $couponId = new MongoId($params['couponId']);
     $coupon = Coupon::findByPk($couponId);
     if (empty($coupon)) {
         LogUtil::error(['message' => 'invalid couponIdi when receive coupon', 'params' => $params], 'product');
         exit;
     }
     $memberId = new MongoId($params['memberId']);
     $member = Member::findByPk($memberId);
     if (empty($member)) {
         LogUtil::error(['message' => 'invalid memberId when receive coupon', 'params' => $params], 'product');
         exit;
     }
     $args = ['mainDomain' => Yii::$app->request->hostInfo . '/mobile/product/coupon', 'couponId' => $params['couponId'], 'id' => $defaultId, 'memberId' => $params['memberId'], 'result' => 'fail', 'channelId' => $params['channelId']];
     //check the total
     if ($coupon->total < $number) {
         $message = Yii::t('product', 'coupon_no_exists');
         return $this->_redirectCouponDetail($message, $args, $params);
     }
     //check limit
     $couponNumber = CouponLog::count(['couponId' => $couponId, 'member.id' => $memberId]);
     if ($couponNumber >= $coupon->limit) {
         $message = Yii::t('product', 'coupon_is_received');
         return $this->_redirectCouponDetail($message, $args, $params);
     }
     //check the time
     $current = new MongoDate(strtotime(date('Y-m-d')));
     if ($coupon->time['type'] == Coupon::COUPON_RELATIVE_TIME && $coupon->time['endTime'] < $current) {
         $message = Yii::t('product', 'coupon_expired');
         return $this->_redirectCouponDetail($message, $args, $params);
     }
     //receive coupon
     $where = ['total' => ['$gte' => $number], '_id' => $couponId];
     $number -= 2 * $number;
     if (Coupon::updateAll(['$inc' => ['total' => $number]], $where)) {
         $membershipDiscount = MembershipDiscount::transformMembershipDiscount($coupon, $member);
         if (false === $membershipDiscount->save()) {
             //to avoid the error show to user
             LogUtil::error(['message' => 'Failed to save couponLog error:' . $membershipDiscount->getErrors()], 'product');
             $message = Yii::t('common', 'save_fail');
             return $this->_redirectCouponDetail($message, $args, $params);
         }
         $args['id'] = isset($membershipDiscount->_id) ? $membershipDiscount->_id : $defaultId;
         $args['result'] = 'success';
     } else {
         $message = '优惠券库存不足!';
     }
     return $this->_redirectCouponDetail($message, $args, $params);
 }
Esempio n. 9
0
 public function getByMemberIdAndCouponId($memberId, $couponId)
 {
     return ModelCoupon::findByPk($couponId, ['type' => ModelCoupon::COUPON_CASH]);
 }