| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271 |
- <?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\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['颜色备注'])
- ->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('修改成功');
- }
- /**
- * 入库、出库、退还详情数据
- */
- 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 备注
- ';
- $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('设备_报工日志')
- // ->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);
- }
- /**
- * 面料批次列表
- * @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'].'%')
- ->select();
- if (empty($list)){
- $this->error('未找到面料数据');
- }else{
- $this->success('成功',$list);
- }
- }
- }
|