Your IP : 216.73.216.79


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

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\StockIn;

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

    public $searchkeyword;
    public $searchproduct;
    public $searchvendor;
    public $searchcompany;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['id', 'vendor_id', 'product_id', 'quantity', 'created_by', 'updated_by'], 'integer'],
            [['uuid', 'do_no', 'do_file', 'expire_dt', 'location', 'remarks', 'created_dt', 'updated_dt'], 'safe'],
            [['searchkeyword', 'searchproduct', 'searchvendor','searchcompany'], 'safe'],
        ];
    }

    public function attributeLabels() {
        return [
            'searchkeyword' => 'Stock Keyword',
            'searchproduct' => 'Product Keyword',
            'searchvendor' => 'Vendor'
        ];
    }

    /**
     * {@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 = StockIn::find()
                ->leftJoin('company c', 'stock_in.company_id=c.id')->leftJoin('vendor', 'stock_in.vendor_id=vendor.id');

        // 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->orFilterWhere(['like', 'stock_in.do_no', $this->searchkeyword])
                ->orFilterWhere(['like', 'stock_in.remarks', $this->searchkeyword])
                ->orFilterWhere(['like', 'stock_in.location', $this->searchkeyword]);

        if (!empty($this->searchproduct)) {
            $query->innerJoin('product', 'stock_in.product_id=product.id');
            $query->orFilterWhere(['like', 'product.name', $this->searchproduct])
                ->orFilterWhere(['like', 'product.sku', $this->searchproduct])
                ->orFilterWhere(['like', 'product.keywords', $this->searchproduct])
                ->orFilterWhere(['like', 'product.descr', $this->searchproduct])
                ->orFilterWhere(['like', 'product.benefits', $this->searchproduct]);
        }

        if (!empty($this->searchvendor)) {
            //$query->innerJoin('vendor', 'stock_in.vendor_id=vendor.id');
            $query->andFilterWhere(['=', 'vendor.uuid', $this->searchvendor]);
        }
        $query->andFilterWhere(['=', 'c.uuid', $this->searchcompany]);
        $query->andWhere(['=', 'vendor.status', '1']);
        $query->groupBy(['stock_in.id']);

        return $dataProvider;
    }

}