Blame view

common/models/TenderSearch.php 10.8 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  <?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;
  
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'specialization',
                          'budget_currency',
                          'contractual',
                      ],
                      'integer',
                  ],
                  [
                      [
                          'city',
                          'payment',
                      ],
                      'safe',
                  ],
                  [
                      [
                          'budget_from',
                          'budget_to',
                      ],
                      'number',
                      'min' => 0,
                  ],
                  [
                      [
                          'payment',
                      ],
                      'default',
                      'value' => Payment::find()
                                        ->asArray()
                                        ->column(),
                  ],
                  [
                      [
be662d1f   Yarik   test
69
70
71
72
73
74
75
                          'contractual',
                      ],
                      'default',
                      'value' => 1,
                  ],
                  [
                      [
ad6304d6   Yarik   test
76
77
78
79
80
81
82
83
84
85
86
87
88
89
                          'budget_currency',
                      ],
                      'default',
                      'value' => 3,
                  ],
              ];
          }
  
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
06ec2844   Administrator   28.03.16
90
91
92
93
94
95
96
                  'specialization'  => Yii::t('app', 'specialization'),
                  'budget_currency' => Yii::t('app', 'budget_currency'),
                  '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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
              ];
          }
  
          /**
           * @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')
2f324895   Yarik   test
120
                              ->joinWith('projectPayments');
ad6304d6   Yarik   test
121
122
123
124
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,
              ])
                    ->andWhere([
                        'project_payment.payment_id' => $this->payment,
                    ]);
  
2261f70a   Yarik   test
203
204
205
206
207
208
209
210
211
212
              $currencies = Currency::find()
                                    ->select([
                                        'rate',
                                        'currency_id',
                                    ])
                                    ->asArray()
                                    ->indexBy('currency_id')
                                    ->column();
  
              if(empty( $this->contractual )) {
ad6304d6   Yarik   test
213
                  $query->andWhere([
2261f70a   Yarik   test
214
215
                      'not',
                      [ 'contractual' => 1 ],
ad6304d6   Yarik   test
216
                  ]);
2261f70a   Yarik   test
217
                  if(!empty( $this->budget_from ) && !empty( $this->budget_to )) {
ad6304d6   Yarik   test
218
219
220
221
222
223
                      $query->andFilterWhere([
                          'between',
                          'total_budget',
                          $this->budget_from * $currencies[ $this->budget_currency ],
                          $this->budget_to * $currencies[ $this->budget_currency ],
                      ]);
2261f70a   Yarik   test
224
                  } elseif(!empty( $this->budget_from )) {
ad6304d6   Yarik   test
225
226
227
228
229
                      $query->andFilterWhere([
                          '>=',
                          'total_budget',
                          $this->budget_from * $currencies[ $this->budget_currency ],
                      ]);
2261f70a   Yarik   test
230
                  } elseif(!empty( $this->budget_to )) {
ad6304d6   Yarik   test
231
232
233
234
235
236
                      $query->andFilterWhere([
                          '<=',
                          'total_budget',
                          $this->budget_to * $currencies[ $this->budget_currency ],
                      ]);
                  }
2261f70a   Yarik   test
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
              } 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
324
325
326
327
328
              }
  
              return $dataProvider;
          }
      }