|
|
@@ -13,6 +13,9 @@ class ReportingWork extends Api
|
|
|
protected $noNeedLogin = ['*'];
|
|
|
protected $noNeedRight = ['*'];
|
|
|
|
|
|
+ /** @var bool */
|
|
|
+ private static $reportingWorkLogTableReady = false;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 根据机台编号获取大工序和人员信息
|
|
|
@@ -127,6 +130,12 @@ class ReportingWork extends Api
|
|
|
if (empty($params['part_code'])) {
|
|
|
$this->error('部件编号不能为空');
|
|
|
}
|
|
|
+ $orderInfo = db('工单_基本资料')
|
|
|
+ ->where('订单编号', $params['workorder'])
|
|
|
+ ->whereNull('Mod_rq')
|
|
|
+ ->field('订单数量')
|
|
|
+ ->find();
|
|
|
+ $manufactureQty = !empty($orderInfo) ? $orderInfo['订单数量'] : 0;
|
|
|
$data = db('工单_基础工艺资料')
|
|
|
->where('work_order', $params['workorder'])
|
|
|
->where('part_code', $params['part_code'])
|
|
|
@@ -138,6 +147,24 @@ class ReportingWork extends Api
|
|
|
usort($data, function ($a, $b) {
|
|
|
return strnatcmp((string)$a['工艺编号'], (string)$b['工艺编号']);
|
|
|
});
|
|
|
+ $reportList = db('设备_工分计酬')
|
|
|
+ ->where('work_order', $params['workorder'])
|
|
|
+ ->where('part_code', $params['part_code'])
|
|
|
+ ->whereNull('del_rq')
|
|
|
+ ->field('process_code, SUM(number) as completed_qty')
|
|
|
+ ->group('process_code')
|
|
|
+ ->select();
|
|
|
+ $completedMap = [];
|
|
|
+ foreach ($reportList as $report) {
|
|
|
+ $completedMap[trim((string)$report['process_code'])] = floatval($report['completed_qty']);
|
|
|
+ }
|
|
|
+ foreach ($data as $key => $row) {
|
|
|
+ $processCode = trim((string)$row['工艺编号']);
|
|
|
+ $completedQty = $completedMap[$processCode] ?? 0;
|
|
|
+ $data[$key]['制造数量'] = $manufactureQty;
|
|
|
+ $data[$key]['累计完成数'] = $completedQty;
|
|
|
+ $data[$key]['未完成数'] = floatval($manufactureQty) - $completedQty;
|
|
|
+ }
|
|
|
$this->success('成功', $data);
|
|
|
}
|
|
|
|
|
|
@@ -183,6 +210,7 @@ class ReportingWork extends Api
|
|
|
}
|
|
|
|
|
|
$insertData = [];
|
|
|
+ $logList = [];
|
|
|
$now = date('Y-m-d H:i:s');
|
|
|
foreach ($reportList as $idx => $item) {
|
|
|
$rowNo = $idx + 1;
|
|
|
@@ -263,16 +291,33 @@ class ReportingWork extends Api
|
|
|
'mod_rq' => null,
|
|
|
'del_rq' => null,
|
|
|
];
|
|
|
+ $workOrder = !empty($item['work_order']) ? $item['work_order'] : $item['workorder'];
|
|
|
+ $logList[] = $this->buildReportingWorkLogRow([
|
|
|
+ 'oper_type' => '新增报工',
|
|
|
+ 'oper_user' => isset($item['sys_id']) ? $item['sys_id'] : '',
|
|
|
+ 'oper_time' => $now,
|
|
|
+ 'work_order' => $workOrder,
|
|
|
+ 'majorprocess' => $item['majorprocess'],
|
|
|
+ 'part_code' => $partCode,
|
|
|
+ 'process_code' => $item['process_code'],
|
|
|
+ 'process_name' => $item['process_name'],
|
|
|
+ 'staff_no' => $item['staff_no'],
|
|
|
+ 'staff_name' => $item['staff_name'],
|
|
|
+ 'machine' => isset($item['machine']) ? $item['machine'] : '',
|
|
|
+ 'report_date' => !empty($item['date']) ? $item['date'] : date('Y-m-d'),
|
|
|
+ ], '数量', '', (string)$number);
|
|
|
}
|
|
|
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
$result = db('设备_工分计酬')->insertAll($insertData);
|
|
|
if ($result === false) {
|
|
|
- Db::rollback();
|
|
|
- $this->error('报工失败');
|
|
|
+ throw new \Exception('报工失败');
|
|
|
}
|
|
|
+ $this->writeReportingWorkLogs($logList);
|
|
|
Db::commit();
|
|
|
+ } catch (\think\exception\HttpResponseException $e) {
|
|
|
+ throw $e;
|
|
|
} catch (\Exception $e) {
|
|
|
Db::rollback();
|
|
|
$this->error('报工失败:' . $e->getMessage());
|
|
|
@@ -399,9 +444,13 @@ class ReportingWork extends Api
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 修改报工数据
|
|
|
+ * 修改工分报工数据
|
|
|
* @ApiMethod (POST)
|
|
|
- * @param array $list 报工数据
|
|
|
+ * @param int $id 报工记录ID
|
|
|
+ * @param float $number 数量
|
|
|
+ * @param float $production_hour 报工工时(可选,不传则按标准工时×数量重算)
|
|
|
+ * @param float $production_score 报工工分(可选,不传则按标准工分×数量重算)
|
|
|
+ * @param string $mod_id 修改人(可选)
|
|
|
* @return array
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
@@ -416,44 +465,87 @@ class ReportingWork extends Api
|
|
|
if (empty($params['id'])) {
|
|
|
$this->error('ID不能为空');
|
|
|
}
|
|
|
- if (empty($params['mod_id'])) {
|
|
|
- $this->error('修改人不能为空');
|
|
|
- }
|
|
|
if (!isset($params['number']) || $params['number'] === '') {
|
|
|
$this->error('产量不能为空');
|
|
|
}
|
|
|
|
|
|
+ $record = db('设备_工分计酬')
|
|
|
+ ->where('id', $params['id'])
|
|
|
+ ->whereNull('del_rq')
|
|
|
+ ->find();
|
|
|
+ if (empty($record)) {
|
|
|
+ $this->error('报工记录不存在或已删除');
|
|
|
+ }
|
|
|
+
|
|
|
$number = floatval($params['number']);
|
|
|
- $standardScore = (isset($params['standard_score']) && $params['standard_score'] !== '')
|
|
|
- ? floatval($params['standard_score']) : null;
|
|
|
- $productionScore = isset($params['production_score']) && $params['production_score'] !== ''
|
|
|
- ? floatval($params['production_score']) : null;
|
|
|
+ $standardHour = floatval($record['standard_hour']);
|
|
|
+ $standardScore = floatval($record['standard_score']);
|
|
|
|
|
|
- if ($standardScore !== null) {
|
|
|
- $productionScore = round($standardScore * $number, 4);
|
|
|
- $salary = $this->calcReportSalary($number, $standardScore);
|
|
|
- } elseif ($productionScore !== null) {
|
|
|
- $salary = round($productionScore * 0.067, 4);
|
|
|
- } else {
|
|
|
- $this->error('标准工分不能为空');
|
|
|
- }
|
|
|
+ $productionHour = round($standardHour * $number, 4);
|
|
|
+ $productionScore = round($standardScore * $number, 4);
|
|
|
+ $salary = $this->calcReportSalary($number, $standardScore);
|
|
|
+ $operTime = date('Y-m-d H:i:s');
|
|
|
+ $operUser = !empty($params['mod_id']) ? $params['mod_id'] : ($record['sys_id'] ?? '');
|
|
|
|
|
|
$data = [
|
|
|
- 'mod_id' => $params['mod_id'],
|
|
|
- 'mod_rq' => date('Y-m-d H:i:s'),
|
|
|
+ 'mod_rq' => $operTime,
|
|
|
'number' => $number,
|
|
|
- 'production_hour' => $params['production_hour'],
|
|
|
+ 'production_hour' => $productionHour,
|
|
|
'production_score' => $productionScore,
|
|
|
- 'money' => isset($params['money']) ? $params['money'] : 0,
|
|
|
'salary' => $salary,
|
|
|
];
|
|
|
- $result = db('设备_工分计酬')
|
|
|
- ->where('id', $params['id'])
|
|
|
- ->update($data);
|
|
|
- if ($result === false) {
|
|
|
- $this->error('修改失败');
|
|
|
+ if (!empty($params['mod_id'])) {
|
|
|
+ $data['mod_id'] = $params['mod_id'];
|
|
|
+ }
|
|
|
+
|
|
|
+ $logContext = [
|
|
|
+ 'report_id' => intval($record['id']),
|
|
|
+ 'oper_type' => '修改报工',
|
|
|
+ 'oper_user' => $operUser,
|
|
|
+ 'oper_time' => $operTime,
|
|
|
+ 'work_order' => $record['work_order'],
|
|
|
+ 'majorprocess' => $record['majorprocess'],
|
|
|
+ 'part_code' => $record['part_code'],
|
|
|
+ 'process_code' => $record['process_code'],
|
|
|
+ 'process_name' => $record['process_name'],
|
|
|
+ 'staff_no' => $record['staff_no'],
|
|
|
+ 'staff_name' => $record['staff_name'],
|
|
|
+ 'machine' => $record['machine'] ?? '',
|
|
|
+ 'report_date' => $record['date'] ?? null,
|
|
|
+ ];
|
|
|
+ $logList = $this->buildReportingWorkChangeLogs($logContext, [
|
|
|
+ '数量' => [$record['number'], $number],
|
|
|
+ '报工工时' => [$record['production_hour'], $productionHour],
|
|
|
+ '报工工分' => [$record['production_score'], $productionScore],
|
|
|
+ '工资' => [$record['salary'], $salary],
|
|
|
+ ]);
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $result = db('设备_工分计酬')
|
|
|
+ ->where('id', $params['id'])
|
|
|
+ ->whereNull('del_rq')
|
|
|
+ ->update($data);
|
|
|
+ if ($result === false) {
|
|
|
+ throw new \Exception('修改失败');
|
|
|
+ }
|
|
|
+ if ($result === 0) {
|
|
|
+ throw new \Exception('报工记录不存在或已删除');
|
|
|
+ }
|
|
|
+ $this->writeReportingWorkLogs($logList);
|
|
|
+ Db::commit();
|
|
|
+ } catch (\think\exception\HttpResponseException $e) {
|
|
|
+ throw $e;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
}
|
|
|
- $this->success('修改成功');
|
|
|
+ $this->success('修改成功', [
|
|
|
+ 'number' => $number,
|
|
|
+ 'production_hour' => $productionHour,
|
|
|
+ 'production_score' => $productionScore,
|
|
|
+ 'salary' => $salary,
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -474,16 +566,121 @@ class ReportingWork extends Api
|
|
|
if (empty($params['id'])) {
|
|
|
$this->error('ID不能为空');
|
|
|
}
|
|
|
+
|
|
|
+ $record = db('设备_工分计酬')
|
|
|
+ ->where('id', $params['id'])
|
|
|
+ ->whereNull('del_rq')
|
|
|
+ ->find();
|
|
|
+ if (empty($record)) {
|
|
|
+ $this->error('报工记录不存在或已删除');
|
|
|
+ }
|
|
|
+
|
|
|
+ $operTime = date('Y-m-d H:i:s');
|
|
|
+ $operUser = !empty($params['mod_id']) ? $params['mod_id'] : ($record['sys_id'] ?? '');
|
|
|
+ $logContext = [
|
|
|
+ 'report_id' => intval($record['id']),
|
|
|
+ 'oper_type' => '删除报工',
|
|
|
+ 'oper_user' => $operUser,
|
|
|
+ 'oper_time' => $operTime,
|
|
|
+ 'work_order' => $record['work_order'],
|
|
|
+ 'majorprocess' => $record['majorprocess'],
|
|
|
+ 'part_code' => $record['part_code'],
|
|
|
+ 'process_code' => $record['process_code'],
|
|
|
+ 'process_name' => $record['process_name'],
|
|
|
+ 'staff_no' => $record['staff_no'],
|
|
|
+ 'staff_name' => $record['staff_name'],
|
|
|
+ 'machine' => $record['machine'] ?? '',
|
|
|
+ 'report_date' => $record['date'] ?? null,
|
|
|
+ ];
|
|
|
+ $logList = $this->buildReportingWorkChangeLogs($logContext, [
|
|
|
+ '数量' => [$record['number'], ''],
|
|
|
+ '报工工分' => [$record['production_score'], ''],
|
|
|
+ '工资' => [$record['salary'], ''],
|
|
|
+ ]);
|
|
|
+
|
|
|
$data = [
|
|
|
- 'del_rq' => date('Y-m-d H:i:s'),
|
|
|
+ 'del_rq' => $operTime,
|
|
|
];
|
|
|
- $result = db('设备_工分计酬')
|
|
|
- ->where('id', $params['id'])
|
|
|
- ->update($data);
|
|
|
- if ($result === false) {
|
|
|
- $this->error('删除失败');
|
|
|
+ if (!empty($params['mod_id'])) {
|
|
|
+ $data['mod_id'] = $params['mod_id'];
|
|
|
+ $data['mod_rq'] = $operTime;
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $result = db('设备_工分计酬')
|
|
|
+ ->where('id', $params['id'])
|
|
|
+ ->whereNull('del_rq')
|
|
|
+ ->update($data);
|
|
|
+ if ($result === false) {
|
|
|
+ throw new \Exception('删除失败');
|
|
|
+ }
|
|
|
+ if ($result === 0) {
|
|
|
+ throw new \Exception('报工记录不存在或已删除');
|
|
|
+ }
|
|
|
+ $this->writeReportingWorkLogs($logList);
|
|
|
+ Db::commit();
|
|
|
+ } catch (\think\exception\HttpResponseException $e) {
|
|
|
+ throw $e;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Db::rollback();
|
|
|
+ $this->error($e->getMessage());
|
|
|
}
|
|
|
$this->success('删除成功');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 工分报工操作日志查询
|
|
|
+ * @ApiMethod (GET)
|
|
|
+ * @param string $workorder 订单编号(可选)
|
|
|
+ * @param int $report_id 报工记录ID(可选)
|
|
|
+ * @param string $oper_type 操作类型(可选)
|
|
|
+ * @param string $date_start 操作时间起(可选,Y-m-d)
|
|
|
+ * @param string $date_end 操作时间止(可选,Y-m-d)
|
|
|
+ * @param int $page 页码,默认1
|
|
|
+ * @param int $limit 每页条数,默认20
|
|
|
+ */
|
|
|
+ public function GetReportingWorkLog()
|
|
|
+ {
|
|
|
+ if (!$this->request->isGet()) {
|
|
|
+ $this->error('请求方法错误');
|
|
|
+ }
|
|
|
+ $this->ensureReportingWorkLogTable();
|
|
|
+
|
|
|
+ $params = $this->request->param();
|
|
|
+ $page = max(1, intval($params['page'] ?? 1));
|
|
|
+ $limit = max(1, min(200, intval($params['limit'] ?? 20)));
|
|
|
+ $offset = ($page - 1) * $limit;
|
|
|
+
|
|
|
+ $query = Db::table('设备_工分报工操作日志');
|
|
|
+ if (!empty($params['workorder'])) {
|
|
|
+ $query->where('work_order', $params['workorder']);
|
|
|
+ }
|
|
|
+ if (!empty($params['report_id'])) {
|
|
|
+ $query->where('report_id', intval($params['report_id']));
|
|
|
+ }
|
|
|
+ if (!empty($params['oper_type'])) {
|
|
|
+ $query->where('oper_type', $params['oper_type']);
|
|
|
+ }
|
|
|
+ if (!empty($params['date_start'])) {
|
|
|
+ $query->where('oper_time', '>=', $params['date_start'] . ' 00:00:00');
|
|
|
+ }
|
|
|
+ if (!empty($params['date_end'])) {
|
|
|
+ $query->where('oper_time', '<=', $params['date_end'] . ' 23:59:59');
|
|
|
+ }
|
|
|
+
|
|
|
+ $total = (int)(clone $query)->count();
|
|
|
+ $list = $query
|
|
|
+ ->order('oper_time desc,id desc')
|
|
|
+ ->limit($offset, $limit)
|
|
|
+ ->select();
|
|
|
+
|
|
|
+ $this->success('成功', [
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $page,
|
|
|
+ 'limit' => $limit,
|
|
|
+ 'list' => $list,
|
|
|
+ ]);
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -597,6 +794,102 @@ class ReportingWork extends Api
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 确保工分报工操作日志表存在
|
|
|
+ */
|
|
|
+ private function ensureReportingWorkLogTable()
|
|
|
+ {
|
|
|
+ if (self::$reportingWorkLogTableReady) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $sql = <<<SQL
|
|
|
+CREATE TABLE IF NOT EXISTS `设备_工分报工操作日志` (
|
|
|
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
|
|
|
+ `report_id` int(11) DEFAULT NULL COMMENT '报工记录ID',
|
|
|
+ `oper_type` varchar(32) NOT NULL DEFAULT '' COMMENT '操作类型',
|
|
|
+ `oper_user` varchar(64) NOT NULL DEFAULT '' COMMENT '操作人',
|
|
|
+ `oper_time` datetime NOT NULL COMMENT '操作时间',
|
|
|
+ `work_order` varchar(64) NOT NULL DEFAULT '' COMMENT '订单编号',
|
|
|
+ `majorprocess` varchar(32) NOT NULL DEFAULT '' COMMENT '大工序',
|
|
|
+ `part_code` int(11) DEFAULT NULL COMMENT '部件编号',
|
|
|
+ `process_code` varchar(32) NOT NULL DEFAULT '' COMMENT '工艺编号',
|
|
|
+ `process_name` varchar(128) NOT NULL DEFAULT '' COMMENT '工艺名称',
|
|
|
+ `staff_no` varchar(32) NOT NULL DEFAULT '' COMMENT '员工编号',
|
|
|
+ `staff_name` varchar(64) NOT NULL DEFAULT '' COMMENT '员工姓名',
|
|
|
+ `machine` varchar(64) NOT NULL DEFAULT '' COMMENT '机台编号',
|
|
|
+ `report_date` date DEFAULT NULL COMMENT '报工日期',
|
|
|
+ `change_field` varchar(32) NOT NULL DEFAULT '' COMMENT '变更字段',
|
|
|
+ `old_value` varchar(255) NOT NULL DEFAULT '' COMMENT '旧值',
|
|
|
+ `new_value` varchar(255) NOT NULL DEFAULT '' COMMENT '新值',
|
|
|
+ PRIMARY KEY (`id`),
|
|
|
+ KEY `idx_report_id` (`report_id`),
|
|
|
+ KEY `idx_work_order` (`work_order`),
|
|
|
+ KEY `idx_oper_time` (`oper_time`)
|
|
|
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='工分报工操作日志'
|
|
|
+SQL;
|
|
|
+ Db::execute($sql);
|
|
|
+ self::$reportingWorkLogTableReady = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入工分报工操作日志
|
|
|
+ * @param array $logs
|
|
|
+ */
|
|
|
+ private function writeReportingWorkLogs(array $logs)
|
|
|
+ {
|
|
|
+ if (empty($logs)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $this->ensureReportingWorkLogTable();
|
|
|
+ $result = Db::table('设备_工分报工操作日志')->insertAll($logs);
|
|
|
+ if ($result === false) {
|
|
|
+ throw new \Exception('写入工分报工操作日志失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建单条工分报工操作日志
|
|
|
+ */
|
|
|
+ private function buildReportingWorkLogRow(array $context, $changeField, $oldValue, $newValue)
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ 'report_id' => isset($context['report_id']) ? $context['report_id'] : null,
|
|
|
+ 'oper_type' => $context['oper_type'] ?? '',
|
|
|
+ 'oper_user' => $context['oper_user'] ?? '',
|
|
|
+ 'oper_time' => $context['oper_time'] ?? date('Y-m-d H:i:s'),
|
|
|
+ 'work_order' => $context['work_order'] ?? '',
|
|
|
+ 'majorprocess' => $context['majorprocess'] ?? '',
|
|
|
+ 'part_code' => isset($context['part_code']) ? $context['part_code'] : null,
|
|
|
+ 'process_code' => $context['process_code'] ?? '',
|
|
|
+ 'process_name' => $context['process_name'] ?? '',
|
|
|
+ 'staff_no' => $context['staff_no'] ?? '',
|
|
|
+ 'staff_name' => $context['staff_name'] ?? '',
|
|
|
+ 'machine' => $context['machine'] ?? '',
|
|
|
+ 'report_date' => !empty($context['report_date']) ? $context['report_date'] : null,
|
|
|
+ 'change_field' => (string)$changeField,
|
|
|
+ 'old_value' => $oldValue === null ? '' : (string)$oldValue,
|
|
|
+ 'new_value' => $newValue === null ? '' : (string)$newValue,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建字段变更日志(仅记录有变化的字段)
|
|
|
+ */
|
|
|
+ private function buildReportingWorkChangeLogs(array $context, array $changes)
|
|
|
+ {
|
|
|
+ $logs = [];
|
|
|
+ foreach ($changes as $field => $values) {
|
|
|
+ $oldValue = $values[0];
|
|
|
+ $newValue = $values[1];
|
|
|
+ if ((string)$oldValue === (string)$newValue) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $logs[] = $this->buildReportingWorkLogRow($context, $field, $oldValue, $newValue);
|
|
|
+ }
|
|
|
+ return $logs;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 报工工资:数量 × 工分 × 0.067
|
|
|
* @param mixed $number
|