示例#1
0
 public static function getMemberStack($transaction, $calculeteVolume = true)
 {
     $volume = $transaction->volume;
     $member_id = $transaction->member_id;
     $stack_id = $transaction->stack_id;
     $model = MemberStack::find()->where(['=', 'member_id', $member_id])->andWhere(['=', 'stack_id', $stack_id])->one();
     if ($model && $model->id) {
         if ($calculeteVolume) {
             if ($transaction->type === 0) {
                 $model->lock_volume += $volume;
             } else {
                 $model->lock_volume -= $volume;
             }
         }
     } else {
         if ($transaction->type === 0) {
             $model = new MemberStack();
             $data = array('member_id' => $member_id, 'stack_id' => $stack_id, 'lock_volume' => $calculeteVolume ? $volume : 0, 'sell_volume' => 0);
             $model->load($data, '');
         }
     }
     return $model;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = MemberStack::find()->joinWith(['member' => function ($query) {
         $query->from(['member' => 'member']);
     }])->joinWith(['stack' => function ($query) {
         $query->from(['stack' => 'stack']);
     }])->orderBy(['created_at' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!Yii::$app->user->identity->isAdmin()) {
         $this->member_id = Yii::$app->user->identity->id;
     }
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'member_id' => $this->member_id, 'stack_id' => $this->stack_id, 'sell_volume' => $this->sell_volume, 'lock_volume' => $this->lock_volume, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at])->andFilterWhere(['like', 'member.username', $this->membername])->andFilterWhere(['like', 'stack.code', $this->stackcode])->andFilterWhere(['like', 'stack.name', $this->stackname])->orderBy(['created_at' => SORT_DESC]);
     return $dataProvider;
 }
示例#3
0
 public function actionBuy()
 {
     $model = new StackTransaction();
     $stack = Stack::findOne(Yii::$app->request->get('id'));
     $memberStack = Yii::$app->user->identity->getMemberStack($stack->id);
     if (!$memberStack) {
         $memberStack = new MemberStack();
     }
     $open = false;
     if ($stack->status) {
         Yii::$app->session->setFlash('danger', '股票已锁定,请选择其它股票进行购买.');
     } else {
         if (Date::isWorkingDay()) {
             if (Date::isWorkingTime()) {
                 $open = true;
                 if ($model->load(Yii::$app->request->post())) {
                     if ($model->account_type) {
                         $data = Yii::$app->request->post();
                         $model->price = $stack->price;
                         $model->member_id = Yii::$app->user->identity->id;
                         $model->stack_id = $stack->id;
                         $model->type = 0;
                         $model->total_price = $stack->price * $model->volume;
                         $validate = true;
                         if (!Yii::$app->user->identity->validatePassword2($data['StackTransaction']['password2'])) {
                             $validate = false;
                             $model->addError('password2', '第二密码不正确, 请确认后重新输入.');
                         }
                         if ($model->account_type == 1 && Yii::$app->user->identity->finance_fund < $model->total_price || $model->account_type == 2 && Yii::$app->user->identity->stack_fund < $model->total_price) {
                             $validate = false;
                             $model->addError('volume', '账户余额不足. 理财基金:.' . Yii::$app->user->identity->finance_fund . '. 购股账户:' . Yii::$app->user->identity->stack_fund);
                         }
                         if ($validate) {
                             $memberStack = MemberStack::getMemberStack($model);
                             $member = Member::findOne($model->member_id);
                             if ($model->account_type == 1) {
                                 $member->finance_fund -= $model->total_price;
                                 $outRecord = OutRecord::prepareModelForBuyStack($model->member_id, $model->total_price, $member->finance_fund, 1);
                             } else {
                                 $member->stack_fund -= $model->total_price;
                                 $outRecord = OutRecord::prepareModelForBuyStack($model->member_id, $model->total_price, $member->stack_fund, 2);
                             }
                             $outRecord->note = '股买[' . $stack->code . ']' . $model->volume . '股';
                             $connection = Yii::$app->db;
                             try {
                                 $transaction = $connection->beginTransaction();
                                 $success = false;
                                 if ($model->save() && $memberStack->save() && $member->save() && $outRecord->save()) {
                                     $success = true;
                                 }
                                 if ($success) {
                                     Yii::$app->session->setFlash('success', '购买成功');
                                     $transaction->commit();
                                     return $this->redirect(['transactions']);
                                 } else {
                                     Yii::error('Stack Buy Failed');
                                     Yii::$app->session->setFlash('danger', '购买失败,请稍后再试.');
                                     Yii::error(json_encode($model->getErrors()));
                                     Yii::error(json_encode($memberStack->getErrors()));
                                     Yii::error(json_encode($member->getErrors()));
                                     Yii::error(json_encode($outRecord->getErrors()));
                                     $transaction->rollback();
                                 }
                             } catch (Exception $e) {
                             }
                         }
                     } else {
                         $model->total_price = $stack->price * $model->volume;
                     }
                 }
             } else {
                 Yii::$app->session->setFlash('danger', '非交易时间. 早上10:00 ~ 12:30. 下午2:00 ~ 4:00');
             }
         } else {
             Yii::$app->session->setFlash('danger', '对不起,非交易日不能进行交易!');
         }
     }
     return $this->render('buy', ['model' => $model, 'stack' => $stack, 'memberStack' => $memberStack, 'open' => $open]);
 }
 public function getMemberStack()
 {
     return $this->hasOne(MemberStack::className(), ['member_stack.member_id' => 'member_id', 'member_stack.stack_id' => 'stack_id']);
 }
示例#5
0
 public function getMemberStack($stack_id)
 {
     return MemberStack::find()->where(['member_id' => Yii::$app->user->identity->id, 'stack_id' => $stack_id])->one();
 }