235 lines
8.0 KiB
PHP
235 lines
8.0 KiB
PHP
<?php
|
|
|
|
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".
|
|
*
|
|
* @property integer $id_ticket
|
|
* @property integer $id_user
|
|
* @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
|
|
* @property integer $usage_count
|
|
* @property integer $status
|
|
* @property integer $price_brutto
|
|
* @property string $comment
|
|
* @property string $created_at
|
|
* @property string $updated_at
|
|
*/
|
|
class Ticket extends \common\models\BaseFitnessActiveRecord
|
|
{
|
|
const STATUS_DELETED = 0;
|
|
const STATUS_ACTIVE = 10;
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public static function tableName()
|
|
{
|
|
return 'ticket';
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['id_user', 'id_ticket_type', 'id_account', 'id_discount', 'max_usage_count', 'usage_count', 'status', 'price_brutto'], 'integer'],
|
|
[['start', 'end', 'created_at', 'updated_at'], 'safe'],
|
|
[['comment'], 'required'],
|
|
[['comment'], 'string', 'max' => 255]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function attributeLabels()
|
|
{
|
|
return [
|
|
'id_ticket' => Yii::t('common/ticket', 'Id Ticket'),
|
|
'id_user' => Yii::t('common/ticket', 'Id User'),
|
|
'id_ticket_type' => Yii::t('common/ticket', 'Id Ticket Type'),
|
|
'id_account' => Yii::t('common/ticket', 'Id Account'),
|
|
'id_discount' => Yii::t('common/ticket', 'Id Discount'),
|
|
'start' => Yii::t('common/ticket', 'Start'),
|
|
'end' => Yii::t('common/ticket', 'End'),
|
|
'max_usage_count' => Yii::t('common/ticket', 'Max Usage Count'),
|
|
'usage_count' => Yii::t('common/ticket', 'Usage Count'),
|
|
'status' => Yii::t('common/ticket', 'Status'),
|
|
'price_brutto' => Yii::t('common/ticket', 'Price Brutto'),
|
|
'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'),
|
|
];
|
|
}
|
|
|
|
|
|
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" ] );
|
|
}
|
|
|
|
public function getTicketType(){
|
|
return $this->hasOne( TicketType::className(), ["id_ticket_type" =>"id_ticket_type" ] );
|
|
}
|
|
public function getDiscount(){
|
|
return $this->hasOne( Discount::className(), ["id_discount" =>"id_discount" ] );
|
|
}
|
|
|
|
public function getAccount() {
|
|
return $this->hasOne ( Account::className (), [
|
|
'id_account' => 'id_account'
|
|
] );
|
|
}
|
|
public function getAccountName() {
|
|
$result = "";
|
|
$account = $this->account;
|
|
if ( isset($account) ){
|
|
$result = $this->account->name;
|
|
}
|
|
return $result;
|
|
}
|
|
public function getDiscountName() {
|
|
$result = "";
|
|
$discount = $this->discount;
|
|
if ( isset($discount) ){
|
|
$result = $this->discount->name;
|
|
}
|
|
return $result;
|
|
}
|
|
public function getTicketTypeName() {
|
|
$result = "";
|
|
$ticketType = $this->ticketType;
|
|
if ( isset($ticketType) ){
|
|
$result = $ticketType->name;
|
|
}
|
|
return $result;
|
|
}
|
|
public function getUserName() {
|
|
$result = "";
|
|
$user = $this->user;
|
|
if ( isset($user) ){
|
|
$result = $user->username;
|
|
}
|
|
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] );
|
|
$query->orderBy([ "created_at" =>SORT_DESC] );
|
|
$result = $query->all();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
}
|