Add backend ticket changes

This commit is contained in:
rocho 2015-11-06 14:42:16 +01:00
parent de8991b88f
commit a6e34bff61
7 changed files with 254 additions and 23 deletions

View File

@ -27,6 +27,7 @@ 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();

View File

@ -8,6 +8,7 @@ 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`.
@ -26,6 +27,9 @@ class TicketSearch extends Ticket
public $created_in_interval;
public $expire_in_interval;
public $statistics;
public $statisticsTotal;
/**
* @inheritdoc
*/
@ -49,6 +53,28 @@ class TicketSearch extends Ticket
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
*
@ -64,6 +90,9 @@ class TicketSearch extends Ticket
$query->with('ticketType' );
$query->with('user');
$query->with('customer');
$dataProvider = new ActiveDataProvider([
'query' => $query,
@ -74,9 +103,10 @@ class TicketSearch extends Ticket
if (!$this->validate()) {
$query->where('0=1');
// return $dataProvider;
return $query;
}
$query->andFilterWhere([
'id_user' => $this->id_user,
'id_ticket_type' => $this->id_ticket_type,
@ -91,7 +121,6 @@ class TicketSearch extends Ticket
$dateConditions = [];
$start = $this->timestampStart;
$end = $this->timestampEnd;
Yii::info('valid all: ' .$all);
if( $all || $this->created_in_interval ){
$dateConditions[] = Helper::queryInIntervalRule('ticket.created_at', $start, $end);
@ -117,4 +146,71 @@ class TicketSearch extends Ticket
return $dataProvider;
}
public function searchTotals(){
$query = Ticket::mkStatisticQuery($this->timestampStart, $this->timestampEnd);
$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);
}
}
}

View File

@ -3,6 +3,7 @@
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\ArrayHelper;
use yii\data\ArrayDataProvider;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\TicketSearch */
@ -21,10 +22,71 @@ $this->params['breadcrumbs'][] = $this->title;
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<div class="panel panel-default">
<div class="panel-heading">Bérlet statisztika</div>
<div class="panel-body">
<p>Bérlet statisztika a kiválasztott időszakra</p>
<?php
echo GridView::widget([
'dataProvider' => 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']
]
]
]);
?>
</div>
</div>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'attribute' => 'id_customer',
'value' => 'customerName'
],
[
'attribute' => 'id_card',
'value' => 'cardNumber'
],
'start:date',
'end:date',
[
'attribute' => 'id_user',
'value' => 'userName'
@ -37,10 +99,8 @@ $this->params['breadcrumbs'][] = $this->title;
'attribute' => 'id_account',
'value' => 'accountName'
],
'start:date',
'end:date',
'max_usage_count',
'usage_count',
// 'max_usage_count',
// 'usage_count',
['class' => 'yii\grid\ActionColumn',
'template' => '{view}'

View File

@ -1,6 +1,8 @@
<?php
namespace common\components;
use \Yii;
class Helper
{
@ -29,13 +31,25 @@ class Helper
public static function queryExpireRule( $field_start,$field_end , $start,$end ){
return ['and' ,['<',$field_start, $end], ['>=' , $field_end , $start ], ['<' , $field_end , $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' );

View File

@ -0,0 +1,27 @@
<?php
/**
* Message translations.
*
* This file is automatically generated by 'yii message' command.
* It contains the localizable messages extracted from source code.
* You may modify this file by translating the extracted messages.
*
* Each array element represents the translation (value) of a message (key).
* If the value is empty, the message is considered as not translated.
* Messages that no longer need translation will have their translations
* enclosed between a pair of '@@' marks.
*
* Message string can be used with plural forms format. Check i18n section
* of the guide for details.
*
* NOTE: this file must be saved in UTF-8 encoding.
*/
return [
'Card' => 'Bérlet kártya',
'Customer' => 'Vendég',
'Created in interval' => 'Létrehozva 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',
];

View File

@ -17,13 +17,10 @@
* 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?',
'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 +29,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',

View File

@ -6,6 +6,7 @@ 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".
@ -71,6 +72,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'),
];
}
@ -78,6 +81,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" ] );
@ -151,7 +175,7 @@ class Ticket extends \common\models\BaseFitnessActiveRecord
}
public static function statistic($start,$end){
public static function mkStatisticQuery($start,$end){
// $sql = "select
// ticket_type.name,
// count(ticket.id_ticket) as total ,
@ -169,28 +193,39 @@ class Ticket extends \common\models\BaseFitnessActiveRecord
$query = new Query();
$query->addSelect( [
new Expression( 'ticket_type.id as id'),
new Expression( 'ticket_type.id_ticket_type as id'),
new Expression( 'ticket_type.name as name'),
new Expression( 'count(ticket.id_ticket) as total'),
new Expression( "sum( case when ticket.end > ':start' and ticket.start <= ':end' then 1 else 0 end) as valid" ),
new Expression( "sum(case when ticket.created_at < ':end' and ticket.created_at >= ':start' then 1 else 0 end) as created" ),
new Expression( "sum(case when ticket.created_at < ':end' and ticket.created_at >= ':start' then transfer.money else 0 end) as created_money" ),
new Expression( "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" ),
new Expression( "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" ),
new Expression( "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" ),
new Expression( "sum(case when ticket.end < '2015-11-06' and ticket.created_at >= '2015-11-05' then 1 else 0 end) as expired" ),
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 = 20');
$query->innerJoin('transfer', 'ticket.id_ticket = transfer.id_object and transfer.type = '. Transfer::TYPE_TICKET );
$query->groupBy("ticket_type.id_ticket, ticket_type.name");
Helper::queryAccountConstraint($query, 'ticket.id_account');
$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;
}
}