Exemplo n.º 1
0
 public static function createStatsCouponLog($dateStr, $stats)
 {
     //check data whether exists today
     $result = ModelStatsCouponLogDaily::findOne(['date' => $dateStr]);
     if ($result) {
         LogUtil::info(['message' => $dateStr . ' coupon log data is exists'], 'resque');
         return true;
     }
     $stats = CouponLog::formatStruct($stats);
     $data = $couponIds = [];
     foreach ($stats as $stat) {
         $receivedKey = $stat['couponId'] . '_' . CouponLog::RECIEVED;
         $redeemedKey = $stat['couponId'] . '_' . CouponLog::REDEEMED;
         if (!in_array($stat['couponId'], $couponIds)) {
             $couponIds[] = $stat['couponId'];
             $receivedNum = isset($stats[$receivedKey]['count']) ? $stats[$receivedKey]['count'] : 0;
             $redeemedNum = isset($stats[$redeemedKey]['count']) ? $stats[$redeemedKey]['count'] : 0;
             $data[] = ['couponId' => $stat['couponId'], 'accountId' => $stat['accountId'], 'recievedNum' => $receivedNum + $redeemedNum, 'redeemedNum' => $redeemedNum, 'date' => $dateStr];
         }
     }
     unset($stat, $stats, $couponIds);
     if (false === ModelStatsCouponLogDaily::batchInsert($data)) {
         LogUtil::error(['message' => 'Faild to create statis daily couponlog', 'date' => $dateStr, 'data' => json_encode($data)], 'resque');
     }
 }
Exemplo n.º 2
0
 public function actionIndex()
 {
     $params = $this->getQuery();
     if (!isset($params['status']) || empty($params['status'])) {
         throw new InvalidParameterException(Yii::t('product', 'invalide_params'));
     }
     $accountId = $this->getAccountId();
     return CouponLog::search($params, $accountId);
 }
 public function operationCoupon($membershipDiscount)
 {
     //record log when get coupon
     $memberItem = Member::getMemberInfo($membershipDiscount->member['id'], ['tel']);
     $memberInfo = ['id' => $membershipDiscount->member['id'], 'name' => $membershipDiscount->member['name'], 'phone' => empty($memberItem['tel']) ? '' : $memberItem['tel'], 'receiveType' => $membershipDiscount->coupon['receiveType']];
     $couponLog = new CouponLog();
     $couponLog->couponId = $membershipDiscount->coupon['id'];
     $couponLog->membershipDiscountId = $membershipDiscount->_id;
     $couponLog->type = $membershipDiscount->coupon['type'];
     $couponLog->title = $membershipDiscount->coupon['title'];
     $couponLog->status = CouponLog::RECIEVED;
     $couponLog->member = $memberInfo;
     $couponLog->total = 1;
     $couponLog->operationTime = $couponLog->createdAt = new MongoDate();
     $couponLog->accountId = $membershipDiscount->accountId;
     if (false === $couponLog->save()) {
         //to avoid the error show to user
         LogUtil::error(['message' => 'Failed to save couponLog error:' . $couponLog->getErrors()], 'product');
     }
 }
Exemplo n.º 4
0
 public function perform()
 {
     $args = $this->args;
     $accountId = new MongoId($args['accountId']);
     $storeId = new MongoId($args['storeId']);
     $store = Store::findByPk($storeId);
     if (!empty($args['location'])) {
         // create new store location
         $location = $args['location'];
         if ($this->_addNewLocation($location, $accountId)) {
             $storeLocation = new StoreLocation();
             $storeLocation->_id = $store->_id;
             $storeLocation->name = $store->name;
             $storeLocation->parentName = $location['district'];
             $storeLocation->level = StoreLocation::LOCATION_LEVEL_STORE;
             $storeLocation->accountId = $accountId;
             $storeLocation->save();
         }
     } else {
         if (!empty($args['oldLocation']) && !empty($args['newLocation'])) {
             // update store location
             $oldLocation = $args['oldLocation'];
             $newLocation = $args['newLocation'];
             StoreLocation::updateAll(['name' => $store->name, 'parentName' => $newLocation['district']], ['_id' => $store->_id]);
             $this->_updateLocation($oldLocation, $newLocation, $accountId);
         } else {
             if (!empty($args['removeLocation'])) {
                 // delete store location
                 $location = $args['removeLocation'];
                 StoreLocation::deleteAll(['_id' => $storeId]);
                 $this->_removeOldLocation($location, $accountId);
             }
         }
     }
     //update store name
     if (!empty($args['newStoreName'])) {
         //update storeLocation name
         StoreLocation::updateAll(['name' => $args['newStoreName']], ['_id' => $storeId]);
         //update couponLog
         CouponLog::updateAll(['store.name' => $store->name], ['_id' => $storeId]);
     }
     //update coupon store name
     $this->_updateCouponStroeInfo($store);
 }
 /**
  * @args {"description": "Direct: update Stats of coupon"}
  */
 public function perform()
 {
     $args = $this->args;
     $startTime = strtotime($args['startTime']);
     $endTime = strtotime($args['endTime']);
     $current = strtotime(date('Y-m-d'));
     if ($endTime > $current) {
         $endTime = $current;
     }
     for ($t = $startTime; $t <= $endTime; $t += 3600 * 24) {
         $dateStr = date('Y-m-d', $t);
         ModelStatsCouponLogDaily::deleteAll(['date' => $dateStr]);
         $stats = CouponLog::getStats($dateStr);
         if (!empty($stats)) {
             StatsCouponLogDaily::createStatsCouponLog($dateStr, $stats);
         }
         LogUtil::info(['message' => $dateStr . ': Update StatsCouponLogDaily'], 'update_coupon_log');
     }
     return true;
 }
Exemplo n.º 6
0
 /**
  * get coupon detail info
  */
 public function actionOpenCoupon()
 {
     $couponId = $this->getQuery("couponId");
     $memberId = $this->getQuery("memberId");
     $isReceived = true;
     $message = '';
     if (empty($couponId)) {
         throw new InvalidParameterException(Yii::t('common', 'parameters_missing'));
     }
     $coupon = Coupon::findByPk(new MongoId($couponId));
     if (empty($coupon)) {
         throw new InvalidParameterException(Yii::t('product', 'membershipDiscount_is_deleted'));
     }
     // check expired
     $current = new MongoDate(strtotime(date('Y-m-d')));
     if ($coupon->time['type'] == Coupon::COUPON_ABSOLUTE_TIME && $coupon->time['endTime'] < $current) {
         $isReceived = false;
         $message = Yii::t('product', 'coupon_expired');
     }
     //check total
     if ($coupon->total <= 0) {
         $isReceived = false;
         $message = Yii::t('product', 'coupon_no_exists');
     }
     if (!empty($memberId)) {
         $where = ['couponId' => new MongoId($couponId), 'member.id' => new MongoId($memberId)];
         $couponNum = CouponLog::count($where);
         if ($couponNum >= $coupon->limit) {
             $isReceived = false;
             $message = Yii::t('product', 'coupon_is_received');
         }
     }
     return array_merge($coupon->toArray(), ['isReceived' => $isReceived, 'message' => $message]);
 }
Exemplo n.º 7
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');
                 }
             }
         }
     }
 }
Exemplo n.º 8
0
 /**
  * Search for coupon recieved, redeemed,deleted records.
  * @param array $params The search condition
  * @param string $accountId
  * @return array The couponLog list for recieved, redeemed,deleted records
  */
 public static function search($params, $accountId)
 {
     $condition = ['accountId' => $accountId, 'status' => $params['status']];
     $query = CouponLog::find();
     if (!empty($params['startTime'])) {
         $startTime = MongodbUtil::msTimetamp2MongoDate($params['startTime']);
         $condition['operationTime']['$gte'] = $startTime;
     }
     if (!empty($params['endTime'])) {
         $endTime = MongodbUtil::msTimetamp2MongoDate($params['endTime']);
         $condition['operationTime']['$lte'] = $endTime;
     }
     if (!empty($params['searchKey'])) {
         $key = $params['searchKey'];
         $key = StringUtil::regStrFormat(trim($key));
         $keyReg = new \MongoRegex("/{$key}/i");
         $condition['$or'] = [['member.name' => $keyReg], ['member.phone' => $keyReg], ['title' => $keyReg]];
     }
     if (empty($params['orderBy'])) {
         $orderBy = ['operationTime' => SORT_DESC];
     } else {
         switch ($params['orderBy']) {
             case 'asc':
                 $orderBy = ['operationTime' => SORT_ASC];
                 break;
             default:
                 $orderBy = ['operationTime' => SORT_DESC];
                 break;
         }
     }
     $query = $query->where($condition)->orderBy($orderBy);
     $searchQuery = ['query' => $query];
     return new ActiveDataProvider($searchQuery);
 }
Exemplo n.º 9
0
 private function getRewardMemberIds($accountId, $memberId, $birthdayRule)
 {
     if (!empty($memberId)) {
         $memberIds = $this->getMembers($accountId, $birthdayRule->triggerTime, $memberId);
     } else {
         $memberIds = $this->getMembers($accountId, $birthdayRule->triggerTime);
     }
     $thisYear = TimeUtil::thisYear();
     if ($birthdayRule->rewardType == ScoreRule::REWARD_SCORE_TYPE) {
         $scoreHistoryMemberIds = ScoreHistory::getAllMemberIdByRuleName(ScoreRule::NAME_BIRTHDAY, $memberIds, $accountId, $thisYear);
         $memberIds = array_diff($memberIds, $scoreHistoryMemberIds);
         //check this rule whether reward in coupon
         $couponLogMemberIds = CouponLog::getAllMemberIdByRuleName(ScoreRule::NAME_BIRTHDAY, $memberIds, $accountId);
         $memberIds = array_diff($memberIds, $couponLogMemberIds);
     } else {
         if ($birthdayRule->rewardType == ScoreRule::REWARD_COUPON_TYPE) {
             $couponLogMemberIds = CouponLog::getAllMemberIdByRuleName(ScoreRule::NAME_BIRTHDAY, $memberIds, $accountId, $thisYear);
             $memberIds = array_diff($memberIds, $couponLogMemberIds);
             //check this rule whethe reward in score
             $scoreHistoryMemberIds = ScoreHistory::getAllMemberIdByRuleName(ScoreRule::NAME_BIRTHDAY, $memberIds, $accountId, $thisYear);
             $memberIds = array_diff($memberIds, $scoreHistoryMemberIds);
         }
     }
     return $memberIds;
 }
 public function actionIndex($accountId)
 {
     if (empty($accountId)) {
         echo 'accountId is invaild' . PHP_EOL;
         exit;
     }
     $where['accountId'] = new \MongoId($accountId);
     // delete member info
     Member::getCollection()->remove($where);
     //delete MemberLogs
     MemberLogs::getCollection()->remove($where);
     //delete scoreHistory
     ScoreHistory::getCollection()->remove($where);
     //delete CampaignLog
     CampaignLog::getCollection()->remove($where);
     //delete GoodsExchangeLog
     GoodsExchangeLog::getCollection()->remove($where);
     //delete MemberStatistics
     MemberStatistics::getCollection()->remove($where);
     //delete ReMemberCampaign
     ReMemberCampaign::getCollection()->remove($where);
     //delete StatsCampaignProductCodeQuarterly
     StatsCampaignProductCodeQuarterly::getCollection()->remove($where);
     //delete StatsMemberCampaignLogDaily
     StatsMemberCampaignLogDaily::getCollection()->remove($where);
     //delete StatsMemberDaily
     StatsMemberDaily::getCollection()->remove($where);
     //delete StatsMemberGrowthMonthly
     StatsMemberGrowthMonthly::getCollection()->remove($where);
     //delete StatsMemberGrowthQuarterly
     StatsMemberGrowthQuarterly::getCollection()->remove($where);
     //delete StatsMemberMonthly
     StatsMemberMonthly::getCollection()->remove($where);
     //delete StatsMemberPropAvgTradeQuarterly
     StatsMemberPropAvgTradeQuarterly::getCollection()->remove($where);
     //delete StatsMemberPropMonthly
     StatsMemberPropMonthly::getCollection()->remove($where);
     //delete StatsMemberPropQuaterly
     StatsMemberPropQuaterly::getCollection()->remove($where);
     //delete StatsMemberPropTradeCodeAvgQuarterly
     StatsMemberPropTradeCodeAvgQuarterly::getCollection()->remove($where);
     //delete StatsMemberPropTradeCodeQuarterly
     StatsMemberPropTradeCodeQuarterly::getCollection()->remove($where);
     //delete StatsMemberPropTradeQuarterly
     StatsMemberPropTradeQuarterly::getCollection()->remove($where);
     //delete PromotionCodeAnalysis
     PromotionCodeAnalysis::getCollection()->remove($where);
     //delete order
     Order::getCollection()->remove($where);
     //delete stats member order
     StatsMemberOrder::getCollection()->remove($where);
     //delete stats order
     StatsOrder::getCollection()->remove($where);
     //delete MembershipDiscount
     MembershipDiscount::getCollection()->remove($where);
     //delete couponLog
     CouponLog::getCollection()->remove($where);
     echo 'delete data successful.' . PHP_EOL;
 }
 /**
  * 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);
 }
Exemplo n.º 12
0
 private function getTimeAndmemberCountWithCoupon($ruleName, $accountId)
 {
     $times = CouponLog::getTotalTimesByRuleName($ruleName, $accountId);
     $memberCount = count(CouponLog::getAllMemberIdByRuleName($ruleName, [], $accountId, 0));
     return [$times, $memberCount];
 }