From: fomichev Date: Thu, 29 May 2025 09:49:03 +0000 (+0300) Subject: Модификация метода getSalesHistory X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=a24596e9f0815752894a9933beacd6c1a0dc9300;p=erp24_rep%2Fyii-erp24%2F.git Модификация метода getSalesHistory --- diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index d660a6d3..a1f346f5 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -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']; } } }