fitness-web/frontend/controllers/TicketController.php
2015-10-30 09:28:58 +01:00

131 lines
4.0 KiB
PHP

<?php
namespace frontend\controllers;
use Yii;
use common\models\Ticket;
use frontend\models\TicketSearch;
use frontend\models\ReceptionForm;
use frontend\models\TicketCreate;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Discount;
use common\models\TicketType;
use common\models\Account;
use yii\base\Object;
use common\models\Transfer;
use common\models\User;
use common\models\ShoppingCart;
use common\models\UserSoldItem;
use frontend\components\FrontendController;
/**
* TicketController implements the CRUD actions for Ticket model.
*/
class TicketController extends FrontendController
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['create', 'index' ],
'rules' => [
// allow authenticated users
[
'allow' => true,
'roles' => ['@'],
],
// everything else is denied
],
],
];
}
/**
* Lists all Ticket models.
* @return mixed
*/
public function actionIndex($number = null)
{
$receptionForm = $this->mkReceptionForm($number);
if ( !isset($receptionForm->card ) ){
throw new NotFoundHttpException( Yii::t('frontend/ticket', 'The requested card does not exist.'));
}
$searchModel = new TicketSearch();
$dataProvider = $searchModel->search( $receptionForm->card, Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'receptionForm' => $receptionForm,
]);
}
/**
* Creates a new Ticket model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate($number = null)
{
$receptionForm =$this->mkReceptionForm($number);
if ( !isset($receptionForm->card ) ){
throw new NotFoundHttpException( Yii::t('frontend/ticket', 'The requested card does not exist.'));
}
if ( isset($_POST['payout_customer_cart']) && $this->payoutCustomerCart($receptionForm) ){
return $this->redirect(['customer/reception' ]);
}else if ( isset($_POST['payout_user_cart']) && $this->payoutUserCart($receptionForm)){
return $this->redirect(['customer/reception' ]);
}
$model = new TicketCreate();
$discounts = Discount::read();
$ticketTypes = TicketType::read();
$accounts = Account::readAccounts();
$user = User::findOne( [ 'id' => Yii::$app->user->id ] );
$model->customer = $receptionForm->customer;
$model->id_user = \Yii::$app->user->id;
$model->status = Ticket::STATUS_ACTIVE;
$model->usage_count = 0;
$model->id_card = $receptionForm->card->id_card;
$model->id_account = Account::readDefault();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', Yii::t('frontend/ticket', 'Ticket added to customer') );
return $this->redirect(['product/sale', 'number' => $receptionForm->card->number]);
}
$model->userCart = Transfer::modelsToArray( Transfer::readUserSoldTransfers($user) );
$model->customerCart = Transfer::modelsToArray( Transfer::readCustomerCart( $receptionForm->customer ) );
return $this->render('create', [
'model' => $model,
'discounts' => $discounts,
'ticketTypes' => $ticketTypes,
'accounts' => $accounts,
'receptionForm' => $receptionForm,
]);
}
}