Skip to content

Instantly share code, notes, and snippets.

@mgerasimchuk
Last active October 26, 2016 15:33
Show Gist options
  • Save mgerasimchuk/671bc879bd47701d1a04470c1806345f to your computer and use it in GitHub Desktop.
Save mgerasimchuk/671bc879bd47701d1a04470c1806345f to your computer and use it in GitHub Desktop.
Overcharging with use DB transaction Controller action
<?php
namespace backend\modules\manager\controllers;
use backend\modules\manager\Controller;
use common\core\Core;
use common\models\billing\Account;
use common\models\billing\Transaction;
use common\models\billing\TransactionSearch;
use Yii;
use yii\db\Exception;
use yii\helpers\Url;
/**
* BillingTransactionController implements the CRUD actions for BillingTransaction model.
*/
class BillingTransactionController extends Controller
{
/**
* @return string|\yii\web\Response
* @throws Exception
* @throws \Exception
*/
public function actionOvercharging()
{
$model = new Transaction(['scenario' => 'overcharging']);
if ($model->load(Yii::$app->request->post())) {
$fromAccount = Account::findOne(['userId' => Core::getRoot()->id, 'currencyId' => $model->currencyId]);
$toAccount = Account::findOne([
'projectId' => Yii::$app->request->get('id'),
'currencyId' => $model->currencyId
]);
$dbTransaction = \Yii::$app->db->beginTransaction();
try {
$fromAccount->transfer($toAccount, $model->amount, $model->type, $model->description);
Yii::$app->session->setFlash('success', Yii::t('app', 'Overcharging is success!'));
$dbTransaction->commit();
} catch (Exception $e) {
$dbTransaction->rollBack();
Yii::$app->session->setFlash('success', Yii::t('app', 'Overcharging error!'));
throw $e;
}
return $this->redirect(Url::previous());
} else {
return $this->render('create', [
'model' => $model,
'types' => Transaction::getProjectDebit(),
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment