request->filter(['strip_tags']); if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $search = input('search'); $option = array(); if (!empty($search)){ if (strlen($search) == 7){ $option['order_number'] = $search; }else{ $option['bach'] = $search; } } $total = Db::name('formula_book_detail') ->where($option) // ->where($where) // ->where('status',1) ->order($sort, $order) ->count(); // print_r($option);die; $list = Db::name('formula_book_detail') // ->where($where) ->where($option) // ->where('status',1) // ->fetchSql(true) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } //获取基础数据 public function add(){ $params = input('ids'); if (empty($params)){ $this->error('未知数据格式'); } $result = Db::name('formula_book_detail')->where('id',$params)->find(); $formula = json_decode($result['warehousing'],true); //获取专色调配数据 $warehousingData = []; foreach ($formula as $k=>$v){ if (count($v) == 6){ $warehousingData[$k]['color'] = $v[0]; $warehousingData[$k]['need_weight'] = $v[4]; $warehousingData[$k]['has_weight'] = 0; $warehousingData[$k]['book_weight'] = 0; } } $warehousingData = array_values($warehousingData); foreach ($warehousingData as $k1=>$v1){ $cid = Db::name('warehousing')->where('formula',$result['formula'])->where('color',$v1['color'])->find(); if (!empty($cid)){ $weight = Db::name('warehousing_detail')->where('order_number',$result['order_number'])->where('cid',$cid['cid'])->column('book_weight'); $weight = array_sum($weight); // print_r($result['order_number']); // print_r($weight);die; if (!empty($weight)){ $warehousingData[$k1]['book_weight'] = $weight / 1000; } $warehousingData[$k1]['has_weight'] = $cid['weight'] / 1000; }else{ $warehousingData[$k1]['has_weight'] = 0; } } return $this->view->fetch('',compact('result','warehousingData')); } //渲染表格里配方和大小盒计划数据 public function doDetail(){ $params = input('id'); if (empty($params)){ $this->error('未知数据格式'); } $result = Db::name('formula_book_detail')->where('id',$params)->find(); $formula = json_decode($result['warehousing'],true); $consume = json_decode($result['consume'],true); $formulaSpan = json_decode($result['formula_span'],true); $formulaSpanData = []; $i = 0; foreach ($formulaSpan as $key=>$value){ $formulaSpanData[$key]['beginRow'] = $i; $count = $value; $formulaSpanData[$key]['rowSpan'] = $value; $i = $count + $i; } return array('status'=>1,'data'=>$formula,'consume'=>$consume,'formulaSpanData'=>$formulaSpanData,'consumeSpan'=>$result['consume_span']); } //入库 public function warehousing(){ $data = input('data/a'); // print_r($data);die; if (empty($data)){ return array('status'=>0,'msg'=>'数据错误,请联系管理员'); } foreach ($data as $key=>$params){ if ($params['weight'] == 0 || $params['weight'] == ''){ continue; }else{ //产品编号 $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params['order_number']}' order by Uniqid asc limit 1"); if (empty($product)){ return array('status'=>0,'msg'=>'数据错误,请联系管理员'); } $product_number = rtrim($product[0]['Gd_cpdh']); // print_r($product_number);die; $params['product_number'] = $product_number; //时间取6位 $time = substr(date('Ymd'),-6); $user = Session::get('admin'); $name = $user['nickname']; //人员编号,根据缓存的nickname确认人员编号 $people_number = Db::name('people')->where('name',$name)->value('number'); //色号,根据产品编码和颜色 确定它的色号 $params['color'] = trim($params['color']);//去掉空格 $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")]; $colorInfo = Db::name('formula')->where($where)->where('color',$params['color'])->find(); // print_r($colorInfo);die; if(!empty($colorInfo)){ $params['cid'] = $colorInfo['id']; $color_number = $colorInfo['color_number']; }else{ return array('status'=>0,'msg'=>'未查到配方数据'); } // print_r($params);die; //查这个色号当日第几次调配 $num = Db::name('warehousing_detail')->where('product_number',$product_number)->where('cid',$params['cid'])->whereTime('create','today')->count(); if (empty($num)){ $colorNum = 1; }else{ $colorNum = $num + 1; } //专色墨批次号 $params['bach_number'] = $product_number.$time.$color_number.$people_number.'-'.$colorNum; $params['weight'] = $params['weight'] * 1000; $params['book_weight'] = $params['weight']; $params['create'] = date('Y-m-d H:i:s'); /* 专色墨数据整合完毕,下面整理分解原墨数据 根据产品编号,配方颜色获得配方原墨 获取配方总量,算出占比,再根据入库的专色墨重量相乘,得到各原墨的消耗重量,扣减库存 */ $matterTotal = Db::name('formula_detail')->where('product_name',$params['formula'])->where('color',$params['color'])->sum('ink_number'); $formula = Db::name('formula_detail')->where('product_name',$params['formula'])->where('color',$params['color'])->select(); foreach ($formula as $k=>$v){ $matterMix = ceil((intval($v['ink_number']) / $matterTotal) * $params['weight']);//本次调墨消耗重量(向上取整) $matterDetail = Db::name('matter_detail')->where('code',$v['code'])->where('weight','>',0)->order('id desc')->limit(1)->select();//按照最新调拨单 扣减原墨库存 if(empty($matterDetail)){ $matter = array(); $matter['code'] = $v['code']; $matter['name'] = $v['ink']; $matter['weight'] = -$matterMix; $matter['create'] = date('Y-m-d H:i:s'); $isExit = Db::name('matter_detail')->where('code',$v['code'])->find(); if ($isExit){ $weight = $isExit['weight'] + $matter['weight']; Db::name('matter_detail')->where('code',$v['code'])->setField('weight',$weight); Db::name('matter')->where('code',$v['code'])->setField('weight',$weight); }else{ Db::name('matter')->insert($matter); Db::name('matter_detail')->insert($matter); } }else{ $matterDeductions = $matterDetail[0]; $matterDetailWeight = $matterDeductions['weight'] - $matterMix;//详细表库存-本次调墨消耗 $matterWeight = Db::name('matter')->where('code',$v['code'])->value('weight'); $newMatterWeight = $matterWeight - $matterMix;//总表库存重量 Db::name('matter_detail')->where('id',$matterDeductions['id'])->setField('weight',$matterDetailWeight); Db::name('matter')->where('code',$v['code'])->setField('weight',$newMatterWeight); } $log = array(); $log['code'] = $v['code']; $log['people'] = $name; $log['content'] = '扣减了存货编码为'.$v['code'].'的油墨'.$matterMix.'g库存'; $log['weight'] = $matterMix; $log['create'] = date('Y-m-d H:i:s'); Db::name('matter_log')->insert($log); } //专色墨入库 Db::startTrans(); try{ $id = Db::name('warehousing_detail')->insertGetId($params); if (empty($id)){ throw new Exception('入库失败,请联系开发人员'); } $totalWeight = Db::name('warehousing')->where('cid',$params['cid'])->find(); if (!empty($totalWeight)){ $totalWeight['weight'] = $totalWeight['weight'] + $params['weight']; Db::name('warehousing')->where('formula',$params['formula'])->where('cid',$params['cid'])->setField('weight',$totalWeight['weight']); Db::name('warehousing')->where('formula',$params['formula'])->where('cid',$params['cid'])->setField('update',date('Y-m-d H:i:s')); }else{ $data = array(); $data['weight'] = $params['weight']; $data['product_name'] = $params['product_name']; $data['product_number'] = $params['product_number']; $data['formula'] = $params['formula']; $data['cid'] = $params['cid']; $data['color'] = $params['color']; $data['update'] = date('Y-m-d H:i:s'); Db::name('warehousing')->insert($data); } Db::commit(); $result = 1; }catch (\think\Exception\DbException $exception){ Db::rollback(); $result = -1; } } } if ($result = 1){ $this->success('入库成功'); }else{ $this->error('入库失败'); } } //打印数据 public function getData(){ $params = input('order_number'); if (empty($params)){ return array('status'=>-1,'msg'=>'数据错误'); } $time = date('Y-m-d H:i:s'); $lastTime = date('Y-m-d H:i:s',strtotime($time) - 60);//上一分钟 $result['warehousing'] = Db::name('warehousing_detail') ->where('order_number',$params) ->where('create','between',[$lastTime,$time]) ->field('order_number,product_name,product_number,formula,color,bach_number,weight,create') ->select(); $peopleNumber = strpos($result['warehousing'][0]['bach_number'],'-'); $peopleNumber = substr($result['warehousing'][0]['bach_number'],$peopleNumber-2,2); $result['name'] = Db::name('people')->where('number',$peopleNumber)->value('name'); // $result['formula'] = $result['warehousing'][0]['formula']; $result['order_number'] = $result['warehousing'][0]['order_number']; if (!empty($result)){ return array('status'=>1,'data'=>$result,'warehousing'=>$result['warehousing']); }else{ return array('status'=>-1,'data'=>''); } } }