gogs 2 miesięcy temu
rodzic
commit
ef280ebad4
1 zmienionych plików z 42 dodań i 9 usunięć
  1. 42 9
      app/Service/StatisticService.php

+ 42 - 9
app/Service/StatisticService.php

@@ -1108,10 +1108,12 @@ class StatisticService extends Service
         //查询所有项目人员的工时比例
         //汇总每个人每个月工资
         $salary_map = [];
+        $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['employee_id'] . '_' . $month] = $val['salary']*100;
             }
         }
 //        var_dump($salary_map);die;
@@ -1127,15 +1129,16 @@ class StatisticService extends Service
 
         // 3. 计算分摊天数与工资
         $item_year_list = [];
+        $item_list = [];
         foreach ($month_employee_list as $item) {
             $key = $item['employee_id'] . '_' . $item['order_month'];
-            $item_key = $item['item_id'] . '_' . $item['order_month'];
-            if (!isset($item_year_list[$item_key])) {
-                $item_year_list[$item_key] = [
-                    "allocated_salary" => 0,
-                    "item_id" => $item['item_id'],
-                ];
-            }
+//            $item_key = $item['item_id'] . '_' . $item['order_month'];
+//            if (!isset($item_year_list[$item_key])) {
+//                $item_year_list[$item_key] = [
+//                    "allocated_salary" => 0,
+//                    "item_id" => $item['item_id'],
+//                ];
+//            }
             $total_salary = $salary_map[$key] ?? 0;
             $total_min = $employee_monthly_total_min[$key] ?? 0;
 
@@ -1143,11 +1146,41 @@ class StatisticService extends Service
             // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
             if ($total_min > 0) {
                 $ratio = $item['total_work'] / $total_min;
-                $allocated_salary = round($ratio * $total_salary, 2);
+                $allocated_salary = round($ratio * $total_salary, 2)*100;
             } else {
                 $allocated_salary = 0;
             }
-            $item_year_list[$item_key]['allocated_salary'] += $allocated_salary;
+            $item_list[] = [
+                'employee_id' => $item['employee_id'],
+                'allocated_salary' => $allocated_salary,
+                'month' => $item['order_month'],
+                'item_id' => $item['item_id'],
+            ];
+        }
+        $collect = collect($item_list);
+        $employee_count = $collect->groupBy(function ($item) {
+            // 这里的 $item 是集合中的每一行数据
+            return $item['employee_id'] . '_' . $item['month'];
+        })->map(function ($group) {
+            return $group->count();
+        })->toArray();
+
+        $month_employee_list = $collect->transform(function ($item, $index) use (&$employee_count,&$all_salary) {
+
+            // 如果不是最后一条
+            $key = $item['employee_id'] . '_' . $item['month'];
+            if (--$employee_count[$key] > 0) {
+                $all_salary[$key] -= $item['allocated_salary'];
+            } else {
+                $item['allocated_salary'] = $all_salary[$key];
+            }
+            return $item;
+        })->all();
+        $return_item_list = [];
+        foreach ($month_employee_list as $v){
+            $key = $v['item_id'];
+            if(!isset($return_item_list[$key])) $return_item_list[$key]['salary'] = 0;
+            $return_item_list[$key]['salary'] += $v['allocated_salary'];
         }
 
         return $item_year_list;