}
+ $weeksShareResult = $service->getWeeksSpeciesShare($dateFrom, $dateTo, $filters, null, 'writeOffs');
+ $weeksData = $weeksShareResult['weeksData'];
+ $weeksShareResult = $weeksShareResult['weeksShare'];
+
}
return $this->render('control-species', [
]);
}
- /**
- * Возвращает список подкатегорий для выбранной категории.
- * @param string $category
- * @return array JSON-ответ со списком подкатегорий
- */
- public function actionGetSubcategories($category)
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- $subcategories = Products1cNomenclature::find()
- ->select('subcategory')
- ->distinct()
- ->where(['category' => $category])
- ->orderBy('subcategory')
- ->asArray()
- ->all();
-
- $out = [];
- foreach ($subcategories as $item) {
- if (!empty($item['subcategory'])) {
- $out[] = ['id' => $item['subcategory'], 'name' => $item['subcategory']];
- }
- }
- return $out;
- }
- /**
- * Возвращает список видов для выбранной подкатегории.
- * @param string $subcategory
- * @return array JSON-ответ со списком видов
- */
- public function actionGetSpecies($subcategory)
- {
- \Yii::$app->response->format = Response::FORMAT_JSON;
- $species = Products1cNomenclature::find()
- ->select('species')
- ->distinct()
- ->where(['subcategory' => $subcategory])
- ->orderBy('species')
- ->asArray()
- ->all();
-
- $out = [];
- foreach ($species as $item) {
- if (!empty($item['species'])) {
- $out[] = ['id' => $item['species'], 'name' => $item['species']];
- }
- }
- return $out;
- }
}
return $result;
}
+ /**
+ * Для заданного магазина/фильтров получает по 1–5 неделям внутри месяца
+ * и рассчитывает для каждого вида (species) долю недельного объёма
+ * от месячного итога.
+ *
+ * @param string $dateFrom начало месяца (Y-m-d H:i:s)
+ * @param string $dateTo конец месяца (Y-m-d H:i:s)
+ * @param array $filters ['store_id'=>..., 'category'=>..., ...]
+ * @param array|null $productFilter опц. фильтр по product_id
+ * @param string $type 'sales' или 'writeOffs'
+ * @return array{
+ * weeksData: array<int, array>, // сырые данные по каждой неделе
+ * weeksShare: array<int, array> // доля недели от месячного итога
+ * }
+ */
+ public function getWeeksSpeciesShare(
+ string $dateFrom,
+ string $dateTo,
+ array $filters,
+ ?array $productFilter,
+ string $type = 'writeOffs'
+ ): array
+ {
+ $monthResult = $this->getMonthSpeciesShareOrWriteOffDate(
+ $dateFrom, $dateTo, $filters, $productFilter, $type
+ );
+
+ $speciesMonthTotals = [];
+ foreach ($monthResult as $r) {
+ $speciesMonthTotals
+ [$r['store_id']]
+ [$r['category']]
+ [$r['subcategory']]
+ [$r['species']] = $r['total_sum'];
+ }
+
+ $weeksData = [];
+ $weeksShareResult = [];
+ foreach (range(1, 5) as $ind) {
+ $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($dateFrom)));
+ $weekEnd = date("Y-m-d 23:59:59", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($dateFrom)));
+ if ($weekEnd > $dateTo) {
+ $weekEnd = $dateTo;
+ }
+ if ($weekStart > $dateTo) {
+ continue;
+ }
+ $weekResult = $this->getMonthSpeciesShareOrWriteOffDate(
+ $weekStart,
+ $weekEnd,
+ $filters,
+ null,
+ $type
+ );
+ $weeksData[$ind] = $weekResult;
+ if (!$weekResult) {
+ continue;
+ }
+ $weeksShareResult[$ind] = [];
+ foreach ($weekResult as $weekRow) {
+ $monthSum = $speciesMonthTotals
+ [$weekRow['store_id']]
+ [$weekRow['category']]
+ [$weekRow['subcategory']]
+ [$weekRow['species']] ?? null;
+
+ if ($monthSum) {
+ $weeksShareResult[$ind]
+ [$weekRow['category']]
+ [$weekRow['subcategory']]
+ [$weekRow['species']] = $weekRow['total_sum'] / $monthSum;
+ }
+
+ }
+
+ }
+ return [
+ 'weeksData' => $weeksData,
+ 'weeksShare' => $weeksShareResult,
+ ];
+ }
+
}