예제 #1
0
 /**
  * Auto zeroed member score
  */
 public static function resetScore($memberId, $accountId)
 {
     $member = self::findByPk($memberId);
     $memberScore = $member->score;
     $member->score = 0;
     $member->totalScoreAfterZeroed = 0;
     $updateResult = $member->update();
     if ($updateResult) {
         $scoreHistory = new ScoreHistory();
         $scoreHistory->assigner = ScoreHistory::ASSIGNER_AUTO_ZEROED;
         $scoreHistory->increment = -$memberScore;
         $scoreHistory->memberId = $memberId;
         $scoreHistory->brief = ScoreHistory::ASSIGNER_AUTO_ZEROED;
         $scoreHistory->channel = ['origin' => ScoreHistory::PORTAL];
         $scoreHistory->accountId = $accountId;
         $saveResult = $scoreHistory->save();
         if (!$saveResult) {
             LogUtil::error(['message' => 'Save score history fail', 'errors' => $scoreHistory->getErrors()], 'member');
         }
     } else {
         LogUtil::error(['message' => 'Reset score fail', 'errors' => $member->getErrors(), 'memberId' => $member->_id], 'member');
     }
 }
예제 #2
0
 private function _saveLog(Member $member, $exchanges, $params, $user = null)
 {
     $goodsExchangeLog = new GoodsExchangeLog();
     $allGoods = [];
     $totalCount = 0;
     $scoreHistoryDescription = '';
     foreach ($exchanges as $exchange) {
         $goods = $exchange['goods'];
         $count = $exchange['count'];
         $pictures = $goods->pictures;
         $allGoods[] = ['id' => $goods->_id, 'productId' => $goods->productId, 'sku' => $goods->sku, 'picture' => empty($pictures[0]) ? '' : $pictures[0], 'productName' => $goods->productName, 'count' => $count];
         $totalCount += $count;
         $scoreHistoryDescription .= $goods->productName . "({$count}); ";
     }
     $scoreHistoryDescription = trim($scoreHistoryDescription, '; ');
     $goodsExchangeLog->goods = $allGoods;
     $goodsExchangeLog->memberId = $member->_id;
     $properties = $member->properties;
     $name = '';
     foreach ($properties as $property) {
         if ($property['name'] == Member::DEFAULT_PROPERTIES_NAME) {
             $name = $property['value'];
         }
         if ($property['name'] == Member::DEFAULT_PROPERTIES_MOBILE) {
             $mobile = $property['value'];
         }
     }
     $goodsExchangeLog->memberName = $name;
     $goodsExchangeLog->telephone = empty($params['phone']) ? $mobile : $params['phone'];
     $goodsExchangeLog->usedScore = $params['usedScore'];
     $goodsExchangeLog->expectedScore = $params['expectedScore'];
     $goodsExchangeLog->count = $totalCount;
     $goodsExchangeLog->address = $params['address'];
     $goodsExchangeLog->receiveMode = $params['receiveMode'];
     $goodsExchangeLog->postcode = empty($params['postcode']) ? '' : $params['postcode'];
     $goodsExchangeLog->isDelivered = false;
     $scoreHistoryChannel = [];
     if (!empty($params['channelId'])) {
         $channelInfo = Channel::getByChannelId($params['channelId'], $member->accountId);
         $scoreHistoryChannel = ['id' => $channelInfo->channelId, 'name' => $channelInfo->name, 'origin' => $channelInfo->origin];
         $goodsExchangeLog->usedFrom = ['id' => $params['channelId'], 'type' => $channelInfo->origin, 'name' => $channelInfo->name];
     } else {
         $scoreHistoryChannel = ['origin' => GoodsExchangeLog::PORTAL];
         $goodsExchangeLog->usedFrom = ['id' => '', 'type' => GoodsExchangeLog::PORTAL, 'name' => GoodsExchangeLog::OFFLINE_EXCHANGE];
     }
     $goodsExchangeLog->accountId = $member->accountId;
     $scoreHistory = new ScoreHistory();
     $scoreHistory->assigner = ScoreHistory::ASSIGNER_EXCHAGE_GOODS;
     $scoreHistory->increment = 0 - $params['usedScore'];
     $scoreHistory->memberId = $member->_id;
     $scoreHistory->brief = ScoreHistory::ASSIGNER_EXCHAGE_GOODS;
     $scoreHistory->description = $scoreHistoryDescription;
     $scoreHistory->accountId = $member->accountId;
     $scoreHistory->channel = $scoreHistoryChannel;
     $scoreHistory->user = $user;
     if ($goodsExchangeLog->save(true) && $scoreHistory->save(true)) {
         MemberLogs::record($member->_id, $member->accountId, MemberLogs::OPERATION_REDEEM);
         return true;
     } else {
         LogUtil::error(['exchange fail' => [$scoreHistory->getErrors(), $goodsExchangeLog->getErrors()]]);
         return false;
     }
 }