'type','color','sort','size',
'date_from','date_to',
], 'safe');
+ $filter->addRule(['onlyActive','onlyInactive'], 'boolean');
$filter->load(Yii::$app->request->get());
'pagination' => ['pageSize' => 50],
'sort' => ['defaultOrder'=>['name'=>SORT_ASC]],
]);
-
-
+ $attrMap = [
+ 'type' => ['type', 'тип'],
+ 'color' => ['цвет', 'color'],
+ 'sort' => ['sort', 'сорт'],
+ 'size' => ['size', 'размер'],
+ ];
$filtersUsed = array_filter([
$filter->category,
$filter->subcategory,
$filter->size,
$filter->date_from,
$filter->date_to,
+ $filter->onlyActive,
+ $filter->onlyInactive
]);
if ($filtersUsed) {
$query = Products1cNomenclature::find()->alias('n');
-
- foreach (['category','subcategory','species'] as $attr) {
+ foreach (['category', 'subcategory', 'species'] as $attr) {
if ($filter->$attr) {
$query->andWhere(["n.$attr" => $filter->$attr]);
}
}
-
- if ($filter->date_from || $filter->date_to) {
- $query->innerJoin(
- Products1cNomenclatureActuality::tableName().' a',
- 'a.guid = n.id AND a.active = 1'
- );
-
- if ($filter->date_from && $filter->date_to) {
- $dfObj = \DateTime::createFromFormat('Y-m', $filter->date_from);
- $dtObj = \DateTime::createFromFormat('Y-m', $filter->date_to);
- $dfObj->setTime(0,0,0);
- $dtObj->modify('last day of this month')->setTime(23,59,59);
-
- $query->andWhere(['<=', 'a.date_from', $dtObj->format('Y-m-d H:i:s')])
- ->andWhere(['>=', 'a.date_to', $dfObj->format('Y-m-d H:i:s')]);
- }
-
- elseif ($filter->date_from) {
- $df = \DateTime::createFromFormat('Y-m', $filter->date_from)
- ->format('Y-m-01 00:00:00');
- $query->andWhere(['>=', 'a.date_to', $df]);
- }
-
- elseif ($filter->date_to) {
- $dt = \DateTime::createFromFormat('Y-m', $filter->date_to);
- $dt->modify('last day of this month')->setTime(23,59,59);
- $query->andWhere(['<=', 'a.date_from', $dt->format('Y-m-d H:i:s')]);
- }
- }
-
-
-
- $attrMap = [
- 'type' => ['type','тип'],
- 'color' => ['цвет','color'],
- 'sort' => ['sort','сорт'],
- 'size' => ['size','размер'],
- ];
- foreach (['type','color','sort','size'] as $attr) {
+ foreach (['type', 'color', 'sort', 'size'] as $attr) {
if ($filter->$attr) {
-
$propIds = Products1cPropType::find()
->select('id')
->andWhere(['name' => $attrMap[$attr]])
->column();
-
- $productIds = Products1cAdditionalCharacteristics::find()
+ $ids = Products1cAdditionalCharacteristics::find()
->select('product_id')
->distinct()
- ->andWhere(['property_id' => $propIds, 'value' => $filter->$attr])
+ ->where(['property_id' => $propIds, 'value' => $filter->$attr])
->column();
-
-
- if (empty($productIds)) {
+ if (empty($ids)) {
+ // если нет подходящих свойств — сразу пустая выборка
$query->andWhere('0=1');
break;
}
+ $query->andWhere(['n.id' => $ids]);
+ }
+ }
- $query->andWhere(['n.id' => $productIds]);
+ $needJoin = $filter->onlyActive || $filter->onlyInactive || $filter->date_from || $filter->date_to;
+ if ($needJoin) {
+ $query->innerJoin(
+ Products1cNomenclatureActuality::tableName() . ' a',
+ 'a.guid = n.id'
+ );
+
+ if ($filter->onlyActive) {
+ $query->andWhere(['a.active' => 1]);
+ } elseif ($filter->onlyInactive) {
+ $query->andWhere(['a.active' => 0]);
+ }
+
+ if ($filter->date_from || $filter->date_to) {
+ if (!$filter->onlyActive && !$filter->onlyInactive) {
+ $query->andWhere(['a.active' => 1]);
+ }
+
+ if ($filter->date_from && $filter->date_to) {
+ $df = (new \DateTime("{$filter->date_from}-01"))
+ ->setTime(0, 0, 0)->format('Y-m-d H:i:s');
+ $dt = (new \DateTime("{$filter->date_to}-01"))
+ ->modify('last day of this month')->setTime(23, 59, 59)
+ ->format('Y-m-d H:i:s');
+ $query->andWhere(['<=', 'a.date_from', $dt])
+ ->andWhere(['>=', 'a.date_to', $df]);
+ } elseif ($filter->date_from) {
+ $df = (new \DateTime("{$filter->date_from}-01"))
+ ->setTime(0, 0, 0)->format('Y-m-d H:i:s');
+ $query->andWhere(['>=', 'a.date_to', $df]);
+ } elseif ($filter->date_to) {
+ $dt = (new \DateTime("{$filter->date_to}-01"))
+ ->modify('last day of this month')->setTime(23, 59, 59)
+ ->format('Y-m-d H:i:s');
+ $query->andWhere(['<=', 'a.date_from', $dt]);
+ }
}
}
- $dataProvider->query = $query;
+
+ $dataProvider = new \yii\data\ActiveDataProvider([
+ 'query' => $query,
+ 'pagination' => ['pageSize' => 1000],
+ 'sort' => ['defaultOrder' => ['name' => SORT_ASC]],
+ ]);
}
$categories = Products1cNomenclature::find()->select('category')->distinct()->column();
$species = $species->column();
$lists = [];
- $attrMap = [
- 'type' => ['type','тип'],
- 'color' => ['цвет','color'],
- 'sort' => ['sort','сорт'],
- 'size' => ['size','размер'],
- ];
+
foreach (['type','color','sort','size'] as $attr) {
$propIds = Products1cPropType::find()
->select('id')