<?php

namespace backend\modules\administrator\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Cust;

/**
 * CustSearch represents the model behind the search form of `common\models\Cust`.
 */
class CustSearch extends Cust
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['CustID', 'CustTitle', 'CustName', 'CustAddr1', 'CustAddr2', 'ProvID', 'CustPost', 'CustTel', 'CustFax', 'CustContact', 'UserOffHour', 'CustTID', 'ZoneID', 'CustLabTimeAuto', 'CustLabTime', 'CustBillDue', 'CustChqDue', 'CustCond1', 'CustCond2', 'UserIDAppr', 'UserIDEntry', 'UserDate', 'CustInvDescHeader', 'CustPwd', 'CustAbbr', 'CustComment'], 'safe'],
            [['CustDepSts', 'CustSpecSts', 'CustLabSts', 'CustLabQty', 'CustResultSts', 'CustPaytSts', 'CustInvEditFlag', 'CustLabSpPriceFlag', 'CustPrintHeadFlag'], 'integer'],
            [['CustCrLine', 'CustCrDay', 'CustUseAmt', 'CustBalAmt', 'CustNorDiscSpec', 'CustNorDiscRout', 'CustSpecDiscSpec', 'CustSpecDiscRout'], 'number'],
        ];
    }

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

        // add conditions that should always apply here

        $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;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'CustDepSts' => $this->CustDepSts,
            'CustSpecSts' => $this->CustSpecSts,
            'CustLabSts' => $this->CustLabSts,
            'CustLabTimeAuto' => $this->CustLabTimeAuto,
            'CustLabQty' => $this->CustLabQty,
            'CustResultSts' => $this->CustResultSts,
            'CustPaytSts' => $this->CustPaytSts,
            'CustCrLine' => $this->CustCrLine,
            'CustCrDay' => $this->CustCrDay,
            'CustUseAmt' => $this->CustUseAmt,
            'CustBalAmt' => $this->CustBalAmt,
            'CustNorDiscSpec' => $this->CustNorDiscSpec,
            'CustNorDiscRout' => $this->CustNorDiscRout,
            'CustSpecDiscSpec' => $this->CustSpecDiscSpec,
            'CustSpecDiscRout' => $this->CustSpecDiscRout,
            'UserDate' => $this->UserDate,
            'CustInvEditFlag' => $this->CustInvEditFlag,
            'CustLabSpPriceFlag' => $this->CustLabSpPriceFlag,
            'CustPrintHeadFlag' => $this->CustPrintHeadFlag,
        ]);

        $query->andFilterWhere(['like', 'CustID', $this->CustID])
            ->andFilterWhere(['like', 'CustTitle', $this->CustTitle])
            ->andFilterWhere(['like', 'CustName', $this->CustName])
            ->andFilterWhere(['like', 'CustAddr1', $this->CustAddr1])
            ->andFilterWhere(['like', 'CustAddr2', $this->CustAddr2])
            ->andFilterWhere(['like', 'ProvID', $this->ProvID])
            ->andFilterWhere(['like', 'CustPost', $this->CustPost])
            ->andFilterWhere(['like', 'CustTel', $this->CustTel])
            ->andFilterWhere(['like', 'CustFax', $this->CustFax])
            ->andFilterWhere(['like', 'CustContact', $this->CustContact])
            ->andFilterWhere(['like', 'UserOffHour', $this->UserOffHour])
            ->andFilterWhere(['like', 'CustTID', $this->CustTID])
            ->andFilterWhere(['like', 'ZoneID', $this->ZoneID])
            ->andFilterWhere(['like', 'CustLabTime', $this->CustLabTime])
            ->andFilterWhere(['like', 'CustBillDue', $this->CustBillDue])
            ->andFilterWhere(['like', 'CustChqDue', $this->CustChqDue])
            ->andFilterWhere(['like', 'CustCond1', $this->CustCond1])
            ->andFilterWhere(['like', 'CustCond2', $this->CustCond2])
            ->andFilterWhere(['like', 'UserIDAppr', $this->UserIDAppr])
            ->andFilterWhere(['like', 'UserIDEntry', $this->UserIDEntry])
            ->andFilterWhere(['like', 'CustInvDescHeader', $this->CustInvDescHeader])
            ->andFilterWhere(['like', 'CustPwd', $this->CustPwd])
            ->andFilterWhere(['like', 'CustAbbr', $this->CustAbbr])
            ->andFilterWhere(['like', 'CustComment', $this->CustComment]);

        return $dataProvider;
    }
}
