fitness-web/frontend/controllers/TransferController.php

185 lines
5.0 KiB
PHP

<?php
namespace frontend\controllers;
use Yii;
use common\models\Transfer;
use frontend\models\TransferSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use frontend\models\TransferMoneyMovementSearch;
use common\models\Account;
use frontend\models\TransferListSearch;
/**
* TransferController implements the CRUD actions for Transfer model.
*/
class TransferController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Transfer models.
* @return mixed
*/
public function actionMoneyMovementIndex()
{
$searchModel = new TransferMoneyMovementSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('money_movement/money_movement_index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Lists all Transfer models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new TransferSearch();
$searchModel->accounts = Account::read();
$searchModel->load(Yii::$app->request->queryParams);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$searchModel->totalsTransfers(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Lists all Transfer models.
* @return mixed
*/
public function actionList()
{
$searchModel = new TransferListSearch();
$searchModel->accounts = Account::read();
$searchModel->load(Yii::$app->request->queryParams);
$searchModel->search(Yii::$app->request->queryParams);
return $this->render('list', [
'searchModel' => $searchModel,
]);
}
/**
* Displays a single Transfer model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Transfer model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionMoneyMovementCreate()
{
$model = new Transfer();
$model->type = Transfer::TYPE_MONEY_MOVEMENT_OUT;
$model->direction = Transfer::DIRECTION_OUT;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_transfer]);
} else {
return $this->render('money_movement/create_money_movement', [
'model' => $model,
]);
}
}
/**
* Finds the Transfer model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Transfer the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Transfer::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
* Updates an existing Transfer model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_transfer]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
*/
/**
* Deletes an existing Transfer model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$transfer = $this->findModel($id);
$connection = \Yii::$app->db;
$transaction = $connection->beginTransaction();
try {
if ( $transfer->delete() ){
\Yii::$app->session->setFlash( 'success','Tranzakció törölve' );
$transaction->commit();
}else{
throw new \Exception("Failed to save");
}
} catch(Exception $e) {
$transaction->rollback();
\Yii::$app->session->setFlash( 'danger','Tranzakció törlése nem sikerült' );
}
return $this->redirect(Yii::$app->request->referrer);
}
}