121 lines
3.3 KiB
PHP
121 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace backend\models;
|
|
|
|
use Yii;
|
|
use yii\base\Model;
|
|
use yii\data\ActiveDataProvider;
|
|
use common\models\Ticket;
|
|
use common\components\Helper;
|
|
use yii\db\ActiveRecord;
|
|
|
|
/**
|
|
* 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;
|
|
|
|
/**
|
|
* @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', ], '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']
|
|
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
public function scenarios()
|
|
{
|
|
// bypass scenarios() implementation in the parent class
|
|
return Model::scenarios();
|
|
}
|
|
|
|
/**
|
|
* Creates data provider instance with search query applied
|
|
*
|
|
* @param array $params
|
|
*
|
|
* @return ActiveDataProvider
|
|
*/
|
|
public function search($params)
|
|
{
|
|
$query = Ticket::find();
|
|
|
|
Helper::queryAccountConstraint($query, 'ticket.id_account');
|
|
|
|
$query->with('ticketType' );
|
|
$query->with('user');
|
|
|
|
$dataProvider = new ActiveDataProvider([
|
|
'query' => $query,
|
|
]);
|
|
|
|
$this->load($params);
|
|
|
|
|
|
if (!$this->validate()) {
|
|
$query->where('0=1');
|
|
// return $dataProvider;
|
|
}
|
|
|
|
$query->andFilterWhere([
|
|
'id_user' => $this->id_user,
|
|
'id_ticket_type' => $this->id_ticket_type,
|
|
'id_account' => $this->id_account,
|
|
]);
|
|
|
|
|
|
$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;
|
|
Yii::info('valid all: ' .$all);
|
|
|
|
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;
|
|
}
|
|
}
|