]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Правки и оптимизации
authorVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 3 Dec 2025 12:30:27 +0000 (15:30 +0300)
committerVladimir Fomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 3 Dec 2025 12:30:27 +0000 (15:30 +0300)
erp24/api3/core/services/ReportService.php

index 6f57858d7eebf5186cc66b5b65857a036aee22a2..eb87a4c19f025408fc798a535d35b0447def1280 100644 (file)
@@ -685,7 +685,8 @@ class ReportService
                     $salesAdminIds[$sale['admin_id']] = true;
                 }
 
-                // Добавляем админов из продаж, которых нет в смене
+                // Определяем админов из продаж, которых нет в смене
+                $missingAdminIds = [];
                 foreach ($salesAdminIds as $adminId => $_) {
                     $adminExists = false;
                     foreach ($adminNames[$store->id] as $admin) {
@@ -695,11 +696,23 @@ class ReportService
                         }
                     }
                     if (!$adminExists) {
-                        // Получаем имя админа
-                        $adminName = Admin::findOne($adminId);
+                        $missingAdminIds[] = $adminId;
+                    }
+                }
+                
+                // Batch-загрузка имен отсутствующих админов одним запросом
+                if (!empty($missingAdminIds)) {
+                    $missingAdmins = Admin::find()
+                        ->select(['id', 'name'])
+                        ->where(['id' => $missingAdminIds])
+                        ->indexBy('id')
+                        ->asArray()
+                        ->all();
+                    
+                    foreach ($missingAdminIds as $adminId) {
                         $adminNames[$store->id][] = [
                             'id' => $adminId,
-                            'name' => $adminName ? $adminName->name : 'Unknown',
+                            'name' => $missingAdmins[$adminId]['name'] ?? 'Unknown',
                             'shift_id' => 0, // У админов из продаж нет shift_id
                         ];
                     }
@@ -1294,22 +1307,21 @@ class ReportService
                     "sale_month_total" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0),
                     "sale_total" => $storeSaleTotalTotal[$store_id] ?? 0,
                     "sale_quantity" => $storeSaleQuantityTotal[$store_id] ?? 0,
-                    "sale_avg" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor($storeSaleTotalTotal[$store_id] / $storeSaleQuantityTotal[$store_id]) : 0,
+                    "sale_avg" => ($storeSaleQuantityTotal[$store_id] ?? 0) > 0 ? floor($storeSaleTotalTotal[$store_id] / $storeSaleQuantityTotal[$store_id]) : 0,
                     "total_write_offs_per_date" => $totalWriteOffsPerDateTotal[$store_id] ?? 0,
-                    "total_write_offs_per_date_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor($totalWriteOffsPerDateTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100) : 0,
+                    "total_write_offs_per_date_percent" => ($storeSaleTotalTotal[$store_id] ?? 0) > 0 ? floor($totalWriteOffsPerDateTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100) : 0,
                     "total_write_offs_per_month" => (int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0),//$totalWriteOffsPerMonthTotal[$store_id] ?? 0,
 //                    "total_write_offs_per_month_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor((int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0) / $storeSaleTotalTotal[$store_id] * 100) : 0,
                     "total_write_offs_per_month_percent" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) > 0 ? floor((int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0) / ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) * 100) : 0,
                     "total_payroll_days" => $totalPayrollDaysTotal[$store_id] ?? 0,
-                    "total_payroll_days_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor($totalPayrollDaysTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100) : 0,
-                    "total_payroll_month" => (int)($adminPayrollDaysMonth[$store_id]['total'] ?? 0),//$totalPayrollMonthTotal[$store_id] ?? 0,
-//                    "total_payroll_month_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor(($adminPayrollDaysMonth[$store_id]['total'] ?? 0) / $storeSaleTotalTotal[$store_id] * 100) : 0,
+                    "total_payroll_days_percent" => ($storeSaleTotalTotal[$store_id] ?? 0) > 0 ? floor($totalPayrollDaysTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100) : 0,
+                    "total_payroll_month" => (int)($adminPayrollDaysMonth[$store_id]['total'] ?? 0),
                     "total_payroll_month_percent" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) > 0 ? floor(($adminPayrollDaysMonth[$store_id]['total'] ?? 0) / ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) * 100) : 0,
-                    "employee_sale_avg" => $employeeCount[$store_id] ?? 0 > 0 ? floor($storeSaleTotalTotal[$store_id] / $employeeCount[$store_id]) : 0,
+                    "employee_sale_avg" => ($employeeCount[$store_id] ?? 0) > 0 ? floor($storeSaleTotalTotal[$store_id] / $employeeCount[$store_id]) : 0,
                     "visitors_quantity" => $storeVisitorsQuantityTotal[$store_id] ?? 0,
-                    "conversion" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor($storeVisitorsQuantityTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100) : 0,
+                    "conversion" => ($storeSaleQuantityTotal[$store_id] ?? 0) > 0 ? floor($storeVisitorsQuantityTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100) : 0,
                     "bonus_user_count" => $storeSaleBonusCountTotal[$store_id] ?? 0,
-                    "bonus_user_per_sale_percent" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor($storeSaleBonusCountTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100) : 0,
+                    "bonus_user_per_sale_percent" => ($storeSaleQuantityTotal[$store_id] ?? 0) > 0 ? floor($storeSaleBonusCountTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100) : 0,
                     "bonus_new_user_count" => $storeSaleNewBonusCountTotal[$store_id] ?? 0,
                     "bonus_repeat_user_count" => $storeSaleRepeatBonusCountTotal[$store_id] ?? 0,
                     "sale_return_quantity" => $storeSaleReturnQuantityTotal[$store_id] ?? 0,
@@ -1770,38 +1782,34 @@ class ReportService
                         "sale_month_total" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0),
                         "sale_total" => $storeSaleTotalTotal[$store_id] ?? 0,
                         "sale_quantity" => $storeSaleQuantityTotal[$store_id] ?? 0,
-                        "sale_avg" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor(
+                        "sale_avg" => ($storeSaleQuantityTotal[$store_id] ?? 0) > 0 ? floor(
                             $storeSaleTotalTotal[$store_id] / $storeSaleQuantityTotal[$store_id]
                         ) : 0,
                         "total_write_offs_per_date" => $totalWriteOffsPerDateTotal[$store_id] ?? 0,
-                        "total_write_offs_per_date_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor(
+                        "total_write_offs_per_date_percent" => ($storeSaleTotalTotal[$store_id] ?? 0) > 0 ? floor(
                             $totalWriteOffsPerDateTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100
                         ) : 0,
                         "total_write_offs_per_month" => (int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0),
-                        //$totalWriteOffsPerMonthTotal[$store_id] ?? 0,
-//                    "total_write_offs_per_month_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor((int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0) / $storeSaleTotalTotal[$store_id] * 100) : 0,
                         "total_write_offs_per_month_percent" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) > 0 ? floor(
                             (int)($writeOffsMonth[$store_guids[$store_id]]["total"] ?? 0) / ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) * 100
                         ) : 0,
                         "total_payroll_days" => $totalPayrollDaysTotal[$store_id] ?? 0,
-                        "total_payroll_days_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor(
+                        "total_payroll_days_percent" => ($storeSaleTotalTotal[$store_id] ?? 0) > 0 ? floor(
                             $totalPayrollDaysTotal[$store_id] / $storeSaleTotalTotal[$store_id] * 100
                         ) : 0,
                         "total_payroll_month" => (int)($adminPayrollDaysMonth[$store_id]['total'] ?? 0),
-                        //$totalPayrollMonthTotal[$store_id] ?? 0,
-//                    "total_payroll_month_percent" => $storeSaleTotalTotal[$store_id] ?? 0 > 0 ? floor(($adminPayrollDaysMonth[$store_id]['total'] ?? 0) / $storeSaleTotalTotal[$store_id] * 100) : 0,
                         "total_payroll_month_percent" => ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) > 0 ? floor(
                             ($adminPayrollDaysMonth[$store_id]['total'] ?? 0) / ($salesMonth[$store_guids[$store_id]]["total"] ?? 0) * 100
                         ) : 0,
-                        "employee_sale_avg" => $employeeCount[$store_id] ?? 0 > 0 ? floor(
+                        "employee_sale_avg" => ($employeeCount[$store_id] ?? 0) > 0 ? floor(
                             $storeSaleTotalTotal[$store_id] / $employeeCount[$store_id]
                         ) : 0,
                         "visitors_quantity" => $storeVisitorsQuantityTotal[$store_id] ?? 0,
-                        "conversion" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor(
+                        "conversion" => ($storeSaleQuantityTotal[$store_id] ?? 0) > 0 ? floor(
                             $storeVisitorsQuantityTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100
                         ) : 0,
                         "bonus_user_count" => $storeSaleBonusCountTotal[$store_id] ?? 0,
-                        "bonus_user_per_sale_percent" => $storeSaleQuantityTotal[$store_id] ?? 0 > 0 ? floor(
+                        "bonus_user_per_sale_percent" => ($storeSaleQuantityTotal[$store_id] ?? 0) > 0 ? floor(
                             $storeSaleBonusCountTotal[$store_id] / $storeSaleQuantityTotal[$store_id] * 100
                         ) : 0,
                         "bonus_new_user_count" => $storeSaleNewBonusCountTotal[$store_id] ?? 0,