public function actionIndex() { if (Date::isWorkingDay()) { $limit = (int) System::loadConfig('transaction_rule'); if ($limit) { $limit = $limit - 1; } else { $limit = 0; } if ($limit == 0) { $date = date('Y-m-d'); } else { $dates = Date::find()->where(['<', 'date', new Expression('curdate()')])->andWhere(['=', 'status', 0])->orderBy(['date' => SORT_DESC])->limit(2)->all(); $date = array_pop($dates); $date = $date->date; } $date .= ' 23:59:59'; $transactions = StackTransaction::find()->where(['=', 'status', 0])->andWhere(['<', 'created_at', $date])->all(); foreach ($transactions as $transaction) { if ($transaction->type == 0) { $this->dealBuyAction($transaction); } else { $this->dealSellAction($transaction); } } } else { return false; } }
public function actionIndex() { if (Date::isWorkingDay()) { $transactions = FundTransaction::find()->where(['=', 'locked', 1])->andWhere(['=', 'cleared', 0])->all(); foreach ($transactions as $transaction) { $amount = $transaction->investment * $transaction->fund->daily; $transaction->revenue += $amount; $member = $transaction->member; $member->finance_fund += $amount; $inRecord = new InRecord(); $data = array('member_id' => $transaction->member_id, 'type' => 5, 'fee' => 0, 'amount' => $amount, 'total' => $member->finance_fund, 'account_type' => 1, 'note' => '基金分红: (' . date('Y-m-d', time()) . ')'); $inRecord->load($data, ''); $inRecord->save(); $member->save(); $transaction->save(); } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Date::find()->orderBy(['date' => SORT_DESC]); $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->date) { $date = explode(' - ', $this->date); if (count($date) == 2) { $query->andFilterWhere(['>=', $this::tableName() . '.date', $date[0]]); $query->andFilterWhere(['<=', $this::tableName() . '.date', $date[1]]); } } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at])->orderBy(['date' => SORT_DESC]); return $dataProvider; }
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]); }
public static function isWorkingDay() { $data = Date::find()->where(['=', 'date', new Expression('curdate()')])->one(); return $data && $data->status == 0 ? true : false; }
/** * Finds the Date model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Date the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Date::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }