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; } } }
/** * 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); }
/** * 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; }
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'); } } } } }
public function getByMemberIdAndCouponId($memberId, $couponId) { return ModelCoupon::findByPk($couponId, ['type' => ModelCoupon::COUPON_CASH]); }