]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Кеширование и вынос цены из цикла
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 19 May 2025 13:00:35 +0000 (16:00 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Mon, 19 May 2025 13:00:35 +0000 (16:00 +0300)
erp24/services/StorePlanService.php

index 4b651d33addf19cb1ebb5cd97ff21d729cb5121c..bea2bb5d41f104609a8c8dea4a58bdd53d23b4a1 100755 (executable)
@@ -834,7 +834,7 @@ class StorePlanService
             foreach ($matrixTypesIds as $matrixTypeId) {
                 $bouquetsArray = StorePlanService::getBouqetsByDate($month, $year, $matrixTypeId);
                 $forecasts = ArrayHelper::getColumn($bouquetsArray, 'bouquetForecast');
-
+                var_dump($bouquetsArray); die();
                 foreach ($forecasts as $forecastArray) {
                     if (is_array($forecastArray)) {
                         foreach ($forecastArray as $fc) {
@@ -960,25 +960,25 @@ class StorePlanService
                 ->asArray()
                 ->all();
 
-            $guidList = array_column($forecasts, 'guid');
+            $bouquetGuidList = array_column($forecasts, 'guid');
             $productComponents = Products1c::find()
                 ->select(['id', 'components'])
-                ->where(['id' => $guidList])
+                ->where(['id' => $bouquetGuidList])
                 ->indexBy('id')
                 ->asArray()
                 ->all();
-            $allGuids = [];
+            $allComponentGuids = [];
             foreach ($productComponents as $comp) {
                 if ($json = $comp['components']) {
                     $decoded = @json_decode($json,true);
                     if (json_last_error()===JSON_ERROR_NONE) {
-                        $allGuids = array_merge($allGuids, array_keys($decoded));
+                        $allComponentGuids = array_merge($allComponentGuids, array_keys($decoded));
                     }
                 }
             }
             $speciesCache = Products1cNomenclature::find()
-                ->select(['id','species'])
-                ->where(['id'=>array_unique($allGuids)])
+                ->select(['id', 'species'])
+                ->where(['id' => array_unique($allComponentGuids)])
                 ->indexBy('id')->asArray()->all();
             foreach ($forecasts as $forecast) {
                 $products = [];
@@ -992,7 +992,19 @@ class StorePlanService
                     }
                 }
 
-                $productGuids = array_column($products, 'product_guid');
+                $productComponentGuids = array_column($products, 'product_guid');
+
+                $pricesData =
+                    PricesDynamic::find()
+                        ->select(['price', 'product_id', 'region_id'])
+                        ->where(['product_id' => $productComponentGuids])
+                        ->andWhere(['active' => 1])
+                        ->asArray()
+                        ->all();
+                foreach ($pricesData as $price) {
+                    $priceCache[$price['region_id']][$price['product_id']] = $price['price'];
+                }
+
 
                 foreach ($types as $field => $typeSales) {
                     $typeSalesValue = (int)$forecast[$field];
@@ -1005,29 +1017,14 @@ class StorePlanService
 
                         $regionId = $params['address_region'] ?: match ($storeCities[$sid] ?? null) {
                             1342 => BouquetComposition::REGION_NN,
-                            1    => BouquetComposition::REGION_MSK,
+                            1 => BouquetComposition::REGION_MSK,
                             default => BouquetComposition::REGION_NN,
                         };
 
-                        $priceKey = $regionId . ':' . md5(implode(',', $productGuids));
-                        if (!isset($priceCache[$priceKey])) {
-                            $priceCache[$priceKey] = ArrayHelper::map(
-                                PricesDynamic::find()
-                                    ->select(['price', 'product_id'])
-                                    ->where(['product_id' => $productGuids])
-                                    ->andWhere(['active' => 1, 'region_id' => $regionId])
-                                    ->asArray()
-                                    ->all(),
-                                'product_id',
-                                'price'
-                            );
-                        }
-                        $prices = $priceCache[$priceKey];
-
                         foreach ($products as $product) {
                             $guid = $product['product_guid'];
                             $species = $speciesCache[$guid]['species'] ?? 'Неизвестно';
-                            $price = $prices[$guid] ?? 0;
+                            $price = $priceCache[$regionId][$guid] ?? 0;
                             $raw = $price * $product['count'] * $typeSalesValue;
                             $cost = round($raw * BouquetCompositionPrice::SURCHARGE_ASSEMBLY, 2);