]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Модификация метода getSalesHistory
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 29 May 2025 09:49:03 +0000 (12:49 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Thu, 29 May 2025 09:49:03 +0000 (12:49 +0300)
erp24/services/StorePlanService.php

index d660a6d31d4ecccc5a807b44655ce310aa76a831..a1f346f5d3aa1a7e99aedf2882670db88b4a9353 100755 (executable)
@@ -317,24 +317,13 @@ class StorePlanService
      *
      * @param int    $storeId
      * @param array  $periods     Массив периодов, сформированный функцией getPeriods.
-     * @param string $category
+     * @param string|null $category
      * @param string|null $subcategory
      * @param string|null $species
      *
-     * @return array Структура данных:
-     *      [
-     *          'product_guid1' => [
-     *              'YYYY-MM' => [
-     *                  0 => salesCount_week1,
-     *                  1 => salesCount_week2,
-     *                  ...
-     *              ],
-     *              ...
-     *          ],
-     *          ...
-     *      ]
+     * @return array
      */
-    private static function getSalesHistory($storeId, $periods, $category, $subcategory = null, $species = null)
+    private static function getSalesHistory($storeId, $periods, $category = null, $subcategory = null, $species = null)
     {
         $salesHistory = [];
 
@@ -349,34 +338,40 @@ class StorePlanService
                 $query = Sales::find()->alias('s')
                     ->select([
                         'p1c.id as product_guid',
-                        'COUNT(*) as sales_count'
+                        'COUNT(*) as sales_count',
+                        'p1c.category',
+                        'p1c.subcategory',
+                        'p1c.species',
                     ])
                     ->innerJoin('sales_products sp', 's.id = sp.check_id')
                     ->innerJoin('products_1c_nomenclature p1c', 'p1c.id = sp.product_id')
                     ->where(['s.store_id' => $storeId])
                     ->andWhere(['between', 's.date', $dateStart, $dateEnd])
                     ->andWhere(['order_id' => ['', '0']])
-                    ->andWhere(['p1c.category' => $category]);
-
-                if ($subcategory !== null) {
-                    $query->andWhere(['p1c.subcategory' => $subcategory]);
-                }
-                if ($species !== null) {
-                    $query->andWhere(['p1c.species' => $species]);
-                }
+                    ->andFilterWhere(['p1c.category'    => $category])
+                    ->andFilterWhere(['p1c.subcategory' => $subcategory])
+                    ->andFilterWhere(['p1c.species'     => $species])
+                    ->groupBy([
+                        'p1c.id',
+                        'p1c.category',
+                        'p1c.subcategory',
+                        'p1c.species',
+                    ]);
 
-                $query->groupBy('p1c.id');
                 $results = $query->asArray()->all();
+                foreach ($results as $row) {
+                    $guid = $row['product_guid'];
 
-                foreach ($results as $result) {
-                    $guid = $result['product_guid'];
                     if (!isset($salesHistory[$guid])) {
-                        $salesHistory[$guid] = [];
-                    }
-                    if (!isset($salesHistory[$guid][$periodKey])) {
-                        $salesHistory[$guid][$periodKey] = [];
+                        $salesHistory[$guid] = [
+                            'category'    => $row['category'],
+                            'subcategory' => $row['subcategory'],
+                            'species'     => $row['species'],
+                            'data'        => [],
+                        ];
                     }
-                    $salesHistory[$guid][$periodKey][$weekIndex] = (int)$result['sales_count'];
+
+                    $salesHistory[$guid]['data'][$periodKey][$weekIndex] = (int)$row['sales_count'];
                 }
             }
         }