cqp 4 дней назад
Родитель
Сommit
9c6719d724
2 измененных файлов с 26 добавлено и 10 удалено
  1. 12 0
      app/Exports/ManMonthlyWorkHourSheetExport.php
  2. 14 10
      app/Service/ExportFileService.php

+ 12 - 0
app/Exports/ManMonthlyWorkHourSheetExport.php

@@ -9,6 +9,7 @@ use PhpOffice\PhpSpreadsheet\Style\Alignment;
 use PhpOffice\PhpSpreadsheet\Style\Border;
 use PhpOffice\PhpSpreadsheet\Style\Borders;
 use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
+use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
 
 class ManMonthlyWorkHourSheetExport implements WithEvents, WithTitle
 {
@@ -30,6 +31,17 @@ class ManMonthlyWorkHourSheetExport implements WithEvents, WithTitle
         return $this->monthName;
     }
 
+    /**
+     * 【核心新增】:设置 Excel 单元格显示格式
+     * 将 C 列至 AH 列(即 1 号到 31 号)设置为显示 2 位小数
+     */
+    public function columnFormats(): array
+    {
+        return [
+            'C:AH' => NumberFormat::FORMAT_NUMBER_00,
+        ];
+    }
+
     public function registerEvents(): array
     {
         return [

+ 14 - 10
app/Service/ExportFileService.php

@@ -570,7 +570,6 @@ class ExportFileService extends Service
         $sourceData = collect($result);
 
         // 2. 按月份分组,准备多 Sheet 数据
-        // 结构:[ '2024-04' => [ 'days' => 30, 'data' => [...] ], ... ]
         $monthsData = [];
 
         // 按月分组数据
@@ -578,16 +577,14 @@ class ExportFileService extends Service
             return date('Y年m月', strtotime($item['order_date']));
         });
 
-        //获取公司基本信息
+        // 获取公司基本信息
         $company = EmployeeService::getCompanyDetail($user);
 
         foreach ($groupedByMonth as $monthName => $monthItems) {
-            // A. 计算该月总天数
-            // 转换 '2024年04月' 为 '2024-04' 获取天数
             $formatMonth = str_replace(['年', '月'], ['-', ''], $monthName);
             $daysInMonth = date('t', strtotime($formatMonth . '-01'));
 
-            // B. 进一步按 项目+人员 分组,因为一行显示一个项目一个人的全月工时
+            // 按 项目+人员 分组
             $groupedByUserItem = $monthItems->groupBy(function ($item) {
                 return $item['item_code'] . '_' . $item['employee_id'];
             });
@@ -601,16 +598,23 @@ class ExportFileService extends Service
                     $first['employee_name']
                 ];
 
-                // C. 循环 1 号到该月最后一天,填充工时
-                // 将该员工该项目在这个月的记录转为 日期 => 工时 的映射
+                // 映射日期 => 记录
                 $dayMap = $records->keyBy(function($r){
                     return (int)date('d', strtotime($r['order_date']));
                 });
 
                 for ($d = 1; $d <= $daysInMonth; $d++) {
-                    $workHour = $dayMap->get($d)['total_work_hours'] ?? '';
-                    // 如果工时为 0 或空,按你要求的格式传空字符串
-                    $row[] = ($workHour > 0) ?(float) $workHour : '';
+                    $record = $dayMap->get($d);
+
+                    // ==================== 【核心改动点】 ====================
+                    // 不使用截断后的 $record['total_work_hours']
+                    // 直接使用原始分钟数 $record['total_work'] 除以 60,保留高精度浮点数给 Excel
+                    if ($record && isset($record['total_work']) && $record['total_work'] > 0) {
+                        $row[] = (float)($record['total_work'] / 60);
+                    } else {
+                        $row[] = '';
+                    }
+                    // ========================================================
                 }
 
                 $sheetRows[] = $row;