Feeding.php 8.6 KB

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