Product.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Exception;
  7. /**
  8. * 生产管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Product extends Backend
  13. {
  14. protected $searchFields = 'pname,batch';
  15. /**
  16. *
  17. *
  18. *
  19. *
  20. *
  21. * Product模型对象
  22. * @var \app\admin\model\Product
  23. */
  24. protected $model = null;
  25. protected $detail = null;
  26. protected $noNeedRight = ['ajax','get_formula'];
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\admin\model\Product;
  31. $this->detail = new \app\admin\model\ProductDetail;
  32. $this->view->assign("statusList", $this->model->getStatusList());
  33. }
  34. /**
  35. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  36. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  37. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  38. */
  39. /**
  40. * 查看
  41. */
  42. public function index()
  43. {
  44. //当前是否为关联查询
  45. $this->relationSearch = false;
  46. //设置过滤方法
  47. $this->request->filter(['strip_tags', 'trim']);
  48. if ($this->request->isAjax()) {
  49. //如果发送的来源是Selectpage,则转发到Selectpage
  50. if ($this->request->request('keyField')) {
  51. return $this->selectpage();
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $list = $this->model
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->paginate($limit);
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. //获取生产批次号等相关信息
  64. public function ajax(){
  65. $bach = $this->request->post('bach');
  66. $str = explode(';',$bach);
  67. $batch = $str[(count($str))-1];
  68. $bach_info = Db::name('task')->where('bach',$batch)->find();
  69. $form = Db::name('formula')->find($bach_info['fid']);
  70. $res = Db::name('formula_detail')->where('pid',$bach_info['fid'])->where('version',$form['version'])->field('id,pid,material,version,is_replace,gy_name')->select();
  71. foreach($res as &$v){
  72. // if($v['gy_name'] == null){
  73. // $v['gy_name'] = '';
  74. // }
  75. if($v['is_replace']==1){
  76. $v['replace'] = Db::name('formula_replace')->where('fid',$v['id'])->order('id','desc')->select();
  77. }else{
  78. $v['replace'] = [];
  79. }
  80. }
  81. return json($bach_info);
  82. }
  83. //获取配方单信息
  84. public function get_formula(){
  85. $bach = $this->request->post('bach');
  86. $str = substr($bach,0,-1);
  87. $str = explode(';',$str);
  88. $batch = $str[(count($str))-1];
  89. $bach_info = Db::name('task')->where('bach',$batch)->find();
  90. $formula = $this->request->post('formula');//配方名称
  91. $arr = [];
  92. $arr['material']=$formula;
  93. //查询数据库↓
  94. $form = Db::name('formula')->find($bach_info['fid']);
  95. $res = Db::name('formula_detail')->where('pid = '.$bach_info['fid'].' and material like "%'.$formula.'%" and version = "'.$form['version'].'"')->select();
  96. $arr = [];
  97. if(!$res){
  98. $arr['material'] = $formula;
  99. $arr['error']=1;
  100. }else{
  101. foreach($res as &$v){
  102. if($v['is_replace']==1){
  103. $v['material'] = Db::name('formula_replace')->where('fid',$v['id'])->where('material',encrypt($formula))->order('id','desc')->value('material');
  104. }
  105. $arr=$v;
  106. $arr['error']=0;
  107. }
  108. }
  109. $arr['pname'] = $bach_info['name'];//配方名称
  110. $arr['time'] = date("Y-m-d H:i:s");
  111. $arr['bach'] = $bach_info['bach'];
  112. return json($arr);
  113. }
  114. public function add()
  115. {
  116. if (false === $this->request->isPost()) {
  117. return $this->view->fetch();
  118. }
  119. $params = $this->request->post('row/a');
  120. if (empty($params)) {
  121. $this->error(__('Parameter %s can not be empty', ''));
  122. }
  123. Db::startTrans();
  124. try {
  125. $pro = [];
  126. foreach(explode(';',$params['bach']) as $key=> $val){
  127. if(!empty($val)){
  128. $res = [];
  129. $res['time'] = date("Y-m-d H:i:s");
  130. $res['audit'] = $params['audit'][$key];
  131. $res['status'] = $params['status'];
  132. $res['pickor'] = $params['pickor'];
  133. $res['note'] = $params['note'];
  134. $res['warehouseor'] = $params['warehouseor'];
  135. $res['batch'] = $val;
  136. $res['pname'] = $params['proname'][$key];
  137. $res['specifications'] = $params['specifications'][$key];
  138. $res['unit'] = $params['unit'][$key];
  139. $pro[] = $res;
  140. }
  141. }
  142. foreach($pro as $v){
  143. $this->model->insert($v);
  144. $id = $this->model->getLastInsID();
  145. $arr = [];
  146. $prodet = [];
  147. foreach($params['material'] as $k=>$vv){
  148. if($v['batch']==$params['batch'][$k]){
  149. $arr['weight'] = $params['weight'][$k];
  150. $arr['pid'] = /*1;//*/$id;
  151. $arr['material'] = $params['material'][$k];
  152. $arr['createtime'] = date("Y-m-d H:i:s");
  153. $prodet[] = $arr;
  154. }
  155. }
  156. $this->detail->saveAll($prodet);
  157. }
  158. Db::commit();
  159. }catch(Exception $e){
  160. Db::rollback();
  161. $this->error($e->getMessage());
  162. }
  163. $this->success();
  164. return $this->view->fetch();
  165. }
  166. public function show($ids){
  167. $row = $this->model->get($ids);
  168. $row['data'] =Db::query("select *,sum(weight) as sum from mn_product_detail where pid= {$ids} group by material");
  169. $status = Db::name('product')->where('id',$ids)->value('status');
  170. $this->view->assign("row", $row);
  171. $this->view->assign("status", $status);
  172. return $this->view->fetch();
  173. }
  174. }