public function actionSaveOnTarget() { $model = new SaveOnTargetForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { /** @var Targets $target */ $target = Targets::findOne(['id' => $model->toTargetId]); /** @var Accounts $account */ $account = Accounts::findOne(['id' => $model->fromAccountId]); if ($target && $account) { $target->balance = str_replace(' ', '', $target->balance); $target->balance = bcadd($target->balance, $model->toAmount, 2); $account->amount = bcsub($account->amount, $model->fromTotal, 2); $transaction = Yii::$app->getDb()->beginTransaction(); try { if ($account->save() && $target->save()) { $transaction->commit(); Yii::$app->getSession()->setFlash('success', 'Средства были отложены.'); return $this->redirect(['/targets']); } else { $transaction->rollback(); } } catch (\Exception $e) { $transaction->rollback(); } } } return $this->render('save-on-target', ['model' => $model]); }
/** * Finds the Account model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Accounts the loaded model * @throws NotFoundHttpException if the model cannot be found */ public function findModel($id) { if (($model = Accounts::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @param integer $id * @return mixed * @throws NotFoundHttpException if the model cannot be found */ public function run($id) { $account = Accounts::findOne(['id' => $id]); if (!empty($account)) { $param = ['TransactionSearch' => ['accounts' => $id]]; $extraOptions = ['filterEnable' => true]; return $this->controller->renderIndex($param, $extraOptions); } else { $this->controller->redirect(['/transaction']); } }
/** * @return array */ public static function getListAccounts() { $result = []; $accounts = Accounts::find()->all(); if (isset($accounts)) { foreach ($accounts as $account) { /** @var $account Accounts */ $result[$account->id] = $account->name . ' (' . $account->valuta . ')'; } } return $result; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Accounts::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; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'sort' => $this->sort, 'amount' => $this->amount, 'status' => $this->status]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'color', $this->color]); return $dataProvider; }
/** * Edits an existing Transaction model. * If edit is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed * @throws NotFoundHttpException if the model cannot be found */ public function run($id) { /** @var Transaction $model */ $model = $this->controller->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->validate()) { $transaction2Category = ''; if (isset(Yii::$app->request->post('Transaction')['categoryIds'])) { $transaction2Category = Yii::$app->request->post('Transaction')['categoryIds']; } if ($model->save()) { if (!empty($transaction2Category)) { TransactionHelper::saveTransaction2Category($transaction2Category, $model->id); } Yii::$app->getSession()->setFlash('success', 'Транзакция изменена.'); return $this->controller->redirect(['/transaction']); } } /** @var Accounts $account */ $account = Accounts::find()->andWhere(['id' => $model->accounts])->one(); $model->categoryIds = $model->getTransaction2CategoryList(); return $this->controller->render('_forms/_edit', ['model' => $model, 'account' => $account]); }
/** * @return \yii\db\ActiveQuery */ public function getAccountTransferModel() { return $this->hasOne(Accounts::className(), ['id' => 'accountTransfer']); }
/** * @param $model \common\models\Transaction */ private function _calculateAccountAmount($model) { /** @var Accounts $account */ $account = Accounts::find()->andWhere(['id' => $model->accounts])->one(); if ($account) { if ($model->type_id == TransactionHelper::TYPE_TRANSFER) { $params = new stdClass(); $params->type_id = TransactionHelper::TYPE_EXPENSE; $params->accountAmount = $account->amount; $params->transactionAmount = $model->total; $account->amount = $this->_getAccountAmount($params, true); $account->save(); /** @var Accounts $accountTransfer */ $accountTransfer = Accounts::find()->andWhere(['id' => $model->accountTransfer])->one(); $params->type_id = TransactionHelper::TYPE_INCOME; $params->accountAmount = $accountTransfer->amount; $params->transactionAmount = $model->totalTransfer; $accountTransfer->amount = $this->_getAccountAmount($params, true); $accountTransfer->save(); } else { $params = new stdClass(); $params->type_id = $model->type_id; $params->accountAmount = $account->amount; $params->transactionAmount = $model->total; $account->amount = $this->_getAccountAmount($params, true); $account->save(); } } }
/** * @param $model Transaction * * @return string */ public static function getFullAmount(Transaction $model) { $amount = ''; $currency = ''; /** @var Accounts $account */ $account = Accounts::find()->andWhere(['id' => $model->accounts])->one(); if (isset($account)) { $amount = explode('.', $model->total); $rest = Html::tag('div', $amount[1], ['class' => 'transaction-currency-rest']); $amount = Html::tag('div', $amount[0] . '.' . $rest, ['class' => 'transaction-currency-' . self::getClassesForType($model->type_id)]); $currency = Html::tag('div', $account->valuta, ['class' => 'unit']); } return $amount . $currency; }