$salesAdminIds[$sale['admin_id']] = true;
}
- // Добавляем админов из продаж, которых нет в смене
+ // Определяем админов из продаж, которых нет в смене
+ $missingAdminIds = [];
foreach ($salesAdminIds as $adminId => $_) {
$adminExists = false;
foreach ($adminNames[$store->id] as $admin) {
}
}
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
];
}
"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,
"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,