Beispiel #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Stack::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     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;
     }
     if ($this->created_at) {
         $date = explode(' - ', $this->created_at);
         if (count($date) == 2) {
             $query->andFilterWhere(['>=', $this::tableName() . '.created_at', $date[0] . ' 00:00:00']);
             $query->andFilterWhere(['<=', $this::tableName() . '.created_at', $date[1] . ' 23:59:59']);
         }
     }
     if ($this->updated_at) {
         $date = explode(' - ', $this->updated_at);
         if (count($date) == 2) {
             $query->andFilterWhere(['>=', $this::tableName() . '.updated_at', $date[0] . ' 00:00:00']);
             $query->andFilterWhere(['<=', $this::tableName() . '.updated_at', $date[1] . ' 23:59:59']);
         }
     }
     $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'price' => $this->price])->andFilterWhere(['like', 'stack.code', $this->code])->andFilterWhere(['like', 'stack.name', $this->name]);
     return $dataProvider;
 }
Beispiel #2
0
 public static function getStackOptions()
 {
     $stacks = Stack::findAll(array('status' => 0));
     return ArrayHelper::map($stacks, 'code', 'code');
 }
 public function actionSell($id)
 {
     $stack = Stack::findOne($id);
     $model = new StackTransaction();
     $memberStack = Yii::$app->user->identity->getMemberStack($stack->id);
     if ($model->load(Yii::$app->request->post())) {
         if (Date::isWorkingDay()) {
             if (Date::isWorkingTime()) {
                 if ($model->account_type) {
                     $data = Yii::$app->request->post();
                     $validate = true;
                     if (!Yii::$app->user->identity->validatePassword2($data['StackTransaction']['password2'])) {
                         $validate = false;
                         $model->addError('password2', '第二密码不正确, 请确认后重新输入.');
                     }
                     if (!$model->checkSellVolume($memberStack, $model->volume)) {
                         $validate = false;
                     }
                     if ($validate) {
                         $model->price = $stack->price;
                         $model->member_id = Yii::$app->user->identity->id;
                         $model->stack_id = $stack->id;
                         $model->type = 1;
                         $model->total_price = $stack->price * $model->volume;
                         $memberStack->sell_volume -= $model->volume;
                         $memberStack->lock_volume += $model->volume;
                         $connection = Yii::$app->db;
                         try {
                             $transaction = $connection->beginTransaction();
                             if ($model->save() && $memberStack->save()) {
                                 $transaction->commit();
                                 return $this->redirect(['transactions']);
                             } else {
                                 Yii::error('Sell Stack Failed');
                                 Yii::error(json_encode($model->getErrors()));
                                 Yii::error(json_encode($memberStack->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('sell', ['model' => $model, 'stack' => $stack, 'memberStack' => $memberStack]);
 }
Beispiel #4
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getStack()
 {
     return $this->hasOne(Stack::className(), ['id' => 'stack_id']);
 }
 public function setStackId($stack_id = null)
 {
     if ($stack_id) {
         $this->stack_id = $stack_id;
     } else {
         $stack = Stack::find()->where(['=', 'code', $this->stackcode])->one();
         if ($stack) {
             $this->stack_id = $stack->id;
         }
     }
 }
Beispiel #6
0
/* @var $model common\models\Stack */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="stack-form">

    <?php 
$form = ActiveForm::begin(['enableAjaxValidation' => true, 'validateOnBlur' => true, 'validationUrl' => '/stack/validatebuy?' . ($model->id ? 'id=' . $model->id : '')]);
?>

    <?php 
echo $form->field($model, 'membername')->textInput(['maxlength' => true, 'readOnly' => $model->isNewRecord ? false : true]);
?>

    <?php 
echo $form->field($model, 'stackcode')->dropDownList(\common\models\Stack::getStackOptions());
?>

    <?php 
echo $form->field($model, 'price')->textInput(['maxlength' => true]);
?>

    <?php 
echo $form->field($model, 'volume')->textInput(['maxlength' => true]);
?>

    <div class="form-group">
        <?php 
echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
?>
    </div>