]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Актуальность в планограмме
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 31 Jul 2025 09:14:19 +0000 (12:14 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 31 Jul 2025 09:14:19 +0000 (12:14 +0300)
erp24/services/StorePlanService.php

index bf9a12c355faac4ccf2ba66983128753a27f639d..094eb47f6b36083bfbea1ec6d69239298f7d01aa 100755 (executable)
@@ -20,6 +20,7 @@ use yii_app\records\PricesDynamic;
 use yii_app\records\Products1c;
 use yii_app\records\Products1cAdditionalCharacteristics;
 use yii_app\records\Products1cNomenclature;
+use yii_app\records\Products1cNomenclatureActuality;
 use yii_app\records\Sales;
 use yii_app\records\SalesProducts;
 use yii_app\records\StorePlan;
@@ -323,7 +324,7 @@ class StorePlanService
      *
      * @return array
      */
-    private static function getSalesHistory($storeId, $periods, $category = null, $subcategory = null, $species = null)
+    public static function getSalesHistory($storeId, $periods, $category = null, $subcategory = null, $species = null)
     {
         $salesHistory = [];
 
@@ -338,7 +339,7 @@ class StorePlanService
                 $query = Sales::find()->alias('s')
                     ->select([
                         'p1c.id as product_guid',
-                        'COUNT(*) as sales_count',
+                        'SUM(sp.quantity) as sales_count',
                         'p1c.category',
                         'p1c.subcategory',
                         'p1c.species',
@@ -350,7 +351,7 @@ class StorePlanService
                     ->andWhere(['between', 's.date', $dateStart, $dateEnd])
                     ->andWhere(['order_id' => ['', '0']])
                     ->andWhere(['p1.components' => ''])
-                    ->andWhere(['not in', 'p1c.category', ['', 'букет', 'сборка', 'сервис']])
+                    ->andWhere(['not in', 'p1c.category', ['',  'сервис']])
                     ->andFilterWhere(['p1c.category'    => $category])
                     ->andFilterWhere(['p1c.subcategory' => $subcategory])
                     ->andFilterWhere(['p1c.species'     => $species])
@@ -380,6 +381,22 @@ class StorePlanService
             }
         }
 
+        foreach ($salesHistory as &$history) {
+            foreach ($periods as $periodKey => $periodData) {
+                if (!isset($history['data'][$periodKey])) {
+                    $history['data'][$periodKey] = [];
+                }
+
+                foreach (array_keys($periodData['weeks']) as $weekIndex) {
+                    if (!isset($history['data'][$periodKey][$weekIndex])) {
+                        $history['data'][$periodKey][$weekIndex] = 0;
+                    }
+                }
+                ksort($history['data'][$periodKey]);
+            }
+        }
+        unset($history);
+
         return $salesHistory;
     }
 
@@ -401,7 +418,36 @@ class StorePlanService
         $productsWithHistory    = [];
         $productsWithoutHistory = [];
 
+        $guids = array_keys($salesHistory);
+        $actualities = Products1cNomenclatureActuality::find()
+            ->where(['guid' => $guids, 'active' => 1])
+            ->indexBy('guid')
+            ->all();
+
         foreach ($salesHistory as $guid => $info) {
+            if (!isset($actualities[$guid])) {
+                continue;
+            }
+            $act   = $actualities[$guid];
+            $actFrom = new \DateTime($act->date_from);
+            $actTo   = $act->date_end
+                ? new \DateTime($act->date_end)
+                : (new \DateTime())->modify('+1 years');
+
+            $isInsideActivity = true;
+            foreach ($periods as $periodKey => $periodData) {
+                $pStart = \DateTime::createFromFormat('Y-m', $periodKey);
+                $pStart->modify('first day of this month')->setTime(0, 0, 0);
+                $pEnd = (clone $pStart)->modify('last day of this month')->setTime(23, 59, 59);
+
+                if ($pStart < $actFrom || $pEnd > $actTo) {
+                    $isInsideActivity = false;
+                    break;
+                }
+            }
+            if (!$isInsideActivity) {
+                continue;
+            }
             $monthsData    = $info['data'];
             $metaFields    = [
                 'store_id' => $info['store_id'],