gogs 2 сар өмнө
parent
commit
48a339985e

+ 63 - 34
app/Service/StatisticService.php

@@ -71,7 +71,7 @@ class StatisticService extends Service
 
 
                 // 2. 乘以 100 并强转为整数(例如 2608)
                 // 2. 乘以 100 并强转为整数(例如 2608)
                 // 使用 round 是为了再次修正 26.08 * 100 可能产生的 2607.9999 误差
                 // 使用 round 是为了再次修正 26.08 * 100 可能产生的 2607.9999 误差
-                return (int) round($hours * 100);
+                return (int)round($hours * 100);
             })
             })
             ->toArray();
             ->toArray();
         $collection = collect($month_employee_list);
         $collection = collect($month_employee_list);
@@ -84,15 +84,15 @@ class StatisticService extends Service
             $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
 
 
             // 如果不是最后一条
             // 如果不是最后一条
-            $key = $item['employee_id'].'_'.$item['order_date'];
-            if(--$employee_count[$key] > 0){
+            $key = $item['employee_id'] . '_' . $item['order_date'];
+            if (--$employee_count[$key] > 0) {
                 $current_hours = round($item['total_work'] / 60, 2);
                 $current_hours = round($item['total_work'] / 60, 2);
                 $item['total_work_hours'] = $current_hours;
                 $item['total_work_hours'] = $current_hours;
                 // 关键:递减外部的 $absolute_total_hours
                 // 关键:递减外部的 $absolute_total_hours
-                $employee_work_count[$key] -= $current_hours*100;
+                $employee_work_count[$key] -= $current_hours * 100;
 
 
-            }else{
-                $item['total_work_hours'] =   round($employee_work_count[$key]/100,2);
+            } else {
+                $item['total_work_hours'] = round($employee_work_count[$key] / 100, 2);
             }
             }
             return $item;
             return $item;
         })->all();
         })->all();
@@ -139,8 +139,8 @@ class StatisticService extends Service
             if ($month) {
             if ($month) {
                 $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
                 $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
             }
             }
-            if(!isset($all_salary[$month])) $all_salary[$month] = 0;
-            $all_salary[$month] +=  $val['salary']*100;
+            if (!isset($all_salary[$month])) $all_salary[$month] = 0;
+            $all_salary[$month] += $val['salary'] * 100;
         }
         }
 //        var_dump($salary_map);die;
 //        var_dump($salary_map);die;
         // 2. 计算每个员工在每个月的全月总工时
         // 2. 计算每个员工在每个月的全月总工时
@@ -199,24 +199,24 @@ class StatisticService extends Service
         })->map(function ($group) {
         })->map(function ($group) {
             return $group->count();
             return $group->count();
         })->toArray();
         })->toArray();
-        $item_day_count = $collect->groupBy(fn($item) =>  $item['month'])
-            ->map(fn($group) => round($group->sum('work_minutes') /8/ 60))
+        $item_day_count = $collect->groupBy(fn($item) => $item['month'])
+            ->map(fn($group) => round($group->sum('work_minutes') / 8 / 60))
             ->toArray();
             ->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_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_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['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
-            $item['allocated_salary'] = round($item['allocated_salary'],2);
+            $item['allocated_salary'] = round($item['allocated_salary'], 2);
             // 如果不是最后一条
             // 如果不是最后一条
             $key = $item['month'];
             $key = $item['month'];
-            if(--$item_count[$key] > 0){
+            if (--$item_count[$key] > 0) {
                 // 关键:递减外部的 $absolute_total_hours
                 // 关键:递减外部的 $absolute_total_hours
                 $item_day_count[$key] -= $item['days'];
                 $item_day_count[$key] -= $item['days'];
-                $all_salary[$key] -=  $item['allocated_salary']*100;
+                $all_salary[$key] -= $item['allocated_salary'] * 100;
 
 
-            }else{
-                $item['days'] =   $item_day_count[$key];
-                $item['allocated_salary'] =   round($all_salary[$key]/100,2);
+            } else {
+                $item['days'] = $item_day_count[$key];
+                $item['allocated_salary'] = round($all_salary[$key] / 100, 2);
             }
             }
 
 
 
 
@@ -269,11 +269,11 @@ class StatisticService extends Service
             if ($month) {
             if ($month) {
                 $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
                 $salary_map[$val['employee_id'] . '_' . $month] = $val['salary'];
             }
             }
-            $key = $val['employee_id'].'_'.$month;
-            if(!isset($all_salary[$key])){
+            $key = $val['employee_id'] . '_' . $month;
+            if (!isset($all_salary[$key])) {
                 $all_salary[$key] = 0;
                 $all_salary[$key] = 0;
             }
             }
-            $all_salary[$key] += $val['salary']*100;
+            $all_salary[$key] += $val['salary'] * 100;
 
 
         }
         }
         // 2. 计算每个员工在每个月的全月总工时
         // 2. 计算每个员工在每个月的全月总工时
@@ -317,12 +317,12 @@ class StatisticService extends Service
             $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
             $item_month_list[$employee_key]['allocated_salary'] += $allocated_salary;
             $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
             $item_month_list[$employee_key]['work_minutes'] += $item['total_work'];
 
 
-            $key = $item['employee_id'].'_'.$item['order_month'];
-            if(!isset( $total_work_min[$key]))  $total_work_min[$key] = 0;
+            $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'];
             $total_work_min[$key] += $item['total_work'];
         }
         }
-        foreach ($total_work_min as $k=>$v){
-            $total_work_min[$k] =  round($v / 60, 2)*100;
+        foreach ($total_work_min as $k => $v) {
+            $total_work_min[$k] = round($v / 60, 2) * 100;
         }
         }
 
 
         $collect = collect($item_month_list);
         $collect = collect($item_month_list);
@@ -351,20 +351,20 @@ class StatisticService extends Service
         $employee = Employee::Clear($user, $data);
         $employee = Employee::Clear($user, $data);
         $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
         $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
 
 
-        $item_month_list = collect($item_month_list)->transform(function ($item,$index) use ($item_title_key_list, $item_code_key_list, $employee_key_list,&$total_work_min,&$all_salary,&$employee_count) {
+        $item_month_list = collect($item_month_list)->transform(function ($item, $index) use ($item_title_key_list, $item_code_key_list, $employee_key_list, &$total_work_min, &$all_salary, &$employee_count) {
             $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $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['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
             $item['employee_title'] = $employee_key_list[$item['employee_id']] ?? "未知人员({$item['employee_id']})";
             $item['work_hours'] = round($item['work_minutes'] / 60, 2);
             $item['work_hours'] = round($item['work_minutes'] / 60, 2);
             $item['total_hours'] = round($item['total_min'] / 60);
             $item['total_hours'] = round($item['total_min'] / 60);
-            $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'] =   round($all_salary[$key]/100,2);
-                $item['work_hours'] =    round($total_work_min[$key]/100,2);
+            $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'] = round($all_salary[$key] / 100, 2);
+                $item['work_hours'] = round($total_work_min[$key] / 100, 2);
             }
             }
             return $item;
             return $item;
         })->all();
         })->all();
@@ -419,6 +419,7 @@ class StatisticService extends Service
 
 
         // 3. 计算分摊天数与工资
         // 3. 计算分摊天数与工资
         $item_month_list = [];
         $item_month_list = [];
+        $device_total_depreciatio = [];
         foreach ($month_device_list as $item) {
         foreach ($month_device_list as $item) {
             $key = $item['device_id'] . '_' . $item['order_month'];
             $key = $item['device_id'] . '_' . $item['order_month'];
             $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
             $device_key = $item['order_month'] . '_' . $item['item_id'] . '_' . $item['device_id'];
@@ -434,6 +435,11 @@ class StatisticService extends Service
                     "device_id" => $item['device_id'],
                     "device_id" => $item['device_id'],
                     "total_depreciatio" => $total_depreciatio,
                     "total_depreciatio" => $total_depreciatio,
                 ];
                 ];
+                $key = $item['device_id'] . '_' . $item['order_month'];
+                $device_total_depreciatio[$key] = [
+                    'total_hours' => round($total_min/60,2)*100,
+                    'total_depreciatio' => $total_depreciatio*100
+                ];
             }
             }
 
 
 
 
@@ -448,6 +454,7 @@ class StatisticService extends Service
             $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
             $item_month_list[$device_key]['allocated_depreciatio'] += $allocated_salary;
             $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
             $item_month_list[$device_key]['work_minutes'] += $item['total_work'];
             $item_month_list[$device_key]['ratio'] = $ratio;
             $item_month_list[$device_key]['ratio'] = $ratio;
+
         }
         }
 
 
         foreach ($item_month_list as $k => $v) {
         foreach ($item_month_list as $k => $v) {
@@ -467,11 +474,33 @@ class StatisticService extends Service
         $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
         $device_key_list = $device->wherein('id', $device_ids)->pluck("title", "id")->toArray();
         $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
         $device_original_value_key_list = $device->wherein('id', $device_ids)->pluck("original_value", "id")->toArray();
 
 
-        $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $device_key_list, $device_original_value_key_list) {
+        $collect = collect($item_month_list);
+
+        $device_count = $collect->groupBy(function ($item) {
+            // 这里的 $item 是集合中的每一行数据
+            return $item['device_id'] . '_' . $item['month'];
+        })->map(function ($group) {
+            return $group->count();
+        })->toArray();
+
+        $rate_list = [];
+        $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $device_key_list, $device_original_value_key_list, &$device_total_depreciatio, &$device_count,&$rate_list) {
             $item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $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['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
             $item['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
             $item['device_title'] = $device_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
             $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
             $item['device_original'] = $device_original_value_key_list[$item['device_id']] ?? "未知人员({$item['device_id']})";
+            $key = $item['device_id'] . '_' . $item['month'];
+            if(!isset($rate_list[$key])) $rate_list[$key] = 100;
+            if (--$device_count[$key] > 0) {
+                $rate_list[$key] -= $item['ratio'] * 100;
+                $device_total_depreciatio[$key]['total_hours'] -= $item['hours'] * 100;
+                $device_total_depreciatio[$key]['total_depreciatio'] -= $item['allocated_depreciatio'] * 100;
+
+            } else {
+                $item['ratio'] = round($rate_list[$key] / 100, 2);
+                $item['hours'] = round( $device_total_depreciatio[$key]['total_hours']/ 100, 2);
+                $item['allocated_depreciatio'] = round( $device_total_depreciatio[$key]['total_depreciatio']/ 100, 2);
+            }
             return $item;
             return $item;
         })->all();
         })->all();
         return [true, $item_month_list];
         return [true, $item_month_list];