<?php

namespace frontend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Transfer;

/**
 * TransferSearch represents the model behind the search form about `common\models\Transfer`.
 */
class TransferMoneyMovementSearch extends Transfer
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id_transfer', 'id_discount', 'id_currency', 'id_object', 'status', 'type', 'item_price', 'count', 'money', 'money_currency', 'rate', 'id_user'], 'integer'],
            [['comment', 'created_at', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @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 = Transfer::find();

        $query->andWhere(['type' => Transfer::TYPE_MONEY_MOVEMENT_OUT]);
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $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->andFilterWhere([
            'id_transfer' => $this->id_transfer,
            'id_discount' => $this->id_discount,
            'id_currency' => $this->id_currency,
            'id_object' => $this->id_object,
            'status' => $this->status,
            'type' => $this->type,
            'item_price' => $this->item_price,
            'count' => $this->count,
            'money' => $this->money,
            'money_currency' => $this->money_currency,
            'rate' => $this->rate,
            'id_user' => $this->id_user,
            'created_at' => $this->created_at,
        ]);

        $query->andFilterWhere(['like', 'comment', $this->comment]);

        return $dataProvider;
    }
}
