From: fomichev Date: Wed, 9 Apr 2025 06:44:28 +0000 (+0300) Subject: Вычисление средней цены X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=9eb52f324d8285c07f30885d1607f2441762318a;p=erp24_rep%2Fyii-erp24%2F.git Вычисление средней цены --- 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;