Blame view

common/models/TenderSearch.php 12 KB
ad6304d6   Yarik   test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  <?php
  
      namespace common\models;
  
      use Yii;
      use yii\base\Model;
      use yii\data\ActiveDataProvider;
  
      /**
       * TenderSearch represents the model behind the search form about `common\models\UserInfo`.
       */
      class TenderSearch extends Project
      {
  
          public $specialization;
  
          public $city;
  
          public $budget_from;
  
          public $budget_to;
  
          public $budget_currency;
  
          public $contractual;
  
          public $payment;
  
93a7a3c1   Yarik   test
29
30
          public $info;
  
ad6304d6   Yarik   test
31
32
33
34
35
36
37
38
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
ad6304d6   Yarik   test
39
40
41
42
43
44
45
                          'budget_currency',
                          'contractual',
                      ],
                      'integer',
                  ],
                  [
                      [
39a34915   Yarik   Commit
46
                          'specialization',
ad6304d6   Yarik   test
47
48
                          'city',
                          'payment',
93a7a3c1   Yarik   test
49
                          'info',
ad6304d6   Yarik   test
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
                      ],
                      'safe',
                  ],
                  [
                      [
                          'budget_from',
                          'budget_to',
                      ],
                      'number',
                      'min' => 0,
                  ],
                  [
                      [
                          'payment',
                      ],
                      'default',
                      'value' => Payment::find()
                                        ->asArray()
                                        ->column(),
                  ],
                  [
                      [
be662d1f   Yarik   test
72
73
74
75
76
77
78
                          'contractual',
                      ],
                      'default',
                      'value' => 1,
                  ],
                  [
                      [
ad6304d6   Yarik   test
79
80
81
82
83
84
85
86
87
88
89
90
91
92
                          'budget_currency',
                      ],
                      'default',
                      'value' => 3,
                  ],
              ];
          }
  
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
1a3b0a63   Yarik   Commit
93
                  'specialization'  => Yii::t('app', 'specialization'),
06ec2844   Administrator   28.03.16
94
                  'budget_currency' => Yii::t('app', 'budget_currency'),
1a3b0a63   Yarik   Commit
95
96
97
98
99
                  'contractual'     => Yii::t('app', 'contractual'),
                  'city'            => Yii::t('app', 'city'),
                  'payment'         => Yii::t('app', 'payment'),
                  'budget_from'     => Yii::t('app', 'budget_from'),
                  'budget_to'       => Yii::t('app', 'budget_to'),
ad6304d6   Yarik   test
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
              ];
          }
  
          /**
           * @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 = Project::find()
                              ->joinWith('projectSpecializations')
1a3b0a63   Yarik   Commit
123
124
                              ->joinWith('projectPayments')
                              ->joinWith('user.companyInfo');
ad6304d6   Yarik   test
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  
              $dataProvider = new ActiveDataProvider([
                  'query' => $query,
              ]);
  
              /*$dataProvider->setSort([
                  'defaultOrder' => [
                      'name' => SORT_ASC,
                  ],
                  'attributes'   => [
                      'name'  => [
                          'asc'     => [
                              'company_info.name' => SORT_ASC,
                              'firstname'         => SORT_ASC,
                              'lastname'          => SORT_ASC,
                          ],
                          'desc'    => [
                              'company_info.name' => SORT_DESC,
                              'firstname'         => SORT_DESC,
                              'lastname'          => SORT_DESC,
                          ],
                          'default' => SORT_ASC,
                          'label'   => 'Название',
                      ],
                      'staff' => [
                          'asc'     => [
                              'company_info.staff' => SORT_ASC,
                          ],
                          'desc'    => [
                              'company_info.staff' => SORT_DESC,
                          ],
                          'default' => SORT_DESC,
                          'label'   => 'Количество сотрудников',
                      ],
                      'visit' => [
                          'asc'     => [
                              'user_info.date_visit' => SORT_ASC,
                          ],
                          'desc'    => [
                              'user_info.date_visit' => SORT_DESC,
                          ],
                          'default' => SORT_DESC,
                          'label'   => 'Последний визит',
                      ],
                      'city'  => [
                          'asc'     => [
                              'user_info.city' => SORT_ASC,
                          ],
                          'desc'    => [
                              'user_info.city' => SORT_DESC,
                          ],
                          'default' => SORT_ASC,
                          'label'   => 'Город',
                      ],
                  ],
              ]);*/
  
              $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->distinct(true);
  
              $query->andWhere([
                  '>=',
                  'date_end',
                  date('Y-m-d'),
              ]);
  
              $query->andFilterWhere([
                  'project_specialization.specialization_id' => $this->specialization,
                  'project_payment.payment_id'               => $this->payment,
                  'city'                                     => $this->city,
              ])
93a7a3c1   Yarik   test
203
                    ->andFilterWhere([
1a3b0a63   Yarik   Commit
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
                        'or',
                        [
                            'like',
                            'LOWER(project.name)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(project.description)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER("user".firstname)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER("user".lastname)',
                            mb_strtolower($this->info),
                        ],
                        [
                            'like',
                            'LOWER(company_info.name)',
                            mb_strtolower($this->info),
                        ],
93a7a3c1   Yarik   test
230
                    ])
ad6304d6   Yarik   test
231
232
233
234
                    ->andWhere([
                        'project_payment.payment_id' => $this->payment,
                    ]);
  
2261f70a   Yarik   test
235
236
237
238
239
240
241
242
243
244
              $currencies = Currency::find()
                                    ->select([
                                        'rate',
                                        'currency_id',
                                    ])
                                    ->asArray()
                                    ->indexBy('currency_id')
                                    ->column();
  
              if(empty( $this->contractual )) {
ad6304d6   Yarik   test
245
                  $query->andWhere([
2261f70a   Yarik   test
246
247
                      'not',
                      [ 'contractual' => 1 ],
ad6304d6   Yarik   test
248
                  ]);
2261f70a   Yarik   test
249
                  if(!empty( $this->budget_from ) && !empty( $this->budget_to )) {
ad6304d6   Yarik   test
250
251
252
253
254
255
                      $query->andFilterWhere([
                          'between',
                          'total_budget',
                          $this->budget_from * $currencies[ $this->budget_currency ],
                          $this->budget_to * $currencies[ $this->budget_currency ],
                      ]);
2261f70a   Yarik   test
256
                  } elseif(!empty( $this->budget_from )) {
ad6304d6   Yarik   test
257
258
259
260
261
                      $query->andFilterWhere([
                          '>=',
                          'total_budget',
                          $this->budget_from * $currencies[ $this->budget_currency ],
                      ]);
2261f70a   Yarik   test
262
                  } elseif(!empty( $this->budget_to )) {
ad6304d6   Yarik   test
263
264
265
266
267
268
                      $query->andFilterWhere([
                          '<=',
                          'total_budget',
                          $this->budget_to * $currencies[ $this->budget_currency ],
                      ]);
                  }
2261f70a   Yarik   test
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
              } else {
                  if(!empty( $this->budget_from ) && !empty( $this->budget_to )) {
                      $query->andWhere([
                          'or',
                          [
                              'and',
                              [
                                  'between',
                                  'total_budget',
                                  $this->budget_from * $currencies[ $this->budget_currency ],
                                  $this->budget_to * $currencies[ $this->budget_currency ],
                              ],
                              [
                                  'not',
                                  [
                                      'contractual' => 1,
                                  ],
                              ],
                          ],
                          [
                              'contractual' => 1,
                          ],
                      ]);
                  } elseif(!empty( $this->budget_from )) {
                      $query->andWhere([
                          'or',
                          [
                              'and',
                              [
                                  '>=',
                                  'total_budget',
                                  $this->budget_from * $currencies[ $this->budget_currency ],
                              ],
                              [
                                  'not',
                                  [
                                      'contractual' => 1,
                                  ],
                              ],
                          ],
                          [
                              'contractual' => 1,
                          ],
                      ]);
                  } elseif(!empty( $this->budget_to )) {
                      $query->andWhere([
                          'or',
                          [
                              'and',
                              [
                                  '<=',
                                  'total_budget',
                                  $this->budget_to * $currencies[ $this->budget_currency ],
                              ],
                              [
                                  'not',
                                  [
                                      'contractual' => 1,
                                  ],
                              ],
                          ],
                          [
                              'contractual' => 1,
                          ],
                      ]);
                  } else {
                      $query->andWhere([
                          'or',
                          [
                              'and',
                              [
                                  '>=',
                                  'total_budget',
                                  0,
                              ],
                              [
                                  'not',
                                  [
                                      'contractual' => 1,
                                  ],
                              ],
                          ],
                          [
                              'contractual' => 1,
                          ],
                      ]);
                  }
ad6304d6   Yarik   test
356
357
358
359
360
              }
  
              return $dataProvider;
          }
      }