Parcourir la source

Merge remote-tracking branch 'origin/master'

cqp il y a 1 mois
Parent
commit
f9e8138bfd
2 fichiers modifiés avec 127 ajouts et 104 suppressions
  1. 25 12
      app/Service/Statistic/StatisticCommonService.php
  2. 102 92
      app/Service/StatisticService.php

+ 25 - 12
app/Service/Statistic/StatisticCommonService.php

@@ -177,7 +177,7 @@ class StatisticCommonService extends Service
             ->pluck('month_str', 'id')
             ->pluck('month_str', 'id')
             ->toArray();
             ->toArray();
         $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
         $month_employee_salary = MonthlyPsOrderDetails::wherein('main_id', $monthly_ps_order_ids)
-            ->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
+            ->where('del_time',0)->select("employee_id",DB::raw("(base_salary + performance_salary + bonus + other) as salary"), "main_id")
             ->get()->toArray();
             ->get()->toArray();
         return collect($month_employee_salary)->mapWithKeys(function ($val) use ($monthly_ps_order_key_list) {
         return collect($month_employee_salary)->mapWithKeys(function ($val) use ($monthly_ps_order_key_list) {
             $month = $monthly_ps_order_key_list[$val['main_id']] ?? null;
             $month = $monthly_ps_order_key_list[$val['main_id']] ?? null;
@@ -286,7 +286,10 @@ class StatisticCommonService extends Service
      */
      */
     public function calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,$key1,$key2){
     public function calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,$key1,$key2){
         $item_month_list = [];
         $item_month_list = [];
+        $all_salary = [];
+        $all_key_salary = [];
         foreach ($month_employee_list as $item) {
         foreach ($month_employee_list as $item) {
+
             $key = collect($key1)->map(fn($k) => $item[$k] ?? '')->implode('_');
             $key = collect($key1)->map(fn($k) => $item[$k] ?? '')->implode('_');
             $item_key = collect($key2)->map(fn($k) => $item[$k] ?? '')->implode('_');
             $item_key = collect($key2)->map(fn($k) => $item[$k] ?? '')->implode('_');
             if (!isset($item_month_list[$item_key])) {
             if (!isset($item_month_list[$item_key])) {
@@ -304,11 +307,18 @@ class StatisticCommonService extends Service
             }
             }
             $total_min = $employee_monthly_total_min[$key] ?? 0;
             $total_min = $employee_monthly_total_min[$key] ?? 0;
             $total_salary = $salary_map[$key] ?? 0;
             $total_salary = $salary_map[$key] ?? 0;
+            if(!isset($all_key_salary[$key])) {
+                if(!isset($all_salary[$item['order_month']])) $all_salary[$item['order_month']] = 0;
+                $all_salary[$item['order_month']] += $total_salary;
+                $all_key_salary[$key] = 1;
+            }
+            $item_month_list[$item_key]['total_salary'] = $total_salary;
+            $item_month_list[$item_key]['total_hours'] = round($total_min/60,2);
             $item_month_list[$item_key]['total_min'] += $total_salary;
             $item_month_list[$item_key]['total_min'] += $total_salary;
             // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
             // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
             if ($total_min > 0) {
             if ($total_min > 0) {
-                $ratio = $item['total_work'] / $total_min;
-                $allocated_salary = round($ratio * $total_salary, 2);
+                $ratio = round($item['total_work'] / $total_min,2);
+                $allocated_salary = round($ratio * $total_salary);
             } else {
             } else {
                 $allocated_salary = 0;
                 $allocated_salary = 0;
                 $ratio = 0;
                 $ratio = 0;
@@ -317,7 +327,7 @@ class StatisticCommonService extends Service
             $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
             $item_month_list[$item_key]['allocated_salary'] += $allocated_salary;
             $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
             $item_month_list[$item_key]['work_minutes'] += $item['total_work'];
         }
         }
-        return $item_month_list;
+        return [$item_month_list,$all_salary];
     }
     }
 
 
 
 
@@ -387,20 +397,23 @@ class StatisticCommonService extends Service
 //                ]
 //                ]
 //        ];
 //        ];
         foreach ($word_keys as $k => $v) {
         foreach ($word_keys as $k => $v) {
-
-
-
             if($v['type'] == 'ratio'&&!isset($sum[$k][$key])) $sum[$k][$key] = 100;
             if($v['type'] == 'ratio'&&!isset($sum[$k][$key])) $sum[$k][$key] = 100;
             if( !isset($sum[$k][$key]))  $sum[$k][$key] = 0;
             if( !isset($sum[$k][$key]))  $sum[$k][$key] = 0;
-            if (--$count[$key] > 0) {
+            if (--$count[$k][$key] > 0) {
                 if($v['type'] == "hour") $current_hours = round($item[$v['key']] / 60, 2);
                 if($v['type'] == "hour") $current_hours = round($item[$v['key']] / 60, 2);
                 elseif($v['type'] == "money") $current_hours = round($item[$v['key']] / 100, 2);
                 elseif($v['type'] == "money") $current_hours = round($item[$v['key']] / 100, 2);
                 else $current_hours = $item[$v['key']];
                 else $current_hours = $item[$v['key']];
-                $item[$v['value']] = $current_hours;
-                $sum[$k][$key] -= $current_hours * 100;
+                if($v['type'] == "100b") {
+                    $sum[$k][$key] -= $current_hours ;
+                    $item[$v['value']] = $current_hours/100;
+                }
+                else{
+                    $sum[$k][$key] -= $current_hours * 100;
+                    $item[$v['value']] = $current_hours;
+                }
             } else {
             } else {
-
-                $item[$v['value']] = round($sum[$k][$key] / 100, 2);
+                if($v['type'] == "day")   $item[$v['value']] = $sum[$k][$key];
+                else $item[$v['value']] = round($sum[$k][$key] / 100, 2);
             }
             }
 
 
         }
         }

+ 102 - 92
app/Service/StatisticService.php

@@ -38,22 +38,24 @@ class StatisticService extends StatisticCommonService
         $employee_count = $this->calculateCount($day_employee_list,$keys);
         $employee_count = $this->calculateCount($day_employee_list,$keys);
         $employee_work_count = $this->calculateSumForHour($day_employee_list,$keys,"total_work");
         $employee_work_count = $this->calculateSumForHour($day_employee_list,$keys,"total_work");
         $collection = collect($day_employee_list);
         $collection = collect($day_employee_list);
-        $month_employee_list = $collection->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list, &$employee_work_count, &$employee_count,$keys) {
+        $sums =  ["employee_work_count"=>$employee_work_count];
+        $word_keys = [
+            "employee_work_count" =>
+                [
+                    "key" => "total_work",
+                    "value" => "total_work_hours",
+                    "type" => "hour",
+                ]
+        ];
+        $employee_counts = ['employee_work_count' => $employee_count];
+        $month_employee_list = $collection->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list, &$employee_work_count, &$employee_counts,$keys,$word_keys,&$sums) {
             $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
             $item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
             $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']})";
             // 如果不是最后一条
             // 如果不是最后一条
             $key = collect($keys)->map(fn($k) => $item[$k] ?? '')->implode('_');
             $key = collect($keys)->map(fn($k) => $item[$k] ?? '')->implode('_');
-            $sums =  ["employee_work_count"=>$employee_work_count];
-            $word_keys = [
-                "employee_work_count" =>
-                    [
-                        "key" => "total_work",
-                        "value" => "total_work_hours",
-                        "type" => "hour",
-                    ]
-            ];
-            $this->calculateClosure($key,$item,$employee_count,$sums,$word_keys);
+
+            $this->calculateClosure($key,$item,$employee_counts,$sums,$word_keys);
             return $item;
             return $item;
         })->all();
         })->all();
         return [true, $month_employee_list];
         return [true, $month_employee_list];
@@ -74,8 +76,7 @@ class StatisticService extends StatisticCommonService
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,$keys,"total_work");
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,$keys,"total_work");
 
 
         // 3. 计算分摊比例
         // 3. 计算分摊比例
-        $item_month_list = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id"]);
-
+        list($item_month_list,$all_salary) = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id"]);
         //计算天数
         //计算天数
         foreach ($item_month_list as $k => $v) {
         foreach ($item_month_list as $k => $v) {
             $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
             $item_month_list[$k]['days'] = round($v['work_minutes'] / 8 / 60);
@@ -96,42 +97,43 @@ class StatisticService extends StatisticCommonService
             return $group->count();
             return $group->count();
         })->toArray();
         })->toArray();
         $item_day_count = $collect->groupBy(fn($item) => $item['month'])
         $item_day_count = $collect->groupBy(fn($item) => $item['month'])
-            ->map(fn($group) => round($group->sum('work_minutes') / 8 / 60))
-            ->toArray();
-
-        $all_salary = collect($salary_map)
-            ->groupBy(function ($value, $key) {
-                // 1. 提取下划线后面的内容 (例如: 2024-02)
-                return explode('_', $key)[1];
-            })
-            ->map(function ($group) {
-                // 2. 对每个分组内的数值进行求和
-                return $group->sum();
-            })
+            ->map(fn($group) => round($group->sum('work_minutes') / 8 / 60)*100)
             ->toArray();
             ->toArray();
-
-        $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, &$all_salary, &$item_day_count, $item_count) {
+//        $all_salary = collect($salary_map)
+//            ->groupBy(function ($value, $key) {
+//                // 1. 提取下划线后面的内容 (例如: 2024-02)
+//                return explode('_', $key)[1];
+//            })
+//            ->map(function ($group) {
+//                // 2. 对每个分组内的数值进行求和
+//                return $group->sum();
+//            })
+//            ->toArray();
+        $item_counts = [
+            "all_salary" => $item_count,
+            "item_day_sum" => $item_count,
+        ];
+        $sums =  ["item_day_sum"=>$item_day_count,"all_salary"=>$all_salary];
+        $word_keys = [
+            "all_salary" =>
+                [
+                    "key" => "allocated_salary",
+                    "value" => "allocated_salary",
+                    "type" => "money",
+                ],
+            "item_day_sum" =>
+                [
+                    "key" => "days",
+                    "value" => "days",
+                    "type" => "",
+                ],
+        ];
+
+        $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, &$item_counts,&$sums,&$word_keys) {
             $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']/100, 2);
             $key = $item['month'];
             $key = $item['month'];
-            // 如果不是最后一条
-            $sums =  ["item_day_sum"=>$item_day_count,"all_salary"=>$all_salary];
-            $word_keys = [
-                "all_salary" =>
-                    [
-                        "key" => "allocated_salary",
-                        "value" => "allocated_salary",
-                        "type" => "money",
-                    ],
-                "item_day_sum" =>
-                    [
-                        "key" => "days",
-                        "value" => "days",
-                        "type" => "",
-                    ],
-            ];
-            $this->calculateClosure($key,$item,$item_count,$sums,$word_keys);
+            $this->calculateClosure($key,$item,$item_counts,$sums,$word_keys);
             return $item;
             return $item;
         })->all();
         })->all();
         return [true, $item_month_list];
         return [true, $item_month_list];
@@ -151,8 +153,7 @@ class StatisticService extends StatisticCommonService
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,$keys,"total_work");
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,$keys,"total_work");
         //查询所有项目人员的工时比例
         //查询所有项目人员的工时比例
         // 3. 计算分摊比例
         // 3. 计算分摊比例
-        $item_month_list = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id","employee_id"]);
-
+        list($item_month_list,) = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id","employee_id"]);
         $collect = collect($item_month_list);
         $collect = collect($item_month_list);
         $employee_count = $collect->groupBy(function ($item) {
         $employee_count = $collect->groupBy(function ($item) {
             // 这里的 $item 是集合中的每一行数据
             // 这里的 $item 是集合中的每一行数据
@@ -177,30 +178,35 @@ class StatisticService extends StatisticCommonService
             // 注意:round 会根据你的精度要求处理小数
             // 注意:round 会根据你的精度要求处理小数
             return round($value / 60) * 100;
             return round($value / 60) * 100;
         })->toArray();
         })->toArray();
-
-        $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $employee_key_list, &$month_total_hour, &$salary_map, &$employee_count) {
+        $sums =  ["work_hours"=>$month_total_hour,"allocated_salary"=>$salary_map];
+        $word_keys = [
+            "allocated_salary" =>
+                [
+                    "key" => "allocated_salary",
+                    "value" => "allocated_salary",
+                    "type" => "100b",
+                ],
+            "work_hours" =>
+                [
+                    "key" => "work_minutes",
+                    "value" => "work_hours",
+                    "type" => "hour",
+                ],
+        ];
+        $counts = [];
+        foreach ($word_keys as $k=>$v){
+            $counts[$k] = $employee_count;
+        }
+        $item_month_list = collect($item_month_list)->transform(function ($item) use ($item_title_key_list, $item_code_key_list, $employee_key_list, &$sums, &$counts,$word_keys) {
             $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['total_hours'] = round($item['total_min'] / 60);
+//            $item['total_hours'] = round($item['work_minutes'] / 60,2);
+            $item['total_salary'] = round($item['total_salary'] / 100,2);
             $key = $item['employee_id'] . '_' . $item['month'];
             $key = $item['employee_id'] . '_' . $item['month'];
 //            // 如果不是最后一条
 //            // 如果不是最后一条
-            $sums =  ["work_hours"=>$month_total_hour,"allocated_salary"=>$salary_map];
-            $word_keys = [
-                "allocated_salary" =>
-                    [
-                        "key" => "allocated_salary",
-                        "value" => "allocated_salary",
-                        "type" => "money",
-                    ],
-                "work_hours" =>
-                    [
-                        "key" => "work_minutes",
-                        "value" => "work_hours",
-                        "type" => "hour",
-                    ],
-            ];
-            $this->calculateClosure($key,$item,$employee_count,$sums,$word_keys);
+
+            $this->calculateClosure($key,$item,$counts,$sums,$word_keys);
             return $item;
             return $item;
         })->all();
         })->all();
         return [true, $item_month_list];
         return [true, $item_month_list];
@@ -234,14 +240,40 @@ class StatisticService extends StatisticCommonService
 
 
         $collect = collect($item_month_list);
         $collect = collect($item_month_list);
 
 
+        $word_keys = [
+            "total_depreciation" =>
+                [
+                    "key" => "allocated_depreciation",
+                    "value" => "allocated_depreciation",
+                    "type" => "money",
+                ],
+            "total_hours" =>
+                [
+                    "key" => "hours",
+                    "value" => "hours",
+                    "type" => "",
+                ],
+            "ratio" =>
+                [
+                    "key" => "ratio",
+                    "value" => "ratio",
+                    "type" => "ratio",
+                ],
+        ];
         $device_count = $collect->groupBy(function ($item) {
         $device_count = $collect->groupBy(function ($item) {
             // 这里的 $item 是集合中的每一行数据
             // 这里的 $item 是集合中的每一行数据
             return $item['device_id'] . '_' . $item['month'];
             return $item['device_id'] . '_' . $item['month'];
         })->map(function ($group) {
         })->map(function ($group) {
             return $group->count();
             return $group->count();
         })->toArray();
         })->toArray();
+        $device_counts = [];
+        foreach ($word_keys as  $k=>$v){
+            $device_counts[$k] = $device_count;
+        }
+        $device_total_depreciation['ratio'] = [];
+
 //        dd($item_month_list);
 //        dd($item_month_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_depreciation, &$device_count,&$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_depreciation, &$device_counts,&$rate_list,$word_keys) {
             $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']})";
@@ -250,29 +282,7 @@ class StatisticService extends StatisticCommonService
             $item['total_hours'] = round($item['total_min'] / 60, 1);
             $item['total_hours'] = round($item['total_min'] / 60, 1);
 
 
             $key = $item['device_id'] . '_' . $item['month'];
             $key = $item['device_id'] . '_' . $item['month'];
-            $device_total_depreciation['ratio'] = [];
-//            // 如果不是最后一条
-            $word_keys = [
-                "total_depreciation" =>
-                    [
-                        "key" => "allocated_depreciation",
-                        "value" => "allocated_depreciation",
-                        "type" => "money",
-                    ],
-                "total_hours" =>
-                    [
-                        "key" => "hours",
-                        "value" => "hours",
-                        "type" => "",
-                    ],
-                "ratio" =>
-                    [
-                        "key" => "ratio",
-                        "value" => "ratio",
-                        "type" => "ratio",
-                    ],
-            ];
-            $this->calculateClosure($key,$item,$device_count,$device_total_depreciation,$word_keys);
+            $this->calculateClosure($key,$item,$device_counts,$device_total_depreciation,$word_keys);
             return $item;
             return $item;
         })->all();
         })->all();
         return [true, $item_month_list];
         return [true, $item_month_list];
@@ -332,7 +342,7 @@ class StatisticService extends StatisticCommonService
         // 2. 计算每个员工在每个月的全月总工时
         // 2. 计算每个员工在每个月的全月总工时
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,["employee_id","order_month"],"total_work");
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,["employee_id","order_month"],"total_work");
         // 2. 计算分摊天数与工资
         // 2. 计算分摊天数与工资
-        $item_list = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id","employee_id"]);
+        list($item_list,) = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,["employee_id","order_month"],["order_month","item_id","employee_id"]);
         $collect = collect($item_list);
         $collect = collect($item_list);
         $employee_count = $collect->groupBy(function ($item) {
         $employee_count = $collect->groupBy(function ($item) {
             // 这里的 $item 是集合中的每一行数据
             // 这里的 $item 是集合中的每一行数据
@@ -700,7 +710,7 @@ class StatisticService extends StatisticCommonService
         $key = ["employee_id","order_month"];
         $key = ["employee_id","order_month"];
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,$key,"total_work");
         $employee_monthly_total_min = $this->calculateSum($month_employee_list,$key,"total_work");
         // 3. 计算分摊天数与工资
         // 3. 计算分摊天数与工资
-        $item_list = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,$key,["employee_id","month","item_id"]);
+        list($item_list,) = $this->calculateRatioForMonth($month_employee_list,$employee_monthly_total_min,$salary_map,$key,["employee_id","month","item_id"]);
         $collect = collect($item_list);
         $collect = collect($item_list);
         $employee_count = $collect->groupBy(function ($item) {
         $employee_count = $collect->groupBy(function ($item) {
             // 这里的 $item 是集合中的每一行数据
             // 这里的 $item 是集合中的每一行数据