Exemple #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;
         }
     }
 }
Exemple #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 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'];
 }
Exemple #4
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');
                 }
             }
         }
     }
 }
 /**
  * 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);
 }