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']); } }