cqp 1 month ago
parent
commit
a589aeb32d
2 changed files with 27 additions and 1 deletions
  1. 5 0
      app/Exports/ExportOrder.php
  2. 22 1
      app/Service/ExportFileService.php

+ 5 - 0
app/Exports/ExportOrder.php

@@ -144,6 +144,11 @@ class ExportOrder extends DefaultValueBinder implements WithCustomValueBinder ,
                 $sheet->getStyle('A1:E1')->applyFromArray($styleArray);
                 $sheet->getStyle('A2:B2')->applyFromArray($styleArray);
                 $sheet->getStyle('A3:B3')->applyFromArray($styleArray);
+
+                $lastRow = $sheet->getHighestRow();
+                $range = 'A' . $lastRow . ':B' . $lastRow;
+                $sheet->mergeCells($range);
+                $sheet->getStyle($range)->applyFromArray($styleArray);
             },
         ];
     }

+ 22 - 1
app/Service/ExportFileService.php

@@ -203,6 +203,7 @@ class ExportFileService extends Service
             $dayOfWeek = date('D', strtotime("$year-$mon-" . str_pad($d, 2, '0', STR_PAD_LEFT)));
             $weekdays[] = $weekMap[$dayOfWeek];
         }
+        $dates[] = "合计";
 
         $employee_id = $ergs['employee_id'] ?? [];
         // 获取员工列表
@@ -232,21 +233,41 @@ class ExportFileService extends Service
 
         // 重组数据
         $kqData = [];
+        $columnSums = array_fill(0, $daysInMonth + 3, 0); // 初始化列总和数组 (+3 是因为有编号、姓名和合计列)
+
         foreach($employees as $emp){
             $row = [$emp['number'], $emp['emp_name']];
+            $sum = 0;
             for($d=1;$d<=$daysInMonth;$d++){
                 $workHour = "";
                 foreach($kqList as $k){
                     if($k['number']==$emp['number'] && intval($k['day'])==$d && $k['work_hour'] > 0){
                         $workHour = $k['work_hour'];
+                        $sum += $workHour;
+
+                        // 累加到列总和
+                        $columnIndex = $d + 1; // +1 因为前两列是编号和姓名
+                        $columnSums[$columnIndex] += $workHour;
                         break;
                     }
                 }
                 $row[] = $workHour;
             }
+            $row[] = $sum;
             $kqData[] = $row;
         }
 
+        // 添加合计行
+        $totalRow = ["合计", ""]; // 前两列为"合计"和空字符串
+        for($d=1;$d<=$daysInMonth;$d++){
+            $columnIndex = $d + 1;
+            $totalRow[] = $columnSums[$columnIndex];
+        }
+        $totalRow[] = array_sum(array_slice($columnSums, 2, $daysInMonth)); // 计算总工时列的总和
+
+        // 将合计行添加到数据末尾
+        $kqData[] = $totalRow;
+
         // 构建导出表头
         $header = [];
         // 第一行: 年月
@@ -256,7 +277,7 @@ class ExportFileService extends Service
         // 第三行: 星期
         $header[] = array_merge(['星期',''],$weekdays);
         $header[] = ['工号','姓名'];
-//dd($header);
+
         return $this->saveExportData($kqData, $header,'kq2');
     }