Quellcode durchsuchen

重新计算工资

unknown vor 1 Woche
Ursprung
Commit
b712c5cbf9
1 geänderte Dateien mit 55 neuen und 0 gelöschten Zeilen
  1. 55 0
      application/api/controller/ReportingWork.php

+ 55 - 0
application/api/controller/ReportingWork.php

@@ -520,6 +520,61 @@ class ReportingWork extends Api
         $this->success('成功', $rows);
     }
 
+    /**
+     * 按新公式批量重算历史报工工资(数量 × 工分 × 0.067)
+     * @ApiMethod (POST)
+     * @param string workorder 可选,限定工单编号
+     * @param string date_start 可选,报工日期起(Y-m-d)
+     * @param string date_end 可选,报工日期止(Y-m-d)
+     */
+    public function recalculateHistoricalSalary()
+    {
+        if (!$this->request->isPost()) {
+            $this->error('请求方法错误');
+        }
+        $params = $this->request->post();
+
+        $applyFilters = function ($query) use ($params) {
+            if (!empty($params['workorder'])) {
+                $query->where('work_order', $params['workorder']);
+            }
+            if (!empty($params['date_start'])) {
+                $query->where('date', '>=', $params['date_start']);
+            }
+            if (!empty($params['date_end'])) {
+                $query->where('date', '<=', $params['date_end']);
+            }
+            return $query;
+        };
+
+        $total = (int)$applyFilters(Db::table('设备_工分计酬')->whereNull('del_rq'))->count();
+        if ($total === 0) {
+            $this->error('没有需要重算的报工数据');
+        }
+
+        Db::startTrans();
+        try {
+            $affected = $applyFilters(Db::table('设备_工分计酬')->whereNull('del_rq'))->update([
+                'production_score' => Db::raw('ROUND(`number` * `standard_score`, 4)'),
+                'salary' => Db::raw('ROUND(`number` * `standard_score` * 0.067, 4)'),
+            ]);
+            if ($affected === false) {
+                throw new \Exception('批量更新失败');
+            }
+            Db::commit();
+        } catch (\think\exception\HttpResponseException $e) {
+            throw $e;
+        } catch (\Exception $e) {
+            Db::rollback();
+            $this->error('重算失败:' . $e->getMessage());
+        }
+
+        $this->success('重算成功', [
+            'matched' => $total,
+            'updated' => (int)$affected,
+        ]);
+    }
+
     /**
      * 报工工资:数量 × 工分 × 0.067
      * @param mixed $number