<?php

namespace frontend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\MoneyMovement;
use yii\helpers\ArrayHelper;
use common\components\Helper;

/**
 * MoneyMovementSearch represents the model behind the search form about `common\models\MoneyMovement`.
 */
class MoneyMovementSearch extends MoneyMovement
{
	
	
	public $start;
	public $end;
	
	public $timestampStart;
	public $timestampEnd;
	
    /**
     * @inheritdoc
     */
    public function rules()
    {	
        return [
            [[ 'start',  ], 'date', 'format' =>Yii::$app->formatter->datetimeFormat  , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
            [[ 'end' , ], 'date'  ,'format' =>Yii::$app->formatter->datetimeFormat  , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],            
        ];
    }

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

        $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([ '>=', 'money_movement.created_at', $this->timestampStart ]   );
        $query->andFilterWhere([ '<', 'money_movement.created_at', $this->timestampEnd ]   );
        
        Helper::restrictIfNotAdminTheStartDate($query, $this->timestampStart,['money_movement.created_at']);

        return $dataProvider;
    }
    
    public function attributeLabels( ) {
    	return ArrayHelper::merge(parent::attributeLabels(), 
    			[
    					'start' => Yii::t('frontend/money-movement', 'Start'),
    					'end' => Yii::t('frontend/money-movement', 'End'),
    			]
    			);
    }
}
