public function give() { $transaction = Yii::$app->db->beginTransaction(); $node = new Node(['type_id' => 4, 'user_name' => $this->user_name]); if ($node->invest()) { $this->node_id = $node->id; $this->save(); $node->user->setBundle(['node_id' => $node->type_id]); $node->user->save(); $transaction->commit(); return $node; } $transaction->rollBack(); return false; }
public function actionOpen($id) { if (!in_array($id, [1, 2, 3, 4])) { throw new InvalidParamException('id'); } /** @var \app\models\User $me */ $me = Yii::$app->user->identity; $type = $this->findModel($id); $node = new Node(['type' => $type, 'user' => $me]); if ($me->account >= $type->stake) { $me->account -= $type->stake; $transaction = Yii::$app->db->beginTransaction(); if ($me->update(true, ['account'])) { $node->open($transaction); } else { Yii::$app->session->setFlash('error', json_encode($me, JSON_UNESCAPED_UNICODE)); } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Insufficient funds')); return $this->redirect(['view', 'id' => $node->id]); } return $this->redirect(['view', 'id' => $id]); }
public function canChargeBonus() { //@todo decouple return $this->ref_name && (Node::find()->where(['user_name' => $this->ref_name])->count() > 0 || Income::find()->where(['user_name' => $this->ref_name])->count() > 0); }
public function actionLogin() { $model = new Login(); if ($model->load(Yii::$app->request->post())) { $user = $model->getUser(); if ($user) { if (empty($user->hash)) { Yii::$app->session->setFlash('error', Yii::t('app', Yii::t('app', 'Your account is not activated. Check your email'))); } else { $can = $user->canLogin(); if ($can && $user->validatePassword($model->password)) { if ($user->status > 0) { if (empty($user->auth)) { $user->generateAuthKey(); $user->save(); } if (Yii::$app->user->login($user, $model->remember ? $user->duration * 60 : 0)) { $bundle = $user->getBundle(); if ($bundle && isset($bundle['node_id'])) { $node_id = (int) $bundle['node_id']; $user->setBundle(null); $user->save(); if (Node::find()->where(['id' => $node_id])->count() > 0) { Yii::$app->session->addFlash('success', Yii::t('app', 'Congratulation! You receive a gift')); return $this->redirect(['/pyramid/node/index', 'id' => $node_id]); } } return $this->redirect(['view']); // return $this->actionView($user->name); } else { Yii::$app->session->addFlash('error', Yii::t('app', 'Something wrong happened')); } } else { Yii::$app->session->setFlash('error', Yii::t('app', Yii::t('app', 'Your account is blocked'))); } } else { Journal::write('user', 'login_fail', $user->id); if ($can) { Yii::$app->session->setFlash('error', Yii::t('app', 'Invalid username or password')); } else { $record = Record::find()->where(['object_id' => $user->id, 'event' => 'login_fail'])->orderBy(['time' => SORT_DESC])->one(); Yii::$app->session->setFlash('error', Yii::t('app', 'You have exceeded the maximum number of login attempts, you will be able to enter after {time}', ['time' => $record->time])); } } } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Invalid username or password')); } } return $this->render('login', ['model' => $model]); }
public static function makeFromNode(Node $node) { return static::make($node->id, $node->user, $node->type_id, $node->time, $node->getType()->income); }
/** * Finds the Type model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Node the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Node::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function open(Transaction $transaction) { try { $sum = (int) Node::find()->where(['user_name' => $this->user_name])->count(); $sum += (int) Income::find()->where(['user_name' => $this->user_name])->count(); if ($this->invest()) { do { $i = $this->rise(); } while ($i > 0); if (0 == $sum && $this->user->canChargeBonus()) { $referral = $this->user->referral; $referral->account += $this->type->bonus; $referral->update(true, ['account']); if (4 == $this->type->id) { $count = $referral->getSponsors()->select('user_name')->joinWith('nodes')->groupBy('user_name')->count(); if ($count > 0 && 0 == $count % 10) { $gift = new Gift(['user_name' => $referral->name]); $gift->save(); Yii::$app->session->setFlash('success', Yii::t('app', 'Your referral may receive a gift')); } } } $transaction->commit(); Yii::$app->session->setFlash('success', Yii::t('app', 'The plan is open')); return true; } else { Yii::$app->session->setFlash('error', $this->dump()); } } catch (\Exception $ex) { $transaction->rollBack(); Yii::$app->session->setFlash('error', $ex->getMessage()); } return false; }