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

+ 36 - 10
app/Service/StatisticService.php

@@ -126,13 +126,14 @@ class StatisticService extends Service
         //查询所有项目人员的工时比例
         //汇总每个人每个月工资
         $salary_map = [];
-        $all_salary = 0;
+        $all_salary = [];
         foreach ($month_employee_salary as $val) {
             $month = $monthly_ps_order_key_list[$val['main_id']] ?? '';
             if ($month) {
                 $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
             }
-            $all_salary +=  $val['salary'];
+            if(!isset($all_salary[$month])) $all_salary[$month] = 0;
+            $all_salary[$month] +=  $val['salary'];
         }
 //        var_dump($salary_map);die;
         // 2. 计算每个员工在每个月的全月总工时
@@ -184,20 +185,45 @@ class StatisticService extends Service
         $item = Item::Clear($user, $data);
         $item_title_key_list = $item->wherein('id', $items)->pluck("title", "id")->toArray();
         $item_code_key_list = $item->wherein('id', $items)->pluck("code", "id")->toArray();
-        $item_month_list = collect($item_month_list)->transform(function ($item,$index) use ($item_title_key_list, $item_code_key_list,&$total_day,&$all_salary,$count) {
+
+        $collect = collect($item_month_list);
+
+        $item_count = $collect->groupBy(function ($item) {
+            // 这里的 $item 是集合中的每一行数据
+            return $item['month'];
+        })->map(function ($group) {
+            return $group->count();
+        })->toArray();
+        $item_day_count = $collect->groupBy(fn($item) =>  $item['month'])
+            ->map(fn($group) => round($group->sum('work_minutes') /8/ 60))
+            ->toArray();
+
+        $item_month_list = collect($item_month_list)->transform(function ($item,$index) use ($item_title_key_list, $item_code_key_list,&$all_salary,&$item_day_count,$item_count) {
             $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $item['allocated_salary'] = round($item['allocated_salary'],2);
             // 如果不是最后一条
-            if ($index < $count - 1) {
-                $total_day -= $item['days'];
+            $key = $item['month'];
+            if(--$item_count[$key] > 0){
                 // 关键:递减外部的 $absolute_total_hours
-                $all_salary -= $item['allocated_salary'];
-            } else {
-                // 最后一条:直接拿剩下的“余额”
-                $item['days'] = $total_day;
-                $item['allocated_salary'] = $all_salary;
+                $item_day_count[$key] -= $item['days'];
+                $all_salary[$key] -=  $item['allocated_salary'];
+
+            }else{
+                $item['days'] =   $item_day_count[$key];
+                $item['allocated_salary'] =   $all_salary[$key];
             }
+
+
+//            if ($index < $count - 1) {
+//                $total_day -= $item['days'];
+//                // 关键:递减外部的 $absolute_total_hours
+//                $all_salary -= $item['allocated_salary'];
+//            } else {
+//                // 最后一条:直接拿剩下的“余额”
+//                $item['days'] = $total_day;
+//                $item['allocated_salary'] = $all_salary;
+//            }
             return $item;
         })->all();
         return [true, $item_month_list];