OSDN Git Service

Модификация импорта. Подключение модуля phpoffice/phpexcel
[invent/invent.git] / models / ItemsSearch.php
1 <?php
2
3 namespace app\models;
4
5 use yii\base\Model;
6 use yii\data\ActiveDataProvider;
7 use app\models\Items;
8 use app\models\Status;
9 use app\models\Types;
10 use app\models\Locations;
11 use app\models\Regions;
12 use app\models\Moving;
13
14 /**
15  * ItemsSearch represents the model behind the search form of `app\models\Items`.
16  */
17 class ItemsSearch extends Items
18 {
19     /**
20      * {@inheritdoc}
21      */
22     public function rules()
23     {
24         return [
25             [['id', 'type_id'], 'integer'],
26             [['name', 'model', 'os', 'mac', 'serial', 'product', 'modelnumber', 'invent', 'date', 'comment', 'statusName', 'typeName', 'locationName', 'regionName'], 'safe'],
27         ];
28     }
29
30     /**
31      * {@inheritdoc}
32      */
33     public function scenarios()
34     {
35         // bypass scenarios() implementation in the parent class
36         return Model::scenarios();
37     }
38
39     public function noinvent($params)
40     {
41
42         $query = Moving::find()
43             ->select('MAX(' . Moving::tableName() . '.id) AS mid');
44         if (isset($params->region) && ($params->region != ''))
45         {
46             $query->joinWith([ 'locations' ])
47                 ->where([ 'region_id' => $params->region ]);
48         }
49         if (isset($params->location) && ($params->location != ''))
50         {
51             if (isset($params->region) && ($params->location != ''))
52             {
53                 $query->andWhere(['location_id' => $params->location]);
54             }
55             else
56             {
57                 $query->where(['location_id' => $params->location]);
58             }
59         }
60         $query->distinct('item_id')->groupBy('item_id');
61
62         $query = Items::find()
63             ->select(Items::tableName() . '.*, ' .
64                 Locations::tableName() .  '.name AS locationName, ' .
65                 Types::tableName() .      '.name AS typeName, ' .
66                 Regions::tableName() .    '.name AS regionName, ' .
67                 Status::tableName() .     '.name AS statusName ')
68             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions' ])
69             ->where([ 'in', Moving::tableName() . '.id', $query ])
70             ->andWhere([ 'checked' => false ]);
71
72         $dataProvider = new ActiveDataProvider([
73             'query' => $query,
74         ]);
75         $dataProvider->setSort([
76             'defaultOrder' => [
77                 'id' => SORT_ASC,
78             ],
79         ]);
80         $dataProvider->sort->attributes['statusName'] = [
81             'asc'  => [Status::tableName() . '.name' => SORT_ASC],
82             'desc' => [Status::tableName() . '.name' => SORT_DESC],
83         ];
84         $dataProvider->sort->attributes['typeName'] = [
85             'asc'  => [Types::tableName() . '.name' => SORT_ASC],
86             'desc' => [Types::tableName() . '.name' => SORT_DESC],
87         ];
88         $dataProvider->sort->attributes['locationName'] = [
89             'asc'  => [Locations::tableName() . '.name' => SORT_ASC],
90             'desc' => [Locations::tableName() . '.name' => SORT_DESC],
91         ];
92         $dataProvider->sort->attributes['regionName'] = [
93             'asc'  => [Regions::tableName() . '.name' => SORT_ASC],
94             'desc' => [Regions::tableName() . '.name' => SORT_DESC],
95         ];
96         return $dataProvider;
97     }
98
99     /**
100      * Creates data provider instance with search query applied
101      *
102      * @param array $params
103      *
104      * @return ActiveDataProvider
105      */
106     public function search($params)
107     {
108         // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
109         $subQuery = Moving::find()
110                 ->select('MAX(id) AS id')
111                 ->distinct('item_id')
112                 ->groupBy([ 'item_id' ]);
113
114         $query = Items::find()
115             ->select(Items::tableName() . '.*, ' .
116                 Locations::tableName() .  '.name AS locationName, ' .
117                 Types::tableName() .      '.name AS typeName, ' .
118                 Regions::tableName() .    '.name AS regionName, ' .
119                 Status::tableName() .     '.name AS statusName ')
120             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions' ])
121             ->where([ 'in', Moving::tableName() . '.id', $subQuery ]);
122
123         // add conditions that should always apply here
124
125         $dataProvider = new ActiveDataProvider([
126             'query' => $query,
127         ]);
128
129         $dataProvider->setSort([
130             'defaultOrder' => [
131                 'id' => SORT_ASC,
132             ],
133         ]);
134
135         $this->load($params);
136
137         if (!$this->validate())
138         {
139             // uncomment the following line if you do not want to return any records when validation fails
140             // $query->where('0=1');
141             return $dataProvider;
142         }
143
144         // grid filtering conditions
145         $query->andFilterWhere([
146             'id'   => $this->id,
147         ])->andFilterWhere([
148             'ilike', Status::tableName() .    '.name', $this->statusName
149         ])->andFilterWhere([
150             'ilike', Types::tableName() .     '.name', $this->typeName
151         ])->andFilterWhere([ 'OR', [
152             'ilike', Locations::tableName() . '.name', $this->regionName
153         ], [
154             'ilike', Regions::tableName() .   '.name', $this->regionName
155         ]])->andFilterWhere([ 'OR', [
156             'ilike', Locations::tableName() . '.name', $this->locationName
157         ], [
158             'ilike', Regions::tableName() .   '.name', $this->locationName
159         ]]);
160
161         $query->andFilterWhere(['ilike', 'name',        $this->name])
162             ->andFilterWhere(  ['ilike', 'model',       $this->model])
163             ->andFilterWhere(  ['ilike', 'os',          $this->os])
164             ->andFilterWhere(  ['ilike', 'mac',         $this->mac])
165             ->andFilterWhere(  ['ilike', 'serial',      $this->serial])
166             ->andFilterWhere(  ['ilike', 'product',     $this->product])
167             ->andFilterWhere(  ['ilike', 'modelnumber', $this->modelnumber])
168             ->andFilterWhere(  ['ilike', 'invent',      $this->invent])
169             ->andFilterWhere(  ['ilike', 'comment',     $this->comment]);
170
171         $dataProvider->sort->attributes['statusName'] = [
172             'asc'  => [Status::tableName() . '.name' => SORT_ASC],
173             'desc' => [Status::tableName() . '.name' => SORT_DESC],
174         ];
175         $dataProvider->sort->attributes['typeName'] = [
176             'asc'  => [Types::tableName() . '.name' => SORT_ASC],
177             'desc' => [Types::tableName() . '.name' => SORT_DESC],
178         ];
179         $dataProvider->sort->attributes['locationName'] = [
180             'asc'  => [Locations::tableName() . '.name' => SORT_ASC],
181             'desc' => [Locations::tableName() . '.name' => SORT_DESC],
182         ];
183         $dataProvider->sort->attributes['regionName'] = [
184             'asc'  => [Regions::tableName() . '.name' => SORT_ASC],
185             'desc' => [Regions::tableName() . '.name' => SORT_DESC],
186         ];
187
188         return $dataProvider;
189     }
190 }