request->isGet() === false) { $this->error('请求错误'); } $customerRows = \db('产品_基本资料') ->where('客户编号', '<>', '') ->field('rtrim(客户编号) as 客户编号, MAX(rtrim(客户名称)) as 客户名称') ->group('客户编号') ->order('客户编号 asc') ->select(); $productCustomers = []; foreach ($customerRows as $row) { $customerCode = rtrim((string)$row['客户编号']); if ($customerCode === '') { continue; } $productCustomers[] = [ 'code' => $customerCode, 'name' => rtrim((string)$row['客户名称']), ]; } $orders = \db('工单_基本资料') ->where('行号', '1') ->where('gd_statu', 'in', ['2-生产中', '3-计划中']) ->field('rtrim(成品代号) as 成品代号, rtrim(Gd_客户代号) as Gd_客户代号, MAX(rtrim(Gd_客户名称)) as Gd_客户名称') ->group('成品代号, Gd_客户代号') ->order('Gd_客户代号 asc') ->select(); if (empty($productCustomers) && empty($orders)) { $this->success('', []); } $ordersByInitial = []; foreach ($orders as $index => $order) { $orders[$index]['成品代号'] = rtrim((string)$order['成品代号']); $orders[$index]['Gd_客户代号'] = rtrim((string)$order['Gd_客户代号']); $orders[$index]['Gd_客户名称'] = rtrim((string)$order['Gd_客户名称']); $productCode = $orders[$index]['成品代号']; if ($productCode !== '') { $ordersByInitial[strtoupper($productCode[0])][] = $orders[$index]; } } $data = [ '印刷工单' => [], '糊盒工单' => [], ]; $usedCodes = []; $appendCustomer = function ($code, $name) use (&$data, &$usedCodes) { $code = rtrim((string)$code); $codeKey = strtoupper($code); if ($code === '' || isset($usedCodes[$codeKey])) { return; } $usedCodes[$codeKey] = true; $label = $code . rtrim((string)$name); if (strpos($code, 'J') !== false) { $data['糊盒工单'][] = $label; } elseif (strpos($code, 'Y') !== false) { $data['印刷工单'][] = $label; } }; foreach ($productCustomers as $customer) { $customerCode = $customer['code']; $customerName = $customer['name']; $matchedOrders = []; foreach ($ordersByInitial[strtoupper($customerCode[0])] ?? [] as $order) { if (stripos($order['成品代号'], $customerCode) === 0) { $matchedOrders[] = $order; } } if (empty($matchedOrders)) { continue; } if ($customerName !== '') { $appendCustomer($customerCode, $customerName); continue; } foreach ($matchedOrders as $order) { if ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '') { continue; } $appendCustomer( $order['Gd_客户代号'] !== '' ? $order['Gd_客户代号'] : $customerCode, $order['Gd_客户名称'] ); } } foreach ($orders as $order) { $productCode = $order['成品代号']; $matchedProductCustomer = false; if ($productCode !== '') { foreach ($productCustomers as $customer) { $customerCode = $customer['code']; if ($customerCode !== '' && strcasecmp($productCode[0], $customerCode[0]) === 0 && stripos($productCode, $customerCode) === 0) { $matchedProductCustomer = true; break; } } } if ($matchedProductCustomer || ($order['Gd_客户代号'] === '' && $order['Gd_客户名称'] === '')) { continue; } $appendCustomer($order['Gd_客户代号'], $order['Gd_客户名称']); } $this->success('成功', $data); } /** * 右侧工单详情 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function workOrderDetail() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); $where = []; if (!empty($param['order'])) { $where['a.Gd_cpdh|a.成品代号'] = ['like', $param['order'] . '%']; } $listQuery = \db('工单_基本资料') ->alias('a') ->join('工单_印件资料 b', 'a.Gd_gdbh = b.Yj_Gdbh') ->field('b.Yj_Gdbh as 工单编号,b.yj_Yjno,b.yj_Yjdh as 印件代号,b.yj_yjmc as 印件名称,b.yj_ls as 联数,a.投料大箱 as 投料大箱,b.yj_平张投料 as 计划投料') ->where($where) ->where('a.gd_statu', '2-生产中') ->where('a.Gd_gdbh', 'like', 'Y%'); $workOrderNo = trim((string)($param['工单编号'] ?? '')); if ($workOrderNo !== '') { $listQuery->where('a.Gd_gdbh', 'like', '%' . $this->courseLikeKeyword($workOrderNo) . '%'); } $productName = trim((string)($param['产品名称'] ?? '')); if ($productName !== '') { $nameLike = '%' . $this->courseLikeKeyword($productName) . '%'; $listQuery->where(function ($query) use ($nameLike) { $query->where('a.成品名称', 'like', $nameLike) ->whereOr('a.Gd_cpmc', 'like', $nameLike) ->whereOr('b.yj_yjmc', 'like', $nameLike); }); } $salesOrderNo = trim((string)($param['销售订单号'] ?? '')); if ($salesOrderNo !== '') { $listQuery->where('a.销售订单号', 'like', '%' . $this->courseLikeKeyword($salesOrderNo) . '%'); } $this->applyCourseRange($listQuery, 'a.投料大箱', $param['投料大箱起'] ?? '', $param['投料大箱止'] ?? ''); $this->applyCourseRange($listQuery, 'a.接单日期', $param['接单日期起'] ?? '', $param['接单日期止'] ?? '', true); $this->applyCourseRange($listQuery, 'a.交货日期', $param['交货日期起'] ?? '', $param['交货日期止'] ?? '', true); $this->applyCourseRange($listQuery, 'b.yj_平张投料', $param['计划投料起'] ?? '', $param['计划投料止'] ?? ''); $list = $listQuery ->group('b.Yj_Gdbh,b.yj_Yjno') ->select(); if (empty($list)) { $this->success(''); } $orderNos = []; foreach ($list as $row) { $orderNos[$row['工单编号']] = $row['工单编号']; } $processRows = \db('工单_工艺资料') ->alias('a') ->join('设备_产量计酬 b', 'a.Gy0_gdbh = b.sczl_gdbh AND a.Gy0_yjno = b.sczl_yjno AND a.Gy0_gxh = b.sczl_gxh', 'left') ->where('a.Gy0_gdbh', 'in', array_values($orderNos)) ->field('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh, a.Gy0_gxmc, a.PD_WG, SUM(b.sczl_cl) as 产量') ->group('a.Gy0_gdbh, a.Gy0_yjno, a.Gy0_gxh') ->order('a.Gy0_gxh asc') ->select(); $latestNumSql = \db('设备_产量计酬') ->where('sczl_gdbh', 'in', array_values($orderNos)) ->field('sczl_gdbh, sczl_yjno, sczl_gxh, MAX(sczl_num) as max_num') ->group('sczl_gdbh, sczl_yjno, sczl_gxh') ->buildSql(); $latestRows = \db('设备_产量计酬') ->alias('t') ->join([$latestNumSql => 'm'], 't.sczl_gdbh = m.sczl_gdbh AND t.sczl_yjno = m.sczl_yjno AND t.sczl_gxh = m.sczl_gxh AND t.sczl_num = m.max_num') ->field('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, rtrim(t.sczl_num) as sczl_num, MAX(t.sczl_rq) as sczl_rq') ->group('t.sczl_gdbh, t.sczl_yjno, t.sczl_gxh, t.sczl_num') ->select(); $latestMap = []; foreach ($latestRows as $latest) { $latestKey = rtrim((string)$latest['sczl_gdbh']) . '|' . rtrim((string)$latest['sczl_yjno']) . '|' . (int)$latest['sczl_gxh']; $latestMap[$latestKey] = $latest; } $processMap = []; foreach ($processRows as $process) { $mapKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']); $processMap[$mapKey][] = $process; } foreach ($list as $key => $value) { $mapKey = rtrim((string)$value['工单编号']) . '|' . rtrim((string)$value['yj_Yjno']); $index = 1; foreach ($processMap[$mapKey] ?? [] as $process) { $status = ($process['PD_WG'] !== '1900-01-01 00:00:00') ? '完工' : ''; $latestKey = rtrim((string)$process['Gy0_gdbh']) . '|' . rtrim((string)$process['Gy0_yjno']) . '|' . (int)$process['Gy0_gxh']; $latest = $latestMap[$latestKey] ?? []; $flowNo = rtrim((string)($latest['sczl_num'] ?? '')); if ($flowNo === '0') { $flowNo = ''; } $reportDate = (string)($latest['sczl_rq'] ?? ''); if ($reportDate !== '') { $reportDate = substr($reportDate, 0, 10); } $list[$key]['工序' . $index] = $value['yj_Yjno'] . '-' . $process['Gy0_gxh'] . '-' . rtrim((string)$process['Gy0_gxmc']) . '[' . $process['产量'] . '/' . ']' . $status . '[' . $flowNo . '/' . $reportDate . ']'; $index++; } unset($list[$key]['yj_Yjno']); } $this->success('成功', $list); } /** * 工单印件的全部工艺及报工条数 * 参数:工单编号、印件编号 */ public function workOrderProcess() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); $workOrderNo = trim((string)($param['工单编号'] ?? '')); $printNo = trim((string)($param['印件编号'] ?? '')); if ($workOrderNo === '' || $printNo === '') { $this->error('参数错误'); } $printNos = $this->coursePrintNoValues($printNo); $processes = \db('工单_工艺资料') ->where('Gy0_gdbh', $workOrderNo) ->where('Gy0_yjno', 'in', $printNos) ->field('rtrim(Gy0_gxh) as 工序号, rtrim(Gy0_gxmc) as 工序名称') ->orderRaw('Gy0_gxh+0 asc, Gy0_gxh asc') ->select(); $reportCounts = \db('设备_产量计酬') ->where('sczl_gdbh', $workOrderNo) ->where('sczl_yjno', 'in', $printNos) ->field('rtrim(sczl_gxh) as 工序号, COUNT(*) as 报工条数') ->group('sczl_gxh') ->select(); $countMap = []; foreach ($reportCounts as $row) { $countMap[$this->courseProcessKey($row['工序号'])] = (int)$row['报工条数']; } $data = []; $seen = []; foreach ($processes as $process) { $processNo = rtrim((string)$process['工序号']); $processKey = $this->courseProcessKey($processNo); if (isset($seen[$processKey])) { continue; } $seen[$processKey] = true; $processName = rtrim((string)$process['工序名称']); $count = $countMap[$processKey] ?? 0; $data[] = [ '工序号' => $processNo, '工序名称' => $processName, '报工条数' => $count, '工艺' => $processNo . '-' . $processName . '(' . $count . ')', ]; } $this->success('成功', $data); } /** * 某道工艺的报工明细,按流程单号从小到大 * 参数:工单编号、印件编号、工序号 */ public function processReport() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); $workOrderNo = trim((string)($param['工单编号'] ?? '')); $printNo = trim((string)($param['印件编号'] ?? '')); $processNo = trim((string)($param['工序号'] ?? '')); if ($workOrderNo === '' || $printNo === '' || $processNo === '') { $this->error('参数错误'); } $rows = \db('设备_产量计酬') ->alias('a') ->join('设备_基本资料 m', 'a.sczl_jtbh = m.设备编号', 'left') ->where('a.sczl_gdbh', $workOrderNo) ->where('a.sczl_yjno', 'in', $this->coursePrintNoValues($printNo)) ->where('a.sczl_gxh', 'in', $this->coursePrintNoValues($processNo)) ->field('rtrim(a.sczl_num) as 流程单号, a.sczl_cl as 产量, a.sczl_zcfp as 制程废品, rtrim(a.sczl_jtbh) as 机台编号, rtrim(m.设备名称) as 机台名称, rtrim(a.sczl_bzdh) as 班组代号, a.sys_rq as 上报时间, rtrim(a.sys_id) as 上报人员, rtrim(a.sczl_bh1) as bh1, rtrim(a.sczl_bh2) as bh2, rtrim(a.sczl_bh3) as bh3, rtrim(a.sczl_bh4) as bh4, rtrim(a.sczl_bh5) as bh5, rtrim(a.sczl_bh6) as bh6, rtrim(a.sczl_bh7) as bh7, rtrim(a.sczl_bh8) as bh8, rtrim(a.sczl_bh9) as bh9, rtrim(a.sczl_bh10) as bh10') ->orderRaw('a.sczl_num+0 asc, a.sczl_num asc, a.sys_rq asc') ->select(); $employeeCodes = []; foreach ($rows as $row) { for ($i = 1; $i <= 10; $i++) { $code = rtrim((string)$row['bh' . $i]); if ($code !== '' && $code !== '000000') { $employeeCodes[$code] = $code; } } } $employeeNames = []; if (!empty($employeeCodes)) { $people = \db('人事_基本资料') ->where('员工编号', 'in', array_values($employeeCodes)) ->field('rtrim(员工编号) as 员工编号, rtrim(员工姓名) as 员工姓名') ->select(); foreach ($people as $person) { $employeeNames[rtrim((string)$person['员工编号'])] = rtrim((string)$person['员工姓名']); } } $data = []; foreach ($rows as $row) { $members = []; for ($i = 1; $i <= 10; $i++) { $code = rtrim((string)$row['bh' . $i]); if ($code === '' || $code === '000000') { continue; } $members[] = $employeeNames[$code] ?? $code; } $machineNo = rtrim((string)$row['机台编号']); $machineName = rtrim((string)$row['机台名称']); $data[] = [ '流程单号' => rtrim((string)$row['流程单号']), '产量' => $row['产量'], '制程废品' => $row['制程废品'], '生产机台' => $machineName !== '' ? $machineNo . ' ' . $machineName : $machineNo, '班组代号' => rtrim((string)$row['班组代号']), '班组人员' => implode(' ', $members), '上报人员' => rtrim((string)$row['上报人员']), '上报时间' => rtrim((string)$row['上报时间']), ]; } $totalQty = 0.0; foreach ($data as $item) { $totalQty += (float)$item['产量']; } $this->success('成功', [ '产量合计' => floor($totalQty) == $totalQty ? (int)$totalQty : $totalQty, '报工' => $data, ]); } private function coursePrintNoValues($value) { $value = rtrim((string)$value); $values = [$value]; if ($value !== '' && is_numeric($value)) { $number = (string)(int)$value; $values[] = $number; $values[] = str_pad($number, 2, '0', STR_PAD_LEFT); } return array_values(array_unique($values)); } private function courseProcessKey($value) { $value = rtrim((string)$value); if ($value !== '' && is_numeric($value)) { return (string)(int)$value; } return $value; } /** * 范围条件只在传入起或止时生效,日期止取到当天结束 */ private function applyCourseRange($query, $field, $from, $to, $isDate = false) { $from = trim((string)$from); $to = trim((string)$to); if ($from !== '') { $query->where($field, '>=', $from); } if ($to !== '') { if ($isDate && preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) { $to .= ' 23:59:59'; } $query->where($field, '<=', $to); } } private function courseLikeKeyword($keyword) { return addcslashes((string)$keyword, '%_\\'); } }