request->isGet()) { $this->error('请求方法错误'); } $params = $this->request->param(); if (empty($params['machine'])) { $this->error('机台编号不能为空'); } //获取大工序 $majorprocess = db('设备_基本资料') ->where('设备编号', $params['machine']) ->field('生产工序,设备编组') ->find(); if (empty($majorprocess)) { $this->error('设备信息'); } //小组信息 // $person = db('人员_小组资料') // ->where('team_name', $majorprocess['设备编组']) // ->where('status', 1) // ->field('team_name as 小组名称,staff_no as 员工编号,staff_name as 员工姓名') // ->select(); $person = Db::name('人员_小组资料')->alias('a') ->field(' b.staff_no as 员工编号, b.staff_name as 员工姓名, a.team_name as 小组名称 ') ->join('人员_基本资料 b', 'a.staff_no = b.staff_no') ->where('a.team_name', $majorprocess['设备编组']) ->whereNull('a.mod_rq') ->whereNull('b.mod_rq') ->order('a.id asc') ->select(); if (empty($person)) { $this->error('未找到人员信息'); } $data['majorprocess'] = $majorprocess; $data['person'] = $person; $this->success('成功', $data); } /** * 获取订单工艺数据 * @ApiMethod (GET) * @param string $workorder 工单编号 * @param string $majorprocess 大工序 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * */ public function GetOrderProcess() { if (!$this->request->isGet()) { $this->error('请求方法错误'); } $params = $this->request->param(); if (empty($params['workorder'])) { $this->error('工单编号不能为空'); } if (empty($params['majorprocess'])) { $this->error('大工序不能为空'); } $data = db('工单_部件资料') ->where('work_order', $params['workorder']) ->where('part_type', $params['majorprocess']) ->whereNull('del_rq') ->where('status', 1) ->field('part_code as 部件编号,part_name as 部件名称') ->select(); usort($data, function ($a, $b) { return strnatcmp((string)$a['部件编号'], (string)$b['部件编号']); }); $this->success('成功', $data); } /** * 获取车缝部件的工艺资料 * @ApiMethod (GET) * @param string $part_code 部件编号 * @param string $workorder 工单编号 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function GetCarProcess() { if (!$this->request->isGet()) { $this->error('请求方法错误'); } $params = $this->request->param(); if (empty($params['workorder'])) { $this->error('工单编号不能为空'); } if (empty($params['part_code'])) { $this->error('部件编号不能为空'); } $data = db('工单_基础工艺资料') ->where('work_order', $params['workorder']) ->where('part_code', $params['part_code']) ->whereNull('del_rq') ->where('status', 0) ->field('process_code as 工艺编号,process_name as 工艺名称,standard_hour as 标准工时,standard_minutes as 标准分钟, standard_score as 标准工分,coefficient as 系数,remark as 备注,money as 金额') ->select(); usort($data, function ($a, $b) { return strnatcmp((string)$a['工艺编号'], (string)$b['工艺编号']); }); $this->success('成功', $data); } /** * 工分报工接口 * @ApiMethod (POST) * @param array $list 报工数据 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function ReportingWork() { if (!$this->request->isPost()) { $this->error('请求方法错误'); } $params = $this->request->post(); $reportList = []; if (isset($params[0]) && is_array($params[0])) { $reportList = $params; } elseif (!empty($params['list']) && is_array($params['list'])) { $reportList = $params['list']; } elseif (!empty($params['data']) && is_array($params['data'])) { $reportList = $params['data']; } else { $rawBody = file_get_contents('php://input'); $decoded = json_decode($rawBody, true); if (is_array($decoded)) { if (isset($decoded[0]) && is_array($decoded[0])) { $reportList = $decoded; } elseif (!empty($decoded['list']) && is_array($decoded['list'])) { $reportList = $decoded['list']; } elseif (!empty($decoded['data']) && is_array($decoded['data'])) { $reportList = $decoded['data']; } } } if (empty($reportList)) { $this->error('请传入报工数据'); } $insertData = []; $now = date('Y-m-d H:i:s'); foreach ($reportList as $idx => $item) { $rowNo = $idx + 1; if (empty($item['staff_no'])) { $this->error('第' . $rowNo . '条员工编号不能为空'); } if (empty($item['staff_name'])) { $this->error('第' . $rowNo . '条员工姓名不能为空'); } if (empty($item['work_order']) && empty($item['workorder'])) { $this->error('第' . $rowNo . '条订单编号不能为空'); } if (empty($item['majorprocess'])) { $this->error('第' . $rowNo . '条大工序不能为空'); } $isSewing = $item['majorprocess'] === '车缝'; if ($isSewing && (!isset($item['part_code']) || $item['part_code'] === '')) { $this->error('第' . $rowNo . '条车缝工序必须填写部件编号'); } if (empty($item['process_code'])) { $this->error('第' . $rowNo . '条工艺编号不能为空'); } if (empty($item['process_name'])) { $this->error('第' . $rowNo . '条工艺名称不能为空'); } // if (!isset($item['standard_hour']) || $item['standard_hour'] === '') { // $this->error('第' . $rowNo . '条标准工时不能为空'); // } // if (!isset($item['money']) || $item['money'] === '') { // $this->error('第' . $rowNo . '条金额不能为空'); // } // if (empty($item['standard_minutes'])) { // $this->error('第' . $rowNo . '条标准分钟不能为空'); // } if (!isset($item['standard_score']) || $item['standard_score'] === '') { $this->error('第' . $rowNo . '条标准工分不能为空'); } if (!isset($item['number']) || $item['number'] === '') { $this->error('第' . $rowNo . '条产量不能为空'); } $standardHour = floatval($item['standard_hour']); $standardScore = floatval($item['standard_score']); $money = floatval($item['money']); $coefficient = isset($item['coefficient']) && $item['coefficient'] !== '' ? floatval($item['coefficient']) : 'C'; $number = floatval($item['number']); $standardMinutes = floatval($item['standard_minutes']); $productionHour = $standardHour * $number; $productionScore = $standardScore * $number; $salary = $this->calcReportSalary($number, $standardScore); $partCode = null; if (isset($item['part_code']) && $item['part_code'] !== '') { $partCode = intval($item['part_code']); } $insertData[] = [ 'staff_no' => $item['staff_no'], 'staff_name' => $item['staff_name'], 'work_order' => !empty($item['work_order']) ? $item['work_order'] : $item['workorder'], 'date' => !empty($item['date']) ? $item['date'] : date('Y-m-d'), 'majorprocess' => $item['majorprocess'], 'part_code' => $partCode, 'process_code' => $item['process_code'], 'process_name' => $item['process_name'], 'standard_hour' => $standardHour, 'standard_score' => $standardScore, 'standard_minutes' => $standardMinutes, 'money' => $money, 'coefficient' => $coefficient, 'number' => $number, 'production_hour' => round($productionHour, 4), 'production_score' => round($productionScore, 4), 'salary' => $salary, 'machine' => isset($item['machine']) ? $item['machine'] : '', 'sys_id' => isset($item['sys_id']) ? $item['sys_id'] : '', 'sys_rq' => $now, 'mod_id' => isset($item['mod_id']) ? $item['mod_id'] : '', 'mod_rq' => null, 'del_rq' => null, ]; } Db::startTrans(); try { $result = db('设备_工分计酬')->insertAll($insertData); if ($result === false) { Db::rollback(); $this->error('报工失败'); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error('报工失败:' . $e->getMessage()); } $this->success('报工成功', ['count' => count($insertData)]); } //报工工分数据表左侧菜单栏 public function GetReportingWorkLeft() { if (!$this->request->isGet()) { $this->error('请求方法错误'); } $startDate = date('Y-m-d', strtotime('-39 day')); $endDate = date('Y-m-d'); $list = db('设备_工分计酬') ->where('del_rq', null) ->where('date', 'between time', [$startDate, $endDate]) ->where('machine', '<>', '') ->field('machine,date') ->order('machine asc,date desc') ->select(); $menuMap = []; foreach ($list as $row) { $machine = $row['machine']; $date = $row['date']; if (!isset($menuMap[$machine])) { $menuMap[$machine] = []; } if (!in_array($date, $menuMap[$machine])) { $menuMap[$machine][] = $date; } } $data = []; foreach ($menuMap as $machine => $dateList) { $children = []; foreach ($dateList as $date) { $children[] = [ 'date' => $date, ]; } $data[] = [ 'machine' => $machine, 'children' => $children, ]; } $this->success('成功', $data); } /** * 机台报工数据 * @ApiMethod (GET) * @param string $date 日期 * @param string $machine 机台编号 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function GetReportingWorkData() { if (!$this->request->isGet()) { $this->error('请求方法错误'); } $params = $this->request->param(); if (empty($params['date'])) { $this->error('日期不能为空'); } if (empty($params['machine'])) { $this->error('机台编号不能为空'); } $rows = db('设备_工分计酬') ->where('del_rq', null) ->where('date', 'like', $params['date'] . '%') ->where('machine', $params['machine']) ->field('id,work_order,majorprocess,part_code,process_code,process_name,staff_no,staff_name,standard_hour, standard_score,money,coefficient,number,production_hour,production_score, salary,machine,sys_id,sys_rq') ->order('process_code asc,staff_no asc') ->select(); $processMap = []; foreach ($rows as $row) { $groupKey = $row['majorprocess'] . '|' . $row['process_code'] . '|' . $row['process_name'] . '|' . $row['part_code']; if (!isset($processMap[$groupKey])) { $processMap[$groupKey] = [ 'work_order' => $row['work_order'], 'majorprocess' => $row['majorprocess'], 'part_code' => $row['part_code'], 'process_code' => $row['process_code'], 'process_name' => $row['process_name'], 'machine' => $row['machine'], 'date' => $params['date'], 'staffs' => [], ]; } $processMap[$groupKey]['staffs'][] = [ 'staff_no' => $row['staff_no'], 'staff_name' => $row['staff_name'], 'standard_hour' => $row['standard_hour'], 'standard_score' => $row['standard_score'], 'money' => $row['money'], 'coefficient' => $row['coefficient'], 'number' => $row['number'], 'production_hour' => $row['production_hour'], 'production_score' => $row['production_score'], 'salary' => $row['salary'], 'sys_id' => $row['sys_id'], 'sys_rq' => $row['sys_rq'], 'id' => $row['id'], ]; } $data = array_values($processMap); $this->success('成功', $data); } /** * 修改报工数据 * @ApiMethod (POST) * @param array $list 报工数据 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function UpdateReportingWork() { if (!$this->request->isPost()) { $this->error('请求方法错误'); } $params = $this->request->post(); if (empty($params['id'])) { $this->error('ID不能为空'); } if (empty($params['mod_id'])) { $this->error('修改人不能为空'); } if (!isset($params['number']) || $params['number'] === '') { $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; 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('标准工分不能为空'); } $data = [ 'mod_id' => $params['mod_id'], 'mod_rq' => date('Y-m-d H:i:s'), 'number' => $number, 'production_hour' => $params['production_hour'], '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('修改失败'); } $this->success('修改成功'); } /** * 删除报工数据 * @ApiMethod (POST) * @param array $id ID * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function DeleteReportingWork() { if (!$this->request->isPost()) { $this->error('请求方法错误'); } $params = $this->request->post(); if (empty($params['id'])) { $this->error('ID不能为空'); } $data = [ 'del_rq' => date('Y-m-d H:i:s'), ]; $result = db('设备_工分计酬') ->where('id', $params['id']) ->update($data); if ($result === false) { $this->error('删除失败'); } $this->success('删除成功'); } /** * 查询机台报工记录 * @ApiMethod (GET) * @param string $date 日期 * @param string $machine 机台编号 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function GetReportingWorkRecord() { if (!$this->request->isGet()) { $this->error('请求方法错误'); } $params = $this->request->param(); if (empty($params['date'])) { $this->error('日期不能为空'); } if (empty($params['machine'])) { $this->error('机台编号不能为空'); } $rows = db('设备_工分计酬')->alias('a') ->join('工单_基本资料 b', 'a.work_order = b.订单编号', 'LEFT') ->where('a.del_rq', null) ->where('b.Mod_rq', null) ->where('a.date', 'like', $params['date'] . '%') ->where('a.machine', $params['machine']) ->field(' a.work_order, a.majorprocess, a.part_code, a.process_code, a.process_name, a.staff_no, a.staff_name, a.standard_hour, a.standard_score, a.money, a.coefficient, a.number, a.production_hour, a.production_score, a.salary,machine, a.sys_id, a.sys_rq, a.id, b.生产款号, b.款式 ') ->order('a.process_code asc,a.staff_no asc,a.sys_rq desc') ->select(); $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 * @param mixed $standardScore * @return float */ private function calcReportSalary($number, $standardScore) { return round(floatval($number) * floatval($standardScore) * 0.067, 4); } }