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