Feeding.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\PDOException;
  7. use think\exception\ValidateException;
  8. use think\Session;
  9. /**
  10. * 生产投料
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Feeding extends Backend
  15. {
  16. /**
  17. * Feeding模型对象
  18. * @var \app\admin\model\Feeding
  19. */
  20. protected $model = null;
  21. protected $noNeedRight = ['get_formula','get_task'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Feeding;
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 添加
  34. *
  35. * @return string
  36. * @throws \think\Exception
  37. */
  38. public function add()
  39. {
  40. if (false === $this->request->isPost()) {
  41. return $this->view->fetch();
  42. }
  43. $params = $this->request->post('row/a');
  44. $params = $this->preExcludeFields($params);
  45. $arr = [];
  46. foreach($params['weight'] as $k=>$v){
  47. if($v){
  48. $param['bach'] = $params['bach'];
  49. $param['date'] = $params['date'];
  50. $param['operator'] = $params['operator'];
  51. $param['inspector'] = $params['inspector'];
  52. $param['weight'] = $params['weight'][$k];
  53. $param['nweight'] = $params['nweight'][$k];
  54. $param['material'] = $params['material'][$k];
  55. $param['gy_num'] = $params['gy_num'][$k];
  56. $arr[] = $param;
  57. }
  58. }
  59. $result = false;
  60. Db::startTrans();
  61. try {
  62. //是否采用模型验证
  63. if ($this->modelValidate) {
  64. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  65. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  66. $this->model->validateFailException()->validate($validate);
  67. }
  68. $result = $this->model->allowField(true)->saveAll($arr);
  69. Db::commit();
  70. } catch (ValidateException|PDOException|Exception $e) {
  71. Db::rollback();
  72. $this->error($e->getMessage());
  73. }
  74. if ($result === false) {
  75. $this->error(__('No rows were inserted'));
  76. }
  77. $this->success();
  78. }
  79. //获取作业票信息
  80. public function get_task(){
  81. $bach = $this->request->post('bach');
  82. $row = Db::name('feeding')->where('bach',$bach)->select();
  83. $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select();
  84. //已有过工序
  85. if($row){
  86. Session::set('process',serialize($row));
  87. $result['inspector'] = $row[0]['inspector'];
  88. $result['operator'] = $row[0]['operator'];
  89. }else{
  90. Session::delete('process');
  91. $result['inspector'] = '';
  92. $result['operator'] = '';
  93. }
  94. $result['data'] = $res;
  95. return json($result);
  96. }
  97. //获取配方信息
  98. public function get_formula(){
  99. $bach = $this->request->post('bach');//批次号
  100. $num = $this->request->post('num');//生产量
  101. $process = unserialize(Session::get('process'));
  102. if($num){//如果有,批次号重复,需精确查找
  103. $res = Db::name('task')->alias('t')
  104. ->join('formula_detail f','f.pid=t.fid','left')
  105. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id')
  106. ->where('t.bach',$bach)->where('t.number',$num)->select();
  107. }else{//如果没有,,批次号未重复,直接差出数据
  108. $res = Db::name('task')->alias('t')
  109. ->join('formula_detail f','t.fid = f.pid','left')
  110. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id')
  111. ->where('t.bach',$bach)->select();
  112. }
  113. $pro = 0;
  114. //按照百分比计算出应投重量
  115. foreach($res as &$v){
  116. if($process){//已有工序,接上一次工序
  117. foreach ($process as $val){
  118. if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['t_id']==$v['id']){
  119. $v['weight']=$val['weight'];
  120. $pro = $val['gy_num'];
  121. }
  122. }
  123. }
  124. if($v['percentage']){
  125. $v['nweight']=round($v['number']*decode($v['percentage'])/100,2);
  126. }else{
  127. $v['nweight']='';
  128. }
  129. }
  130. $row['total']=$num;
  131. $row['data'] = $res;
  132. $row['process'] = $pro+1;
  133. return json($row);
  134. }
  135. //查找替代料
  136. /*public function replace(){
  137. $bach = $this->request->post('bach');
  138. $wuliao = $this->request->post('wuliao');
  139. $res = Db::name('task')->alias('t')
  140. ->join('formula f','f.id = t.fid','left')
  141. ->join('formula_detail fd','fd.pid = f.id','left')
  142. ->join('formula_replace fr','fr.fid = fd.id','left')
  143. ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
  144. ->field('fd.material,fd.id')->find();
  145. $res['yuan'] = explode('/',$res['material']);
  146. $key = array_search($wuliao, $res['yuan']);
  147. if ($key !== false) array_splice($res['yuan'], $key, 1);
  148. return json($res);
  149. }*/
  150. }