model = new \app\admin\model\Formula; $this->view->assign("examineStatusList", $this->model->getExamineStatusList()); $this->view->assign("statusList", $this->model->getStatusList()); // $this->view->assign("gyNameList", \app\admin\model\GyName::select()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 * * @return string|Json * @throws \think\Exception * @throws DbException */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } // 获取搜索框的值 $map = []; $filter=input('filter'); if($filter){ $filter=urldecode($filter); $filter=json_decode($filter,TRUE); foreach($filter as $k=>$v){ $map[$k]=['like',"%{$v}%"]; } } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $user_info = Session::get('admin'); if ($user_info['id'] !== 1){//管理员 $map['company_id'] = $user_info['company_id']; $map['examine_status'] = 2; } if(!empty($map['name'])||!empty($map['material'])){ $sort = 'f.id'; $list = DB::name('formula') ->where($where) ->where($map) ->alias('f') ->group('f.id') ->field('f.id,f.name,f.version,f.examine_status,f.create') ->join('formula_detail fd','fd.pid=f.id','left') ->order($sort, $order) ->paginate($limit); }else{ $list = $this->model ->where($where) ->where($map) ->order($sort, $order) ->paginate($limit); } $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 添加 * * @return string * @throws \think\Exception */ public function add() { if (false === $this->request->isPost()) { //技术部=>担当人 $jsdd = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jsdd")->order('name desc')->select(); //技术部=>审核人 $jssh = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jssh")->order('name desc')->select(); $this->assign('jsdd',$jsdd); $this->assign('jssh',$jssh); return $this->view->fetch(); } $base = $this->request->post('baseData/a'); $formula = $this->request->post('formulaData/a'); if (empty($base) || empty($formula)){ $this->error('数据不能为空'); } $params = []; $params['name'] = $base[0]; $params['no'] = $base[1]; $params['charge_name'] = $base[2]; $params['examine_name'] = $base[3]; $params['remark'] = $base[4]; $params['version'] = $base[5]; $params['date'] = $base[6]; $params['model'] = $base[8]; $params['controlled_one'] = $base[9]; $params['controlled_two'] = $base[10]; if ($base[7] !== 99){ $customer= explode(',',$base[7]); // print_r($base[7]);die; $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id'); $usability = implode(',',$customer_id); } // $order = Db::name('order')->order('desc id')->find(); $params['usability'] = $usability; $params['create'] = date('Y-m-d H:i:s'); $user_info = Session::get('admin'); $params['user_id'] = $user_info['id']; $params['company_id'] = $user_info['company_id']; $result = false; Db::startTrans(); try { $pid = Db::name('formula')->insertGetId($params); if (!$pid){ Db::rollback(); $this->error('数据未插入,请重新操作'); } $data = []; $is_replace = 0; // $gy_num = 1; foreach($formula as $key=>$value){ if (strpos($value[0],'/') === false){ $data[$key]['is_replace'] = 0; }else{ $data[$key]['is_replace'] = 1; $is_replace = 1; } $data[$key]['material'] = $value[0]; $data[$key]['percentage'] = encrypt($value[1]); $data[$key]['gy_name'] = $value[2]; // $data[$key]['gy_num'] = $gy_num; $data[$key]['gy_num'] = $value[3]; $data[$key]['pid'] = $pid; $data[$key]['version'] = $params['version']; $data[$key]['create'] = $params['create']; // if(!$value[0]&&!$value[1]&&$value[2]){ // $gy_num++; // } } $result = Db::name('formula_detail')->insertAll($data); //有替代料的话 查出所有替代料 然后分解 插入到数据库 2022年9月19日14:20:37 替代料功能改变 // if ($is_replace == 1){ // $replace = Db::name('formula_detail')->where('pid',$pid)->where('is_replace',1)->field('id,material')->select(); // $replaceData = []; // $j = 0; // foreach ($replace as $k=>$v){ // $material = explode('/',$v['material']); // for ($i=0;$iinsertAll($replaceData); // } Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } public function kuodan_data(){ if( $this->request->isPost()){ $ids = $this->request->post('ids/a'); print_r($ids); exit; $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num,kuodan')->select(); } } /** * 编辑 * * @param $ids * @return string * @throws DbException * @throws \think\Exception */ public function edit($ids = null) { if (!$ids) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { $ids = input('ids'); $formula = Db::name('formula')->field('charge_name,examine_name')->where('id',"=",$ids)->find(); //查询公司 $user_info = Session::get('admin'); $map = []; if ($user_info['id'] !== 1){ $map['company_id'] = $user_info['company_id']; } $customer_name = Db::name('customer')->field('id,customer_name')->where($map)->find(); $this->assign('customer_name',$customer_name); $customer = Db::name('customer')->field('customer_name')->select(); $this->assign('customer',$customer); //查询 经办 审核 $jsdd = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jsdd")->order('name desc')->select(); $jssh = Db::name('personnel')->where('bid',"=",2)->where('position','=',"jssh")->order('name desc')->select(); $personnel_jsdd = Db::name('personnel')->where('name','=',$formula['charge_name'])->find(); $personnel_jssh = Db::name('personnel')->where('name','=',$formula['examine_name'])->find(); $this->assign('jsdd',$jsdd); $this->assign('jssh',$jssh); $this->assign('personnel_jsdd',$personnel_jsdd); $this->assign('personnel_jssh',$personnel_jssh); $list = Db::name('formula')->where('id',$ids)->find(); //可用性,对应客户名称 if ($list['usability'] != 99){ $customer = explode(',',$list['usability']); $name = Db::name('customer')->where('id','in',$customer)->column('customer_name'); } $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select(); foreach ($list['gyinfo'] as $key=>$value){ $list['gyinfo'][$key]['percentage'] = decode($value['percentage']); } // dump($list);die; $this->view->assign('ids',$ids); $this->view->assign('name',$name); $this->view->assign('row', $list); return $this->view->fetch(); } /** * 厂家变更? * 原材料变更? * 百分比变更? ->工艺信息变更生成新配方 * 工艺变更? * $change 0:未更改工艺信息 1:原材料更改 2:百分比更改 3:操作工艺更改 4:增加或减少工艺信息 */ $base = $this->request->post('baseData/a'); $formula = $this->request->post('formulaData/a'); if (empty($base) || empty($formula)){ $this->error('数据不能为空'); } $version = Db::name('formula')->where('id',$ids)->order('id desc')->value('version'); $version = substr($version,1);//获取当前最新版本号,后续看工艺情况是否更改 //基础数据 $params = []; $params['name'] = $base[0]; $params['no'] = $base[1]; $params['charge_name'] = $base[2]; $params['examine_name'] = $base[3]; $params['remark'] = $base[4]; $params['date'] = $base[6]; $params['model'] = $base[8]; //配方对应客户id(可用性) if ($base[7] !== 99){ $customer= explode(',',$base[7]); $customer_id = Db::name('customer')->where('customer_name','in',$customer)->column('id'); $usability = implode(',',$customer_id); } $params['usability'] = $usability; $params['create'] = date('Y-m-d H:i:s'); $user_info = Session::get('admin'); $params['user_id'] = $user_info['id']; $params['company_id'] = $user_info['company_id']; $params['update'] = date('Y-m-d H:i:s'); //数据库的工艺数据 $gy_data = Db::name('formula_detail')->where('pid',$ids)->field('id,material,percentage,gy_name')->order('id asc')->select(); //提交的工艺数据 $data = []; $change = 0; $materialChange = 0; //原材料变更 $percentageChange = 0; //百分比变更 $gy_nameChange = 0; //工艺信息变更 //总工艺已更改,版本号X位直接增加1 if (count($gy_data) != count($formula)){ $change = 1; } foreach($formula as $key=>$value){ foreach ($gy_data as $k=>$v){ //比对同一行原材料 百分比 工艺信息 if ($key == $k){ if ($value[0] != $gy_data[$k]['material']){ $materialChange = 1; } if ($value[1] != $gy_data[$k]['percentage']){ $percentageChange = 1; } if($value[2] != $gy_data[$k]['gy_name']){ $gy_nameChange = 1; } } } } //更改版本号 if ($change === 1){ $version = intval($version) + 1; }else{ if ($materialChange === 1){ $version = intval($version) + 1; }else{ if ($percentageChange === 1 || $gy_nameChange === 1){ $version = $version + 0.1; } } } if (is_int($version)){ $version = $version.'.0'; } //根据版本号的变化来判断工艺有没有变化 if ($version == substr($base[5],1)){ $isChange = false; }else{ $isChange = true; } $params['version'] = 'v'.$version; $result = true; Db::startTrans(); try { if ($isChange === true){ $ids = Db::name('formula')->insertGetId($params); }else{ Db::name('formula')->where('id',$ids)->update($params); } //新工艺信息 $is_replace = 0; for($i=0;$iinsertAll($data); } //有替代料的话 查出所有替代料 然后分解 插入到数据库 //2022年9月19日14:19:09 替代料更改 // if ($is_replace == 1 && ($materialChange == 1 || $change == 1)){ // $replace = Db::name('formula_detail')->where('pid',$ids)->where('is_replace',1)->field('id,material')->select(); // $replaceData = []; // $j = 0; // foreach ($replace as $k=>$v){ // $material = explode('/',$v['material']); // for ($i=0;$iinsertAll($replaceData); // } Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } //生成作业单 public function task(){ // $ceshi = Db::name('formula_detail')->where('id','>',52459)->limit(10000)->select(); // foreach ($ceshi as $key=>$value){ // $ceshi[$key]['percentage'] = encrypt($value['percentage']); // } // Db::name('formula_detail_c')->insertAll($ceshi); // print_r('-------------------------------'); // die; //gyinfo $ids = input('ids'); if (!$ids) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { //生产部=>开票人 $sckp = Db::name('personnel')->where('bid',"=",3)->where('position','=',"sckp")->order('name desc')->select(); //生产部=>审核人 $scsh = Db::name('personnel')->where('bid',"=",3)->where('position','=',"scsh")->order('name desc')->select(); $this->assign('sckp',$sckp); $this->assign('scsh',$scsh); //批次号顺序新增 $tastbach = Db::name('task')->order('bach desc')->find(); $bach = $tastbach['bach'] +1; $this->view->assign('bach',$bach); $list = Db::name('formula')->where('id',$ids)->find(); //查询该配方是否生产过 $taskbach = Db::name('task')->where('fid',$list['id'])->order('create desc')->find(); $count = Db::name('task')->where('fid',$list['id'])->count(); if($taskbach){ $erro = '该配方在于最近 '.$taskbach['create'].' 已生产过'.$count.'次'; }else{ $erro = '该配方未生产过'; } $this->view->assign('erro',$erro); $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num,kuodan')->select(); foreach ($list['gyinfo'] as $key=>$value){ $list['gyinfo'][$key]['percentage'] = decode($value['percentage']); } //新增关联订单 $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select(); $uncompleted_order = array(); if (!empty($order)){ foreach($order as $k=>$v){ $uncompleted_order[$k]['id'] = $v['id']; if (empty($v['completed'])){ $completed = 0; }else{ $completed = $v['completed']; } $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)'; } } $this->view->assign('ids',$ids); $this->view->assign('order',$uncompleted_order); $this->view->assign('machineList',\app\admin\model\Machine::select()); $this->view->assign('row', $list); return $this->view->fetch(); } $base = $this->request->post('baseData/a'); // print_r($base);die; if (empty($base)){ $this->error('数据不能为空'); } //基础数据 $params = []; $params['fid'] = $base[0]; $params['name'] = $base[1]; $params['bach'] = $base[2]; $params['drawer_name'] = $base[3]; $params['examine_name'] = $base[4]; $params['number'] = $base[5]; $params['remark'] = $base[6]; $params['machine'] = $base[7]; $params['oid'] = $base[8]; if(!empty($base[9])) if($base[9]){ $params['kuodan']=$base[9]; } // print_r($list2['gyinfo']); // exit; //percentage //$params['kuodan'] = ($base[9]+$params['number'])*; $params['create'] = date('Y-m-d H:i:s'); $result = false; Db::startTrans(); try { // Db::name('order')->where('id',$params['oid'])->setField('status',2); $result = Db::name('task')->insert($params); //更改订单已完成数量,修改订单状态 $order_info = Db::name('order')->where('id',$params['oid'])->find(); if (empty($order_info['completed'])){ $new_completed = $params['number']; $order_status = 2;//生产中 Db::name('order')->where('id',$params['oid'])->setField('status',$order_status); }else{ $new_completed = $params['number'] + $order_info['completed']; } Db::name('order')->where('id',$params['oid'])->setField('completed',$new_completed); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } //配方审核列表 public function examine(){ //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $user_info = Session::get('admin'); $map = []; $map['examine_status'] = 1; if ($user_info['id'] !== 1){ $map['company_id'] = $user_info['company_id']; } $list = $this->model ->where($where) ->where($map) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } //审核操作 public function status($ids=null){ if (!$ids) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { $list = Db::name('formula')->where('id',$ids)->find(); //可用性,对应客户名称 if ($list['usability'] != 99){ $customer = explode(',',$list['usability']); $name = Db::name('customer')->where('id','in',$customer)->column('customer_name'); } $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('material,percentage,gy_name,gy_num')->select(); foreach ($list['gyinfo'] as $key=>$value){ $list['gyinfo'][$key]['percentage'] = decode($value['percentage']); } $this->view->assign('ids',$ids); $this->view->assign('name',$name); $this->view->assign('row', $list); return $this->view->fetch(); } $status = $this->request->post('status'); if (!isset($status) || !isset($ids)){ $this->error('审核失败'); } $params = []; $user_info = Session::get('admin'); $params['examine_userid'] = $user_info['id'];//审核用户id $params['examine_status'] = $status;//审核状态 $params['update'] = date('Y-m-d H:i:s'); $result = false; Db::startTrans(); try { $list = Db::name('formula')->where('id',$ids)->find(); if ($list['version'] == 'v0.1'){ Db::name('formula')->where('id',$ids)->setField('version','v1.0'); Db::name('formula_detail')->where('pid',$ids)->setField('version','v1.0'); } $result = Db::name('formula')->where('id',$ids)->update($params); Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result){ $this->addFormulaChinese($ids); $this->success('更新成功'); }else{ $this->error('审核失败'); } } //审核记录列表 public function examine_status(){ $user_info = Session::get('admin'); $formula = Db::name('formula')->where('examine_userid',$user_info['id'])->order('update desc') ->paginate(10,false); $page = $formula->render(); $this->view->assign('page',$page); $this->view->assign('formula',$formula); return $this->view->fetch(); } //审核记录详情列表 public function examine_list(){ $get = $this->request->get(); $formula = Db::name('formula')->where('id',$get['id'])->find(); $formula_detail = Db::name('formula_detail')->where('pid',$get['id'])->select(); foreach ($formula_detail as $key=>$value){ $formula_detail[$key]['percentage'] = decode($value['percentage']); } $this->view->assign('formula',$formula); $this->view->assign('formula_detail',$formula_detail); return $this->view->fetch(); } //下拉选择获取客户列表 public function getCustomer(){ $user_info = Session::get('admin'); $where = []; if ($user_info['id'] !== 1){ $where['company_id'] =$user_info['company_id']; } $row = Db::name('customer')->where($where)->field('id,customer_name')->select(); $result = ['total' => count($row), 'rows' => $row]; return json($result); } //获取生产单应加量 public function getNumber(){ $params = input(''); if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){ return array('status'=>0,'msg'=>'请求参数错误'); } $list = Db::name('formula')->where('id',$params['ids'])->find(); $gyinfo= Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->field('id,material,percentage,gy_name,gy_num')->select(); if (empty($gyinfo)){ return array('status'=>0,'msg'=>'数据错误'); } $total = Db::name('formula_detail')->where('pid',$params['ids'])->where('version',$list['version'])->column('percentage'); if (empty($total)){ return array('status'=>0,'msg'=>'数据错误'); } foreach ($total as $k=>$v){ $total[$k] = decode($v); } $num = array_sum($total); foreach ($gyinfo as $key=>$value){ if($gyinfo[$key]['gy_name'] == null){// $gyinfo[$key]['gy_name'] = '';// }// $gyinfo[$key]['num'] = ''; $gyinfo[$key]['percentage'] = decode($value['percentage']); if (!empty($value['percentage'])){ // $gyinfo[$key]['num'] = number_format($gyinfo[$key]['percentage'] / $num * $params['number'],3); $number = ceil($gyinfo[$key]['percentage'] / $num * $params['number'] *1000); $gyinfo[$key]['num'] = number_format($number/1000,3); } } $date = date('Y/m/d'); return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date); } //获取工艺说明 public function gyName(){ $params = input('gy_name'); // print_r($params);die; if ($params){ $list = Db::name('formula_detail')->where('gy_name','like','%'.$params.'%')->field('id,gy_name')->limit(20)->select(); }else{ $list = Db::name('formula_detail')->where('gy_name','neq','')->field('id,gy_name')->limit(20)->select(); } $total = count($list); $result = ['total'=>$total,'rows'=>$list]; // return array('status'=>1,'rows'=>$list); return json($result); } public function addFormulaChinese($ids = null){ $date = date('Ymd',time()); if (!$ids){ Log::mylog('formula_log','id='.$ids.'未获取到',$date.'_01'); } $formula = Db::name('formula')->where('id',$ids)->field('name')->find(); if (!$formula){ Log::mylog('formula_log','id='.$ids.'未获取到配方数据',$date.'_01'); } $material = Db::name('formula_detail')->where('pid',$ids)->field('id,material')->select(); if (!$material){ Log::mylog('formula_log','pid='.$ids.'未获取到配方详情数据',$date.'_01'); } $list = array(); $pinyin = new pinyin(); foreach ($material as $key=>$value){ if (preg_match("/[\x7f-\xff]/", $value['material'])){ $str = $value['material']; $newStr = $pinyin->getpy($str,true,true); $chinese = array(); for ($i=0;$iwhere('f_id',$ids)->select(); if ($is_has){ Db::startTrans(); try { Db::name('formula_material')->where('f_id',$ids)->delete(); Db::name('formula_material')->insertAll($list); Db::commit(); }catch (Exception $e){ $result = false; Db::rollback(); } }else{ $res = Db::name('formula_material')->insertAll($list); if (!$res){ $result = false; } } if (!$result){ Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入失败',$date.'_01'); }else{ Log::mylog('formula_log','id='.$ids.'配方材料转换数据插入成功',$date.'_01'); } } } }