/**
  * @param integer $id Tariff ID
  * @param array $options that will be passed to the object as configuration
  * @return AbstractTariffManager|object
  * @throws NotFoundHttpException
  */
 public static function createById($id, $options = [])
 {
     $model = Tariff::find()->byId($id)->details()->one();
     if ($model === null) {
         throw new NotFoundHttpException('Tariff was not found');
     }
     $model->scenario = ArrayHelper::getValue($options, 'scenario', $model::SCENARIO_DEFAULT);
     return Yii::createObject(array_merge(['class' => static::buildClassName($model->type), 'tariff' => $model], $options));
 }
 protected function findParentTariffs()
 {
     if (!isset($this->tariff)) {
         $availableTariffs = Tariff::find(['scenario' => 'get-available-info'])->andFilterWhere(['type' => $this->type])->all();
         $ids = ArrayHelper::getColumn($availableTariffs, 'id');
     } else {
         $ids = [$this->tariff->parent_id];
     }
     if (empty($ids)) {
         throw new ForbiddenHttpException('No available tariffs found');
     }
     $this->parentTariffs = Tariff::find()->where(['id' => $ids])->details()->all();
 }
 /**
  * Returns the tariff for the domain operations
  * Caches the API request for 3600 seconds and depends on client id and seller login.
  * @throws \yii\base\InvalidConfigException
  * @return Tariff
  */
 public function getTariff()
 {
     if (Yii::$app->user->isGuest) {
         if (isset(Yii::$app->params['user.seller'])) {
             $params = [Yii::$app->params['user.seller'], null];
         } else {
             throw new InvalidConfigException('"seller" is must be set');
         }
     } else {
         $params = [Yii::$app->user->identity->seller, Yii::$app->user->id];
     }
     return Yii::$app->getCache()->getTimeCached(3600, $params, function ($seller, $client_id) {
         return Tariff::find(['scenario' => 'get-available-info'])->joinWith('resources')->andFilterWhere(['type' => 'domain'])->andFilterWhere(['seller' => $seller])->one();
     });
 }
 /**
  * @param string $type (svds|ovds)
  * @param integer $tariff_id
  * @throws NotFoundHttpException
  * @throws UnprocessableEntityHttpException
  * @return Package|array
  */
 public static function getAvailablePackages($type = null, $tariff_id = null)
 {
     $cacheKeys = [Yii::$app->params['user.seller'], Yii::$app->user->id, $type, $tariff_id, 'tariffs'];
     /** @var Tariff[] $tariffs */
     $tariffs = Yii::$app->getCache()->getTimeCached(3600, $cacheKeys, function ($seller, $client_id, $type, $tariff_id) {
         return Tariff::find(['scenario' => 'get-available-info'])->details()->andWhere(['seller' => $seller])->andFilterWhere(['id' => $tariff_id])->andFilterWhere(['type' => $type])->all();
     });
     $cacheKeys = [Yii::$app->params['user.seller'], Yii::$app->user->id, $type, $tariff_id, 'tariffs', 'packages'];
     /** @var Package[] $packages */
     $packages = Yii::$app->getCache()->getTimeCached(3600, $cacheKeys, function () use($tariffs) {
         $calculator = new Calculator($tariffs);
         $packages = [];
         foreach ($tariffs as $tariff) {
             $calculation = $calculator->getCalculation($tariff->id);
             $packages[] = Yii::createObject(['class' => static::buildPackageClass($tariff), 'tariff' => $tariff, 'calculation' => $calculation]);
         }
         return $packages;
     });
     ArrayHelper::multisort($packages, 'price', SORT_ASC, SORT_NUMERIC);
     if (empty($packages)) {
         throw new NotFoundHttpException('Requested tariff is not available');
     }
     if (isset($tariff_id) && !is_array($tariff_id)) {
         return reset($packages);
     }
     return $packages;
 }
 public function actions()
 {
     return ['index' => ['class' => IndexAction::class, 'findOptions' => ['with_requests' => true, 'with_discounts' => true], 'on beforePerform' => function (Event $event) {
         /** @var \hipanel\actions\SearchAction $action */
         $action = $event->sender;
         $dataProvider = $action->getDataProvider();
         $dataProvider->query->joinWith('ips');
         $dataProvider->query->andWhere(['with_ips' => 1])->andWhere(['with_tariffs' => 1])->andWhere(['with_switches' => 1])->andWhere(['with_requests' => 1])->andWhere(['with_discounts' => 1])->select(['*']);
     }, 'filterStorageMap' => ['name_like' => 'server.server.name', 'ips' => 'hosting.ip.ip_in', 'state' => 'server.server.state', 'client_id' => 'client.client.id', 'seller_id' => 'client.client.seller_id']], 'search' => ['class' => SearchAction::class], 'view' => ['class' => ViewAction::class, 'on beforePerform' => function (Event $event) {
         /** @var \hipanel\actions\SearchAction $action */
         $action = $event->sender;
         $dataProvider = $action->getDataProvider();
         $dataProvider->query->joinWith('uses');
         $dataProvider->query->joinWith('ips');
         // TODO: ipModule is not wise yet. Redo
         $dataProvider->query->andWhere(['with_requests' => 1])->andWhere(['show_deleted' => 1])->andWhere(['with_discounts' => 1])->andWhere(['with_uses' => 1])->andWhere(['with_ips' => 1])->select(['*']);
     }, 'data' => function ($action) {
         /**
          * @var Action $action
          * @var self $controller
          * @var Server $model
          */
         $controller = $action->controller;
         $model = $action->getModel();
         $model->vnc = $controller->getVNCInfo($model);
         $panels = $controller->getPanelTypes();
         $tariff = Yii::$app->cache->getAuthTimeCached(3600, [$model->tariff_id], function ($tariff_id) {
             return Tariff::find()->where(['id' => $tariff_id, 'show_final' => true, 'show_deleted' => true, 'with_resources' => true])->joinWith('resources')->one();
         });
         $ispSupported = false;
         if ($tariff !== null) {
             foreach ($tariff->getResources() as $resource) {
                 if ($resource->type === 'isp' && $resource->quantity > 0) {
                     $ispSupported = true;
                 }
             }
         }
         $osimages = $controller->getOsimages($model);
         $groupedOsimages = ServerHelper::groupOsimages($osimages, $ispSupported);
         if ($model->isLiveCDSupported()) {
             $osimageslivecd = $controller->getOsimagesLiveCd();
         }
         $blockReasons = $controller->getBlockReasons();
         return compact(['model', 'osimages', 'osimageslivecd', 'groupedOsimages', 'panels', 'blockReasons']);
     }], 'requests-state' => ['class' => RequestStateAction::class, 'model' => Server::class], 'set-note' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Note changed'), 'error' => Yii::t('hipanel:server', 'Failed to change note')], 'set-label' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Internal note changed'), 'error' => Yii::t('hipanel:server', 'Failed to change internal note')], 'set-lock' => ['class' => RenderAction::class, 'success' => Yii::t('hipanel:server', 'Record was changed'), 'error' => Yii::t('hipanel:server', 'Error occurred'), 'POST pjax' => ['save' => true, 'success' => ['class' => ProxyAction::class, 'action' => 'index']], 'POST' => ['save' => true, 'success' => ['class' => RenderJsonAction::class, 'return' => function ($action) {
         /** @var \hipanel\actions\Action $action */
         return $action->collection->models;
     }]]], 'sale' => ['class' => SmartUpdateAction::class, 'view' => '_saleModal', 'POST' => ['save' => true, 'success' => ['class' => RenderJsonAction::class, 'return' => function ($action) {
         return ['success' => !$action->collection->hasErrors()];
     }]]], 'enable-vnc' => ['class' => ViewAction::class, 'view' => '_vnc', 'data' => function ($action) {
         $model = $action->getModel();
         if ($model->canEnableVNC()) {
             $model->vnc = $this->getVNCInfo($model, true);
         }
         return [];
     }], 'reboot' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Reboot task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the rebooting')], 'reset' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Reset task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the resetting')], 'shutdown' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Shutdown task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the shutting down')], 'power-off' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Power off task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the turning power off')], 'power-on' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Power on task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the turning power on')], 'reset-password' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Root password reset task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the resetting root password')], 'enable-block' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Server was blocked successfully'), 'error' => Yii::t('hipanel:server', 'Error during the server blocking')], 'disable-block' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Server was unblocked successfully'), 'error' => Yii::t('hipanel:server', 'Error during the server unblocking')], 'refuse' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'You have refused the service'), 'error' => Yii::t('hipanel:server', 'Error during the refusing the service')], 'enable-autorenewal' => ['class' => SmartUpdateAction::class, 'success' => Yii::t('hipanel:server', 'Server renewal enabled successfully'), 'error' => Yii::t('hipanel:server', 'Error during the renewing the service')], 'reinstall' => ['class' => SmartUpdateAction::class, 'on beforeSave' => function (Event $event) {
         /** @var Action $action */
         $action = $event->sender;
         foreach ($action->collection->models as $model) {
             $model->osimage = Yii::$app->request->post('osimage');
             $model->panel = Yii::$app->request->post('panel');
         }
     }, 'success' => Yii::t('hipanel:server', 'Server reinstalling task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the server reinstalling')], 'boot-live' => ['class' => SmartUpdateAction::class, 'on beforeSave' => function (Event $event) {
         /** @var Action $action */
         $action = $event->sender;
         foreach ($action->collection->models as $model) {
             $model->osimage = Yii::$app->request->post('osimage');
         }
     }, 'success' => Yii::t('hipanel:server', 'Live CD booting task has been successfully added to queue'), 'error' => Yii::t('hipanel:server', 'Error during the booting live CD')], 'validate-form' => ['class' => ValidateFormAction::class], 'buy' => ['class' => RedirectAction::class, 'url' => Yii::$app->params['orgUrl']], 'add-to-cart-renewal' => ['class' => AddToCartAction::class, 'productClass' => ServerRenewProduct::class], 'delete' => ['class' => SmartDeleteAction::class, 'success' => Yii::t('hipanel:server', 'Server was deleted successfully'), 'error' => Yii::t('hipanel:server', 'Failed to delete server')], 'bulk-delete-modal' => ['class' => PrepareBulkAction::class, 'scenario' => 'delete', 'view' => '_bulkDelete'], 'bulk-enable-block' => ['class' => SmartUpdateAction::class, 'scenario' => 'enable-block', 'success' => Yii::t('hipanel:server', 'Servers were blocked successfully'), 'error' => Yii::t('hipanel:server', 'Error during the servers blocking'), 'POST html' => ['save' => true, 'success' => ['class' => RedirectAction::class]], 'on beforeSave' => function (Event $event) {
         /** @var \hipanel\actions\Action $action */
         $action = $event->sender;
         $type = Yii::$app->request->post('type');
         $comment = Yii::$app->request->post('comment');
         if (!empty($type)) {
             foreach ($action->collection->models as $model) {
                 $model->setAttributes(['type' => $type, 'comment' => $comment]);
             }
         }
     }], 'bulk-enable-block-modal' => ['class' => PrepareBulkAction::class, 'scenario' => 'enable-block', 'view' => '_bulkEnableBlock', 'data' => function ($action, $data) {
         return array_merge($data, ['blockReasons' => $this->getBlockReasons()]);
     }], 'bulk-disable-block' => ['class' => SmartUpdateAction::class, 'scenario' => 'disable-block', 'success' => Yii::t('hipanel:server', 'Servers were unblocked successfully'), 'error' => Yii::t('hipanel:server', 'Error during the servers unblocking'), 'POST html' => ['save' => true, 'success' => ['class' => RedirectAction::class]], 'on beforeSave' => function (Event $event) {
         /** @var \hipanel\actions\Action $action */
         $action = $event->sender;
         $comment = Yii::$app->request->post('comment');
         if (!empty($type)) {
             foreach ($action->collection->models as $model) {
                 $model->setAttribute('comment', $comment);
             }
         }
     }], 'bulk-disable-block-modal' => ['class' => PrepareBulkAction::class, 'scenario' => 'disable-block', 'view' => '_bulkDisableBlock'], 'set-orientation' => ['class' => OrientationAction::class, 'allowedRoutes' => ['@server/index']]];
 }