]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Вычисление средней цены
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 9 Apr 2025 06:44:28 +0000 (09:44 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 9 Apr 2025 06:44:28 +0000 (09:44 +0300)
erp24/services/StorePlanService.php

index cba51c3f30cb22c9570b2808f33d4b160c7e5efe..1bf6d9885442ba5364be2d9a0062e10dbbae9ed0 100755 (executable)
@@ -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;