Feeding.php 7.2 KB

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