Feeding.php 7.6 KB

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