diff --git a/backend/components/AdminMenuStructure.php b/backend/components/AdminMenuStructure.php index 357f19b..67d11cb 100644 --- a/backend/components/AdminMenuStructure.php +++ b/backend/components/AdminMenuStructure.php @@ -9,7 +9,7 @@ class AdminMenuStructure{ public $menuItems; - public function AdminMenu(){ + public function __construct(){ $this->menuItems = []; } @@ -31,7 +31,6 @@ class AdminMenuStructure{ $items = []; if (!Yii::$app->user->isGuest) { - //$today = \Yii::$app->formatter->asDate( time() ); $today = \Yii::$app->formatter->asDate( strtotime('today UTC') ); $tomorrow = \Yii::$app->formatter->asDate( ( 60 *60 *24 + time())); @@ -40,39 +39,63 @@ class AdminMenuStructure{ $tomorrowDatetime = \Yii::$app->formatter->asDatetime( strtotime('tomorrow UTC') ); - // if ( $this->can('backend.user.index')){ - $items[] = ['label' => 'Felhasználók', 'url' =>['/user/index']]; - // } + ///////////////////////////// + // Beállítások + ///////////////////////////// + $items[] = ['label' => 'Felhasználók', 'url' =>['/user/index']]; + $this->menuItems[] = ['label' => 'Beállítások', 'url' => null, + 'items' => $items + ]; + ///////////////////////////// + // Törszadatok + ///////////////////////////// + $items = []; $items[] = ['label' => 'Raktárak', 'url' =>['/warehouse/index']]; $items[] = ['label' => 'Kasszák', 'url' =>['/account/index']]; $items[] = ['label' => 'Kedvezmények', 'url' => ['/discount/index'] ]; $items[] = ['label' => 'Termék kategóriák', 'url' => ['/product-category/index'] ]; $items[] = ['label' => 'Bérlet típusok', 'url' => ['/ticket-type/index'] ]; - $items[] = ['label' => 'Termékek', 'url' => ['/product/index'] ]; - $items[] = ['label' => 'Beszerzések', 'url' => ['/procurement/index'] ]; +// $items[] = ['label' => 'Pénznem', 'url' => ['/currency/index'] ]; + $this->menuItems[] = ['label' => 'Törszadatok', 'url' => null, + 'items' => $items + ]; + + ///////////////////////////// + // BÉRLETEK + ///////////////////////////// + $items = []; $items[] = ['label' => 'Vendégek', 'url' => ['/customer/index'] ]; $items[] = ['label' => 'Bérletkártyák', 'url' => ['/card/index'] ]; -// $items[] = ['label' => 'Pénznem', 'url' => ['/currency/index'] ]; + $items[] = ['label' => 'Bérletek', 'url' => ['/ticket/index' , 'TicketSearch[start]' =>$today,'TicketSearch[end]' => $tomorrow ] ]; + $this->menuItems[] = ['label' => 'Bérletek/Vendégek', 'url' => null, + 'items' => $items + ]; + ///////////////////////////// + // Termékek + ///////////////////////////// + $items = []; + $items[] = ['label' => 'Termékek', 'url' => ['/product/index'] ]; + $items[] = ['label' => 'Beszerzések', 'url' => ['/procurement/index'] ]; + $this->menuItems[] = ['label' => 'Termékek', 'url' => null, + 'items' => $items + ]; + ///////////////////////////// + // Pénzügy + ///////////////////////////// + $items = []; $items[] = ['label' => 'Tranzakciók', 'url' => ['/transfer/index' , 'TransferSearch[start]' =>$today,'TransferSearch[end]' => $tomorrow ] ]; $items[] = ['label' => 'Kassza müveletek', 'url' => ['/account-state/index'] ]; $items[] = ['label' => 'Zárások', 'url' => ['/collection/index' , 'CollectionSearch[start]' =>$todayDatetime,'CollectionSearch[end]' => $tomorrowDatetime ] ]; + $this->menuItems[] = ['label' => 'Pénzügy', 'url' => null, + 'items' => $items + ]; - if ( count($items) > 0 ){ - $userMainMenu = ['label' => 'Beállítások', 'url' => null, - 'items' => $items - ]; - } - - if ( isset($userMainMenu)){ - $this->menuItems[] = $userMainMenu; - } } } - protected function addLoginMainMenu(){ if (Yii::$app->user->isGuest) { $mainMenuItem= ['label' => 'Login', 'url' => ['/site/login']]; diff --git a/backend/controllers/TicketController.php b/backend/controllers/TicketController.php index 9c80307..68793a6 100644 --- a/backend/controllers/TicketController.php +++ b/backend/controllers/TicketController.php @@ -11,6 +11,9 @@ use yii\filters\VerbFilter; use common\models\Discount; use common\models\TicketType; use common\models\Account; +use common\models\User; +use common\models\Customer; +use common\models\Card; /** * TicketController implements the CRUD actions for Ticket model. @@ -18,6 +21,25 @@ use common\models\Account; class TicketController extends \backend\controllers\BackendController { + + public function behaviors() + { + return [ + 'access' => [ + 'class' => \yii\filters\AccessControl::className(), + 'rules' => [ + // allow authenticated users + [ + 'actions' => ['create','index','view','update','index-customer'], + 'allow' => true, + 'roles' => ['admin','employee','reception'], + ], + // everything else is denied + ], + ], + ]; + } + /** * Lists all Ticket models. * @return mixed @@ -26,12 +48,55 @@ class TicketController extends \backend\controllers\BackendController { $searchModel = new TicketSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + $searchModel->searchTotals(); + + $searchModel->users = User::read(); + $searchModel->accounts = Account::read(); + $searchModel->ticketTypes = TicketType::read(); + + return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } + + + /** + * Lists all Ticket models. + * @return mixed + */ + public function actionIndexCustomer($id) + { + + $customer = Customer::findOne($id); + $card = Card::findOne($id); + + + if ( $customer == null ){ + throw new NotFoundHttpException('The requested page does not exist.'); + } + + $searchModel = new TicketSearch(); + $searchModel->id_card = $customer->id_customer_card; + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + $searchModel->searchTotals(); + + $searchModel->users = User::read(); + $searchModel->accounts = Account::read(); + $searchModel->ticketTypes = TicketType::read(); + + + + return $this->render('index_customer', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + 'customer' => $customer, + 'card' => $card + ]); + } + /** * Displays a single Ticket model. diff --git a/backend/models/TicketSearch.php b/backend/models/TicketSearch.php index 19d5813..c71bbf5 100644 --- a/backend/models/TicketSearch.php +++ b/backend/models/TicketSearch.php @@ -6,20 +6,42 @@ use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use common\models\Ticket; +use common\components\Helper; +use yii\db\ActiveRecord; +use yii\helpers\ArrayHelper; /** * TicketSearch represents the model behind the search form about `common\models\Ticket`. */ class TicketSearch extends Ticket { + + public $timestampStart; + public $timestampEnd; + + public $users; + public $accounts; + public $ticketTypes; + + public $valid_in_interval; + public $created_in_interval; + public $expire_in_interval; + + public $statistics; + public $statisticsTotal; + + /** * @inheritdoc */ public function rules() { return [ - [['id_ticket', 'id_user', 'id_ticket_type', 'id_account', 'id_discount', 'max_usage_count', 'usage_count', 'status', 'price_brutto'], 'integer'], - [['start', 'end', 'comment', 'created_at', 'updated_at'], 'safe'], + [[ 'id_user', 'id_ticket_type', 'id_account'], 'integer'], + [[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], + [[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ], + [['valid_in_interval','created_in_interval','expire_in_interval'],'boolean'] , + ]; } @@ -31,7 +53,29 @@ class TicketSearch extends Ticket // bypass scenarios() implementation in the parent class return Model::scenarios(); } + + public function attributeLabels(){ + return ArrayHelper::merge(parent::attributeLabels(), [ + 'start' => Yii::t('backend/ticket','Start of interval'), + 'end' => Yii::t('backend/ticket','End of interval'), + 'valid_in_interval' => Yii::t('backend/ticket','Valid in interval'), + 'created_in_interval' => Yii::t('backend/ticket','Created in interval'), + 'expire_in_interval' => Yii::t('backend/ticket','Expire in interval'), + ]); + } + public function afterValidate(){ + + if ( !isset($this->timestampStart)) { + $this->timestampStart ='1900-01-01'; + } + + if ( !isset($this->timestampEnd)) { + $this->timestampEnd ='3000-01-01'; + } + + } + /** * Creates data provider instance with search query applied * @@ -43,36 +87,137 @@ class TicketSearch extends Ticket { $query = Ticket::find(); + Helper::queryAccountConstraint($query, 'ticket.id_account'); + + $query->with('ticketType' ); + $query->with('user'); + $query->with('customer'); + + + $dataProvider = new ActiveDataProvider([ 'query' => $query, + 'sort' =>[ + 'defaultOrder' => ['end' => SORT_DESC], + 'attributes' => ['end'] + ] ]); $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'); - return $dataProvider; + $query->where('0=1'); + return $query; } + $query->andFilterWhere([ - 'id_ticket' => $this->id_ticket, 'id_user' => $this->id_user, 'id_ticket_type' => $this->id_ticket_type, 'id_account' => $this->id_account, - 'id_discount' => $this->id_discount, - 'start' => $this->start, - 'end' => $this->end, - 'max_usage_count' => $this->max_usage_count, - 'usage_count' => $this->usage_count, - 'status' => $this->status, - 'price_brutto' => $this->price_brutto, - 'created_at' => $this->created_at, - 'updated_at' => $this->updated_at, + 'id_card' => $this->id_card, ]); - $query->andFilterWhere(['like', 'comment', $this->comment]); + + + $all = (!($this->valid_in_interval) && !($this->expire_in_interval) && !($this->created_in_interval) ) + || + ($this->valid_in_interval == true && $this->expire_in_interval == true && $this->created_in_interval); + + $dateConditions = []; + $start = $this->timestampStart; + $end = $this->timestampEnd; + + if( $all || $this->created_in_interval ){ + $dateConditions[] = Helper::queryInIntervalRule('ticket.created_at', $start, $end); + } + + if ( $all || $this->valid_in_interval ){ + $dateConditions[] = Helper::queryValidRule('ticket.start', 'ticket.end', $start, $end); + } + + if ( $all || $this->expire_in_interval ){ + $dateConditions[] = Helper::queryExpireRule('ticket.start', 'ticket.end', $start, $end); + } + + if ( count($dateConditions) == 1 ){ + $query->andWhere($dateConditions[0]); + }else if ( count($dateConditions) > 1 ){ + $cond = ['or']; + foreach ($dateConditions as $c){ + $cond[] = $c; + } + $query->andWhere($cond); + } return $dataProvider; } + + + + public function searchTotals(){ + $query = Ticket::mkStatisticQuery($this->timestampStart, $this->timestampEnd,$this->id_card); + $this->statistics = $query->all(); + + $this->statisticsTotal =[ + 'valid' => 0, + 'created' => 0, + 'created_at_money' => 0, + 'expired' => 0, + ]; + + $this->statisticsTotal['valid'] = array_sum(array_column($this->statistics, 'valid')); + $this->statisticsTotal['created'] = array_sum(array_column($this->statistics, 'created')); + $this->statisticsTotal['created_money'] = array_sum(array_column($this->statistics, 'created_money')); + $this->statisticsTotal['expired'] = array_sum(array_column($this->statistics, 'expired')); + } + + + public static function mkSearchCondition( $timestampStart, $timestampEnd, $id_user,$id_ticket_tpye,$id_account,$valid_in_interval ,$expire_in_interval,$created_in_interval ){ + $query = Ticket::find(); + + Helper::queryAccountConstraint($query, 'ticket.id_account'); + + $query->with('ticketType' ); + $query->with('user'); + $query->with('customer'); + + $query->andFilterWhere([ + 'id_user' => $id_user, + 'id_ticket_type' => $id_ticket_type, + 'id_account' => $id_account, + ]); + + $all = (!($valid_in_interval) && !($expire_in_interval) && !($created_in_interval) ) + || + ($valid_in_interval == true && $expire_in_interval == true && $created_in_interval); + + $dateConditions = []; + $start = $timestampStart; + $end = $timestampEnd; + + if( $all || $created_in_interval ){ + $dateConditions[] = Helper::queryInIntervalRule('ticket.created_at', $start, $end); + } + + if ( $all || $valid_in_interval ){ + $dateConditions[] = Helper::queryValidRule('ticket.start', 'ticket.end', $start, $end); + } + + if ( $all || $expire_in_interval ){ + $dateConditions[] = Helper::queryExpireRule('ticket.start', 'ticket.end', $start, $end); + } + + if ( count($dateConditions) == 1 ){ + $query->andWhere($dateConditions[0]); + }else if ( count($dateConditions) > 1 ){ + $cond = ['or']; + foreach ($dateConditions as $c){ + $cond[] = $c; + } + $query->andWhere($cond); + } + } + } diff --git a/backend/views/customer/index.php b/backend/views/customer/index.php index db4c9e4..a4c7a25 100644 --- a/backend/views/customer/index.php +++ b/backend/views/customer/index.php @@ -2,6 +2,7 @@ use yii\helpers\Html; use yii\grid\GridView; +use yii\helpers\Url; /* @var $this yii\web\View */ /* @var $searchModel backend\models\CustomerSearch */ @@ -24,30 +25,31 @@ $this->params['breadcrumbs'][] = $this->title; 'columns' => [ [ 'attribute' => 'customerCardNumber' , -// 'value' => 'customerCardNumber' ], -// 'id_user', -// 'id_partner_card', -// 'id_proposer', 'name', 'email:email', - // 'password', 'phone', - // 'sex', - // 'date_stundent_card_expire', - // 'birthdate', - // 'image', - // 'description', - // 'tax_number', - // 'country', - // 'zip', - // 'city', - // 'address', 'created_at:datetime', - // 'updated_at', ['class' => 'yii\grid\ActionColumn', - 'template' =>'{view}{update}' + 'template' =>'{view} {update} {ticket/index-customer}', + 'buttons' => [ + 'ticket/index-customer' => function ($url) { return Html::a( ' ', $url, [ 'title' => 'Bérletek' ] ); } + ], + 'urlCreator' =>function ($action, $model, $key, $index){ + $params = is_array($key) ? $key : ['id' => (string) $key]; + if ( $action == 'ticket/index-customer' ){ + $today = \Yii::$app->formatter->asDate( strtotime('today UTC') ); + $tomorrow = \Yii::$app->formatter->asDate( strtotime( 'tomorrow UTC' )); + + $params['TicketSearch[start]'] = $today; + $params['TicketSearch[end]'] = $tomorrow; + + } + $params[0] = $action; + + return Url::toRoute($params); + } ], ], diff --git a/backend/views/ticket/_search.php b/backend/views/ticket/_search.php index c89f75f..04e7bf5 100644 --- a/backend/views/ticket/_search.php +++ b/backend/views/ticket/_search.php @@ -2,12 +2,21 @@ use yii\helpers\Html; use yii\widgets\ActiveForm; +use yii\helpers\ArrayHelper; +use kartik\widgets\DatePicker; /* @var $this yii\web\View */ /* @var $model backend\models\TicketSearch */ /* @var $form yii\widgets\ActiveForm */ ?> + 'Mind'] + ArrayHelper::map($model->ticketTypes, 'id_ticket_type', 'name'); +$accountOptions = ['' => 'Mind'] + ArrayHelper::map($model->accounts, 'id_account', 'name'); +$userOptions = ['' => 'Mind'] + ArrayHelper::map($model->users, 'id', 'username'); + +?> +
- = Html::a(Yii::t('common/ticket', 'Create Ticket'), ['create'], ['class' => 'btn btn-success']) ?> -
+ + +Bérlet statisztika a kiválasztott időszakra
+ new ArrayDataProvider([ + 'allModels' => $searchModel->statistics, + 'sort' => false, + 'pagination' => false, + ]), + 'showFooter'=>TRUE, + 'footerRowOptions'=>['style'=>'font-weight:bold; '], + 'columns' =>[ + [ + 'attribute' => 'name', + 'label' => 'Bérlet', + 'footer' => 'Összesen' + ], + [ + 'attribute' => 'created', + 'label' => 'Kiadva (Db)', + 'footer' => $searchModel->statisticsTotal['created'] + ], + [ + 'attribute' => 'created_money', + 'label' => 'Kiadott érték (Ft)', + 'footer' => $searchModel->statisticsTotal['created_money'] + ], + [ + 'attribute' => 'valid', + 'label' => 'Érvényes (Db)', + 'footer' => $searchModel->statisticsTotal['valid'] + + ], + [ + 'attribute' => 'expired', + 'label' => 'Lejár az adott időszakban (Db)', + 'footer' => $searchModel->statisticsTotal['expired'] + ] + + ] + ]); + + ?> +Bérlet statisztika a kiválasztott időszakra
+ new ArrayDataProvider([ + 'allModels' => $searchModel->statistics, + 'sort' => false, + 'pagination' => false, + ]), + 'showFooter'=>TRUE, + 'footerRowOptions'=>['style'=>'font-weight:bold; '], + 'columns' =>[ + [ + 'attribute' => 'name', + 'label' => 'Bérlet', + 'footer' => 'Összesen' + ], + [ + 'attribute' => 'created', + 'label' => 'Kiadva (Db)', + 'footer' => $searchModel->statisticsTotal['created'] + ], + [ + 'attribute' => 'created_money', + 'label' => 'Kiadott érték (Ft)', + 'footer' => $searchModel->statisticsTotal['created_money'] + ], + [ + 'attribute' => 'valid', + 'label' => 'Érvényes (Db)', + 'footer' => $searchModel->statisticsTotal['valid'] + + ], + [ + 'attribute' => 'expired', + 'label' => 'Lejár az adott időszakban (Db)', + 'footer' => $searchModel->statisticsTotal['expired'] + ] + + ] + ]); + + ?> +- = Html::a(Yii::t('common/ticket', 'Update'), ['update', 'id' => $model->id_ticket], ['class' => 'btn btn-primary']) ?> - = Html::a(Yii::t('common/ticket', 'Delete'), ['delete', 'id' => $model->id_ticket], [ - 'class' => 'btn btn-danger', - 'data' => [ - 'confirm' => Yii::t('common/ticket', 'Are you sure you want to delete this item?'), - 'method' => 'post', - ], - ]) ?> -
= DetailView::widget([ 'model' => $model, diff --git a/changelog.txt b/changelog.txt index b14d9e6..746add5 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,7 @@ +-0.0.3 + admin oldal bérlet statisztika + admin oldal módosított menü + admin oldal vendég bérlet történet -0.0.2 alpha verzió -0.0.1 diff --git a/common/components/Helper.php b/common/components/Helper.php index 639f67d..fa1199b 100644 --- a/common/components/Helper.php +++ b/common/components/Helper.php @@ -1,6 +1,8 @@ andFilterWhere([ '<' , $field , $end ] ); } + public static function queryInIntervalRule( $field , $start,$end ){ + return ['and',[ '>=', $field , $start ] , [ '<' , $field , $end ] ]; + } + + public static function queryExpireRule( $field_start,$field_end , $start,$end ){ + + return ['and' ,['<',$field_start, $end], ['>=' , $field_end , $start ], ['<=' , $field_end , $end ] ]; + } + + public static function queryValidRule( $field_start ,$field_end , $start,$end ){ + return ['and' ,['<',$field_start, $end], ['>=' , $field_end , $start ] ]; + } + + public static function sqlInIntervalRule( $field , $paramStart,$paramEnd ){ + return ' ' .$field . ' >= ' . $paramStart . ' and ' . $field . ' < ' . $paramEnd ; + } + + public static function sqlExpireRule( $field_start,$field_end , $paramStart,$paramEnd ){ + return ' ' .$field_start . ' < ' . $paramEnd . ' and ' . $field_end . ' < ' . $paramEnd ; + } + + public static function sqlValidRule( $field_start ,$field_end , $paramStart,$paramEnd ){ + return ' ' .$field_start . ' < ' . $paramEnd . ' and ' . $field_end . ' >=' . $paramStart ; + } + + public static function queryAccountConstraint($query,$field){ + if ( !RoleDefinition::isAdmin() ){ + $query->innerJoin("user_account_assignment", $field . ' = user_account_assignment.id_account' ); + $query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id ]); + } + } + + public static function roleLabels(){ return [ 'reception' => Yii::t('common/role' ,'Reception'), diff --git a/common/config/params.php b/common/config/params.php index 96cf988..a6e6005 100644 --- a/common/config/params.php +++ b/common/config/params.php @@ -3,5 +3,5 @@ return [ 'adminEmail' => 'rocho02@gmail.com', 'supportEmail' => 'rocho02@gmail.com', 'user.passwordResetTokenExpire' => 3600, - 'version' => 'v0.0.2' + 'version' => 'v0.0.3' ]; diff --git a/common/messages/hu/backend/ticket.php b/common/messages/hu/backend/ticket.php new file mode 100644 index 0000000..392eae7 --- /dev/null +++ b/common/messages/hu/backend/ticket.php @@ -0,0 +1,27 @@ + 'Bérlet kártya', + 'Customer' => 'Vendég', + 'Created in interval' => 'Kiadva az időszakban', + 'End of interval' => 'Időszak vége', + 'Expire in interval' => 'Lejár az időszakban', + 'Start of interval' => 'Idászak kezdete', + 'Valid in interval' => 'Érvényes az időszakban', +]; diff --git a/common/messages/hu/common/ticket.php b/common/messages/hu/common/ticket.php index c906fe4..0ce934b 100644 --- a/common/messages/hu/common/ticket.php +++ b/common/messages/hu/common/ticket.php @@ -17,13 +17,11 @@ * NOTE: this file must be saved in UTF-8 encoding. */ return [ - 'Reset' => 'Reset', - 'Are you sure you want to delete this item?' => 'Biztosan törölni szeretné a bérletet?', + '{customer} Tickets' => '{customer} bérlet történet', 'Comment' => 'Megjegyzés', 'Create' => 'Mentés', 'Create Ticket' => 'Új bérlet', 'Created At' => 'Hozzáadva', - 'Delete' => 'Törlés', 'End' => 'Érvényesség vége', 'Id Account' => 'Kassza', 'Id Discount' => 'Kedvezmény', @@ -32,6 +30,7 @@ return [ 'Id User' => 'Felhasználó', 'Max Usage Count' => 'Max alkalmak', 'Price Brutto' => 'Bruttó ár', + 'Reset' => 'Reset', 'Search' => 'Keresés', 'Start' => 'Érvényesség kezdete', 'Status' => 'Státusz', diff --git a/common/models/Ticket.php b/common/models/Ticket.php index 1f1152b..0079d76 100644 --- a/common/models/Ticket.php +++ b/common/models/Ticket.php @@ -4,6 +4,9 @@ namespace common\models; use Yii; use yii\db\ActiveRecord; +use yii\db\Query; +use yii\db\Expression; +use common\components\Helper; /** * This is the model class for table "ticket". @@ -13,6 +16,7 @@ use yii\db\ActiveRecord; * @property integer $id_ticket_type * @property integer $id_account * @property integer $id_discount + * @property integer $id_card * @property string $start * @property string $end * @property integer $max_usage_count @@ -69,6 +73,8 @@ class Ticket extends \common\models\BaseFitnessActiveRecord 'comment' => Yii::t('common/ticket', 'Comment'), 'created_at' => Yii::t('common/ticket', 'Created At'), 'updated_at' => Yii::t('common/ticket', 'Updated At'), + 'id_card' => Yii::t('backend/ticket','Card'), + 'id_customer' => Yii::t('backend/ticket','Customer'), ]; } @@ -76,6 +82,27 @@ class Ticket extends \common\models\BaseFitnessActiveRecord public function getCard(){ return $this->hasOne( Card::className(), ["id_card" =>"id_card" ] ); } + public function getCardNumber(){ + $result = ""; + $card = $this->card; + if ( isset($card)){ + $result = $card->number; + } + return $result; + } + + public function getCustomer(){ + return $this->hasOne( Customer::className(), ["id_customer_card" =>"id_card" ] )->via('card'); + } + + public function getCustomerName(){ + $result = ""; + $customer = $this->customer; + if ( isset($customer)){ + $result = $customer->name; + } + return $result; + } public function getUser(){ return $this->hasOne( User::className(), ["id" =>"id_user" ] ); @@ -148,4 +175,60 @@ class Ticket extends \common\models\BaseFitnessActiveRecord } + + public static function mkStatisticQuery($start,$end,$id_card = null){ +// $sql = "select +// ticket_type.name, +// count(ticket.id_ticket) as total , +// sum(case when ticket.end > '2015-11-05' and ticket.start <= '2015-11-05' then 1 else 0 end) as valid, +// sum(case when ticket.created_at < '2015-11-06' and ticket.created_at >= '2015-11-05' then 1 else 0 end) as created, +// sum(case when ticket.created_at < '2015-11-06' and ticket.created_at >= '2015-11-05' then transfer.money else 0 end) as created_money, +// sum(case when ticket.created_at < '2015-11-06' and ticket.created_at >= '2015-11-05' and transfer.paid_at is not null then transfer.money else 0 end) as created_money_paid, +// sum(case when ticket.created_at < '2015-11-06' and ticket.created_at >= '2015-11-05' and transfer.paid_at is null then transfer.money else 0 end) as created_money_not_paid, +// sum(case when ticket.created_at >= '2015-11-06' and ticket.created_at < '2015-11-05' and transfer.paid_at < '2015-11-06' and transfer.paid_at >= '2015-11-05' then transfer.money else 0 end) as dept_paid, +// sum(case when ticket.end < '2015-11-06' and ticket.created_at >= '2015-11-05' then 1 else 0 end) as expired +// from ticket_type +// inner join ticket on ticket.id_ticket_type = ticket_type.id_ticket_type +// inner join transfer on ticket.id_ticket = transfer.id_object and transfer.type = 20 +// group by ticket_type.name;"; + + $query = new Query(); + $query->addSelect( [ + new Expression( 'ticket_type.id_ticket_type as id'), + new Expression( 'ticket_type.name as name'), + new Expression( 'coalesce( count(ticket.id_ticket),0) as total'), + + new Expression( "coalesce( sum( case when ". Helper::sqlValidRule('ticket.start', 'ticket.end', ':start', ':end') . " then 1 else 0 end) , 0) as valid" ), //valid + new Expression( "coalesce( sum( case when ". Helper::sqlInIntervalRule('ticket.created_at', ':start', ':end') . " then 1 else 0 end) , 0) as created" ),//created + new Expression( "coalesce( sum( case when ". Helper::sqlInIntervalRule('ticket.created_at', ':start', ':end') . " then transfer.money else 0 end) , 0) as created_money" ),//created_money + new Expression( "coalesce( sum( case when " . Helper::sqlExpireRule('ticket.start', 'ticket.end', ':start', ":end") . " then 1 else 0 end) , 0) as expired" ), + + + ]); + + $query->from('ticket_type'); + $query->innerJoin('ticket', 'ticket.id_ticket_type = ticket_type.id_ticket_type'); + $query->innerJoin('transfer', 'ticket.id_ticket = transfer.id_object and transfer.type = '. Transfer::TYPE_TICKET ); + + Helper::queryAccountConstraint($query, 'ticket.id_account'); + + $query->andFilterWhere(['id_card' =>$id_card]); + + $query->andWhere( + [ + 'or', + Helper::queryInIntervalRule('ticket.created_at', $start, $end), + Helper::queryValidRule('ticket.start', 'ticket.end', $start, $end), + Helper::queryExpireRule('ticket.start', 'ticket.end', $start, $end), + ] + ); + + + $query->groupBy("ticket_type.id_ticket_type, ticket_type.name"); + + $query->params([':start' => $start, ':end' => $end]); + + return $query; + } + }