Browse Source

按工序归结人工费添加月份优化

zck 5 days ago
parent
commit
45c6478274
1 changed files with 45 additions and 8 deletions
  1. 45 8
      application/api/controller/CostAccounting.php

+ 45 - 8
application/api/controller/CostAccounting.php

@@ -1096,6 +1096,7 @@ class CostAccounting extends Api
     }
 
 
+
     /**
      * 按工序归结人工费统计表
      * @return void
@@ -1112,6 +1113,8 @@ class CostAccounting extends Api
 
         // 2. 参数获取和验证
         $year = $this->request->param('year');
+        $month = $this->request->param('month');
+        
         if (empty($year)) {
             $this->error('缺少必要参数:year');
         }
@@ -1123,35 +1126,69 @@ class CostAccounting extends Api
             '烫金', '模切', '机检', '手检'
         ];
 
-        // 4. 批量查询并组装数据
+        // 4. 构建查询条件(使用传统数组方式)
+        if (!empty($month)) {
+            // 传了年份和月份,查询具体月份
+            $dateStr = $year . str_pad($month, 2, '0', STR_PAD_LEFT);
+            $where = "sys_ny = '{$dateStr}'";
+            $queryType = 'month';
+        } else {
+            // 只传了年份,查询整年
+            $where = "sys_ny like '{$year}%'";
+            $queryType = 'year';
+        }
+
+        // 5. 批量查询并组装数据
         $result = [];
         foreach ($processList as $process) {
             // 查询当前工序的数据
-            $processData = db('绩效工资汇总')
-                ->where('sys_ny', 'like', $year . '%')
-                ->where('sczl_type', 'like', '%' . $process . '%')
+            $query = db('绩效工资汇总')
+                ->where($where)
+                ->where("sczl_type like '%{$process}%'")
                 ->field('
                     SUM(班组车头产量) as 车头产量,
                     SUM(班组换算产量) as 补产产量,
                     SUM(个人计件工资) as 计件工资,
                     SUM(个人加班工资) as 加班工资,
                     sys_ny as 年月
-                ')
-                ->group('sys_ny')
-                ->select();
+                ');
+            
+            // 根据查询粒度决定是否分组
+            if ($queryType == 'year') {
+                // 查询整年数据,按年月分组
+                $processData = $query->group('sys_ny')->select();
+            } else {
+                // 查询具体月份,不需要分组
+                $processData = $query->select();
+            }
 
             // 处理查询结果
             if (!empty($processData)) {
                 foreach ($processData as &$item) {
                     $item['核算产量'] = $item['车头产量'] + $item['补产产量'];
                     $item['产量工资合计'] = $item['计件工资'] + $item['加班工资'];
+                    
+                    // 格式化年月显示
+                    if (isset($item['年月']) && strlen($item['年月']) == 6) {
+                        $item['年份'] = substr($item['年月'], 0, 4);
+                        $item['月份'] = substr($item['年月'], 4, 2);
+                    }
                 }
             }
 
             $result[$process] = $processData;
         }
 
-        // 5. 返回结果(开发调试用)
+        // 6. 添加查询参数说明
+        $result['_meta'] = [
+            'query_type' => $queryType,
+            'year' => $year,
+            'month' => $month ?: 'all',
+            'total_processes' => count($processList)
+        ];
+
+        // 7. 返回结果
         $this->success('成功', $result);
     }
+
 }