model = new \app\admin\model\Formula; $this->view->assign("examineStatusList", $this->model->getExamineStatusList()); $this->view->assign("statusList", $this->model->getStatusList()); } /** * 默认生成的控制器所继承的父类中有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(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $user_info = Session::get('admin'); $map = []; if ($user_info['id'] !== 1){ $map['company_id'] = $user_info['company_id']; $map['examine_status'] = 2; } $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()) { return $this->view->fetch(); } // $ceshi = encrypt('111'); // $ceshitwo = decode('eGWIu/HvS5g4CbzdItvkig=='); // print_r($ceshi); // print_r('-------------------------------'); // print_r($ceshitwo);die; $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['formula_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]; 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); } $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; 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'] = $value[3]; $data[$key]['pid'] = $pid; $data[$key]['version'] = $params['version']; $data[$key]['create'] = $params['create']; } $result = Db::name('formula_detail')->insertAll($data); //有替代料的话 查出所有替代料 然后分解 插入到数据库 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(); } /** * 编辑 * * @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()) { $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(); } /** * 厂家变更? * 原材料变更? * 百分比变更? ->工艺信息变更生成新配方 * 工艺变更? * $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('name',$base[0])->order('id desc')->value('version'); $version = substr($version,1);//获取当前最新版本号,后续看工艺情况是否更改 //基础数据 $params = []; $params['name'] = $base[0]; $params['formula_no'] = $base[1]; $params['charge_name'] = $base[2]; $params['examine_name'] = $base[3]; $params['remark'] = $base[4]; $params['date'] = $base[6]; //配方对应客户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); } //有替代料的话 查出所有替代料 然后分解 插入到数据库 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(){ $ids = input('ids'); if (!$ids) { $this->error(__('No Results were found')); } if (false === $this->request->isPost()) { $list = Db::name('formula')->where('id',$ids)->find(); $list['gyinfo'] = Db::name('formula_detail')->where('pid',$ids)->where('version',$list['version'])->field('id,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('row', $list); return $this->view->fetch(); } $base = $this->request->post('baseData/a'); 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['create'] = date('Y-m-d H:i:s'); $result = false; Db::startTrans(); try { $result = Db::name('task')->insert($params); 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]['material'] = 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 = []; $params['examine_status'] = $status; $params['update'] = date('Y-m-d H:i:s'); $res = Db::name('formula')->where('id',$ids)->update($params); if ($res){ $this->success('更新成功'); }else{ $this->error('审核失败'); } } //下拉选择获取客户列表 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')->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){ $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); } } $date = date('Y/m/d'); return array('status'=>1,'data'=>$gyinfo,'formula_no'=>$list['formula_no'],'date'=>$date); } }