| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Cache;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Session;
- /**
- * 生产投料
- *
- * @icon fa fa-circle-o
- */
- class Feeding extends Backend
- {
- /**
- * Feeding模型对象
- * @var \app\admin\model\Feeding
- */
- protected $model = null;
- protected $noNeedRight = ['get_formula','get_task'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Feeding;
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 添加
- *
- * @return string
- * @throws \think\Exception
- */
- public function add()
- {
- if (false === $this->request->isPost()) {
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- $params = $this->preExcludeFields($params);
- $arr = [];
- foreach($params['weight'] as $k=>$v){
- if($v){
- $param['bach'] = $params['bach'];
- $param['date'] = $params['date'];
- $param['operator'] = $params['operator'];
- $param['inspector'] = $params['inspector'];
- $param['weight'] = $params['weight'][$k];
- $param['nweight'] = $params['nweight'][$k];
- $param['material'] = $params['material'][$k];
- $param['gy_num'] = $params['gy_num'][$k];
- $arr[] = $param;
- }
- }
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException()->validate($validate);
- }
- $result = $this->model->allowField(true)->saveAll($arr);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result === false) {
- $this->error(__('No rows were inserted'));
- }
- $this->success();
- }
- //获取作业票信息
- public function get_task(){
- $bach = $this->request->post('bach');
- $row = Db::name('feeding')->where('bach',$bach)->select();
- $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select();
- //已有过工序
- if($row){
- Session::set('process',serialize($row));
- $result['inspector'] = $row[0]['inspector'];
- $result['operator'] = $row[0]['operator'];
- }else{
- Session::delete('process');
- $result['inspector'] = '';
- $result['operator'] = '';
- }
- $result['data'] = $res;
- return json($result);
- }
- //获取配方信息
- public function get_formula(){
- $bach = $this->request->post('bach');//批次号
- $num = $this->request->post('num');//生产量
- $process = unserialize(Session::get('process'));
- if($num){//如果有,批次号重复,需精确查找
- $res = Db::name('task')->alias('t')
- ->join('formula_detail f','f.pid=t.fid','left')
- ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id')
- ->where('t.bach',$bach)->where('t.number',$num)->select();
- }else{//如果没有,,批次号未重复,直接差出数据
- $res = Db::name('task')->alias('t')
- ->join('formula_detail f','t.fid = f.pid','left')
- ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id')
- ->where('t.bach',$bach)->select();
- }
- $pro = 0;
- //按照百分比计算出应投重量
- foreach($res as &$v){
- if($process){//已有工序,接上一次工序
- foreach ($process as $val){
- if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['t_id']==$v['id']){
- $v['weight']=$val['weight'];
- $pro = $val['gy_num'];
- }
- }
- }
- if($v['percentage']){
- $v['nweight']=round($v['number']*decode($v['percentage'])/100,2);
- }else{
- $v['nweight']='';
- }
- }
- $row['total']=$num;
- $row['data'] = $res;
- $row['process'] = $pro+1;
- return json($row);
- }
- //查找替代料
- /*public function replace(){
- $bach = $this->request->post('bach');
- $wuliao = $this->request->post('wuliao');
- $res = Db::name('task')->alias('t')
- ->join('formula f','f.id = t.fid','left')
- ->join('formula_detail fd','fd.pid = f.id','left')
- ->join('formula_replace fr','fr.fid = fd.id','left')
- ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
- ->field('fd.material,fd.id')->find();
- $res['yuan'] = explode('/',$res['material']);
- $key = array_search($wuliao, $res['yuan']);
- if ($key !== false) array_splice($res['yuan'], $key, 1);
- return json($res);
- }*/
- }
|