gogs 2 месяцев назад
Родитель
Сommit
50ce1dbffb
1 измененных файлов с 41 добавлено и 25 удалено
  1. 41 25
      app/Service/StatisticService.php

+ 41 - 25
app/Service/StatisticService.php

@@ -65,7 +65,14 @@ class StatisticService extends Service
             return $group->count();
         })->toArray();
         $employee_work_count = $collect->groupBy(fn($item) => $item['employee_id'] . '_' . $item['order_date'])
-            ->map(fn($group) => round($group->sum('total_work') / 60, 2))
+            ->map(function ($group) {
+                // 1. 算出小时并保留两位小数(例如 26.08)
+                $hours = round($group->sum('total_work') / 60, 2);
+
+                // 2. 乘以 100 并强转为整数(例如 2608)
+                // 使用 round 是为了再次修正 26.08 * 100 可能产生的 2607.9999 误差
+                return (int) round($hours * 100);
+            })
             ->toArray();
         $collection = collect($month_employee_list);
         // 注意:这里使用 transform,参数名建议写准确
@@ -82,10 +89,10 @@ class StatisticService extends Service
                 $current_hours = round($item['total_work'] / 60, 2);
                 $item['total_work_hours'] = $current_hours;
                 // 关键:递减外部的 $absolute_total_hours
-                $employee_work_count[$key] -= $current_hours;
+                $employee_work_count[$key] -= $current_hours*100;
 
             }else{
-                $item['total_work_hours'] =   $employee_work_count[$key];
+                $item['total_work_hours'] =   round($employee_work_count[$key]/100,2);
             }
             return $item;
         })->all();
@@ -133,7 +140,7 @@ class StatisticService extends Service
                 $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
             }
             if(!isset($all_salary[$month])) $all_salary[$month] = 0;
-            $all_salary[$month] +=  $val['salary'];
+            $all_salary[$month] +=  $val['salary']*100;
         }
 //        var_dump($salary_map);die;
         // 2. 计算每个员工在每个月的全月总工时
@@ -178,8 +185,6 @@ class StatisticService extends Service
             $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
         }
         $collect = collect($item_month_list);
-        $total_day = round($collect->sum('work_minutes')/8/60);
-        $count = $collect->count();
         $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
         $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
         $item = Item::Clear($user, $data);
@@ -207,11 +212,11 @@ class StatisticService extends Service
             if(--$item_count[$key] > 0){
                 // 关键:递减外部的 $absolute_total_hours
                 $item_day_count[$key] -= $item['days'];
-                $all_salary[$key] -=  $item['allocated_salary'];
+                $all_salary[$key] -=  $item['allocated_salary']*100;
 
             }else{
                 $item['days'] =   $item_day_count[$key];
-                $item['allocated_salary'] =   $all_salary[$key];
+                $item['allocated_salary'] =   round($all_salary[$key]/100,2);
             }
 
 
@@ -264,10 +269,11 @@ class StatisticService extends Service
             if ($month) {
                 $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
             }
-            if(!isset($all_salary[$val['employee_id']])){
-                $all_salary[$val['employee_id']] = 0;
+            $key = $val['employee_id'].'_'.$month;
+            if(!isset($all_salary[$key])){
+                $all_salary[$key] = 0;
             }
-            $all_salary[$val['employee_id']] += $val['salary'];
+            $all_salary[$key] += $val['salary']*100;
 
         }
         // 2. 计算每个员工在每个月的全月总工时
@@ -310,20 +316,29 @@ class StatisticService extends Service
             }
             $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
             $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
-            if(!isset( $total_work_min[$item['employee_id']]))  $total_work_min[$item['employee_id']] = 0;
-            $total_work_min[$item['employee_id']] += $item['total_work'];
+
+            $key = $item['employee_id'].'_'.$item['order_month'];
+            if(!isset( $total_work_min[$key]))  $total_work_min[$key] = 0;
+            $total_work_min[$key] += $item['total_work'];
         }
         foreach ($total_work_min as $k=>$v){
-            $total_work_min[$k] =  round($v / 60, 2);
+            $total_work_min[$k] =  round($v / 60, 2)*100;
         }
 
-
         $collect = collect($item_month_list);
-        $employee_count = $collect->groupBy('employee_id')
-            ->map(function ($group) {
-                return $group->count(); // 统计每个分组里的数量
-            })
-            ->toArray();
+        $employee_count = $collect->groupBy(function ($item) {
+            // 这里的 $item 是集合中的每一行数据
+            return $item['employee_id'] . '_' . $item['month'];
+        })->map(function ($group) {
+            return $group->count();
+        })->toArray();
+
+//        $collect = collect($item_month_list);
+//        $employee_count = $collect->groupBy('employee_id')
+//            ->map(function ($group) {
+//                return $group->count(); // 统计每个分组里的数量
+//            })
+//            ->toArray();
 
         $item_month_list = collect($item_month_list)->sortBy('month')->values()->all();
         $items = collect($item_month_list)->pluck('item_id')->unique()->values()->all();
@@ -342,13 +357,14 @@ class StatisticService extends Service
             $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
             $item['work_hours'] = round($item['work_minutes'] / 60, 2);
             $item['total_hours'] = round($item['total_min'] / 60);
-            if(--$employee_count[$item['employee_id']] > 0){
-                $all_salary[$item['employee_id']] -= $item['allocated_salary'];
-                $total_work_min[$item['employee_id']] -=   $item['work_hours'];
+            $key = $item['employee_id'].'_'.$item['month'];
+            if(--$employee_count[$key] > 0){
+                $all_salary[$key] -= $item['allocated_salary']*100;
+                $total_work_min[$key] -=   $item['work_hours']*100;
 
             }else{
-                $item['allocated_salary'] =  $all_salary[$item['employee_id']];
-                $item['work_hours'] =   $total_work_min[$item['employee_id']];
+                $item['allocated_salary'] =   round($all_salary[$key]/100,2);
+                $item['work_hours'] =    round($total_work_min[$key]/100,2);
             }
             return $item;
         })->all();