Feeding.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. $tid = Db::name('task')->where('bach',$params['bach'])->order('id desc')->find();
  46. $arr = [];
  47. foreach($params['weight'] as $k=>$v){
  48. if($v){
  49. $param['bach'] = $params['bach'];
  50. $param['date'] = $params['date'];
  51. $param['operator'] = $params['operator'];
  52. $param['inspector'] = $params['inspector'];
  53. $param['weight'] = $params['weight'][$k];
  54. $param['nweight'] = $params['nweight'][$k];
  55. $param['material'] = $params['material'][$k];
  56. $param['gy_num'] = $params['gy_num'][$k];
  57. $param['tid'] = $tid['id'];
  58. $arr[] = $param;
  59. }
  60. }
  61. $result = false;
  62. Db::startTrans();
  63. try {
  64. //是否采用模型验证
  65. if ($this->modelValidate) {
  66. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  67. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  68. $this->model->validateFailException()->validate($validate);
  69. }
  70. $result = $this->model->allowField(true)->saveAll($arr);
  71. Db::commit();
  72. } catch (ValidateException|PDOException|Exception $e) {
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. if ($result === false) {
  77. $this->error(__('No rows were inserted'));
  78. }
  79. $this->success();
  80. }
  81. //获取作业票信息
  82. public function get_task(){
  83. $bach = $this->request->post('bach');
  84. $row = Db::name('feeding')->where('bach',$bach)->select();
  85. $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select();
  86. //已有过工序
  87. if($row){
  88. // Session::set('process',serialize($row));
  89. $result['inspector'] = $row[0]['inspector'];
  90. $result['operator'] = $row[0]['operator'];
  91. }else{
  92. // Session::delete('process');
  93. $result['inspector'] = '';
  94. $result['operator'] = '';
  95. }
  96. $result['data'] = $res;
  97. return json($result);
  98. }
  99. //获取配方信息
  100. public function get_formula(){
  101. $bach = $this->request->post('bach');//批次号
  102. $num = $this->request->post('num');//生产量
  103. // $process = unserialize(Session::get('process'));
  104. // print_r($process);die;
  105. $process = Db::name('feeding')->where('bach',$bach)->select();
  106. if($num){//如果有,批次号重复,需精确查找
  107. $res = Db::name('task')->alias('t')
  108. ->join('formula_detail f','f.pid=t.fid','left')
  109. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id')
  110. ->where('t.bach',$bach)->where('t.number',$num)->select();
  111. }else{//如果没有,,批次号未重复,直接差出数据
  112. $res = Db::name('task')->alias('t')
  113. ->join('formula_detail f','t.fid = f.pid','left')
  114. ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id')
  115. ->where('t.bach',$bach)->select();
  116. }
  117. // print_r($res);die;
  118. $pro = 0;
  119. //按照百分比计算出应投重量
  120. foreach($res as &$v){
  121. if($process){ //已有工序,接上一次工序
  122. foreach ($process as $val){
  123. if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id']){
  124. $v['weight']=$val['weight'];
  125. $pro = $val['gy_num'];
  126. }
  127. }
  128. }
  129. if($v['gy_name'] == null){
  130. $v['gy_name'] = '';
  131. }
  132. if($v['gy_num'] == null){
  133. $v['gy_num'] = '';
  134. }
  135. if($v['material'] == null){
  136. $v['material'] = '';
  137. }
  138. $total = array_column($res,'percentage');
  139. foreach ($total as $key=>$value){
  140. $total[$key] = decode($value);
  141. }
  142. $num = array_sum($total);
  143. if($v['percentage']){
  144. // $v['nweight']=round($v['number']*decode($v['percentage'])/100,2);
  145. $v['nweight'] = number_format(decode($v['percentage']) / $num * $v['number'],3);
  146. }else{
  147. $v['nweight']='';
  148. }
  149. }
  150. $row['total']=$num;
  151. $row['data'] = $res;
  152. $row['process'] = $pro+1;
  153. return json($row);
  154. }
  155. //查找替代料
  156. /*public function replace(){
  157. $bach = $this->request->post('bach');
  158. $wuliao = $this->request->post('wuliao');
  159. $res = Db::name('task')->alias('t')
  160. ->join('formula f','f.id = t.fid','left')
  161. ->join('formula_detail fd','fd.pid = f.id','left')
  162. ->join('formula_replace fr','fr.fid = fd.id','left')
  163. ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
  164. ->field('fd.material,fd.id')->find();
  165. $res['yuan'] = explode('/',$res['material']);
  166. $key = array_search($wuliao, $res['yuan']);
  167. if ($key !== false) array_splice($res['yuan'], $key, 1);
  168. return json($res);
  169. }*/
  170. }