| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Cache;
- use think\Db;
- use think\Exception;
- /**
- * 生产管理
- *
- * @icon fa fa-circle-o
- */
- class Product extends Backend
- {
- protected $searchFields = 'pname,batch';
- /**
- *
- *
- *
- *
- *
- * Product模型对象
- * @var \app\admin\model\Product
- */
- protected $model = null;
- protected $detail = null;
- protected $noNeedRight = ['ajax','get_formula'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Product;
- $this->detail = new \app\admin\model\ProductDetail;
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- //获取生产批次号等相关信息
- public function ajax(){
- $bach = $this->request->post('bach');
- $str = explode(';',$bach);
- $batch = $str[(count($str))-1];
- $bach_info = Db::name('task')->where('bach',$batch)->find();
- $form = Db::name('formula')->find($bach_info['fid']);
- $res = Db::name('formula_detail')->where('pid',$bach_info['fid'])->where('version',$form['version'])->field('id,pid,material,version,is_replace,gy_name')->select();
- foreach($res as &$v){
- // if($v['gy_name'] == null){
- // $v['gy_name'] = '';
- // }
- if($v['is_replace']==1){
- $v['replace'] = Db::name('formula_replace')->where('fid',$v['id'])->order('id','desc')->select();
- }else{
- $v['replace'] = [];
- }
- }
- return json($bach_info);
- }
- //获取配方单信息
- public function get_formula(){
- $bach = $this->request->post('bach');
- $str = substr($bach,0,-1);
- $str = explode(';',$str);
- $batch = $str[(count($str))-1];
- $bach_info = Db::name('task')->where('bach',$batch)->find();
- $formula = $this->request->post('formula');//配方名称
- $arr = [];
- $arr['material']=$formula;
- //查询数据库↓
- $form = Db::name('formula')->find($bach_info['fid']);
- $res = Db::name('formula_detail')->where('pid = '.$bach_info['fid'].' and material like "%'.$formula.'%" and version = "'.$form['version'].'"')->select();
- $arr = [];
- if(!$res){
- $arr['material'] = $formula;
- $arr['error']=1;
- }else{
- foreach($res as &$v){
- if($v['is_replace']==1){
- $v['material'] = Db::name('formula_replace')->where('fid',$v['id'])->where('material',encrypt($formula))->order('id','desc')->value('material');
- }
- $arr=$v;
- $arr['error']=0;
- }
- }
- $arr['pname'] = $bach_info['name'];//配方名称
- $arr['time'] = date("Y-m-d H:i:s");
- $arr['bach'] = $bach_info['bach'];
- return json($arr);
- }
- public function add()
- {
- if (false === $this->request->isPost()) {
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- Db::startTrans();
- try {
- $pro = [];
- foreach(explode(';',$params['bach']) as $key=> $val){
- if(!empty($val)){
- $res = [];
- $res['time'] = date("Y-m-d H:i:s");
- $res['audit'] = $params['audit'][$key];
- $res['status'] = $params['status'];
- $res['pickor'] = $params['pickor'];
- $res['note'] = $params['note'];
- $res['warehouseor'] = $params['warehouseor'];
- $res['batch'] = $val;
- $res['pname'] = $params['proname'][$key];
- $res['specifications'] = $params['specifications'][$key];
- $res['unit'] = $params['unit'][$key];
- $pro[] = $res;
- }
- }
- foreach($pro as $v){
- $this->model->insert($v);
- $id = $this->model->getLastInsID();
- $arr = [];
- $prodet = [];
- foreach($params['material'] as $k=>$vv){
- if($v['batch']==$params['batch'][$k]){
- $arr['weight'] = $params['weight'][$k];
- $arr['pid'] = /*1;//*/$id;
- $arr['material'] = $params['material'][$k];
- $arr['createtime'] = date("Y-m-d H:i:s");
- $prodet[] = $arr;
- }
- }
- $this->detail->saveAll($prodet);
- }
- Db::commit();
- }catch(Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success();
- return $this->view->fetch();
- }
- public function show($ids){
- $row = $this->model->get($ids);
- $row['data'] =Db::query("select *,sum(weight) as sum from mn_product_detail where pid= {$ids} group by material");
- $status = Db::name('product')->where('id',$ids)->value('status');
- $this->view->assign("row", $row);
- $this->view->assign("status", $status);
- return $this->view->fetch();
- }
- }
|