|
|
@@ -1157,10 +1157,11 @@ class WorkOrderProcess extends Api
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 确认拆分工序,修改工序状态
|
|
|
- * @param workorder 工单编号
|
|
|
- * @param process_code 工艺编号
|
|
|
- * @param sys_id 操作人
|
|
|
+ * 确认拆分工序:母工序置为已拆分,按数量生成子工序
|
|
|
+ * @param id 工艺ID
|
|
|
+ * @param number 拆分数量
|
|
|
+ * @param sys_id 操作人员
|
|
|
+ * @param sys_rq 操作时间(可选,默认当前时间)
|
|
|
*/
|
|
|
public function confirmProcessSplit()
|
|
|
{
|
|
|
@@ -1168,225 +1169,163 @@ class WorkOrderProcess extends Api
|
|
|
$this->error('请求方法错误');
|
|
|
}
|
|
|
$params = $this->request->post();
|
|
|
- if (empty($params['workorder'])) {
|
|
|
- $this->error('工单编号不能为空');
|
|
|
+ if (empty($params['id'])) {
|
|
|
+ $this->error('工艺ID不能为空');
|
|
|
}
|
|
|
- if (empty($params['process_code'])) {
|
|
|
- $this->error('工艺编号不能为空');
|
|
|
+ if (empty($params['number']) || intval($params['number']) < 2) {
|
|
|
+ $this->error('拆分工序数量不能小于2');
|
|
|
}
|
|
|
if (empty($params['sys_id'])) {
|
|
|
$this->error('操作人不能为空');
|
|
|
}
|
|
|
|
|
|
+ $splitCount = intval($params['number']);
|
|
|
+ $operTime = !empty($params['sys_rq']) ? $params['sys_rq'] : date('Y-m-d H:i:s');
|
|
|
+
|
|
|
$process = Db::table('工单_基础工艺资料')
|
|
|
- ->where('work_order', $params['workorder'])
|
|
|
- ->where('process_code', $params['process_code'])
|
|
|
- ->where('status', 0)
|
|
|
- ->whereNull('del_rq')
|
|
|
+ ->where('id', intval($params['id']))
|
|
|
+ ->where('status', 'in', [0, '0'])
|
|
|
->find();
|
|
|
if (empty($process)) {
|
|
|
$this->error('工序不存在或已确认拆分');
|
|
|
}
|
|
|
|
|
|
- $operTime = date('Y-m-d H:i:s');
|
|
|
- $logData = [
|
|
|
- 'order_no' => $params['workorder'],
|
|
|
- 'process_code' => $params['process_code'],
|
|
|
- 'change_field' => '状态',
|
|
|
- 'old_value' => '0',
|
|
|
- 'new_value' => '1',
|
|
|
- 'oper_type' => '确认拆分工序',
|
|
|
- 'oper_user_name' => $params['sys_id'],
|
|
|
- 'oper_time' => $operTime,
|
|
|
- ];
|
|
|
-
|
|
|
- Db::startTrans();
|
|
|
- try {
|
|
|
- $updateResult = Db::table('工单_基础工艺资料')
|
|
|
- ->where('id', $process['id'])
|
|
|
- ->where('status', 0)
|
|
|
- ->whereNull('del_rq')
|
|
|
- ->update(['status' => 1]);
|
|
|
- if ($updateResult === false) {
|
|
|
- throw new \Exception('更新工序状态失败');
|
|
|
- }
|
|
|
- if ($updateResult === 0) {
|
|
|
- throw new \Exception('工序不存在或已确认拆分');
|
|
|
- }
|
|
|
- $logResult = Db::name('work_order_operation_log')->insert($logData);
|
|
|
- if ($logResult === false) {
|
|
|
- throw new \Exception('写入操作日志失败');
|
|
|
- }
|
|
|
- Db::commit();
|
|
|
- } catch (\Exception $e) {
|
|
|
- Db::rollback();
|
|
|
- $this->error('修改失败');
|
|
|
- }
|
|
|
- $this->success('确认拆分工序成功');
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取工单最大工序编号
|
|
|
- * @param workorder 工单编号
|
|
|
- * @return array
|
|
|
- * @throws \think\db\exception\DataNotFoundException
|
|
|
- * @throws \think\db\exception\ModelNotFoundException
|
|
|
- * @throws \think\exception\DbException
|
|
|
- */
|
|
|
- public function getMaxProcessCode()
|
|
|
- {
|
|
|
- if (!$this->request->isGet()) {
|
|
|
- $this->error('请求方法错误');
|
|
|
- }
|
|
|
- $params = $this->request->param();
|
|
|
- if (empty($params['workorder'])) {
|
|
|
- $this->error('工单编号不能为空');
|
|
|
- }
|
|
|
- $processCode = db('工单_基础工艺资料')
|
|
|
- ->where('work_order', $params['workorder'])
|
|
|
- ->max('process_code');
|
|
|
- $this->success('最大工序编号', $processCode);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 批量新增工序资料(请求体为 JSON 数组,每条含 work_order 等字段)
|
|
|
- */
|
|
|
- public function batchAddProcess()
|
|
|
- {
|
|
|
- if (!$this->request->isPost()) {
|
|
|
- $this->error('请求方法错误');
|
|
|
- }
|
|
|
-
|
|
|
- $processList = [];
|
|
|
- $params = $this->request->post();
|
|
|
- if (isset($params[0]) && is_array($params[0])) {
|
|
|
- $processList = $params;
|
|
|
- } else {
|
|
|
- $rawBody = file_get_contents('php://input');
|
|
|
- $decoded = json_decode($rawBody, true);
|
|
|
- if (is_array($decoded) && isset($decoded[0]) && is_array($decoded[0])) {
|
|
|
- $processList = $decoded;
|
|
|
- } elseif (is_string($params) || (count($params) === 1 && is_string(reset($params)))) {
|
|
|
- $jsonStr = is_string($params) ? $params : reset($params);
|
|
|
- $decoded = json_decode($jsonStr, true);
|
|
|
- if (is_array($decoded)) {
|
|
|
- $processList = $decoded;
|
|
|
+ $workorder = $process['work_order'];
|
|
|
+ $processCodes = Db::table('工单_基础工艺资料')
|
|
|
+ ->where('work_order', $workorder)
|
|
|
+ ->whereNull('del_rq')
|
|
|
+ ->column('process_code');
|
|
|
+ $maxNumeric = 0;
|
|
|
+ foreach ($processCodes as $code) {
|
|
|
+ if (preg_match('/^(\d+)/', (string)$code, $match)) {
|
|
|
+ $num = intval($match[1]);
|
|
|
+ if ($num > $maxNumeric) {
|
|
|
+ $maxNumeric = $num;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- if (!is_array($processList) || empty($processList)) {
|
|
|
- $this->error('工艺数据不能为空');
|
|
|
- }
|
|
|
+ $baseCode = $maxNumeric + 1;
|
|
|
|
|
|
- $requiredFields = [
|
|
|
- 'work_order' => '工单编号',
|
|
|
- 'part_code' => '部件编号',
|
|
|
- 'part_name' => '部件名称',
|
|
|
- 'process_code' => '工序编号',
|
|
|
- 'process_name' => '工序名称',
|
|
|
- 'big_process' => '大工序',
|
|
|
- 'standard_hour' => '秒',
|
|
|
- 'standard_score' => '定额分',
|
|
|
- 'coefficient' => '难度系数',
|
|
|
- 'standard_minutes' => '分',
|
|
|
- 'money' => '金额',
|
|
|
- 'remark' => '备注',
|
|
|
- 'sys_id' => '操作人员',
|
|
|
- ];
|
|
|
+ $splitFields = ['standard_hour', 'standard_minutes', 'standard_score', 'money'];
|
|
|
+ $splitValues = [];
|
|
|
+ foreach ($splitFields as $field) {
|
|
|
+ $splitValues[$field] = $this->splitNumericValue($process[$field], $splitCount);
|
|
|
+ }
|
|
|
|
|
|
- $workorder = '';
|
|
|
- $now = date('Y-m-d H:i:s');
|
|
|
$insertList = [];
|
|
|
- $processCodes = [];
|
|
|
-
|
|
|
- foreach ($processList as $index => $process) {
|
|
|
- if (!is_array($process)) {
|
|
|
- $this->error('第' . ($index + 1) . '条工艺数据格式错误');
|
|
|
- }
|
|
|
- foreach ($requiredFields as $field => $label) {
|
|
|
- if (!isset($process[$field]) || $process[$field] === '' || $process[$field] === null) {
|
|
|
- $this->error('第' . ($index + 1) . '条' . $label . '不能为空');
|
|
|
- }
|
|
|
- }
|
|
|
- $itemWorkOrder = trim($process['work_order']);
|
|
|
- if ($workorder === '') {
|
|
|
- $workorder = $itemWorkOrder;
|
|
|
- } elseif ($itemWorkOrder !== $workorder) {
|
|
|
- $this->error('第' . ($index + 1) . '条工单编号与其它数据不一致');
|
|
|
- }
|
|
|
- $processCode = $process['process_code'];
|
|
|
- if (in_array((string)$processCode, $processCodes, true)) {
|
|
|
- $this->error('批量数据中存在重复的工序编号:' . $processCode);
|
|
|
- }
|
|
|
- $processCodes[] = (string)$processCode;
|
|
|
-
|
|
|
+ for ($i = 1; $i <= $splitCount; $i++) {
|
|
|
+ $newProcessCode = $baseCode + $i - 1;
|
|
|
$exists = Db::table('工单_基础工艺资料')
|
|
|
->where('work_order', $workorder)
|
|
|
- ->where('process_code', $processCode)
|
|
|
+ ->where('process_code', $newProcessCode)
|
|
|
->whereNull('del_rq')
|
|
|
->count();
|
|
|
if ($exists > 0) {
|
|
|
- $this->error('工序编号已存在:' . $processCode);
|
|
|
+ $this->error('工序编号已存在:' . $newProcessCode);
|
|
|
}
|
|
|
|
|
|
$insertList[] = [
|
|
|
'work_order' => $workorder,
|
|
|
'part_code' => $process['part_code'],
|
|
|
- 'part_name' => $process['part_name'],
|
|
|
- 'process_code' => $processCode,
|
|
|
- 'process_name' => $process['process_name'],
|
|
|
+ 'part_name' => isset($process['part_name']) ? $process['part_name'] : '',
|
|
|
+ 'process_code' => $newProcessCode,
|
|
|
+ 'process_name' => $process['process_name'] . '-' . $i,
|
|
|
'big_process' => $process['big_process'],
|
|
|
- 'standard_hour' => $process['standard_hour'],
|
|
|
- 'standard_minutes' => $process['standard_minutes'],
|
|
|
- 'standard_score' => $process['standard_score'],
|
|
|
- 'coefficient' => $process['coefficient'],
|
|
|
- 'money' => $process['money'],
|
|
|
- 'remark' => $process['remark'],
|
|
|
- 'status' => isset($process['status']) ? $process['status'] : 0,
|
|
|
- 'sys_id' => $process['sys_id'],
|
|
|
- 'sys_rq' => $now,
|
|
|
+ 'standard_hour' => $splitValues['standard_hour'][$i - 1],
|
|
|
+ 'standard_minutes' => $splitValues['standard_minutes'][$i - 1],
|
|
|
+ 'standard_score' => $splitValues['standard_score'][$i - 1],
|
|
|
+ 'money' => $splitValues['money'][$i - 1],
|
|
|
+ 'coefficient' => isset($process['coefficient']) && $process['coefficient'] !== '' && $process['coefficient'] !== null
|
|
|
+ ? $process['coefficient'] : 'C',
|
|
|
+ 'remark' => isset($process['remark']) && $process['remark'] !== null ? $process['remark'] : '',
|
|
|
+ 'status' => 0,
|
|
|
+ 'sys_id' => $params['sys_id'],
|
|
|
+ 'sys_rq' => $operTime,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- $workOrder = Db::table('工单_基本资料')
|
|
|
- ->where('订单编号', $workorder)
|
|
|
- ->where('Mod_rq', null)
|
|
|
- ->field('订单编号,计划制造工分')
|
|
|
- ->find();
|
|
|
- if (empty($workOrder)) {
|
|
|
- $this->error('工单不存在');
|
|
|
- }
|
|
|
-
|
|
|
- $existingTotal = Db::table('工单_基础工艺资料')
|
|
|
- ->where('work_order', $workorder)
|
|
|
- ->where('status', 0)
|
|
|
- ->whereNull('del_rq')
|
|
|
- ->sum('standard_score');
|
|
|
- $existingTotal = $existingTotal ? floatval($existingTotal) : 0;
|
|
|
- $newBatchStatus0Sum = 0;
|
|
|
+ $logList = [
|
|
|
+ [
|
|
|
+ 'order_no' => $workorder,
|
|
|
+ 'process_code' => $process['process_code'],
|
|
|
+ 'change_field' => '状态',
|
|
|
+ 'old_value' => '0',
|
|
|
+ 'new_value' => '1',
|
|
|
+ 'oper_type' => '确认拆分工序',
|
|
|
+ 'oper_user_name' => $params['sys_id'],
|
|
|
+ 'oper_time' => $operTime,
|
|
|
+ ],
|
|
|
+ ];
|
|
|
foreach ($insertList as $row) {
|
|
|
- if ((string)$row['status'] === '0' || $row['status'] === 0) {
|
|
|
- $newBatchStatus0Sum += floatval($row['standard_score']);
|
|
|
- }
|
|
|
- }
|
|
|
- $newTotal = $existingTotal + $newBatchStatus0Sum;
|
|
|
- if ($newTotal > floatval($workOrder['计划制造工分'])) {
|
|
|
- $this->error('工序定额分和大于制造工分,请确认之后再修改');
|
|
|
+ $logList[] = [
|
|
|
+ 'order_no' => $workorder,
|
|
|
+ 'process_code' => $row['process_code'],
|
|
|
+ 'change_field' => '工序拆分',
|
|
|
+ 'old_value' => (string)$process['process_code'],
|
|
|
+ 'new_value' => sprintf(
|
|
|
+ '秒:%s,分:%s,定额分:%s,金额:%s',
|
|
|
+ $row['standard_hour'],
|
|
|
+ $row['standard_minutes'],
|
|
|
+ $row['standard_score'],
|
|
|
+ $row['money']
|
|
|
+ ),
|
|
|
+ 'oper_type' => '确认拆分工序',
|
|
|
+ 'oper_user_name' => $params['sys_id'],
|
|
|
+ 'oper_time' => $operTime,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
+ $updateResult = Db::table('工单_基础工艺资料')
|
|
|
+ ->where('id', $process['id'])
|
|
|
+ ->where('status', 'in', [0, '0'])
|
|
|
+ ->update(['status' => 1]);
|
|
|
+ if ($updateResult === false) {
|
|
|
+ throw new \Exception('更新母工序状态失败');
|
|
|
+ }
|
|
|
+ if ($updateResult === 0) {
|
|
|
+ throw new \Exception('母工序状态已变更,请刷新后重试');
|
|
|
+ }
|
|
|
$insertResult = Db::name('工单_基础工艺资料')->insertAll($insertList);
|
|
|
- if ($insertResult === false) {
|
|
|
- throw new \Exception('拆分工序失败');
|
|
|
+ if ($insertResult === false || $insertResult === 0) {
|
|
|
+ throw new \Exception('拆分工序新增失败');
|
|
|
+ }
|
|
|
+ $logResult = Db::name('work_order_operation_log')->insertAll($logList);
|
|
|
+ if ($logResult === false || $logResult === 0) {
|
|
|
+ throw new \Exception('写入操作日志失败,请确认日志表已包含 process_code 字段');
|
|
|
}
|
|
|
Db::commit();
|
|
|
+ } catch (\think\exception\HttpResponseException $e) {
|
|
|
+ throw $e;
|
|
|
} catch (\Exception $e) {
|
|
|
Db::rollback();
|
|
|
- $this->error('拆分工序资料失败');
|
|
|
+ $this->error('修改失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ $this->success('确认拆分工序成功', ['count' => $splitCount]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按拆分数量分配数值:不能整除时第一条为剩余后的最大值,其余为向下取整的平均值
|
|
|
+ * @param mixed $total
|
|
|
+ * @param int $count
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ private function splitNumericValue($total, $count)
|
|
|
+ {
|
|
|
+ $count = intval($count);
|
|
|
+ if ($count <= 0) {
|
|
|
+ return [];
|
|
|
}
|
|
|
- $this->success('拆分工序资料成功', ['count' => count($insertList)]);
|
|
|
+ $total = floatval($total);
|
|
|
+ $avg = floor($total / $count * 100) / 100;
|
|
|
+ $first = round($total - $avg * ($count - 1), 2);
|
|
|
+ $values = [$first];
|
|
|
+ for ($i = 1; $i < $count; $i++) {
|
|
|
+ $values[] = $avg;
|
|
|
+ }
|
|
|
+ return $values;
|
|
|
}
|
|
|
+
|
|
|
|
|
|
}
|