TenderSearch.php 11 KB
<?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;

        public $info;

        /**
         * @inheritdoc
         */
        public function rules()
        {
            return [
                [
                    [
                        'specialization',
                        'budget_currency',
                        'contractual',
                    ],
                    'integer',
                ],
                [
                    [
                        'city',
                        'payment',
                        'info',
                    ],
                    'safe',
                ],
                [
                    [
                        'budget_from',
                        'budget_to',
                    ],
                    'number',
                    'min' => 0,
                ],
                [
                    [
                        'payment',
                    ],
                    'default',
                    'value' => Payment::find()
                                      ->asArray()
                                      ->column(),
                ],
                [
                    [
                        'contractual',
                    ],
                    'default',
                    'value' => 1,
                ],
                [
                    [
                        'budget_currency',
                    ],
                    'default',
                    'value' => 3,
                ],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                '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'),
            ];
        }

        /**
         * @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')
                            ->joinWith('projectPayments');

            $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,
            ])
                  ->andFilterWhere([
                      'like',
                      'LOWER(project.name)',
                      mb_strtolower($this->info),
                  ])
                  ->andWhere([
                      'project_payment.payment_id' => $this->payment,
                  ]);

            $currencies = Currency::find()
                                  ->select([
                                      'rate',
                                      'currency_id',
                                  ])
                                  ->asArray()
                                  ->indexBy('currency_id')
                                  ->column();

            if(empty( $this->contractual )) {
                $query->andWhere([
                    'not',
                    [ 'contractual' => 1 ],
                ]);
                if(!empty( $this->budget_from ) && !empty( $this->budget_to )) {
                    $query->andFilterWhere([
                        'between',
                        'total_budget',
                        $this->budget_from * $currencies[ $this->budget_currency ],
                        $this->budget_to * $currencies[ $this->budget_currency ],
                    ]);
                } elseif(!empty( $this->budget_from )) {
                    $query->andFilterWhere([
                        '>=',
                        'total_budget',
                        $this->budget_from * $currencies[ $this->budget_currency ],
                    ]);
                } elseif(!empty( $this->budget_to )) {
                    $query->andFilterWhere([
                        '<=',
                        'total_budget',
                        $this->budget_to * $currencies[ $this->budget_currency ],
                    ]);
                }
            } 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,
                        ],
                    ]);
                }
            }

            return $dataProvider;
        }
    }