]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Добавление фильтров активности
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 7 Aug 2025 10:05:22 +0000 (13:05 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 7 Aug 2025 10:05:22 +0000 (13:05 +0300)
erp24/controllers/Products1cNomenclatureActualityController.php
erp24/records/Products1cNomenclature.php
erp24/views/products1c-nomenclature-actuality/index.php

index 4ca2248202da29d78b05db7625517dc4a829c444..fd600e7cf491a2d76eb2b5fef02daf916bf6e573 100644 (file)
@@ -54,6 +54,7 @@ class Products1cNomenclatureActualityController extends Controller
             'type','color','sort','size',
             'date_from','date_to',
         ], 'safe');
+        $filter->addRule(['onlyActive','onlyInactive'], 'boolean');
         $filter->load(Yii::$app->request->get());
 
 
@@ -70,8 +71,12 @@ class Products1cNomenclatureActualityController extends Controller
             'pagination' => ['pageSize' => 50],
             'sort' => ['defaultOrder'=>['name'=>SORT_ASC]],
         ]);
-
-
+        $attrMap = [
+            'type' => ['type', 'тип'],
+            'color' => ['цвет', 'color'],
+            'sort' => ['sort', 'сорт'],
+            'size' => ['size', 'размер'],
+        ];
         $filtersUsed = array_filter([
             $filter->category,
             $filter->subcategory,
@@ -82,80 +87,83 @@ class Products1cNomenclatureActualityController extends Controller
             $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();
@@ -180,12 +188,7 @@ class Products1cNomenclatureActualityController extends Controller
         $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')
index 5342425f2484065c7860167a6ffd7808479b8e6c..345275708570df0204abe51762527f00681cc3e5 100644 (file)
@@ -97,4 +97,19 @@ class Products1cNomenclature extends \yii\db\ActiveRecord
     {
         return (bool)$this->getActiveActuality()->exists();
     }
+
+    /**
+     * Проверяет, есть ли для этой номенклатуры запись актуальности с active = 0
+     *
+     * @return bool
+     */
+    public function hasInactiveActuality(): bool
+    {
+        return (bool) Products1cNomenclatureActuality::find()
+            ->where([
+                'guid'   => $this->id,
+                'active' => 0,
+            ])
+            ->exists();
+    }
 }
index e3d7a499f40367771b91763371b904c8f344140e..8491d17863f903f99113dc1bd81841ffedc36a9a 100644 (file)
@@ -178,6 +178,18 @@ $months = monthList();
                     </div>
                 </div>
             </div>
+            <div class="mb-3">
+                <?= $formFilter->field($filter, 'onlyActive')->checkbox([
+                    'label' => 'Только активные',
+                    'uncheck' => 0,
+                    'checked' => (bool)$filter->onlyActive,
+                ])->label(false) ?>
+                <?= $formFilter->field($filter, 'onlyInactive')->checkbox([
+                    'label' => 'Только неактивные',
+                    'uncheck' => 0,
+                    'checked' => (bool)$filter->onlyInactive,
+                ])->label(false) ?>
+            </div>
         </div>
 
         <!-- 3-я колонка: поставщик/плантация + кнопка Применить -->
@@ -229,7 +241,12 @@ $months = monthList();
         'tableOptions' => ['class' => 'table table-bordered'],
         'containerOptions' => ['style' => 'overflow:auto; max-height:500px;'],
         'rowOptions' => function($model) {
-            return $model->hasActiveActuality() ? ['class' => 'table-success'] : [];
+            if ($model->hasActiveActuality()) {
+                return ['class' => 'table-success'];
+            }
+             if ($model->hasInactiveActuality()) {
+                return ['class' => 'table-danger'];
+            }
         },
         'columns' => [
             [