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