diff --git a/backend/controllers/TransferController.php b/backend/controllers/TransferController.php index a41f25c..682f990 100644 --- a/backend/controllers/TransferController.php +++ b/backend/controllers/TransferController.php @@ -8,6 +8,8 @@ use backend\models\TransferSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; +use common\models\Account; +use common\models\User; /** * TransferController implements the CRUD actions for Transfer model. @@ -34,10 +36,20 @@ class TransferController extends Controller { $searchModel = new TransferSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + $accounts = Account::readAccounts(); + $users = User::read(); + + $searchModel->totals(Yii::$app->request->queryParams); + + return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, + 'accounts' => $accounts, + 'users' => $users, +// 'totals' =>$totals ]); } diff --git a/backend/models/ProcurementSearch.php b/backend/models/ProcurementSearch.php index ef371da..eac7970 100644 --- a/backend/models/ProcurementSearch.php +++ b/backend/models/ProcurementSearch.php @@ -77,7 +77,7 @@ class ProcurementSearch extends Procurement 'id_product' => $this->id_product, ]); - $query->andFilterWhere([ '>=', 'created_at', $this->date_start ] ); + $query->andFilterWhere([ '>=', 'created_at', $this->timestampStart ] ); $query->andFilterWhere([ '<', 'created_at', $this->timestampEnd ] ); diff --git a/backend/models/TransferSearch.php b/backend/models/TransferSearch.php index 903aaeb..1604c29 100644 --- a/backend/models/TransferSearch.php +++ b/backend/models/TransferSearch.php @@ -6,20 +6,39 @@ use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use common\models\Transfer; +use yii\db\Expression; +use yii\base\Object; +use yii\db\Query; +use yii\helpers\ArrayHelper; +use common\models\Account; /** * TransferSearch represents the model behind the search form about `common\models\Transfer`. */ class TransferSearch extends Transfer { + public $searchObjectName; + public $searchTypeName; + public $searchUserName; + public $start; + public $end; + + public $timestampStart; + public $timestampEnd; + + public $accountTotals; + public $fullTotal; + /** * @inheritdoc */ public function rules() { return [ - [['id_transfer', 'id_discount', 'id_currency', 'id_object', 'status', 'type', 'item_price', 'count', 'money', 'money_currency', 'rate', 'id_user'], 'integer'], - [['comment', 'created_at', 'updated_at'], 'safe'], + [[ 'id_account','id_user', 'type'], 'integer'], + [[ 'searchObjectName' ], 'string'], + [[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], + [[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], ]; } @@ -47,33 +66,108 @@ class TransferSearch extends Transfer 'query' => $query, ]); + $selectObjectName = ""; + $selectObjectName .= " case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end as object_name"; + + $query->addSelect( ['*', new Expression($selectObjectName) ]); + $query->joinWith('ticket'); + $query->joinWith('ticketType'); + $query->joinWith('product'); + $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'); + $query->where('0=1'); return $dataProvider; } $query->andFilterWhere([ - 'id_transfer' => $this->id_transfer, - 'id_discount' => $this->id_discount, - 'id_currency' => $this->id_currency, - 'id_object' => $this->id_object, - 'status' => $this->status, - 'type' => $this->type, - 'item_price' => $this->item_price, - 'count' => $this->count, - 'money' => $this->money, - 'money_currency' => $this->money_currency, - 'rate' => $this->rate, - 'id_user' => $this->id_user, - 'created_at' => $this->created_at, - 'updated_at' => $this->updated_at, + 'transfer.id_account' => $this->id_account, + 'transfer.type' => $this->type, + 'transfer.id_user' => $this->id_user, ]); + + $query->andFilterWhere([ '>=', 'transfer.created_at', $this->timestampStart ] ); + $query->andFilterWhere([ '<', 'transfer.created_at', $this->timestampEnd ] ); - $query->andFilterWhere(['like', 'comment', $this->comment]); - + if ( isset($this->searchObjectName)) + $query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'")); + + + return $dataProvider; } + + public function totals($params){ + $query = new Query(); + + $result = [ ]; + + $accountTotals = []; + $fullTotal = [ + 'label' => Yii::t('common/transfer', 'Total'), + 'money' => 0 + ]; + + $query->addSelect( [ new Expression( 'transfer.id_account as account'), new Expression( ' COALESCE(sum( transfer.money ),0) as money' )]); + $query->from('transfer'); + $query->leftJoin('ticket', 'transfer.type = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' ); + $query->leftJoin('ticket_type', 'ticket.id_ticket_type = ticket_type.id_ticket_type ' ); + $query->leftJoin('product', 'product.id_product = ' .self::TYPE_TICKET .' and transfer.id_object = ticket.id_ticket ' ); + + $this->load($params); + + if (!$this->validate()) { + return ; + } + + $query->andFilterWhere([ + 'transfer.id_account' => $this->id_account, + 'transfer.type' => $this->type, + 'transfer.id_user' => $this->id_user, + ]); + + $query->andFilterWhere([ '>=', 'transfer.created_at', $this->timestampStart ] ); + $query->andFilterWhere([ '<', 'transfer.created_at', $this->timestampEnd ] ); + + + if ( isset($this->searchObjectName) && !empty($this->searchObjectName)){ + $query->andWhere( new Expression(" case when transfer.type = " .self::TYPE_PRODUCT ." then product.name else ticket_type.name end like '%" . $this->searchObjectName ."%'")); + } + + $query->groupBy('transfer.id_account'); + + $command = $query->createCommand(); + // $command->sql returns the actual SQL + $result = $command->queryAll(); + + + $accounts = Account::find()->orderBy("name asc")->all(); + $accountMap = ArrayHelper::map( $accounts ,'id_account','name' ); + + + + foreach ($result as $item){ + $account = ""; + if ( array_key_exists($item['account'], $accountMap)){ + $account = $accountMap[$item['account']]; + } + + $accountTotal = [ + 'label' => $account, + 'money' => $item['money'] + ]; + + $accountTotals[] = $accountTotal; + + $fullTotal['money'] += $item['money']; + } + + $this->accountTotals = $accountTotals; + $this->fullTotal = $fullTotal; + +// return $result; + } + } diff --git a/backend/views/transfer/_search.php b/backend/views/transfer/_search.php index 3e67083..d55f5d4 100644 --- a/backend/views/transfer/_search.php +++ b/backend/views/transfer/_search.php @@ -2,52 +2,68 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; +use frontend\components\HtmlHelper; +use yii\helpers\ArrayHelper; +use common\models\Transfer; +use kartik\widgets\DatePicker; /* @var $this yii\web\View */ /* @var $model backend\models\TransferSearch */ /* @var $form yii\widgets\ActiveForm */ ?> +
- = Html::a(Yii::t('frontend/transfer', 'Create Transfer'), ['create'], ['class' => 'btn btn-success']) ?> -
+ render('_search', ['model' => $searchModel, 'accounts' => $accounts,'users' => $users,]); ?> + +- = Html::a(Yii::t('frontend/transfer', 'Update'), ['update', 'id' => $model->id_transfer], ['class' => 'btn btn-primary']) ?> - = Html::a(Yii::t('frontend/transfer', 'Delete'), ['delete', 'id' => $model->id_transfer], [ - 'class' => 'btn btn-danger', - 'data' => [ - 'confirm' => Yii::t('frontend/transfer', 'Are you sure you want to delete this item?'), - 'method' => 'post', - ], - ]) ?> -
- = DetailView::widget([ 'model' => $model, 'attributes' => [ - 'id_transfer', - 'id_discount', - 'id_currency', - 'id_object', - 'status', - 'type', + [ + 'attribute' => 'id_transfer', + ], + [ + 'attribute' => 'id_account', + 'value' => $model->accountName + ], + [ + 'attribute' => 'type', + 'value' => $model->transferTypeName, + ], + [ + 'attribute' => 'id_object', + 'value' => $model->objectName, + ], + [ + 'attribute' => 'id_user', + 'value' => $model->userName, + ], 'item_price', 'count', 'money', - 'money_currency', - 'rate', - 'id_user', 'comment', 'created_at', - 'updated_at', ], ]) ?> diff --git a/common/models/Ticket.php b/common/models/Ticket.php index 82c7293..86bbf24 100644 --- a/common/models/Ticket.php +++ b/common/models/Ticket.php @@ -124,4 +124,27 @@ class Ticket extends \common\models\BaseFitnessActiveRecord } return $result; } + + + /** + * @param common\models\Card $card the card to which we are looking for + * */ + public static function readActive($card){ + + if ( $card == null ) + return []; + + $query = Ticket::find(); + $today = date('Y-m-d'); + + $query->andWhere(['id_card' => $card->id_card]); + $query->andWhere( 'start <= :today' ,[ 'today' => $today] ); + $query->andWhere( 'end >= :today' ,[ 'today' => $today] ); + + $result = $query->all(); + + return $result; + + } + } diff --git a/common/models/Transfer.php b/common/models/Transfer.php index 295409e..29a42cf 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -79,12 +79,21 @@ class Transfer extends \yii\db\ActiveRecord ]; } + public function getUser(){ + return $this->hasOne( User::className(), ["id" =>"id_user" ] ); + } + public function getProduct(){ return $this->hasOne( Product::className(), ["id_product" =>"id_object" ] ); } + public function getTicket(){ return $this->hasOne( Ticket::className(), ["id_ticket" =>"id_object" ] ); } + + public function getTicketType(){ + return $this->hasOne( TicketType::className(), ["id_ticket_type" =>"id_ticket_type" ] )->via('ticket') ; + } public function getAccount(){ return $this->hasOne( Account::className(), ["id_account" =>"id_account" ] ) ; @@ -102,6 +111,65 @@ class Transfer extends \yii\db\ActiveRecord return $this->hasOne( UserSoldItem::className(), ["id_transfer" =>"id_transfer" ] ); } + public function getObjectName(){ + $result = ""; + if ( $this->type == Transfer::TYPE_TICKET ){ + $result = $this->ticketName; + }else{ + $result = $this->productName; + } + return $result; + } + public function getUserName(){ + $result = ""; + $user = $this->user; + if (isset($this->user)){ + $result = $user->username; + } + + return $result; + } + + public function getProductName(){ + $result = ""; + $product = $this->product; + if (isset($product)){ + $result = $product->name; + } + + return $result; + } + + public function getTicketName(){ + $result = ""; + $ticket = $this->ticket; + if (isset($ticket)){ + $result = $this->ticket->ticketTypeName; + } + + return $result; + } + public function getAccountName(){ + $result = ""; + $account = $this->account; + if (isset($account)){ + $result = $account->name; + } + + return $result; + } + + public function getTransferTypeName(){ + $result = ""; + if ( $this->type == Transfer::TYPE_TICKET ){ + $result = Yii::t('common/transfer','Ticket'); + }else{ + $result = Yii::t('common/transfer','Product'); + } + + return $result; + } + public function toProductSoldString(){ $s = ""; @@ -239,4 +307,11 @@ class Transfer extends \yii\db\ActiveRecord return $transfers; } + public static function types( ) { + return [ + self::TYPE_PRODUCT => Yii::t('common/transfer','Product'), + self::TYPE_TICKET => Yii::t('common/transfer','Ticket'), + ]; + } + } diff --git a/frontend/components/HtmlHelper.php b/frontend/components/HtmlHelper.php index 327d409..aeb5bf4 100644 --- a/frontend/components/HtmlHelper.php +++ b/frontend/components/HtmlHelper.php @@ -9,6 +9,39 @@ use yii\helpers\ArrayHelper; class HtmlHelper{ + /** + * Mehtod used in reception widgets to render card dependent buttons + * + * @see ReceptionCardNumberTicketsWidget + * @see ReceptionCardNumberWidget + * @see ReceptionMenuWidget + * */ + public static function mkReceptionCardBtn($card, $label,$route = null ){ + + $url = null; + $classes = 'btn btn-primary btn-reception'; + if ( $card == null ){ + $classes .= ' disabled'; + } + if ( isset($route)){ + $url = [$route, 'number' => ( isset( $card ) ? $card->number : '' )]; + } + return Html::a( $label , $url, ['class' => $classes ] ); + } + + public static function mkReceptionBtn($card, $label,$route = null ){ + + $url = null; + $classes = 'btn btn-primary btn-reception'; + + if ( isset($route)){ + $url = [$route, 'number' => ( isset( $card ) ? $card->number : '' )]; + } + + return Html::a( $label , $url, ['class' => $classes ] ); + } + + public static function mkOptions( $models, $key, $value = 'name' ){ $result = []; $result = ArrayHelper::map( $models, $key, $value ); diff --git a/frontend/components/ReceptionCardNumberTicketsWidget.php b/frontend/components/ReceptionCardNumberTicketsWidget.php new file mode 100644 index 0000000..4e8a3e9 --- /dev/null +++ b/frontend/components/ReceptionCardNumberTicketsWidget.php @@ -0,0 +1,24 @@ +render($this->viewFile,[ 'tickets' => $this->tickets, 'card' => $this->card, 'customer' =>$this->customer , 'route' => $this->route ]); + } + + +} \ No newline at end of file diff --git a/frontend/components/ReceptionWidget.php b/frontend/components/ReceptionWidget.php new file mode 100644 index 0000000..d7fbce4 --- /dev/null +++ b/frontend/components/ReceptionWidget.php @@ -0,0 +1,36 @@ +form)){ + $this->card = $this->form->card; + $this->customer = $this->form->customer; + $this->tickets = $this->form->tickets; + } + + } + + + public function run(){ + echo $this->render($this->viewFile,['card' => $this->card, 'customer' =>$this->customer, 'tickets' => $this->tickets ]); + } + + +} \ No newline at end of file diff --git a/frontend/controllers/ProductController.php b/frontend/controllers/ProductController.php index 320f2a1..e39b015 100644 --- a/frontend/controllers/ProductController.php +++ b/frontend/controllers/ProductController.php @@ -39,6 +39,18 @@ class ProductController extends Controller 'delete' => ['post'], ], ], + 'access' => [ + 'class' => \yii\filters\AccessControl::className(), + 'only' => [ 'sale','clear-list', 'lookup'], + 'rules' => [ + // allow authenticated users + [ + 'allow' => true, + 'roles' => ['@'], + ], + // everything else is denied + ], + ], ]; } @@ -126,82 +138,8 @@ class ProductController extends Controller } - /** - * Lists all Product models. - * @return mixed - */ - public function actionIndex() - { - $searchModel = new ProductSearch(); - $dataProvider = $searchModel->search(Yii::$app->request->queryParams); - return $this->render('index', [ - 'searchModel' => $searchModel, - 'dataProvider' => $dataProvider, - ]); - } - /** - * Displays a single Product model. - * @param integer $id - * @return mixed - */ - public function actionView($id) - { - return $this->render('view', [ - 'model' => $this->findModel($id), - ]); - } - - /** - * Creates a new Product model. - * If creation is successful, the browser will be redirected to the 'view' page. - * @return mixed - */ - public function actionCreate() - { - $model = new Product(); - - if ($model->load(Yii::$app->request->post()) && $model->save()) { - return $this->redirect(['view', 'id' => $model->id_product]); - } else { - return $this->render('create', [ - 'model' => $model, - ]); - } - } - - /** - * Updates an existing Product 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_product]); - } else { - return $this->render('update', [ - 'model' => $model, - ]); - } - } - - /** - * Deletes an existing Product model. - * If deletion is successful, the browser will be redirected to the 'index' page. - * @param integer $id - * @return mixed - */ - public function actionDelete($id) - { - $this->findModel($id)->delete(); - - return $this->redirect(['index']); - } /** */ @@ -250,4 +188,86 @@ class ProductController extends Controller } + /** + * Lists all Product models. + * @return mixed + */ + /* + public function actionIndex() + { + $searchModel = new ProductSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } +*/ + /** + * Displays a single Product model. + * @param integer $id + * @return mixed + */ + /* + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } +*/ + /** + * Updates an existing Product 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_product]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + */ + /** + * Deletes an existing Product model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + /* + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + */ + /** + * Creates a new Product model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + /* + public function actionCreate() + { + $model = new Product(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id_product]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } +*/ } diff --git a/frontend/controllers/TicketController.php b/frontend/controllers/TicketController.php index 0f77ecc..f8dd1d8 100644 --- a/frontend/controllers/TicketController.php +++ b/frontend/controllers/TicketController.php @@ -31,6 +31,18 @@ class TicketController extends Controller 'delete' => ['post'], ], ], + 'access' => [ + 'class' => \yii\filters\AccessControl::className(), + 'only' => ['create', 'index' ], + 'rules' => [ + // allow authenticated users + [ + 'allow' => true, + 'roles' => ['@'], + ], + // everything else is denied + ], + ], ]; } @@ -59,18 +71,6 @@ class TicketController extends Controller ]); } - /** - * Displays a single Ticket model. - * @param integer $id - * @return mixed - */ - public function actionView($id) - { - return $this->render('view', [ - 'model' => $this->findModel($id), - ]); - } - /** * Creates a new Ticket model. * If creation is successful, the browser will be redirected to the 'view' page. @@ -123,38 +123,6 @@ class TicketController extends Controller } } - /** - * Updates an existing Ticket 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_ticket]); - } else { - return $this->render('update', [ - 'model' => $model, - ]); - } - } - - /** - * Deletes an existing Ticket model. - * If deletion is successful, the browser will be redirected to the 'index' page. - * @param integer $id - * @return mixed - */ - public function actionDelete($id) - { - $this->findModel($id)->delete(); - - return $this->redirect(['index']); - } - /** * Finds the Ticket model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. @@ -170,4 +138,51 @@ class TicketController extends Controller throw new NotFoundHttpException('The requested page does not exist.'); } } + /** + * Displays a single Ticket model. + * @param integer $id + * @return mixed + */ + /* + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + */ + /** + * Deletes an existing Ticket model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + /* + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + */ + /** + * Updates an existing Ticket 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_ticket]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } +*/ } diff --git a/frontend/models/ReceptionForm.php b/frontend/models/ReceptionForm.php index d8f224c..372314f 100644 --- a/frontend/models/ReceptionForm.php +++ b/frontend/models/ReceptionForm.php @@ -6,6 +6,7 @@ use Yii; use yii\base\Model; use common\models\Card; use common\models\Customer; +use common\models\Ticket; /** * ContactForm is the model behind the contact form. @@ -15,6 +16,7 @@ class ReceptionForm extends Model public $number; public $card; public $customer; + public $tickets; /** @@ -39,10 +41,18 @@ class ReceptionForm extends Model } public function readCard(){ + $this->card = Card::findOne(['number' => $this->number]); if ( $this->card != null ){ $this->customer = $this->card->customer; + $this->readValidTickets(); } } - + + public function readValidTickets(){ + $this->tickets = Ticket::readActive($this->card); + } + + + } diff --git a/frontend/models/TicketSearch.php b/frontend/models/TicketSearch.php index 46c075f..07d54aa 100644 --- a/frontend/models/TicketSearch.php +++ b/frontend/models/TicketSearch.php @@ -45,7 +45,7 @@ class TicketSearch extends Ticket $dataProvider = new ActiveDataProvider([ 'query' => $query, - 'sort'=> ['defaultOrder' => ['end'=>SORT_DESC]] + 'sort'=> ['defaultOrder' => ['created_at'=>SORT_DESC]] ]); diff --git a/frontend/views/common/_card_number_tickets.php b/frontend/views/common/_card_number_tickets.php new file mode 100644 index 0000000..3061142 --- /dev/null +++ b/frontend/views/common/_card_number_tickets.php @@ -0,0 +1,41 @@ + + +| getAttributeLabel('id_ticket_type') ?> | +getAttributeLabel('start') ?> | +getAttributeLabel('end') ?> | +
|---|---|---|
| + ticketTypeName ?> + | ++ formatter->asDate( $ticket->start ) ?> + | ++ formatter->asDate( $ticket->end ) ?> + | +