| Current Path : /var/www/html/crm/models/ |
| Current File : /var/www/html/crm/models/ProductSearch.php |
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Product;
/**
* ProductSearch represents the model behind the search form of `app\models\Product`.
*/
class ProductSearch extends Product {
public $searchstring;
public $searchbrand;
public $searchvendor;
public $searchcompany;
/**
* {@inheritdoc}
*/
public function rules() {
return [
[['id', 'parent_id', 'status', 'brand_id', 'cat_id', 'vendor_id', 'created_by', 'updated_by'], 'integer'],
[['uuid', 'name', 'uom', 'sku', 'cost', 'retail_price', 'selling_price', 'photo', 'keywords', 'descr', 'benefits', 'weblink', 'created_dt', 'updated_dt'], 'safe'],
[['searchstring', 'searchbrand', 'searchcompany', 'searchvendor'], 'safe'],
];
}
public function attributeLabels() {
return [
'searchstring' => 'Keyword',
'searchbrand' => 'Brand'
];
}
/**
* {@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 = Product::find()
->leftJoin('company c', 'product.com_id=c.id')
->innerJoin('vendor v', 'product.vendor_id=v.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', 'product.name', $this->searchstring])
->orFilterWhere(['like', 'product.sku', $this->searchstring])
->orFilterWhere(['like', 'product.keywords', $this->searchstring])
->orFilterWhere(['like', 'product.benefits', $this->searchstring])
->orFilterWhere(['like', 'product.descr', $this->searchstring]);
if (!empty($this->searchbrand)) {
$query->innerJoin('brand', 'product.brand_id=brand.id');
$query->orFilterWhere(['=', 'brand.uuid', $this->searchbrand]);
}
$query->andFilterWhere(['=', 'product.status', '1']);
$query->andFilterWhere(['=', 'c.uuid', $this->searchcompany]);
$query->andFilterWhere(['=', 'v.status', '1']);
$query->groupBy(['product.id']);
return $dataProvider;
}
public function overview($params) {
$query = Product::find()
->leftJoin('company c', 'product.com_id=c.id')
->innerJoin('vendor v', 'product.vendor_id=v.id');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10,]]);
$dataProvider->setSort(['defaultOrder' => ['name' => SORT_ASC]]);
$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 (!empty($this->searchbrand)) {
$query->innerJoin('brand', 'product.brand_id=brand.id');
$query->andFilterWhere(['IN', 'brand.uuid', $this->searchbrand]);
}
if (!empty($this->searchvendor)) {
$query->innerJoin('vendor', 'product.vendor_id=vendor.id');
$query->andFilterWhere(['IN', 'vendor.uuid', $this->searchvendor]);
}
// grid filtering conditions
$query->andFilterWhere(['like', 'product.name', $this->searchstring])
->orFilterWhere(['like', 'product.sku', $this->searchstring]);
$query->andFilterWhere(['=', 'product.status', '1']);
$query->andFilterWhere(['=', 'c.uuid', $this->searchcompany]);
$query->andFilterWhere(['=', 'v.status', '1']);
$query->groupBy(['product.id']);
return $dataProvider;
}
}