From 655addeed713f37014bfec5558a616ef26bb0835 Mon Sep 17 00:00:00 2001 From: fomichev Date: Mon, 5 May 2025 18:01:36 +0300 Subject: [PATCH] =?utf8?q?=D0=9C=D0=B5=D1=82=D0=BE=D0=B4=20=D0=B2=D1=8B?= =?utf8?q?=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BD=D0=B5?= =?utf8?q?=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20=D0=B4=D0=BE=D0=BB?= =?utf8?q?=D0=B8=20=D0=B2=D0=B8=D0=B4=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../AutoPlannogrammaController.php | 52 +----------- erp24/services/AutoPlannogrammaService.php | 82 +++++++++++++++++++ 2 files changed, 86 insertions(+), 48 deletions(-) diff --git a/erp24/controllers/AutoPlannogrammaController.php b/erp24/controllers/AutoPlannogrammaController.php index bf1d674a..2ba5a775 100644 --- a/erp24/controllers/AutoPlannogrammaController.php +++ b/erp24/controllers/AutoPlannogrammaController.php @@ -316,6 +316,10 @@ class AutoPlannogrammaController extends BaseController } + $weeksShareResult = $service->getWeeksSpeciesShare($dateFrom, $dateTo, $filters, null, 'writeOffs'); + $weeksData = $weeksShareResult['weeksData']; + $weeksShareResult = $weeksShareResult['weeksShare']; + } return $this->render('control-species', [ @@ -330,53 +334,5 @@ class AutoPlannogrammaController extends BaseController ]); } - /** - * Возвращает список подкатегорий для выбранной категории. - * @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; - } } diff --git a/erp24/services/AutoPlannogrammaService.php b/erp24/services/AutoPlannogrammaService.php index 79c5de6b..0cf0498f 100644 --- a/erp24/services/AutoPlannogrammaService.php +++ b/erp24/services/AutoPlannogrammaService.php @@ -495,4 +495,86 @@ class AutoPlannogrammaService 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, // сырые данные по каждой неделе + * weeksShare: 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, + ]; + } + } -- 2.39.5