fitness-web/backend/models/CollectionSearch.php

93 lines
2.5 KiB
PHP

<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Collection;
use common\components\Helper;
use common\components\RoleDefinition;
/**
* CollectionSearch represents the model behind the search form about `common\models\Collection`.
*/
class CollectionSearch extends Collection
{
public $accounts;
public $accountMap;
public $users;
public $timestampStart;
public $timestampEnd;
public $totals;
/**
* @inheritdoc
*/
public function rules()
{
return [
[[ 'start', ], 'date', 'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[[ 'end' , ], 'date' ,'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[['id_account','id_user'],'integer']
];
}
/**
* @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 = Collection::find();
if ( !RoleDefinition::isAdmin() ){
$query->innerJoin("user_account_assignment",'collection.id_account = user_account_assignment.id_account' );
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id ]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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->andFilterWhere([
'collection.id_user' => $this->id_user,
'collection.id_account' => $this->id_account,
]);
Helper::inInterval($query, 'collection.end', $this->timestampStart, $this->timestampEnd);
return $dataProvider;
}
public function searchTotal(){
$this->totals = Collection::mkReceptionTotal($this->timestampStart, $this->timestampEnd , Yii::$app->user->id, [Collection::TYPE_RECEPTION], $this->id_account, $this->accounts, $this->accountMap);
}
}