gogs před 2 měsíci
rodič
revize
60f6c006b3
1 změnil soubory, kde provedl 49 přidání a 15 odebrání
  1. 49 15
      app/Service/StatisticService.php

+ 49 - 15
app/Service/StatisticService.php

@@ -977,7 +977,7 @@ class StatisticService extends Service
                         "voucher_remark" => $v['voucher_remark'],
                         "voucher_amount" => $v['voucher_amount'],
                         "aggregation_amount" => $v['aggregation_amount'],
-                        "total_amount" => $v['type'] == 1 ? $item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary'] : ($device_depreciation[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_depreciation'] ?? 0),
+                        "total_amount" => $v['type'] == 1 ?round( $item_salary[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_salary']/100,2) : (isset($device_depreciation[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_depreciation']) ?round($device_depreciation[$vv['id'] . "_" . date("Y-m", $v['voucher_date'])]['allocated_depreciation']/100,2) : 0),
                         "fee_id" => $v['fee_id'],
                         "entrust1_amount" => $v['entrust1_amount'],
                         "entrust2_amount" => $v['entrust2_amount'],
@@ -1128,7 +1128,7 @@ class StatisticService extends Service
         }
 
         // 3. 计算分摊天数与工资
-        $item_year_list = [];
+//        $item_year_list = [];
         $item_list = [];
         foreach ($month_employee_list as $item) {
             $key = $item['employee_id'] . '_' . $item['order_month'];
@@ -1178,12 +1178,12 @@ class StatisticService extends Service
         })->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'];
+            $key = $v['item_id'] . '_' . $v['month'];
+            if(!isset($return_item_list[$key])) $return_item_list[$key]['allocated_salary'] = 0;
+            $return_item_list[$key]['allocated_salary'] += $v['allocated_salary'];
         }
 
-        return $item_year_list;
+        return $return_item_list;
     }
 
     public function auxiliaryDevice($user, $data, $month_start, $month_end)
@@ -1212,10 +1212,12 @@ class StatisticService extends Service
         //查询所有项目人员的工时比例
         //汇总每个人每个月工资
         $depreciatio_map = [];
+        $all_depreciatio = [];
         foreach ($month_device_salary as $val) {
             $month = $monthly_dd_order_key_list[$val['main_id']] ?? '';
             if ($month) {
                 $depreciatio_map[$val['device_id'] . '_' . $month] = $val['depreciation_amount'];
+                $all_depreciatio[$val['device_id'] . '_' . $month] = $val['depreciation_amount']*100;
             }
         }
         // 2. 计算每个员工在每个月的全月总工时
@@ -1230,26 +1232,58 @@ class StatisticService extends Service
 
         // 3. 计算分摊天数与工资
         $item_year_list = [];
+        $item_list = [];
         foreach ($month_device_list as $item) {
-            $key = $item['device_id'] . '_' . $row['order_month'];
-            $device_key = $item['item_id'] . '_' . $item['order_month'];
+            $key = $item['device_id'] . '_' . $item['order_month'];
+//            $device_key = $item['item_id'] . '_' . $item['order_month'];
             $total_depreciatio = $depreciatio_map[$key] ?? 0;
             $total_min = $device_monthly_total_min[$key] ?? 0;
-            if (!isset($item_year_list[$device_key])) {
-                $item_year_list[$device_key] = [
-                    "allocated_depreciation" => 0,
-                ];
-            }
+//            if (!isset($item_year_list[$device_key])) {
+//                $item_year_list[$device_key] = [
+//                    "allocated_depreciation" => 0,
+//                ];
+//            }
 
 
             // B. 计算工资分摊:(项目工时 / 月总工时) * 月工资
             if ($total_min > 0) {
                 $ratio = round($item['total_work'] / $total_min, 3);
-                $allocated_salary = round($ratio * $total_depreciatio, 2);
+                $allocated_salary = round($ratio * $total_depreciatio, 2)*100;
             } else {
                 $allocated_salary = 0;
             }
-            $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
+//            $item_year_list[$device_key]['allocated_depreciation'] += $allocated_salary;
+            $item_list[] = [
+                'device_id' => $item['device_id'],
+                'depreciation' => $allocated_salary,
+                'month' => $item['order_month'],
+                'item_id' => $item['item_id'],
+            ];
+        }
+        $collect = collect($item_list);
+        $device_count = $collect->groupBy(function ($item) {
+            // 这里的 $item 是集合中的每一行数据
+            return $item['device_id'] . '_' . $item['month'];
+        })->map(function ($group) {
+            return $group->count();
+        })->toArray();
+
+        $month_employee_list = $collect->transform(function ($item) use (&$device_count,&$all_depreciatio) {
+
+            // 如果不是最后一条
+            $key = $item['device_id'] . '_' . $item['month'];
+            if (--$device_count[$key] > 0) {
+                $all_depreciatio[$key] -= $item['depreciation'];
+            } else {
+                $item['depreciation'] = $all_depreciatio[$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]['depreciation'] = 0;
+            $return_item_list[$key]['depreciation'] += $v['depreciation'];
         }
 
         return $item_year_list;