Feeding.php 6.6 KB

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