OSDN Git Service

Корректировка формы проведения инвентаризации. Теперь при учёте оборудования, отображ...
[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', 'model_id'], 'integer'],
26             [['name', 'modelName', 'os', 'mac', 'serial', '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                 Models::tableName() . '.name AS modelName, ' .
66                 Types::tableName() .      '.name AS typeName, ' .
67                 Regions::tableName() .    '.name AS regionName, ' .
68                 Status::tableName() .     '.name AS statusName ')
69             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models', ])
70             ->where([ 'in', Moving::tableName() . '.id', $query ])
71             ->andWhere([ 'checked' => false ]);
72
73         $dataProvider = new ActiveDataProvider([
74             'query' => $query,
75         ]);
76         $dataProvider->setSort([
77             'defaultOrder' => [
78                 'id' => SORT_ASC,
79             ],
80         ]);
81         $dataProvider->sort->attributes['modelName'] = [
82             'asc'  => [ Models::tableName() . '.name' => SORT_ASC ],
83             'desc' => [ Models::tableName() . '.name' => SORT_DESC ],
84         ];
85         $dataProvider->sort->attributes['statusName'] = [
86             'asc'  => [ Status::tableName() . '.name' => SORT_ASC ],
87             'desc' => [ Status::tableName() . '.name' => SORT_DESC ],
88         ];
89         $dataProvider->sort->attributes['typeName'] = [
90             'asc'  => [ Types::tableName() . '.name' => SORT_ASC ],
91             'desc' => [ Types::tableName() . '.name' => SORT_DESC ],
92         ];
93         $dataProvider->sort->attributes['locationName'] = [
94             'asc'  => [ Locations::tableName() . '.name' => SORT_ASC ],
95             'desc' => [ Locations::tableName() . '.name' => SORT_DESC ],
96         ];
97         $dataProvider->sort->attributes['regionName'] = [
98             'asc'  => [ Regions::tableName() . '.name' => SORT_ASC ],
99             'desc' => [ Regions::tableName() . '.name' => SORT_DESC ],
100         ];
101         return $dataProvider;
102     }
103
104     /**
105      * Creates data provider instance with search query applied
106      *
107      * @param array $params
108      *
109      * @return ActiveDataProvider
110      */
111     public function search($params)
112     {
113         // Особенность postgresql - нет first и last, потому последнее перемещение всегда имеет наибольший номер
114         $subQuery = Moving::find()
115                 ->select('MAX(id) AS id')
116                 ->distinct('item_id')
117                 ->groupBy([ 'item_id' ]);
118
119         $query = Items::find()
120             ->select(Items::tableName() . '.*, ' .
121                 Locations::tableName() .  '.name AS locationName, ' .
122                 Models::tableName() . '.name AS modelName, ' .
123                 Types::tableName() .      '.name AS typeName, ' .
124                 Regions::tableName() .    '.name AS regionName, ' .
125                 Status::tableName() .     '.name AS statusName ')
126             ->joinWith([ 'types', 'moving', 'status', 'locations', 'regions', 'models', ])
127             ->where([ 'in', Moving::tableName() . '.id', $subQuery ]);
128
129         // add conditions that should always apply here
130
131         $dataProvider = new ActiveDataProvider([
132             'query' => $query,
133         ]);
134
135         $dataProvider->setSort([
136             'defaultOrder' => [
137                 'id' => SORT_ASC,
138             ],
139         ]);
140
141         $this->load($params);
142
143         if (!$this->validate())
144         {
145             // uncomment the following line if you do not want to return any records when validation fails
146             // $query->where('0=1');
147             return $dataProvider;
148         }
149
150         // grid filtering conditions
151         $query->andFilterWhere([
152             'id'   => $this->id,
153         ])->andFilterWhere([
154             'ilike', Status::tableName() .    '.name', $this->statusName
155         ])->andFilterWhere([
156             'ilike', Models::tableName() .    '.name', $this->modelName
157         ])->andFilterWhere([
158             'ilike', Types::tableName() .     '.name', $this->typeName
159         ])->andFilterWhere([ 'OR', [
160             'ilike', Locations::tableName() . '.name', $this->regionName
161         ], [
162             'ilike', Regions::tableName() .   '.name', $this->regionName
163         ]])->andFilterWhere([ 'OR', [
164             'ilike', Locations::tableName() . '.name', $this->locationName
165         ], [
166             'ilike', Regions::tableName() .   '.name', $this->locationName
167         ]]);
168
169         $query->andFilterWhere(['ilike', 'name',        $this->name])
170             ->andFilterWhere(  ['ilike', 'os',          $this->os])
171             ->andFilterWhere(  ['ilike', 'mac',         $this->mac])
172             ->andFilterWhere(  ['ilike', 'serial',      $this->serial])
173             ->andFilterWhere(  ['ilike', 'invent',      $this->invent])
174             ->andFilterWhere(  ['ilike', 'comment',     $this->comment]);
175
176         $dataProvider->sort->attributes['modelName'] = [
177             'asc'  => [Models::tableName() . '.name' => SORT_ASC],
178             'desc' => [Models::tableName() . '.name' => SORT_DESC],
179         ];
180         $dataProvider->sort->attributes['statusName'] = [
181             'asc'  => [Status::tableName() . '.name' => SORT_ASC],
182             'desc' => [Status::tableName() . '.name' => SORT_DESC],
183         ];
184         $dataProvider->sort->attributes['typeName'] = [
185             'asc'  => [Types::tableName() . '.name' => SORT_ASC],
186             'desc' => [Types::tableName() . '.name' => SORT_DESC],
187         ];
188         $dataProvider->sort->attributes['locationName'] = [
189             'asc'  => [Locations::tableName() . '.name' => SORT_ASC],
190             'desc' => [Locations::tableName() . '.name' => SORT_DESC],
191         ];
192         $dataProvider->sort->attributes['regionName'] = [
193             'asc'  => [Regions::tableName() . '.name' => SORT_ASC],
194             'desc' => [Regions::tableName() . '.name' => SORT_DESC],
195         ];
196
197         return $dataProvider;
198     }
199 }