model = new \app\admin\model\MatterUse; $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("isScrapList", $this->model->getIsScrapList()); $this->view->assign("lStatusList", $this->model->getLStatusList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ public function index(){ //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { // $params = $this->request->request(); // print_r($params);die; //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $list = collection($list)->toArray(); foreach ($list as $key=>$value){ $list[$key]['out_weight'] = $value['out_weight']/1000; $list[$key]['back_weight'] = $value['back_weight']/1000; } $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); $params['status'] = 1; $params['out_weight'] = $params['out_weight'] * 1000; $params['create'] = date('Y-m-d H:i:s'); Db::startTrans(); try{ //查询nfc表是否有数据 $nfc = Db::name('bnfc')->whereTime('create','today')->where('status',0)->order('id desc')->find(); if (empty($nfc)){ throw new Exception('没有查询到nfc标签,请贴nfc标签并扫描'); } //将绑定的nfc插入到nfc总表,绑定到领用的数据上 $nfcData = array(); $nfcData['content'] = preg_replace('# #', '',$nfc['content']); $nfcData['b_nfc'] = $nfc['id']; $nfcData['create'] = date('Y-m-d H:i:s'); $nfc_id = Db::name('nfc')->insertGetId($nfcData); if (empty($nfc_id)){ throw new Exception('插入数据失败,请联系开发人员'); } $info = Db::name('matter_detail')->where('code',$params['code'])->order('id desc')->find(); $params['ink_name'] = $info['name']; $params['out'] = $info['bach']; $matter_id = Db::name('matter_use')->insertGetId($params); if (empty($matter_id)){ throw new Exception('插入领用数据失败,请联系开发人员'); } //绑定 Db::name('matter_use')->where('id',$matter_id)->setField('nfc',$nfc_id); //将ndc状态改为已绑定 Db::name('nfc')->where('id',$nfc_id)->setField('status',2); //将bnfc状态更改 $bnfcData = array(); $bnfcData['status'] = 1; $bnfcData['content'] = $nfcData['content']; $bnfcData['update'] = date('Y-m-d H:i:s'); Db::name('bnfc')->where('id',$nfc['id'])->update($bnfcData); Db::commit(); $this->success(); }catch (\think\Exception\DbException $exception){ Db::rollback(); $this->error($exception->getMessage()); } } return $this->view->fetch(); } //出库 public function out(){ if ($this->request->isPost()){ $params = input('order_number'); $order_number = substr($params,0,7); $process = substr($params,8,3); $current_process = substr($params,12,3); $machine_number = input('machine_number'); Db::startTrans(); try{ $cnfc = Db::name('cnfc')->where('status',0)->whereTime('create','today')->order('id desc')->select(); if (empty($cnfc)){ throw new Exception('暂无数据'); } $data = array(); //查出所有机台号nfc,排除人员信息 $allMachine = Db::name('machine_nfc')->column('content'); foreach ($cnfc as $key => $value){ $value['content'] = preg_replace('# #', '',$value['content']); if (in_array($value['content'],$allMachine)){ continue; // $machine_number = Db::name('machine_nfc')->where('content',$value['content'])->value('machine_number'); }else{ $data[$key] = Db::name('nfc')->where('content',$value['content'])->where('status',2)->value('id'); if (empty($data[$key])){ throw new Exception('未查询到nfc数据'); } } $changeNfc = Db::name('nfc')->where('content',$value['content'])->where('status',2)->setField('c_nfc',$value['id']); Db::name('nfc')->where('content',$value['content'])->setField('status',3); if (!$changeNfc){ throw new Exception('更改nfc出库id失败'); } $changeCnfc = Db::name('cnfc')->where('id',$value['id'])->setField('status',1); $changeContent = Db::name('cnfc')->where('id',$value['id'])->setField('content',$value['content']); $changeUpdate = Db::name('cnfc')->where('id',$value['id'])->setField('update',date('Y-m-d H:i:s')); if (!$changeCnfc || !$changeUpdate){ throw new Exception('更改cnfc状态失败'); } } //专色墨领用的记录 // $collar = Db::name('collar_use')->where('nfc','in',$data)->where('l_status',1)->where('status',1)->select(); $matter = Db::name('matter_use')->where('nfc','in',$data)->where('l_status',1)->where('status',1)->select(); if (!empty($matter)){ //扣减油墨重量,更改领用状态,更改nfc状态 foreach ($matter as $k1=>$v1){ $totalWeight = Db::name('matter')->where('code',$v1['code'])->find(); $matterWeight = $totalWeight['weight'] - $v1['out_weight']; $res = Db::name('matter')->where('id',$totalWeight['id'])->setField('weight',$matterWeight); //更改详细库存 $detailWeight = Db::name('matter_detail')->where('code',$v1['code'])->order('weight desc')->find(); $newDetailWeight = $detailWeight['weight'] - $v1['out_weight']; $resDetail = Db::name('matter_detail')->where('id',$detailWeight['id'])->setField('weight',$newDetailWeight); if (!$res){ throw new Exception('扣减油墨库存重量失败'); } $matterUpdate= array(); $matterUpdate['l_status'] = 2; $matterUpdate['process'] = $process; $matterUpdate['current_process'] = $current_process; $matterUpdate['machine_number'] = $machine_number; $result = Db::name('matter_use')->where('id',$v1['id'])->update($matterUpdate); if (!$result){ throw new Exception('更新领用状态失败'); } } }else{ throw new Exception('未查询到领用数据或此批次已出库'); } // if (!empty($collar)){ // //扣减专色墨重量,更改领用状态,更改nfc状态 // foreach ($collar as $k=>$v){ // //根据批次号去查详细重量和总重量 // $detail = Db::name('warehousing_detail')->where('bach_number',$v['warehousing'])->find(); // $detailWeight = $detail['weight'] - $v['weight']; // //根据产品编号和专色墨颜色,查出总重量,扣减 // $total = Db::name('warehousing')->where('product_number',$detail['product_number'])->where('cid',$detail['cid'])->find(); // $totalWeight = $total['weight'] - $v['weight']; // $detailRes = Db::name('warehousing_detail')->where('bach_number',$v['warehousing'])->setField('weight',$detailWeight); // if (!$detailRes){ // throw new Exception('扣减详细库存重量失败'); // } // $totalRes = Db::name('warehousing')->where('product_number',$detail['product_number'])->where('cid',$detail['cid'])->setField('weight',$totalWeight); // if (!$totalRes){ // throw new Exception('扣减总库存重量失败'); // } // $update = array(); // $update['l_status'] = 2; // $update['process'] = $process; // $update['current_process'] = $current_process; // $update['machine_number'] = $machine_number; // $changData = Db::name('collar_use')->where('id',$v['id'])->update($update); // if (!$changData){ // throw new Exception('更改数据失败'); // } // } // } Db::commit(); $this->success(); }catch (\think\Exception\DbException $exception){ Db::rollback(); $this->error($exception->getMessage()); } } return $this->view->fetch(); } //退回 public function back(){ if ($this->request->isPost()){ $params = input('row/a'); $params = $this->preExcludeFields($params); if (!is_numeric($params['back_weight'])){ $this->error('重量只能为数字'); } $params['back_weight'] = $params['back_weight'] * 1000; //暂时退回锁定不是下机墨 $params['is_scrap'] = 0; Db::startTrans(); try{ //先去查tnfc有没有数据 $nfc = Db::name('tnfc')->whereTime('create','today')->where('status',0)->order('id desc')->find(); if (empty($nfc)){ throw new Exception('没有查询到nfc标签'); } //去nfc总表查出id $nfcData = preg_replace('# #', '',$nfc['content']); $nfcId = Db::name('nfc')->where('content',$nfcData)->order('id desc')->value('id'); $matterData = Db::name('matter_use')->where('nfc',$nfcId)->where('out_weight','neq','')->find(); $backData = array(); $backData['order_number'] = $matterData['order_number']; $backData['code'] = $matterData['code']; $backData['ink_name'] = $matterData['ink_name']; $backData['back'] = $matterData['out']; $backData['back_weight'] = $params['back_weight']; $backData['nfc'] = $nfcId; $backData['l_status'] = 2; $backData['status'] = 2; $backData['machine_number'] = $matterData['machine_number']; $backData['create'] = date('Y-m-d H:i:s'); $backResult = Db::name('matter_use')->insert($backData); if (!$backResult){ throw new Exception('插入数据失败,请联系开发人员'); } //将ndc状态改为已绑定 Db::name('nfc')->where('id',$nfcId)->setField('status',4); //将tnfc状态更改 $tnfcData = array(); $tnfcData['status'] = 1; $tnfcData['content'] = $nfcData; $tnfcData['update'] = date('Y-m-d H:i:s'); Db::name('tnfc')->where('id',$nfc['id'])->update($tnfcData); //增加库存 $detail = Db::name('matter_detail')->where('code',$matterData['code'])->order('id desc')->find(); $detailWeight = $detail['weight'] + $params['back_weight']; $total = Db::name('matter')->where('code',$matterData['code'])->find(); $totalWeight = $total['weight'] + $params['back_weight']; $detailResult = Db::name('matter_detail')->where('id',$detail['id'])->setField('weight',$detailWeight); $totalResult = Db::name('matter')->where('id',$total['id'])->setField('weight',$totalWeight); if (empty($detailResult) || empty($totalResult)){ throw new Exception('退库失败'); } Db::commit(); $this->success(); }catch (\think\Exception\DbException $exception){ Db::rollback(); $this->error($exception->getMessage()); } } return $this->view->fetch(); } public function getData(){ $params = input(''); if (empty($params['id'])){ return array('status'=>0,'msg'=>'请选中打印数据'); } $res = Db::name('matter_use')->where('id',$params['id'])->find(); if (!empty($res)){ return array('status'=>1,'data'=>$res); }else{ return array('status'=>0,'msg'=>'数据错误'); } } //根据生产批次号获取原墨重量 public function getCode(){ $code = input('code'); if (empty($code)){ return array('status'=>0,'msg'=>'参数错误'); } $productInfo = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = $code limit 1"); $productNumber = $productInfo[0]['Gd_cpdh']; $productName = $productInfo[0]['Gd_cpmc']; //在大小盒计划消耗表查出所有原墨,去掉数据里的专色墨 $where[] = ['exp',Db::raw("FIND_IN_SET($productNumber,product_number)")]; $result = Db::name('formula_consume')->where($where)->field('ink,code')->select(); //查出所有专色墨 $warehousing = Db::name('formula')->where($where)->column('color'); foreach ($result as $key=>$value){ if (in_array($value['ink'],$warehousing) || empty($value['ink'])){ unset($result[$key]); }else{ $result[$key]['weight'] = Db::name('matter')->where('code',$value['code'])->value('weight'); if ($result[$key]['weight'] == null){ $result[$key]['weight'] = 0; } } } $result = array_merge($result); if ($result){ return array('status'=>1,'msg'=>'请求成功','result'=>$result,'name'=>$productName); }else{ return array('status'=>0,'msg'=>'请求失败'); } } }