public function actionDelHistory() { $createdAt = $this->getParams('createdAt'); $productId = $this->getParams('productId'); if (empty($createdAt) || empty($productId)) { throw new BadRequestHttpException('missing param createdAt or productId'); } $product = Product::findByPk(new \MongoId($productId)); if (empty($product)) { throw new BadRequestHttpException('invalid productId'); } //check the productId belong to which campaign that it is begining $campaignWhere = ['promotion.data' => ['$all' => [$product->_id]], 'isActivated' => true]; $campaign = Campaign::findOne($campaignWhere); if (!empty($campaign)) { throw new BadRequestHttpException(Yii::t('product', 'can_not_delete')); } $usedCount = PromotionCode::countByProductIdAndCreatedAt(new \MongoId($productId), new \MongoDate($createdAt), true); if ($usedCount > 0) { throw new BadRequestHttpException(Yii::t('product', 'can_not_delete')); } else { $deleteArgs = ['productId' => $product->_id . '', 'createdAt' => $createdAt, 'type' => 'delete', 'description' => 'Direct: Delete promotionCodes']; $jobId = Yii::$app->job->create('backend\\modules\\product\\job\\PromotionCode', $deleteArgs); } return ['message' => 'OK', 'data' => $jobId]; }
public static function getHistoryByProduct($productId) { $raws = self::getCollection()->aggregate([['$match' => ['productId' => $productId]], ['$group' => ['_id' => ['createdAt' => '$createdAt', 'isUsed' => '$isUsed'], 'count' => ['$sum' => 1]]], ['$sort' => ['createdAt' => -1]]]); $raws = empty($raws) ? [] : $raws; //check th campaign $enable = true; $campaignWhere = ['promotion.data' => ['$all' => [$productId]], 'isActivated' => true]; $campaign = Campaign::findOne($campaignWhere); if (!empty($campaign)) { $enable = false; } $result = []; foreach ($raws as $raw) { $createdAt = $raw['_id']['createdAt']; $timestampCreatedAt = MongodbUtil::MongoDate2TimeStamp($createdAt); $strCreatedAt = MongodbUtil::MongoDate2String($createdAt, 'Y-m-d H:i:s'); $result[$timestampCreatedAt]['createdAt'] = $strCreatedAt; $result[$timestampCreatedAt]['all'] = empty($result[$timestampCreatedAt]['all']) ? 0 : $result[$timestampCreatedAt]['all']; $result[$timestampCreatedAt]['timestamp'] = $timestampCreatedAt; $result[$timestampCreatedAt]['used'] = empty($result[$timestampCreatedAt]['used']) ? 0 : $result[$timestampCreatedAt]['used']; $result[$timestampCreatedAt]['rest'] = empty($result[$timestampCreatedAt]['rest']) ? 0 : $result[$timestampCreatedAt]['rest']; if ($raw['_id']['isUsed']) { $result[$timestampCreatedAt]['used'] = $raw['count']; } else { $result[$timestampCreatedAt]['rest'] = $raw['count']; } $result[$timestampCreatedAt]['all'] += $raw['count']; $result[$timestampCreatedAt]['enable'] = $enable; } $result = array_values($result); ArrayHelper::multisort($result, 'createdAt', SORT_DESC); return $result; }