| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Writer\Pdf\Tcpdf;
- use think\Config;
- use think\Db;
- use think\Exception;
- use think\Request;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use function fast\e;
- /**
- * 工单资料管理
- */
- class WorkOrder extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function _initialize()
- {
- if (isset($_SERVER['HTTP_ORIGIN'])) {
- header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
- }
- //跨域检测
- check_cors_request();
- if (!isset($_COOKIE['PHPSESSID'])) {
- Config::set('session.id', $this->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['颜色备注'])
- ->where('Mod_rq',null)
- ->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_物料名称'],
- 'U8UID' => $value['UNIQID'],
- '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);
- if (!empty($colorList)){
- //工单面料录入
- $fabricSql = \db('工单_面料资料')->fetchSql(true)->insertAll($colorList);
- $fabricRes = \db()->query($fabricSql);
- }
- if (!empty($MaterielList)){
- //工单关联表录入
- $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,
- 'BOM_desc' => $massage,
- '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,
- 'BOM_desc' => $massage,
- '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('修改成功');
- }
- /**
- * 入库、出库、退还详情数据
- */
- public function FabricDetaillist()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- $where = [];
- if (isset($param['order']) && !empty($param['order'])){
- $where['a.order_id'] = $param['order'];
- }
- if (isset($param['lotNumber']) && !empty($param['lotNumber'])){
- $where['a.批次号'] = $param['lotNumber'];
- }
- // 定义查询字段
- $fields = '
- a.id,
- a.批次号,
- a.order_id as 订单编号,
- a.客户编号,
- a.款号 as 生产款号,
- a.款式,
- a.物料名称,
- c.BOM_计划用量 as 计划用料,
- c.BOM_标准用量 as 定额用料,
- c.BOM_计划门幅 as 计划门幅,
- c.BOM_定额门幅 as 定额门幅,
- b.入仓总量 as 入库总量,
- b.库存数量 as 面料结余,
- a.number as 入库数量,
- a.number as 出库数量,
- a.库存数量 as 库存数量,
- b.单位 as 投料单位,
- a.rq as 入库时间,
- a.rq as 出库时间,
- a.sys_id as 入库人员,
- a.sys_id as 出库人员,
- a.recipient as 领用人员,
- a.receipt_number as 单号,
- c.BOM_desc as 备注,
- a.type as 退还类型
- ';
- $list['入库记录'] = \db('设备_报工日志')
- ->alias('a')
- ->join('物料_库存 b', 'a.批次号 = b.批次号 AND a.物料编码 = b.物料编号', 'left') // 多条件关联
- ->join('工单_面料资料 c','a.order_id = c.BOM_工单编号 AND a.物料编码 = c.BOM_物料编码','left')
- ->where($where)
- ->where('a.name', '入库')
- ->field($fields)
- ->order('a.rq desc')
- ->whereNull('a.Mod_rq')
- ->select();
- //出库记录查询
- $list['出库记录'] = \db('设备_报工日志')->alias('a')
- ->join('物料_库存 b', 'a.批次号 = b.批次号 AND a.物料编码 = b.物料编号', 'left') // 多条件关联
- ->join('工单_面料资料 c','a.order_id = c.BOM_工单编号 AND a.物料编码 = c.BOM_物料编码','left')
- ->where($where)
- ->where('a.name', '出库')
- ->field($fields)
- ->order('a.rq desc')
- ->whereNull('a.Mod_rq')
- ->select();
- //退还记录查询
- $list['退还记录'] = \db('设备_报工日志')->alias('a')
- ->join('物料_库存 b', 'a.批次号 = b.批次号 AND a.物料编码 = b.物料编号', 'left') // 多条件关联
- ->join('工单_面料资料 c','a.order_id = c.BOM_工单编号 AND a.物料编码 = c.BOM_物料编码','left')
- ->where($where)
- ->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('物料_库存')
- ->group('date')
- ->order('date desc')
- ->column('DATE_FORMAT(sys_rq, "%Y-%m") as date');
- // 格式化返回的数据,生成期望的结构
- $result = [];
- foreach ($list as $item){
- $result[$item] = \db('物料_库存')
- ->group('date')
- ->order('date desc')
- ->column('DATE_FORMAT(sys_rq, "%Y-%m-%d") as date');
- }
- 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('设备_报工日志')
- ->alias('a')
- ->join('工单_面料资料 b','a.order_id = b.BOM_工单编号 AND b.BOM_物料编码 = a.物料编码')
- ->join('物料_库存 c','a.物料编码 = c.物料编号 AND a.批次号 = c.批次号')
- ->where('a.receipt_number',$param['receipt'])
- ->field('a.order_id as 订单编号,a.款号,a.物料名称,a.number as 数量,a.rq as 日期,a.sys_id as 操作机台,
- a.receipt_number as 出库单据编号,a.recipient as 领用人员,b.BOM_颜色,c.单位,b.BOM_计划门幅 as 计划门幅,
- b.BOM_定额门幅 as 定额门幅,c.实际门幅,c.状态,c.库存数量,c.入仓总量')
- ->whereNull('a.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 单号类型,sum(number) 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);
- }
- /**
- * 面料批次列表
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function FabricLotList()
- {
- if ($this->request->isGet() === false){
- $this->error('请求错误');
- }
- $param = $this->request->param();
- if (empty($param)){
- $this->error('参数错误');
- }
- $field = '
- a.批次号,
- a.关联号,
- a.物料编号,
- a.物料名称,
- a.入仓总量,
- a.库存数量,
- a.领用数量,
- a.退还数量 as 裁切退还,
- a.单位,
- a.实际门幅,
- a.状态,
- a.sys_id as 入仓人员,
- a.sys_rq as 入仓日期,
- b.BOM_颜色 as 颜色,
- b.BOM_计划用量 as 计划用料,
- b.BOM_标准用量 as 定额用料,
- b.BOM_计划门幅 as 计划门幅,
- b.BOM_定额门幅 as 定额门幅,
- GROUP_CONCAT(DISTINCT c.订单编号 SEPARATOR ",") AS 关联订单
- ';
- $list = \db('物料_库存')
- ->alias('a')
- ->join('工单_面料资料 b','a.物料编号 = b.BOM_物料编码')
- ->join('工单关联表 c','a.关联号 = c.关联编号')
- ->field($field)
- ->where('a.sys_rq','like',$param['date'].'%')
- ->group('a.批次号')
- ->select();
- if (empty($list)){
- $this->error('未找到面料数据');
- }else{
- $this->success('成功',$list);
- }
- }
- //单条工单BOM删除接口
- }
|