Feeding.php 5.5 KB

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