Your IP : 216.73.216.79


Current Path : /var/www/html/crm/models/
Upload File :
Current File : /var/www/html/crm/models/StockDeliverySearch.php

<?php

namespace app\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\StockDelivery;

/**
 * StockDeliverySearch represents the model behind the search form of `app\models\StockDelivery`.
 */
class StockDeliverySearch extends StockDelivery {

    /**
     * {@inheritdoc}
     */
    public $keyword;
    public $status;
    public $receiver_search;
    public $do_search;

    public function rules() {
        return [
            [['id', 'do_id', 'created_by', 'updated_by'], 'integer'],
            [['status', 'keyword', 'customer_name', 'customer_phone', 'customer_email',
            'customer_address1', 'customer_address2', 'customer_address3', 'customer_postcode',
            'customer_city', 'customer_state', 'stock_details', 'courier', 'tracking_no', 'upload_file',
            'delivery_date', 'delivery_status', 'receive_date', 'receive_proof', 'remarks', 'created_dt', 'updated_dt', 'company_name', 'details_status', 'industry',
            'rider_id', 'rider_wages_status', 'rider_wages_amount', 'receiver_search', 'do_search', 'search_year'], '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 = StockDelivery::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10,]]);
        $dataProvider->setSort(['defaultOrder' => ['id' => SORT_DESC]]);

        $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(['OR',
//            ['like', 'company_name', $this->keyword],
//            ['like', 'customer_name', $this->keyword],
//            ['like', 'customer_address1', $this->keyword],
//            ['like', 'customer_address2', $this->keyword],
//            ['like', 'customer_address3', $this->keyword],
//            ['like', 'tracking_no', $this->keyword],
//            ['like', 'remarks', $this->keyword],
//        ]);

        $query->andFilterWhere(['LIKE', 'company_name', $this->company_name]);
        $query->andFilterWhere(['LIKE', 'customer_name', $this->customer_name]);
        $query->andFilterWhere(['=', 'delivery_status', $this->delivery_status]);
        $query->andFilterWhere(['=', 'courier', $this->courier]);
        $query->andFilterWhere(['LIKE', 'tracking_no', $this->tracking_no]);
        if (!empty($this->stock_details))
            $query->andFilterWhere(['LIKE', 'stock_details', '"product_id":"' . $this->stock_details . '"']);

        $query->andFilterWhere(['=', 'customer_postcode', $this->customer_postcode]);
        $query->andFilterWhere(['=', 'customer_state', $this->customer_state]);
        $query->andFilterWhere(['=', 'details_status', $this->details_status]);
        if (!empty($this->customer_address1)) {
            $query->andFilterWhere(['OR',
                ['LIKE', 'customer_address1', $this->customer_address1],
                ['LIKE', 'customer_address2', $this->customer_address1],
                ['LIKE', 'customer_address3', $this->customer_address1],
                ['LIKE', 'customer_city', $this->customer_address1],
            ]);
        }
        $query->andFilterWhere(['LIKE', 'industry', $this->industry]);
        if ($this->status == 'waiting') {
            $query->andFilterWhere(['IN', 'delivery_status', ['prepare', 'waiting']]);
        }

        if ($this->status == 'intransit') {
            $query->andFilterWhere(['IN', 'delivery_status', ['pickup', 'intransit']]);
        }

        if ($this->status == 'delivered') {
            $query->andFilterWhere(['=', 'delivery_status', 'delivered']);
        }
        
        if(!empty($this->search_year))
            $query->andFilterWhere(['=', 'YEAR(created_dt)', $this->search_year]);


        return $dataProvider;
    }

    public function searchforrider($params, $type) {

        $query = StockDelivery::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10,]]);
        $dataProvider->setSort(['defaultOrder' => ['id' => SORT_DESC]]);

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


        if ($type == 'total_job') {
            $query->andFilterWhere(['OR', ['=', 'rider_id', $this->rider_id], ['=', 'wages_rider_id', $this->rider_id]]);
        }

        if ($type == 'job_complete') {
            $query->andFilterWhere(['OR', ['=', 'rider_id', $this->rider_id], ['=', 'wages_rider_id', $this->rider_id]]);
            $query->andFilterWhere(['IN', 'delivery_status', ['delivered', 'undeliver']]);
        }
        if ($type == 'job_pending') {
            $query->andFilterWhere(['OR', ['=', 'rider_id', $this->rider_id], ['=', 'wages_rider_id', $this->rider_id]]);
            $query->andFilterWhere(['NOT IN', 'delivery_status', ['delivered', 'undeliver']]);
        }
        if ($type == 'job_undeliver') {
            $query->andFilterWhere(['OR', ['=', 'rider_id', $this->rider_id], ['=', 'wages_rider_id', $this->rider_id]]);
            $query->andFilterWhere(['=', 'delivery_status', 'undeliver']);
        }
        if ($type == 'wages_paid') {
            $query->andFilterWhere(['=', 'wages_rider_id', $this->rider_id]);
            $query->andFilterWhere(['=', 'rider_wages_status', 'paid']);
        }
        if ($type == 'wages_pending') {
            $query->andFilterWhere(['=', 'wages_rider_id', $this->rider_id]);
            $query->andFilterWhere(['=', 'rider_wages_status', 'pending']);
        }

        if (!empty($this->receiver_search)) {
            $query->andFilterWhere(['OR',
                ['LIKE', 'company_name', $this->receiver_search],
                ['LIKE', 'customer_name', $this->receiver_search],
                ['LIKE', 'customer_address1', $this->receiver_search],
                ['LIKE', 'customer_address2', $this->receiver_search],
                ['LIKE', 'customer_address3', $this->receiver_search],
                ['LIKE', 'customer_city', $this->receiver_search],
                ['LIKE', 'customer_postcode', $this->receiver_search],
            ]);
        }

        if (!empty($this->do_search)) {
            $query->innerJoin('delivery_order', 'delivery_order.id=stock_delivery.do_id');
            $query->andWhere(['LIKE', 'delivery_order.doc_no', $this->do_search]);
        }
        
        if(!empty($this->search_year))
            $query->andFilterWhere(['=', 'YEAR(created_dt)', $this->search_year]);

        return $dataProvider;
    }

}