*
* @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 = [];
$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'];
}
}
}