request->server("HTTP_SID")); } parent::_initialize(); } /** * 工单资料菜单列表 * * @ApiMethod (GET) * @return false|string * @throws \think\Exception */ public function DataList() { $where['j.Mod_rq'] = null; $allCustomers = \db('erp_客户供应商')->alias('e') ->field('e.编号 as 客户编号') ->select(); $customerData = []; foreach ($allCustomers as $customer) { $customerID = $customer['客户编号']; $customerData[$customerID] = ['计划中' => 0, '生产中' => 0]; } $data = \db('erp_客户供应商')->alias('e') ->join('工单_基本资料 j', 'e.编号 = j.客户编号', 'LEFT') ->field('e.编号 as 客户编号, j.订单编号, j.gd_statu') ->where($where) ->select(); foreach ($data as $row) { $customerID = $row['客户编号']; $status = $row['gd_statu']; if ($status == '1-计划中') { $customerData[$customerID]['计划中']++; } elseif ($status == '2-生产中') { $customerData[$customerID]['生产中']++; } } ksort($customerData); $output = []; foreach ($customerData as $customerID => $statusCounts) { $statusString = []; if ($statusCounts['计划中'] > 0) { $statusString[] = "计划中:{$statusCounts['计划中']}"; } if ($statusCounts['生产中'] > 0) { $statusString[] = "生产中:{$statusCounts['生产中']}"; } $output[] = "{$customerID}【" . implode(' ', $statusString) . "】"; } $this->success('成功', $output); } /** * 工单基本资料列表 * @ApiMethod (GET) * @param string $limit 查询长度 * @param string $Gd_khdh 客户代号 * @param string $startTime 接单日期开始时间 * @param string $endTime 接单日期结束时间 * @return \think\response\Json * @throws \think\exception\DbException */ public function WorkOrderList() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $search = input('search'); $page = input('page'); $limit = input('limit'); $param = $this->request->param(); $where = []; if (!empty($search)) { $where['订单编号|生产款号|客户编号|款式|审核|Sys_id'] = ['like', '%' . $search . '%']; } $where['Mod_rq'] = null; $list = \db('工单_基本资料') ->where($where) ->order('订单编号 desc, Gd_statu desc, Sys_rq desc') ->limit(($page - 1) * $limit, $limit) ->select(); $count = \db('工单_基本资料') ->where($where) ->order('订单编号 desc, Gd_statu desc, Sys_rq desc') ->select(); // 提取所有订单编号 $orderIds = array_column($list, '订单编号'); // 查询所有在“工单_相关附件”表中存在的订单编号 $relatedOrders = \db('工单_相关附件') ->whereIn('关联编号', $orderIds) ->whereIn('附件备注', '技术附件') ->column('关联编号'); // 遍历数据,判断每个订单编号是否在相关附件表中 foreach ($list as &$value) { if (in_array($value['订单编号'], $relatedOrders)) { $value['status'] = ''; // 有相关附件,status为空 } else { $value['status'] = '*'; // 没有相关附件,标记为新订单 } } $this->success('成功', ['data' => $list, 'total' => count($count)]); } /** * 月度客户订单汇总 */ public function ProductInformation() { if ($this->request->isGet() === false) { $this->error('请求错误'); } // 查询生产中和未生产的数据,使用 CASE WHEN 进行分类统计 $data = \db('工单_基本资料')->alias('j') ->field(' j.客户编号, REPLACE(DATE_FORMAT(j.Sys_rq, "%Y-%m"), "-", "") as 年月, SUM(CASE WHEN j.gd_statu = "2-生产中" THEN 1 ELSE 0 END) as 生产中数量, SUM(CASE WHEN j.gd_statu = "1-计划中" THEN 1 ELSE 0 END) as 未生产数量 ') ->where('j.Mod_rq', null) // 统一的查询条件 ->group('j.客户编号, 年月') ->order('j.客户编号 asc') ->select(); // 格式化数据 $result = []; foreach ($data as $item) { $result[$item['年月']][] = $item['客户编号'] . '【生产中' . $item['生产中数量'] . ',计划中' . $item['未生产数量'] . '】'; } $this->success('请求成功', ['data' => $result]); } /** * U8工单资料删除 * @param string $workOrder 工单编号 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function orderDataDel() { if($this->request->isGet() === false){ $this->error('请求错误'); } $workOrder = input('Uniqid'); if (empty($workOrder)){ $this->error('参数错误'); } $order = \db('工单_基本资料')->where('UniqId',$workOrder)->find(); \db()->startTrans(); try { \db('工单_印件资料')->where('订单编号',$order['订单编号'])->update(['Mod_rq'=>date('Y-m-d H:i:s')]); \db('工单_工艺资料')->where('订单编号',$order['订单编号'])->update(['Mod_rq'=>date('Y-m-d H:i:s')]); $res = \db('工单_基本资料')->where('UniqId',$workOrder)->update(['Mod_rq'=>date('Y-m-d H:i:s')]); \db()->commit(); } catch (\Exception $e){ \db()->rollback(); } if ($res !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * 印件资料修改 * @ApiMethod (POST) * @param void * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function PrintedEdit() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param) || isset($param['Uniqid']) === false){ $this->error('参数错误'); } $param['Yj_Gdbh'] = \db('工单_印件资料')->where('Uniqid',$param['Uniqid'])->value('Yj_Gdbh'); $data = [ 'Yj_Gdbh' =>$param['Yj_Gdbh'], 'yj_Yjno' =>$param['yjno'], 'yj_Yjdh' =>$param['yjdh'], 'yj_yjmc' =>$param['yjmc'], 'yj_zzdh' =>$param['zzdh'], 'yj_zzdh1' =>$param['zzdh1'], 'yj_zzdh2' =>$param['zzdh2'], 'yj_zzdh3' =>$param['zzdh3'], 'yj_zzdh4' =>$param['zzdh4'], 'yj_zzmc' =>$param['zzmc'], 'yj_zzmc1' =>$param['zzmc1'], 'yj_zzmc2' =>$param['zzmc2'], 'yj_zzmc3' =>$param['zzmc3'], 'yj_zzmc4' =>$param['zzmc4'], 'yj_tlgg' =>$param['tlgg'], 'yj_klgg' =>$param['klgg'], 'Yj_核算规格' =>$param['hsgg'], 'yj_成品数量' =>$param['cpsl'], 'yj_平张投料' =>$param['pztl'], 'yj_ks' =>$param['ks'], 'yj_ls' =>$param['ls'], 'yj_desc' =>$param['desc'], ]; $UniqId = $param['Uniqid']; $sql = \db('工单_印件资料')->where('Uniqid',$UniqId)->fetchSql(true)->update($data); $res = Db::query($sql); $process = \db('工单_工艺资料') ->where('Gy0_gdbh',$data['Yj_Gdbh']) ->where('Gy0_yjno',$data['yj_Yjno']) ->select(); if ((int)$data['yj_平张投料'] > 0 && !empty($process)){ //重新分配工序计划产量 $result = $this->PlannedProcessYield($data['Yj_Gdbh'],$data['yj_Yjno'],0,$data['yj_平张投料']); if ($result === false){ $this->success('修改工序产量失败'); } } if ($res !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * 工艺资料修改 * @ApiMethod (POST) */ public function ProcessDetailEdit() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param) || isset($param['UniqId']) === false){ $this->error('参数错误'); } if (empty($param['Gy0_shdh'])){ $rate['rate0'] = 0; $rate['rate1'] = 0; }else{ $rate = \db('dic_lzsh')->where('sys_bh',$param['Gy0_shdh'])->field('rtrim(sys_rate0) as rate0,rtrim(sys_rate1) as rate1')->find(); } $param['Gy0_Rate0']= isset($rate['rate0'])?$rate['rate0']:0; $param['Gy0_Rate1'] = isset($rate['rate1'])?$rate['rate1']:0; $param['Mod_rq'] = date('Y-m-d H:i:s',time()); $UniqId = $param['UniqId']; unset($param['UniqId']); //修改工艺资料 $sql = \db('工单_工艺资料')->where('UniqId',$UniqId)->fetchSql(true)->update($param); $res = Db::query($sql); //获取工艺资料数据 $list = \db('工单_工艺资料')->where('UniqId',$UniqId)->field('Gy0_yjno,Gy0_gxh,rtrim(Gy0_计划接货数) as 计划接货数')->find(); // //修改工单状态 // $status = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->field('rtrim(gd_statu) as status')->find(); // if ($status['status'] !== '2-生产中'){ // $statusSql = \db('工单_基本资料')->where('Gd_gdbh',$param['Gy0_gdbh'])->fetchSql(true)->update(['gd_statu'=>'2-生产中']); // Db::query($statusSql); // } //重新分配工序计划产量 if ((int)$list['计划接货数'] > 0){ $result = $this->PlannedProcessYield($param['Gy0_gdbh'],$list['Gy0_yjno'],$list['Gy0_gxh'],$list['计划接货数']); } if ($res !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * 新增工单->添加工单 * @ApiMethod (POST) * @param */ public function WorkOrderAdd() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $param['Sys_rq'] = date('Y-m-d H:i:s'); $param['gd_statu'] = '1-计划中'; $prefix = substr($param['订单编号'], 0, 2); // e.g., "DC" $maxOrder = \db('工单_基本资料') ->where('订单编号', 'like', "{$prefix}%") ->order('订单编号', 'desc') ->limit(1) ->value('订单编号'); if ($maxOrder) { $numericPart = substr($maxOrder, 2); $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT); $param['订单编号'] = $prefix . $newNumericPart; } else { $param['订单编号'] = $param['订单编号']; } if ($maxOrder) { $numericPart = substr($maxOrder, 2); $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT); $param['订单编号'] = $prefix . $newNumericPart; } else { $param['订单编号'] = $param['订单编号']; } $data = $param; unset($data['关联订单']); unset($data['关联面料ID']); //新增订单插入至工单基本资料 $sql = \db('工单_基本资料')->fetchSql(true)->insert($data); \db()->query($sql); if (!empty($param['关联订单']) && !empty($param['关联面料ID'])){ //关联订单编号 $OrderId = $param['关联订单']; $OrderNumber = explode(',',$OrderId); //关联面料ID $RelevanceId = $param['关联面料ID']; //查询订单BOM资料,面料资料 $RelevanceList = explode(',',$RelevanceId); $FabricList = $BomList = $MaterielList = []; foreach ($RelevanceList as $item){ //工单面料信息 $Fabric = \db('工单_面料资料') ->where('UNIQID',$item) ->where('Mod_rq',null) ->field('BOM_工单编号,BOM_颜色,BOM_物料编码,BOM_物料名称,BOM_投料单位,BOM_计划门幅,BOM_定额门幅') ->find(); $OrderData = $Fabric; $OrderData['BOM_工单编号'] = $param['订单编号']; $OrderData['Sys_ID'] = $param['Sys_id']; $OrderData['Sys_rq'] = date('Y-m-d H:i:s',time()); $OrderData['BOM_desc'] = $param['要求']; array_push($FabricList,$OrderData); //关联订单 $AssociatedNumber = \db('工单关联表') ->where('订单编号',$Fabric['BOM_工单编号']) ->where('颜色',$Fabric['BOM_颜色']) ->where('物料编号',$Fabric['BOM_物料编码']) ->value('关联编号'); $materiel = [ '关联编号' => $AssociatedNumber, '订单编号' => $Fabric['BOM_工单编号'], '生产款号' => $param['生产款号'], '颜色' => $Fabric['BOM_颜色'], '物料编号' => $Fabric['BOM_物料编码'], '物料名称' => $Fabric['BOM_物料名称'], '备注' => $param['要求'], 'Sys_id' => $param['Sys_id'], 'Sys_rq' => date('Y-m-d H:i:s',time()), ]; array_push($MaterielList,$materiel); } foreach ($MaterielList as $index => $item){ $MaterielList[$index]['订单编号'] = $param['订单编号']; //工单BOM信息 $Bom = \db('工单_bom资料') ->where('BOM_工单编号',$item['订单编号']) ->where('BOM_物料名称',$item['物料名称']) ->where('Mod_rq',null) ->find(); unset($Bom['UNIQID']); unset($Bom['Mod_rq']); $Bom['Sys_ID'] = $param['Sys_id']; $Bom['Sys_rq'] = date('Y-m-d H:i:s',time()); if (empty($BomList)){ array_push($BomList,$Bom); }else{ foreach ($BomList as $value){ if ($value['BOM_工单编号'] !== $Bom['BOM_工单编号'] && $value['BOM_物料名称'] !== $Bom['BOM_物料名称']){ array_push($BomList,$Bom); } } } } foreach ($BomList as $key => $value){ $BomList[$key]['BOM_工单编号'] = $param['订单编号']; } //插入数据 Db::startTrans(); try { //BOM表数据插入 $BomSql = \db('工单_bom资料')->fetchSql(true)->insertAll($BomList); $BomRes = \db()->query($BomSql); //面料表数据插入 $FabricSql = \db('工单_面料资料')->fetchSql(true)->insertAll($FabricList); $FabricRes = \db()->query($FabricSql); //工单关联表数据插入 $materielSql = \db('工单关联表')->fetchSql(true)->insertAll($MaterielList); $materielRes = \db()->query($materielSql); //提交数据 Db::commit(); }catch (\Exception $e){ //回滚事务 Db::rollback(); } }else{ //判断新增时面料是否存在,如果有调用gtp自动生成BOM资料 if (!empty($param['面料'])) { // 只有面料不为空时,才调用 GdGtpAiOrder 方法 $this->GdGtpAiOrder($param['订单编号']); } } $this->success('成功'); } /** * 新增印件资料->印件资料添加 * @return void * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function PrintDetailAdd() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $param['Sys_rq'] = date('Y-m-d H:i:s',time()); $process = [ ['1','仓库出库'],['2','裁剪'],['3','车缝'],['4','后道收样'],['5','大烫'],['6','总检'],['7','包装'] ]; $processDetail = []; foreach ($process as $key=>$value){ $total = null; if ($key !== 0){ $total = $param['zdtotal']; } $processDetail[$key] = [ '订单编号' => $param['订单编号'], '子订单编号' => $param['子订单编号'], '款号' => $param['款号'], '颜色' => $param['颜色'], '颜色备注' => $param['颜色备注'], '工序编号' => $value[0], '工序名称' => $value[1], '计划产量' => $total, 'Sys_id' => $param['Sys_id'], 'Sys_rq' => $param['Sys_rq'] ]; } $color = \db('工单_面料资料') ->where('Bom_工单编号',$param['订单编号']) ->where('BOM_颜色',$param['颜色备注']) ->select(); $colorList = []; if (empty($color)){ $BomList = \db('工单_bom资料') ->where('BOM_工单编号',$param['订单编号']) ->where('Mod_rq',null) ->select(); foreach ($BomList as $key=>$value){ $colorList[$key] = [ 'BOM_工单编号' => $param['订单编号'], 'BOM_颜色' => $param['颜色备注'], 'BOM_物料编码' => $param['款号'].'-'.$param['颜色备注'].($key+1), 'BOM_物料名称' => $value['BOM_物料名称'], 'BOM_投料单位' => '米', 'Sys_ID' => $param['Sys_id'], 'Sys_rq' => date('Y-m-d H:i:s',time()), 'BOM_desc' => $value['BOM_desc'] ]; } } //获取最新的关联编号 $AssociatedNumber = \db('工单关联表')->order('关联编号 desc')->column('关联编号'); if (empty($AssociatedNumber)){ $number = 0; }else{ $number = (int)substr($AssociatedNumber[0],3); } $MaterielList = []; //插入工单关联数据表 foreach ($colorList as $key=>$value){ $MaterielList[$key] = [ '关联编号' => 'GDGL'.($number + $key + 1), '订单编号' => $value['BOM_工单编号'], '生产款号' => $param['款号'], '颜色' => $param['颜色备注'], '物料编号' => $value['BOM_物料编码'], '物料名称' => $value['BOM_物料名称'], '备注' => $value['BOM_desc'], 'Sys_id' => $param['Sys_id'], 'Sys_rq' => date('Y-m-d H:i:s',time()) ]; } //开启事务 db()->startTrans(); try{ //工单颜色录入 $priSql = \db('工单_印件资料')->fetchSql(true)->insert($param); $priRes = \db()->query($priSql); //工单工艺录入 $proSql = \db('工单_工艺资料')->fetchSql(true)->insertAll($processDetail); $proRes = \db()->query($proSql); //工单面料录入 $fabricSql = \db('工单_面料资料')->fetchSql(true)->insertAll($colorList); $fabricRes = \db()->query($fabricSql); //工单关联表录入 $meterieSql = \db('工单关联表')->fetchSql(true)->insertAll($MaterielList); $meterieRes = \db()->query($meterieSql); // 提交事务 db()->commit(); } catch (\Exception $e) { // 回滚事务 db()->rollback(); $this->error($e->getMessage()); } if ($priRes !== false && $proRes !== false){ $this->success('成功'); }else{ $this->error('失败'); } } /** * 子订单列表 * @return null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function PrintListData() { // 检查请求方式 if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); // 检查参数是否存在 if (isset($param) === false) { $this->error('参数错误'); } // 查询型号 $where['Mod_rq'] = null; $xhdata = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->field('cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10') ->select(); $arr = []; // 收集cm1到cm10的非空非null值 foreach ($xhdata as $key => $value) { for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { array_push($arr, $value['cm' . $i]); } } } $arr = array_unique($arr); sort($arr); // 查询详细列表 $list = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->field('订单编号,子订单编号,款号,颜色,船样,zdtotal,Sys_id,Sys_rq,ck_rq,sc_rq,cm1,cm2,cm3,cm4,cm5,cm6,updatatime as 更新时间,颜色备注,color_id,cm7,cm8,cm9,cm10,cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid') ->select(); // 遍历列表并处理cm和cmsl字段 foreach ($list as $key => $value) { for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '') { // 如果 cmsl 的值为 0,则设置为空字符串 $list[$key][$value['cm' . $i]] = ($value['cmsl' . $i] === 0) ? '' : $value['cmsl' . $i]; } // 删除原有的 cm 和 cmsl 字段 // unset($list[$key]['cm' . $i], $list[$key]['cmsl' . $i]); } } // 自定义型号排序 $customOrder = array('XXS','XS','S', 'M', 'L', 'XL','XXL', 'XXXL', 'XXXXL', '2XL', '3XL', '4XL'); usort($arr, function ($a, $b) use ($customOrder) { $posA = array_search($a, $customOrder); $posB = array_search($b, $customOrder); return $posA - $posB; }); // 返回结果 $data['型号'] = $arr; $data['列表'] = $list; $this->success('成功', $data); } /** * 工单资料管理->印件资料删除 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function PrintDetailDel() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (isset($param['UniqId']) === false){ $this->error('参数错误'); } $printId = explode(',',$param['UniqId']); $i = 0; foreach ($printId as $value){ $res = \db('工单_印件资料') ->where('Uniqid',$value) ->update(['Mod_rq'=>date('Y-m-d H:i:s',time())]); if ($res === false){ $i++; } } if ($i === 0){ $this->success('删除成功'); }else{ $this->error('删除失败'); } } /** * 月度车间报工汇总->报工删除记录 */ public function ProcessDetailDel() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)) { $this->error('参数错误'); } $where['a.mod_rq'] = ['neq', '']; $list = \db('设备_产量计酬')->alias('a') ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号') ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT') ->field(' b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注, a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh, j.客户编号,j.生产款号,j.款式 ') ->where($where) ->order('a.mod_rq desc') ->limit($param['page'],$param['limit']) ->group('a.UniqId') ->select(); $count = \db('设备_产量计酬')->alias('a') ->join('工单_印件资料 b', 'b.订单编号 = a.订单编号 AND a.子订单编号 = a.子订单编号') ->join('工单_基本资料 j', 'b.订单编号 = j.订单编号', 'LEFT') ->field(' b.订单编号, b.子订单编号, b.款号, b.颜色, b.船样, a.尺码, b.zdtotal as 制单数,b.颜色备注, a.数量, a.sys_rq as 上报时间,a.UniqId,a.mod_rq,a.delsys_id,a.sczl_bh, j.客户编号,j.生产款号,j.款式 ') ->where($where) ->order('a.mod_rq desc') ->group('a.UniqId') ->select(); // 提取所有的尺码,并去重 $sizeList = array_values(array_unique(array_column($list, '尺码'))); // 动态将尺码的数量添加到每个订单中,并替换已完成字段 foreach ($list as &$item) { $size = $item['尺码']; $item[$size] = $item['数量']; // 动态添加尺码字段,值为数量 // unset($item['数量']); // 移除原来的已完成字段 } $data['total'] = count($count); $data['table'] = $list; $this->success('成功',$data); } /** * 产品附件新增 * 1.前端进行上传xlsx文件表格 * 2.接口接受数据文件进行保存xlsx文件以及pdf转换 * 3.前端进行预览pdf图片显示文件中的内容再页面上 */ public function gdAnnexAdd() { ini_set('display_errors', 'On'); ini_set('error_reporting', E_ALL); if (!$this->request->isPost()) { $this->error('请求方式错误'); } // 获取请求参数 $req = $this->request->param(); $relateId = $req['关联编号']; $attachmentContent = $req['附件内容']; $attachmentType = $req['附件类型']; $prefixDir = ROOT_PATH . '/public/'; $uploadDir = ''.'uploads/' . date('Ymd') . '/' . $relateId; DB::name('工单_基本资料') ->where('订单编号', $relateId) ->update(['gd_statu' => '2-生产中']); // 检查并创建目录 if (!is_dir($prefixDir . $uploadDir)) { mkdir($prefixDir . $uploadDir, 0777, true); } // 处理 Base64 附件内容 if (strpos($attachmentContent, 'base64,') !== false) { $attachmentContent = explode('base64,', $attachmentContent)[1]; } $fileContent = base64_decode($attachmentContent); $filename = time(); // 初始化文件路径变量 $xlsxFilePath = ''; $pdfFilePath = ''; if ($attachmentType === 'pdf') { // 保存 PDF 文件 $pdfFilePath = $uploadDir . '/' . $filename . '.pdf'; file_put_contents($prefixDir . $pdfFilePath, $fileContent); } elseif ($attachmentType === 'xlsx') { // 保存 Excel 文件 $xlsxFilePath = $uploadDir . '/' . $filename . '.xlsx'; file_put_contents($prefixDir . $xlsxFilePath, $fileContent); // 转换为 PDF 文件 $pdfFilePath = $uploadDir . '/' . $filename . '.pdf'; $cmd = sprintf( 'libreoffice --headless --convert-to pdf --outdir %s %s', escapeshellarg($prefixDir . $uploadDir), escapeshellarg($prefixDir . $xlsxFilePath) ); exec($cmd, $out, $retval); if ($retval !== 0) { $this->error('Excel 转 PDF 失败'); } } else { $this->error('不支持的附件类型'); } // 组织数据 $data = [ '关联编号' => $relateId, 'sys_id' => $req['sys_id'], '附件备注' => $req['附件备注'], '附件类型' => $attachmentType, 'sys_rq' => date('Y-m-d H:i:s'), 'url' => $xlsxFilePath, // 保存 xlsx 文件路径(如果存在) 'pdf' => $pdfFilePath // 保存 pdf 文件路径 ]; // 数据库事务处理 db()->startTrans(); try { // 插入数据 $sql = db('工单_相关附件')->fetchSql(true)->insert($data); $result = db()->query($sql); db()->commit(); } catch (\Exception $e) { // 回滚事务 db()->rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error('失败'); } $this->success('成功'); } /** * 订单打印接口 * order:订单编号 * 通过订单编号获取订单表数据以及子订单数据 */ public function orderPrint(){ if ($this->request->isGet() === false){$this->error('请求错误');} $param = $this->request->param(); if (empty($param['order'])){$this->error('参数错误');} $where['Mod_rq'] = null; //订单信息 $list = \db('工单_基本资料') ->where('订单编号',$param['order']) ->where($where) ->field('订单编号,img,生产款号,客户编号,款式,落货日期,箱唛要求,面料,船样描述,船样合计,粘衬,订单数量,审核,审核日期,要求,water') ->find(); //尺码表格表头 $xhdata = \db('工单_印件资料')->where('订单编号', $param['order'])->where($where)->select(); $arr = []; foreach ($xhdata as $key => $value) { for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { array_push($arr, $value['cm' . $i]); } } } // 去重并重新索引 $arr = array_unique($arr); $arr = array_values($arr); // 自定义排序函数 usort($arr, function($a, $b) { // 判断是否为数字 $isNumericA = is_numeric($a); $isNumericB = is_numeric($b); if ($isNumericA && $isNumericB) { // 如果都是数字,按从小到大排序 return $a - $b; } elseif (!$isNumericA && !$isNumericB) { // 如果都是字母,按字母顺序排序(可以自定义顺序) $sizeOrder = ['XXS', 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL','XXXXL']; $posA = array_search($a, $sizeOrder); $posB = array_search($b, $sizeOrder); return $posA - $posB; } else { // 如果一个是数字一个是字母,数字排在前 return $isNumericA ? -1 : 1; } }); //打印table数据表格 $porlis = \db('工单_印件资料') ->where('订单编号',$param['order']) ->where('船样',0) ->where($where) ->field('子订单编号') ->select(); //合并后的子订单条码数据 $subOrder = $porlis[0]['子订单编号']; // 找到子订单编号中的 '-' 位置 $dashPos = strpos($subOrder, '-'); if ($dashPos !== false) { // 提取 '-' 后面的部分 $afterDash = substr($subOrder, $dashPos + 1); // 判断长度是否等于2或等于4 if (strlen($afterDash) == 2) { // 查询船样为0的数据 $processlist = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->where('船样', 0) ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id, cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10, cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid') ->select(); // 初始化汇总数组 $scslTotals = [ "cmsl1" => 0, "cmsl2" => 0, "cmsl3" => 0, "cmsl4" => 0, "cmsl5" => 0, "cmsl6" => 0, "cmsl7" => 0, "cmsl8" => 0, "cmsl9" => 0, "cmsl10" => 0, "zdtotal" => 0, // 总计数量 ]; // 遍历数据集进行汇总 foreach ($processlist as $item) { // 遍历每个尺码字段(cmsl1 到 cmsl10) for ($i = 1; $i <= 10; $i++) { // 获取当前字段的数量 $sizeQty = $item['cmsl' . $i]; // 判断数量是否有效(非空且大于0) if (!empty($sizeQty)) { // 累加当前字段的数量 $scslTotals['cmsl' . $i] += $sizeQty; // 累加到总计字段 $scslTotals['zdtotal'] += $sizeQty; } } } $data['scslTotals'] = $scslTotals; foreach ($processlist as $key => $value) { // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对 for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { $processlist[$key][$value['cm' . $i]] = $value['cmsl' . $i]; } // 移除原始的 cm 和 cmsl 字段 unset($processlist[$key]['cm' . $i], $processlist[$key]['cmsl' . $i]); } } // 用于存储合并后的数据 $mergedData = []; // 按颜色备注进行合并 foreach ($processlist as $item) { $key = $item['颜色备注']; if (!isset($mergedData[$key])) { $mergedData[$key] = $item; // 添加条码字段,值为子订单编号 $mergedData[$key]['条码'] = $item['子订单编号']; } else { // 合并尺码对应的数量 foreach ($item as $size => $quantity) { if (is_numeric($size)) { if (!isset($mergedData[$key][$size])) { $mergedData[$key][$size] = 0; } $mergedData[$key][$size] += $quantity; } } // 合并 zdtotal $mergedData[$key]['zdtotal'] += $item['zdtotal']; } } // 查询船样为1的数据 $chuanyang = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where($where) ->where('船样', 1) ->field('子订单编号,颜色,款号,zdtotal,颜色备注,color_id, cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10, cmsl1,cmsl2,cmsl3,cmsl4,cmsl5,cmsl6,cmsl7,cmsl8,cmsl9,cmsl10,Uniqid') ->select(); foreach ($chuanyang as $key => $value) { // 将尺码和对应的数量从 cm1-cm10 和 cmsl1-cmsl10 转换为动态键值对 for ($i = 1; $i <= 10; $i++) { if ($value['cm' . $i] !== '' && $value['cm' . $i] !== null) { $chuanyang[$key][$value['cm' . $i]] = $value['cmsl' . $i]; } // 移除原始的 cm 和 cmsl 字段 unset($chuanyang[$key]['cm' . $i], $chuanyang[$key]['cmsl' . $i]); } // 添加条码字段,值为子订单编号 $chuanyang[$key]['条码'] = $value['子订单编号']; } // 将合并后的数据插入到相应颜色备注的原始数据尾部 $finalList = []; $groupedData = []; // 将原始数据按颜色备注分组 foreach ($processlist as $item) { $key = $item['颜色备注']; if (!isset($groupedData[$key])) { $groupedData[$key] = []; } $groupedData[$key][] = $item; } // 将合并后的数据插入到对应的颜色备注组的尾部 foreach ($groupedData as $key => $items) { $finalList = array_merge($finalList, $items); // 先添加原始数据 if (isset($mergedData[$key])) { $finalList[] = $mergedData[$key]; // 在组的尾部添加合并数据 } } // 将船样为1的数据添加到最终列表中 $finalList = array_merge($finalList, $chuanyang); $data['process'] = $finalList; $data['order'] = $list; $data['xhdata'] = $arr; $this->success('成功',$data); } elseif (strlen($afterDash) == 4) { //一条子订单编号一个条码,统计颜色 // $processlist = \db('工单_印件资料') // ->where('订单编号', $param['order']) // ->whereNull('Mod_rq') // 查询未删除数据 // ->select(); // // $table = []; // foreach ($processlist as $item) { // // 当前子订单编号的数据 // $subOrder = [ // '颜色备注' => $item['颜色备注'], // '色系名称' => $item['颜色'], // '订单编号' => $item['订单编号'], // '子订单编号' => $item['子订单编号'], // '条码' => $item['子订单编号'], // '款号' => $item['款号'], // ]; // // 当前子订单编号的合计 // $subOrderTotal = 0; // for ($i = 1; $i <= 10; $i++) { // if (!empty($item['cm' . $i])) { // $subOrder[$item['cm' . $i]] = $item['cmsl' . $i]; // // 累加每个尺码的数量 // $subOrderTotal += $item['cmsl' . $i]; // } // unset($item['cm' . $i], $item['cmsl' . $i]); // } // $subOrder['合计'] = $subOrderTotal; // 添加合计 // $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中 // } $processlist = \db('工单_印件资料') ->where('订单编号', $param['order']) ->whereNull('Mod_rq') // 查询未删除数据 ->select(); $table = []; foreach ($processlist as $item) { // 当前子订单编号的数据 $subOrder = [ '颜色备注' => $item['颜色备注'], '色系名称' => $item['颜色'], '订单编号' => $item['订单编号'], '子订单编号' => $item['子订单编号'], '条码' => $item['子订单编号'], '款号' => $item['款号'], ]; // 当前子订单编号的合计 $subOrderTotal = 0; for ($i = 1; $i <= 10; $i++) { // 判断 cm 和 cmsl 是否有值,空值不显示,0 则显示 if (isset($item['cm' . $i]) && $item['cm' . $i] !== '') { $subOrder[$item['cm' . $i]] = $item['cmsl' . $i] ?? 0; // 默认数量为 0 // 累加每个尺码的数量 $subOrderTotal += (int)$item['cmsl' . $i]; // 确保是数值类型 } // 清理字段 unset($item['cm' . $i], $item['cmsl' . $i]); } $subOrder['合计'] = $subOrderTotal; // 添加合计 $table[] = $subOrder; // 将当前子订单的数据添加到 $table 中 } // 初始化汇总数组 $scslTotals = [ "cmsl1" => 0, "cmsl2" => 0, "cmsl3" => 0, "cmsl4" => 0, "cmsl5" => 0, "cmsl6" => 0, "cmsl7" => 0, "cmsl8" => 0, "cmsl9" => 0, "cmsl10" => 0, "zdtotal" => 0, // 总计数量 ]; // 遍历数据集进行汇总 foreach ($processlist as $item) { // 遍历每个尺码字段(cmsl1 到 cmsl10) for ($i = 1; $i <= 10; $i++) { // 获取当前字段的数量 $sizeQty = $item['cmsl' . $i]; // 判断数量是否有效(非空且大于0) if (!empty($sizeQty)) { // 累加当前字段的数量 $scslTotals['cmsl' . $i] += $sizeQty; // 累加到总计字段 $scslTotals['zdtotal'] += $sizeQty; } } } $data['scslTotals'] = $scslTotals; $data['process'] = $table;//汇总数据集 $data['order'] = $list;//表格数据 $data['xhdata'] = $arr;//尺码表头 $this->success('成功', $data); // } } else { echo "子订单编号 - 后不是2位也不是4位:$afterDash"; } } else { echo "子订单编号中没有找到'-'"; } } /** * 订单编号自动获取 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getWorkOrder() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $time =substr( date('Ym',time()),2); $lastOrder = \db('工单_基本资料') ->where('订单编号','like','%'.$time.'%') ->order('Uniqid desc') ->find(); if (empty($lastOrder)){ $newNumber = 1; }else{ $lastNumber = substr($lastOrder['订单编号'],6); $newNumber = (int)$lastNumber + 1; } if ($newNumber<10){ $newOrder = 'DC'.$time.'00'.$newNumber; }elseif ($newNumber>=10 && $newNumber<100){ $newOrder = 'DC'.$time.'0'.$newNumber; }else{ $newOrder = 'DC'.$time.$newNumber; } $this->success('成功',$newOrder); } /** * 获取子订单编号 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getSuborder(){ // 确保是GET请求 if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['cy']) || !isset($param['order'])) { $this->error('参数错误'); } // 判断是否“船样”获取对应的子订单编号 if ($param['cy'] == '否') { //1.通过色系名称查询对应的编号 $colorlist = \db('工单_颜色编号') ->field('colorcode, colorname') ->where('colorname', $param['colorname'] ?? '') ->find(); if (empty($colorlist)) { $this->error('未找到对应的颜色编号'); } $num = $param['order'] . '-' . $colorlist['colorcode']; // 查询子订单编号 $data = \db('工单_印件资料') ->field('子订单编号,cm1,cm2,cm3,cm4,cm5,cm6,cm7,cm8,cm9,cm10') ->where('子订单编号', 'like', '%' . $num . '%') ->where('船样', '=', 0) ->order('子订单编号', 'desc') ->find(); if (empty($data)) { // 如果没有找到数据,生成默认的订单编号,后两位从00开始 $order = $param['order'] . '-' . $colorlist['colorcode'] . '00'; } else { if (strlen($data['子订单编号']) == 12) { $data = \db('工单_印件资料') ->where('订单编号',$param['order']) ->where('船样',0) ->order('子订单编号 desc') ->find(); if(empty($data)){ $order = $param['order'].'-01'; }else{ $num = (int)substr($data['子订单编号'],10) + 1; if ($num<10){ $order = $param['order'].'-0'.$num; }else{ $order = $param['order'].'-'.$num; } } }else{ // 如果找到数据,提取子订单编号并递增 $order = $data['子订单编号']; if (preg_match('/(.*-' . $colorlist['colorcode'] . ')(\d{2})$/', $order, $matches)) { $prefix = $matches[1]; // 前缀部分(包括订单编号和颜色代码) $number = $matches[2]; // 数字部分(后两位) // 循环生成子订单编号并检查数据库中是否存在相同编号 do { $incrementedNumber = (int)$number + 1; // 将数字部分补充为两位数,例如 1 -> 01,2 -> 02 $order = $prefix . str_pad($incrementedNumber, 2, '0', STR_PAD_LEFT); $exists = \db('工单_印件资料')->where('子订单编号', '=', $order)->find(); $number = $incrementedNumber; // 更新 number 用于下一次循环 } while ($exists); // 如果存在相同编号则继续循环递增 } else { $this->error('订单编号格式错误'); } } } //2.获取色系名称信息 $colorlist = \db('工单_颜色编号')->select(); //3.获取历史尺码数据 $cm_list = \db('工单_印件资料') ->where('订单编号', $param['order']) ->group('订单编号') ->find(); $cm_data = []; if ($cm_list) { // 精准筛选字段名以 "cm" 开头并跟1到2位数字的字段 foreach ($cm_list as $key => $value) { if (preg_match('/^cm\d{1,2}$/', $key)) { $cm_data[$key] = $value ?? ''; // 如果值为 null,设为空字符串 } } } else { // 如果查询结果为空,将 cm1 到 cm10 都设置为空 for ($i = 1; $i <= 10; $i++) { $cm_data["cm$i"] = ''; } } $result = ['order' => $order,'colorlist' => $colorlist,'cm' => $cm_data]; $this->success('成功', $result); } else if ($param['cy'] == '是') { //1.获取船样子订单编号 $data = \db('工单_印件资料') ->where('订单编号', $param['order']) ->where('船样', '=', 1) ->order('子订单编号 asc') ->find(); if (empty($data)) { // 如果没有数据,初始子订单编号为 '99' $order = $param['order'] . '-99'; } else { // 提取子订单编号中的数字部分 $subOrder = $data['子订单编号']; // 例如 "DC2410006-99" if (preg_match('/-(\d+)$/', $subOrder, $matches)) { $numberPart = (int)$matches[1]; // 提取到的数字部分 $newNumber = $numberPart - 1; // 减 1 // 将新的数字拼接回订单编号 $order = preg_replace('/-\d+$/', '-' . $newNumber, $subOrder); } else { $this->error('订单编号格式错误'); } } $this->success('成功', $order); } } /** * 获取PO号 【代码展示未用到】 */ public function getPonumber() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } // $num = substr($param['child_order'], 0, 12); // 如果需要,可以用这个方式截取子订单编号 // $sql = "SELECT * FROM `工单_印件资料` WHERE `子订单编号` LIKE '{$num}%' ORDER BY `子订单编号` DESC LIMIT 1"; // $data = \db()->query($sql); $colorlist = \db('工单_颜色编号') ->field('colorcode,colorname') ->where('colorname',$param['child_order']) ->find(); $num = $param['order'] . '-' . $colorlist['colorcode']; // 生成 num,用于模糊查询 $data = \db('工单_印件资料') ->where('子订单编号', 'like', '%' . $num . '%') ->order('子订单编号', 'desc') ->find(); if(count($data) === 1){ $order = $num.'01'; }else{ $number = (int)substr($data['子订单编号'],12,14) + 1; if ($num<10){ $order = $num.'0'.$number; }else{ $order = $num.$number; } } $this->success('成功',$order); } /** * 订单资料修改 * @return void * @throws \think\Exception * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function WorkOrderEdit() { if (Request::instance()->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $id = $param['id']; unset($param['id']); $sql = \db('工单_基本资料') ->where('Uniqid',$id) ->fetchSql(true) ->update($param); $res = \db()->query($sql); if ($res === false){ $this->error('失败'); }else{ $this->success('成功'); } } /** * 颜色资料修改 * @return void * @throws \think\Exception * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function PrintDataEdit() { if(Request::instance()->post() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $updata = [ '订单编号' => $param['订单编号'], '子订单编号' => $param['子订单编号'], '款号' => $param['款号'], '船样' => $param['船样'], '颜色' => $param['颜色'], 'color_id' => $param['color_id'], '颜色备注' => $param['颜色备注'], 'zdtotal' => $param['zdtotal'] ]; for ($i = 1; $i <= 10; $i++) { $updata["cmsl{$i}"] = isset($param["cmsl{$i}"]) ? $param["cmsl{$i}"] : ''; } $sql = \db('工单_印件资料') ->where('Uniqid', $param['id']) ->fetchSql(true) ->update($updata); $res = \db()->query($sql); if ($res !== false) { $this->success('修改成功'); } else { $this->error('修改失败'); } } /** * 图片上传 * @return void */ public function ImgUpload(){ $file = request()->file('image'); if($file){ $info = $file->validate(['size'=>10485760,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public' . DS . 'uploads'); if($info){ $fileName = $info->getSaveName(); // $ymd = date('Ymd'); // $imageUrl = '/uploads/' . $ymd.'/'.$fileName; $imageUrl = '/uploads/' . str_replace('\\', '/', $fileName); return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]); }else{ $res = $file->getError(); return json(['code' => 1, 'msg' => '失败', 'data' => $res]); } } return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]); } /** * 工单技术附件 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function OrderAttachments() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } $list = \db('工单_相关附件') ->where('关联编号',$param['order']) ->where('附件备注',$param['desc']) ->whereNull('Mod_rq') ->order('sys_rq desc') ->limit(1) ->select(); $this->success('成功',$list); } /** * 订单BOM资料显示 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function OrderBomList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['order'])){ $this->error('参数错误'); } $where = ['a.BOM_工单编号'=>$param['order']]; $list = \db('工单_bom资料') ->alias('a') ->join('工单_基本资料 b','b.订单编号 = a.BOM_工单编号') ->field('a.BOM_工单编号 as 订单编号,b.生产款号 as 生产款号,b.客户编号 as 客户编号,b.款式 as 款式, a.BOM_物料名称 as 物料名称,a.BOM_投料单位 as 投料单位,a.BOM_计划用量 as 计划用料,a.BOM_标准用量 as 定额用料, a.BOM_实际用量 as 裁床实际用料,a.BOM_desc as 备注,a.UNIQID, a.BOM_计划门幅 as 计划门幅, a.BOM_定额门幅 as 定额门幅,a.Sys_ID as ID,a.Sys_rq as 日期') ->where($where) ->whereNull('a.Mod_rq') ->select(); if (!empty($list)) { $this->success('成功', $list); } else { // 调用其他方法处理逻辑(例如记录日志、执行其他操作等) $this->GdGtpAiOrder($param['order']); // 返回空数据的统一处理 return $this->success('没有找到相关数据', []); } } /** * Bom资料删除 * */ public function Bomdel() { if ($this->request->isGet() === false) { $this->error('请求错误'); } // 获取请求参数 $param = $this->request->param(); // 检查参数是否为空或者缺少UNIQID if (empty($param) || !isset($param['UNIQID'])) { $this->error('参数错误'); } // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID $uniqids = strpos($param['UNIQID'], ',') !== false ? explode(',', $param['UNIQID']) : [$param['UNIQID']]; $where = []; $where['Mod_rq'] = date('Y-m-d H:i:s', time()); // 定义一个标志变量来追踪是否所有更新都成功 $allUpdated = true; $failedUniqids = []; // 遍历所有UNIQID并更新数据库 foreach ($uniqids as $uniqid) { // 更新指定UNIQID的记录 $result = \db('工单_bom资料') ->where('UNIQID', $uniqid) ->update($where); // 检查更新结果 if (!$result) { // 如果某个UNIQID更新失败,记录失败的ID $allUpdated = false; $failedUniqids[] = $uniqid; } } // 如果所有更新都成功,返回成功信息 if ($allUpdated) { $list = \db('工单_bom资料') ->whereIn('UNIQID', $uniqids) // 查询所有传入的UNIQID ->select(); if (!empty($list)) { $this->success('删除成功'); } else { $this->GdGtpAiOrder($param['order']); return $this->success('没有找到相关数据', []); } } else { // 如果有更新失败的记录,返回失败的UNIQID $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids)); } } /** * 前端选择订单时如果BOM资料数据为空则单独调用,重新生成最新 */ public function GdGtpAiOrder($order){ // 判断是否有指定的订单号 if (!empty($order)) { // 查询单个订单的最大编号 $maxOrder = \db('工单_基本资料') ->where('订单编号', 'like', "{$order}%") ->order('订单编号', 'desc') ->limit(1) ->value('订单编号'); // 查询该订单的基本资料 $list = \db('工单_基本资料') ->where('订单编号', 'like', "{$order}%") ->order('订单编号', 'desc') ->limit(1) ->find(); // 如果面料数据为空,提示错误 if (empty($list['面料'])) { $this->error('面料数据为空无法定义BOM'); } // 处理订单编号 $numericPart = substr($maxOrder, 2); $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT); $param['订单编号'] = $order . $newNumericPart; // 处理物料信息 $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬']; $mianliao = $this->Gpt($massage); // 插入物料数据 $data = []; foreach ($mianliao as $key => $value) { if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬 $data[] = [ 'BOM_工单编号' => $list['订单编号'], 'BOM_物料名称' => $value, 'Sys_rq' => date('Y-m-d H:i:s'), 'Sys_ID' => '超级管理员' ]; } } // 批量插入BOM资料 if (!empty($data)) { \db('工单_bom资料')->insertAll($data); } $this->success('成功',$order); } else { // 如果没有指定订单号,批量查询订单号并处理 $has_bom = \db('工单_bom资料')->alias('a')->field('a.BOM_工单编号')->group('a.BOM_工单编号')->select(); $all_orders = \db('工单_基本资料')->alias('a')->field('a.订单编号')->where('a.面料', '<>', '')->group('a.订单编号')->select(); // 提取有BOM资料的订单号 $has_bom_orders = array_column($has_bom, 'BOM_工单编号'); // 筛选出没有对应BOM资料的订单号 $no_bom_orders = array_filter($all_orders, function ($order) use ($has_bom_orders) { return !in_array($order['订单编号'], $has_bom_orders); }); // 遍历没有BOM资料的订单 foreach ($no_bom_orders as $orderData) { // 获取该订单号的最大订单编号 $maxOrder = \db('工单_基本资料') ->where('订单编号', 'like', "{$orderData['订单编号']}%") ->order('订单编号', 'desc') ->limit(1) ->value('订单编号'); // 获取该订单号的具体数据 $list = \db('工单_基本资料') ->where('订单编号', 'like', "{$orderData['订单编号']}%") ->order('订单编号', 'desc') ->limit(1) ->find(); if (empty($list['面料'])) { $this->error("订单 {$orderData['订单编号']} 面料数据为空,无法定义BOM"); } // 处理订单编号 $numericPart = substr($maxOrder, 2); $newNumericPart = str_pad((int)$numericPart + 1, strlen($numericPart), '0', STR_PAD_LEFT); $param['订单编号'] = $order . $newNumericPart; // 处理物料信息 $massage = empty($list['粘衬']) || $list['粘衬'] == '无' ? $list['面料'] : $list['面料'] . ',粘衬:' . $list['粘衬']; $mianliao = $this->Gpt($massage); // 插入物料数据 $data = []; foreach ($mianliao as $key => $value) { if (!empty($value) && $value !== '粘衬') { // 排除空值和粘衬 $data[] = [ 'BOM_工单编号' => $list['订单编号'], 'BOM_物料名称' => $value, 'Sys_rq' => date('Y-m-d H:i:s'), 'Sys_ID' => '超级管理员' ]; } } // 批量插入BOM资料 if (!empty($data)) { \db('工单_bom资料')->insertAll($data); } } $this->success('成功'); } } /** * 订单面料修改接口 * @return void * @throws \think\Exception * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function FabricEdit() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('请求错误'); } // 判断前端传来的参数是否为空,避免空订单编号造成问题 if(empty($param[0]['BOM_工单编号'])){ $this->error('请求错误,请重新打开此页面'); } foreach ($param as $key=>$value){ $data = $value; unset($data['UNIQID']); if ($value['UNIQID'] !== '' || !empty($value['UNIQID'])){ $sql = \db('工单_bom资料') ->where('UNIQID',$value['UNIQID']) ->fetchSql(true) ->update($data); $res = \db()->query($sql); }else{ $sql = \db('工单_bom资料') ->fetchSql(true) ->insert($value); $res = \db()->query($sql); } if ($res === false){ $this->error('修改失败'); } } $this->success('修改成功'); } /** * 订单BOM出库、退还详情显示 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function FabricDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['order'])){ $this->error('参数错误'); } if (isset($param['searchh']) || !empty($param['search'])){ $list['入库记录'] = \db('设备_报工日志') ->where('order_id|款号',$param['order']) ->where('name','入库') ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 入库时间,sys_id as 入库人员,recipient as 入仓人员,receipt_number as 单号') ->group('receipt_number') ->order('入库时间 desc') ->whereNull('Mod_rq') ->select(); }else{ //出库记录查询 $list['出库记录'] = \db('设备_报工日志') ->where('order_id|款号',$param['order']) ->where('name','出库') ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 出库时间,sys_id as 出库人员,receipt_number as 出库单据编号,recipient as 出库人员,receipt_number as 单号') ->group('receipt_number') ->order('出库时间 desc') ->whereNull('Mod_rq') ->select(); //退还记录查询 $list['退还记录'] = \db('设备_报工日志') ->where('order_id|款号',$param['order']) ->where('name','退还') ->field('id,order_id as 订单编号,款号,sum(number) as 数量,rq as 退还时间,sys_id as 退还机台,recipient as 退还人员,receipt_number as 单号') ->group('receipt_number') ->order('退还时间 desc') ->whereNull('Mod_rq') ->select(); } $this->success('成功',$list); } /** * 入库、出库、退还详情数据 */ public function FabricDetaillist() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['order'])){ $this->error('参数错误'); } // 定义查询字段 $fields = ' a.id, a.order_id as 订单编号, a.客户编号, a.款号 as 生产款号, a.款式, a.物料名称, b.BOM_计划用量 as 计划用料, b.BOM_标准用量 as 定额用料, b.BOM_计划门幅 as 计划门幅, b.BOM_定额门幅 as 定额门幅, b.BOM_实际用量 as 裁床实际用料, b.Bom_领用数量 as 裁床领用面料, b.BOM_退还数量 as 裁床退还数量, b.BOM_库存总量 as 库存总量, b.BOM_面料结余 as 面料结余, a.number as 入库数量, a.number as 出库数量, b.BOM_投料单位 as 投料单位, a.rq as 入库时间, a.rq as 出库时间, a.sys_id as 入库人员, a.sys_id as 出库人员, a.recipient as 领用人员, a.receipt_number as 单号, b.BOM_desc as 备注 '; if (isset($param['searchh']) || !empty($param['search'])){ $list['入库记录'] = \db('设备_报工日志')->alias('a') ->join('工单_bom资料 b', 'b.BOM_工单编号 = a.order_id AND a.物料名称 = b.BOM_物料名称', 'left') // 多条件关联 ->where('a.receipt_number', $param['order']) ->where('a.name', '入库') ->field($fields) ->order('a.rq desc') ->whereNull('a.Mod_rq') ->select(); }else{ //出库记录查询 $list['出库记录'] = \db('设备_报工日志')->alias('a') ->join('工单_bom资料 b', 'b.BOM_工单编号 = a.order_id AND a.物料名称 = b.BOM_物料名称', 'left') // 多条件关联 ->where('a.receipt_number', $param['order']) ->where('a.name', '出库') ->field($fields) ->order('a.rq desc') ->whereNull('a.Mod_rq') ->select(); //退还记录查询 $list['退还记录'] = \db('设备_报工日志')->alias('a') ->join('工单_bom资料 b', 'b.BOM_工单编号 = a.order_id AND a.物料名称 = b.BOM_物料名称', 'left') // 多条件关联 ->where('a.receipt_number', $param['order']) ->where('a.name', '退还') ->field($fields) ->order('a.rq desc') ->whereNull('a.Mod_rq') ->select(); } $this->success('成功',$list); } /** * 入库、出库、退还删除 */ public function FabricDetaildel() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('请求错误'); } // 判断UNIQID是否是逗号分隔的多个ID,如果是则拆分成数组,否则直接处理为单个ID $ids = strpos($param['id'], ',') !== false ? explode(',', $param['id']) : [$param['id']]; $where = []; $where['Mod_id'] = $param['Mod_id']; $where['Mod_rq'] = date('Y-m-d H:i:s', time()); // 定义一个标志变量来追踪是否所有更新都成功 $allUpdated = true; $failedUniqids = []; // 遍历所有UNIQID并更新数据库 foreach ($ids as $id) { // 更新指定UNIQID的记录 $result = \db('设备_报工日志') ->where('id', $id) ->update($where); // 检查更新结果 if (!$result) { // 如果某个UNIQID更新失败,记录失败的ID $allUpdated = false; $failedUniqids[] = $id; } } // 如果所有更新都成功,返回成功信息 if ($allUpdated) { $list = \db('设备_报工日志') ->whereIn('id', $id) // 查询所有传入的UNIQID ->select(); if (!empty($list)) { $this->success('删除成功'); } else { $this->GdGtpAiOrder($param['order']); return $this->success('没有找到相关数据', []); } } else { // 如果有更新失败的记录,返回失败的UNIQID $this->error('部分更新失败,无法更新以下UNIQID: ' . implode(', ', $failedUniqids)); } } //GPT public function Gpt($massage) { // 设置 API 密钥 $apiKey = 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY'; // 替换为您的 API 密钥 // 要发送给 GPT 的消息 $messages = [ [ 'role' => 'user', 'content' => '你好,帮我按照:面料1、面料2、面料3……来整理归纳下面的面料信息,我只需要面料信息,不需要其他:'.$massage ] ]; // 创建请求数据 $data = [ 'model' => 'gpt-3.5-turbo', // 使用的模型 'messages' => $messages, 'max_tokens' => 100, // 设置最大 token 数 ]; // 初始化 cURL $ch = curl_init('https://niubi.zeabur.app/v1/chat/completions'); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Authorization: Bearer ' . $apiKey, ]); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_CAINFO, ROOT_PATH . '/public/uploads/cacert.pem'); // 执行请求 $response = curl_exec($ch); // 检查错误 if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } // 关闭 cURL curl_close($ch); // 解析和输出响应 $responseData = json_decode($response, true); // 获取 GPT 的回复 if (isset($responseData['choices'][0]['message']['content'])) { //获取返回内容 $gptReply = $responseData['choices'][0]['message']['content']; // halt($gptReply); //返回面料信息 $gptArray = explode('面料',$gptReply); array_shift($gptArray); foreach ($gptArray as $key=>$value){ $gptArray[$key] = preg_replace('/\s+/', '', substr($value,4)); } return $gptArray; } else { echo "未能获取 GPT 的回复。"; } } /** * 面料库存月份查询 */ public function fabricListmonth() { // 使用DATE_FORMAT函数提取年月部分,格式为 "YYYY-MM" $list = \db('工单_bom资料') ->alias('a') ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号') ->field('DATE_FORMAT(a.Sys_rq, "%Y-%m") as date') // 提取年月,格式化为 "YYYY-MM" ->group('date') ->order('date desc') ->select(); // 格式化返回的数据,生成期望的结构 $result = []; foreach ($list as $item) { $result[] = ['date' => $item['date']]; // 返回格式为 {"date": "YYYY-MM"} } if (!empty($result)) { $this->success('成功', $result); // 返回查询到的结果 } else { $this->success('没有找到相关数据', []); // 如果没有数据,返回空数组 } } /** * 面料库存列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function fabricList() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); $where = []; // 根据传入的参数构造查询条件 if (isset($param['order'])) { $where['a.BOM_工单编号|b.生产款号|a.BOM_物料名称'] = ['like', $param['order'] . '%']; } if (isset($param['mouth'])) { $where['a.Sys_rq'] = ['like', $param['mouth'] . '%']; } // 分页参数,防止未传递时出错 $page = isset($param['page']) ? (int)$param['page'] : 1; $limit = isset($param['limit']) ? (int)$param['limit'] : 50; // 默认每页50条数据 // 获取数据 $data = \db('工单_面料资料') ->alias('a') ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号') ->field('a.BOM_工单编号 as 订单编号,a.BOM_颜色 as 颜色, b.生产款号 as 生产款号, b.客户编号 as 客户编号, b.款式 as 款式, a.BOM_物料编码,a.BOM_物料名称 as 物料名称, a.BOM_投料单位 as 投料单位, a.BOM_实际用量 as 裁床实际用料, a.BOM_领用数量 as 裁床领用面料, a.BOM_退还数量 as 裁床退回仓库面料, a.BOM_desc as 备注, a.UNIQID, a.BOM_库存总量 as 入库总量, a.BOM_计划门幅 as 计划门幅, a.BOM_定额门幅 as 定额门幅, a.BOM_面料结余 as 面料结余, a.Sys_ID as ID, a.Sys_rq as 日期') ->where($where) ->group('a.BOM_物料编码') ->order("a.BOM_工单编号 desc") ->limit(($page - 1) * $limit, $limit) ->select(); // 获取数据总数 $count = \db('工单_面料资料') ->alias('a') ->join('工单_基本资料 b', 'b.订单编号 = a.BOM_工单编号') ->where($where) ->count(); // 如果查询到数据 if (!empty($data)) { // 构建返回数据 $list = [ 'total' => $count, // 总数 'table' => $data // 数据内容 ]; // 返回成功响应 $this->success('成功', $list); } else { // 没有查询到数据 $this->success('没有找到相关数据', []); } } /** * 单条面料详情 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function oneFabricDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } //面料入库记录 $list['入库'] = \db('设备_报工日志') ->where('',$param['order']) ->where('物料名称',$param['fabricName']) ->where('name','入库') ->where('Mod_rq',null) ->field('order_id as 订单编号,款号,物料编码,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 入仓人员') ->select(); //面料出库记录 $list['出库'] = \db('设备_报工日志') ->where('order_id',$param['order']) ->where('物料名称',$param['fabricName']) ->where('name','出库') ->where('Mod_rq',null) ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员') ->select(); //面料退还记录 $list['退还'] = \db('设备_报工日志') ->where('order_id',$param['order']) ->where('物料名称',$param['fabricName']) ->where('name','退还') ->where('Mod_rq',null) ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,recipient as 退还人员') ->select(); $this->success('成功',$list); } /** * 单据号查询数据 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function ReceiptDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('单据编号参数错误'); } $list = \db('设备_报工日志') // ->where('receipt_number','like','%',$param['receipt'].'%') ->where('receipt_number',$param['receipt']) ->field('order_id as 订单编号,款号,物料名称,number as 数量,rq as 日期,sys_id as 操作机台,receipt_number as 出库单据编号,recipient as 领用人员') ->whereNull('Mod_rq') ->select(); if (empty($list)){ $this->error('未找到该出库单'); }else{ $this->success('成功',$list); } } /** * 出库单列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function ReceiptList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } $where= []; if (isset($param['search'])) { $where['物料名称|款号|order_id|receipt_number'] = ['like', $param['search'] . '%']; } if (isset($param['mouth'])) { $where['rq'] = ['like', $param['mouth'] . '%']; } $page = $param['page']; $limit = $param['limit']; $list = \db('设备_报工日志') ->where($where) ->field('receipt_number as 出库单,order_id as 订单编号,款号,物料名称,rq as 日期,sys_id as 操作机台,recipient as 领料人员,name as 单号类型') ->group('出库单') ->order('rq desc') ->where('Mod_rq',null) ->limit(($page-1)*$limit,$limit) ->select(); $count = \db('设备_报工日志') ->where($where) ->field('receipt_number as 出库单') ->group('出库单') ->where('Mod_rq',null) ->order('rq desc') ->select(); if (empty($list)){ $this->success('未找到数据'); }else{ $data['total'] = count($count); $data['table'] = $list; $this->success('成功',$data); } } /** * 出库单左侧菜单 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getReceiptTab() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $list = \db('设备_报工日志') ->field([ "DATE_FORMAT(rq, '%Y-%m') AS month", ]) ->group('month') ->order('month DESC') ->select(); if (empty($list)){ $this->error('未查询到入库、出库、退还数据'); }else{ $this->success('成功',$list); } } /** * 获取入库单号、出库单号 * @return void */ public function gitReceiptNumber() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } $lastNumber = \db('设备_报工日志') ->where('receipt_number','like',$param['number'].'%') ->order('receipt_number desc') ->limit(1) ->column('receipt_number as 单号'); if (empty($lastNumber)){ $num = 1; }else{ $num = (int)(substr($lastNumber[0],12,3))+1; } if ($num < 10){ $num = '00'.$num; }elseif ($num>=10 && $num<100){ $num = '0'.$num; }else{ $num; } $number = $param['number'].date('Ymd',time()).'-'.$num; $lastNumber = \db('设备_报工日志') ->where('name','出库') ->order('recipient desc') ->group('recipient') ->limit(1) ->value('recipient as 人员'); $data = [ 'number' => $number, 'username' => $lastNumber ]; $this->success('成功', $data); } }