From 9eb52f324d8285c07f30885d1607f2441762318a Mon Sep 17 00:00:00 2001 From: fomichev Date: Wed, 9 Apr 2025 09:44:28 +0300 Subject: [PATCH] =?utf8?q?=D0=92=D1=8B=D1=87=D0=B8=D1=81=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D0=B8=D0=B5=20=D1=81=D1=80=D0=B5=D0=B4=D0=BD=D0=B5=D0=B9=20?= =?utf8?q?=D1=86=D0=B5=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/services/StorePlanService.php | 48 +++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/erp24/services/StorePlanService.php b/erp24/services/StorePlanService.php index cba51c3f..1bf6d988 100755 --- a/erp24/services/StorePlanService.php +++ b/erp24/services/StorePlanService.php @@ -5,6 +5,7 @@ namespace yii_app\services; use DateTime; use yii\db\Expression; use yii\helpers\ArrayHelper; +use yii_app\records\CityStore; use yii_app\records\Motivation; use yii_app\records\PricesDynamic; use yii_app\records\Products1cAdditionalCharacteristics; @@ -450,22 +451,43 @@ class StorePlanService * @param string $month Месяц в формате "mm" (например, "03"). * @return float Цена, если найдена запись, иначе 0. */ - public static function getPriceForProductAndMonth($productId, $year, $month) + public static function getPriceForProductAndMonth($productId, $year, $month, $storeId) { - $dateToCheck = "$year-$month-01"; - $priceRecord = PricesDynamic::find() - ->where(['product_id' => $productId, 'active' => 1]) - ->andWhere(['<=', 'date_from', $dateToCheck]) + $monthStart = "$year-$month-01"; + $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); + $monthEnd = sprintf('%04d-%02d-%02d', $year, $month, $daysInMonth); + + $priceRecords = PricesDynamic::find() + ->where(['product_id' => $productId]) + ->andWhere(['<=', 'date_from', $monthEnd]) ->andWhere([ 'or', - ['>=', 'date_to', $dateToCheck], - ['date_to' => null] - ]) - ->one(); - if ($priceRecord) { - return (float)$priceRecord->price; + ['>=', 'date_to', $monthStart], + ['date_to' => '2100-01-01 03:00:00+03'] + ]); + if (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1342) { + $region = 52; + } elseif (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1) { + $region = 77; + } else { + $region = null; } - return 0; + $priceRecords->andWhere(['or',['region_id' => $region],['region_id' => null]])->all(); + + + if (!empty($priceRecords)) { + $totalPrice = 0; + $count = count($priceRecords); + foreach ($priceRecords as $record) { + $totalPrice += $record->price; // предполагаем, что поле с ценой называется "price" + } + $averagePrice = $totalPrice / $count; + } else { + $averagePrice = 0; + } + + + return $averagePrice; } /** @@ -520,7 +542,7 @@ class StorePlanService $mKey = $mInfo['month']; $sales = isset($product['weekly_sales'][$mKey]) ? array_sum($product['weekly_sales'][$mKey]) : 0; - $price = self::getPriceForProductAndMonth($guid, $mInfo['year'], $mInfo['month']); + $price = self::getPriceForProductAndMonth($guid, $mInfo['year'], $mInfo['month'], $storeId); $monthlySales[$mKey] = $sales; $weightedValue = $sales * $price * $mInfo['weight']; $monthlyWeighted[$mKey] = $weightedValue; -- 2.39.5