<?php

namespace modules\activities\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use modules\activities\models\Activities;

/**
 * ActivitiesSearch represents the model behind the search form about `frontend\models\Activities`.
 */
class ActivitiesSearch extends Activities
{
   public $account;
   public $created_at_range;
   public $updated_at_range;
    /**
     * @inheritdoc
     */
    public function rules(): array {
        return [
            [['id', 'shiftlength', 'jobno'], 'integer'],
            [['welder', 'jobstatus', 'travelstarttime', 'starttime', 'finishtime', 'travelfinishtime', 'company', 'assistant', 'contact', 'contactno', 'location', 'createdby', 'createdon', 'noofwelds', 'contactemail', 'noofshifts', 'account', 'billingstatus'], '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 = Activities::find()
		->where(['activities.status' => ['active', null]]);
	
		$query->joinWith(['account']);
        $dataProvider = new ActiveDataProvider(['query' => $query,
			'sort' => [
			'defaultOrder' => [
				'jobno' => SORT_DESC,
				]
			],
        ]);
		
		$dataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.account_name' => SORT_ASC],
        'desc' => ['accounts.account_name' => SORT_DESC],
        ];
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
		
		if (Yii::$app->user->identity->role != 'Oadmin') {
        	$query->andFilterWhere(['!=', 'jobstatus', 'Hidden']);
		}
        $query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);

        $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['like', 'jobstatus', $this->jobstatus])
			->andFilterWhere(['like', 'billingstatus', $this->billingstatus])
            ->andFilterWhere(['like', 'activities.jobno', '%'.$this->jobno, false])
            ->andFilterWhere(['like', 'account_name', $this->account]);

        return $dataProvider;
    }
    
	public function deleted($params) {
        $query = Activities::find()
        ->where(['activities.status' => 'Deleted']);
		$query->joinWith(['account']);
        $dataProvider = new ActiveDataProvider(['query' => $query,
			'sort' => [
			'defaultOrder' => [
				'jobno' => SORT_DESC,
				]
			],
        ]);
		
		$dataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.Account_Name' => SORT_ASC],
        'desc' => ['accounts.Account_Name' => SORT_DESC],
        ];
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);

        $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['=', 'jobno', $this->jobno])
            ->andFilterWhere(['like', 'Account_Name', $this->account]);

        return $dataProvider;
    }
    
	public function completedsearch($params) {
		$query = Activities::find()
		->where(['activities.status' => ['active', null]])
		->andwhere(['jobstatus'=> "Completed"])
		->andwhere(['or', ['!=', 'billingstatus', 'Invoiced'], ['is', 'billingstatus', null]])
		->orderBy('jobno DESC');
		$query->joinWith(['account']);
	    $completedDataProvider = new ActiveDataProvider([
			'query' => $query,
			'sort' => [
				'defaultOrder' => [
					'jobno' => SORT_DESC,
				]
			],
		]);
		$completedDataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.Account_Name' => SORT_ASC],
        'desc' => ['accounts.Account_Name' => SORT_DESC],
		  ];
		$this->load($params);

		$query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
			]);
      $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['like', 'jobno', $this->jobno])
            ->andFilterWhere(['like', 'Account_Name', $this->account]);
		return $completedDataProvider;
	}
    
	public function Allabsencesearch($params) {
    $query = Activities::find()
        ->where(['activities.status' => ['active', null]])
		->andwhere(['in', 'jobstatus' , ['Annual Leave',  "Sickness"]])->orderBy('sdate DESC');
        
    $query->joinWith(['account']);
    $AllabsenceDataProvider = new ActiveDataProvider([
        'query' => $query,
        'sort' => [
        'defaultOrder' => [
            'jobno' => SORT_DESC,
        ]
    ],]);

		$AllabsenceDataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.account_name' => SORT_ASC],
        'desc' => ['accounts.account_name' => SORT_DESC],
        ];

     $this->load($params);
     
		$query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);
            			
      $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'jobstatus', $this->jobstatus])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['like', 'jobno', $this->jobno])
            ->andFilterWhere(['like', 'account_name', $this->account]);
    return $AllabsenceDataProvider;
    }
    
    //-----------------------------------------\\
    public function pendingsearch($params) {
    $query = Activities::find()
    ->where(['activities.status' => ['active', null]])
	->andwhere(['jobstatus'=> "Booked"])->orderBy('jobno DESC');
    $query->joinWith(['account']);
    $PendingDataProvider = new ActiveDataProvider([
     'query' => $query,
     'sort' => [
     'defaultOrder' => [
     'jobno' => SORT_DESC,
        ]
    ],]);
    
    $PendingDataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.Account_Name' => SORT_ASC],
        'desc' => ['accounts.Account_Name' => SORT_DESC],
        ];
        
     $this->load($params);

		$query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);
            			
      $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['like', 'jobno', $this->jobno])
            ->andFilterWhere(['like', 'Account_Name', $this->account]);
    return $PendingDataProvider;
    }
    //-----------------------------------------\\
    public function attendedsearch($params) {
    $query = Activities::find()
        ->where(['activities.status' => ['active', null]])  
		->andwhere(['jobstatus'=> "Attended"])
		->orderBy('jobno DESC');
		
    $attendedDataProvider = new ActiveDataProvider([
             'query' => $query,
     'sort' => [
     'defaultOrder' => [
     'jobno' => SORT_DESC,
        ]
    ],]);
    
    $attendedDataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.account_name' => SORT_ASC],
        'desc' => ['accounts.account_name' => SORT_DESC],
        ];
    
     $this->load($params);      
	 if(empty($this->account) == false){
			$query->joinWith(['account']);
		}
        
        
		$query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);
            			
      $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['like', 'jobno', $this->jobno])
            ->andFilterWhere(['like', 'account_name', $this->account]);
    return $attendedDataProvider;

    }
    //-----------------------------------------\\
    public function prebookedsearch($params) {
    $query = Activities::find()
        ->where(['activities.status' => ['active', null]])
		->andwhere(['jobstatus'=> "Pre-Booked"])->orderBy('jobno DESC');
 	$query->joinWith(['account']);
  	$prebookedDataProvider = new ActiveDataProvider([
                     'query' => $query,
     'sort' => [
     'defaultOrder' => [
     'jobno' => SORT_DESC,
        ]
    ],]);
    $prebookedDataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.Account_Name' => SORT_ASC],
        'desc' => ['accounts.Account_Name' => SORT_DESC],
        ];
     $this->load($params);      
        
		$query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);
            			
      $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['like', 'jobno', $this->jobno])
            ->andFilterWhere(['like', 'Account_Name', $this->account]);
    return $prebookedDataProvider;
    }

	public function Gensearch($param1, $param2, $params) {
    $query = Activities::find()->where(['activities.status' => ['active', null]])
        ->where(['jobstatus'=> $param1])
        ->andwhere(['billingstatus' => $param2])->orderBy('jobno DESC');
   $query->joinWith(['account']);
    $completedDataProvider = new ActiveDataProvider([
    'query' => $query,
        'sort' => [
        'defaultOrder' => [
            'jobno' => SORT_DESC,
        ]
    ],]);
		$completedDataProvider->sort->attributes['account'] = [
        // The tables are the ones our relation are configured to
        // in my case they are prefixed with "tbl_"
        'asc' => ['accounts.Account_Name' => SORT_ASC],
        'desc' => ['accounts.Account_Name' => SORT_DESC],
        ];
     $this->load($params);

		$query->andFilterWhere([
            'id' => $this->id,
            'shiftlength' => $this->shiftlength,
            'createdon' => $this->createdon,
        ]);
            			
      $query->andFilterWhere(['like', 'welder', $this->welder])
            ->andFilterWhere(['like', 'travelstarttime', $this->travelstarttime])
            ->andFilterWhere(['like', 'sdate', $this->sdate])
            ->andFilterWhere(['like', 'edate', $this->edate])
            ->andFilterWhere(['like', 'travelfinishtime', $this->travelfinishtime])
            ->andFilterWhere(['like', 'assistant', $this->assistant])
            ->andFilterWhere(['like', 'contact', $this->contact])
            ->andFilterWhere(['like', 'contactno', $this->contactno])
            ->andFilterWhere(['like', 'location', $this->location])
            ->andFilterWhere(['like', 'createdby', $this->createdby])
            ->andFilterWhere(['like', 'noofwelds', $this->noofwelds])
            ->andFilterWhere(['like', 'contactemail', $this->contactemail])
            ->andFilterWhere(['like', 'noofshifts', $this->noofshifts])
            ->andFilterWhere(['like', 'jobno', $this->jobno])
            ->andFilterWhere(['like', 'Account_Name', $this->account]);
    return $completedDataProvider;
    }
    }