Task.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 作业票管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Task extends Backend
  11. {
  12. /**
  13. * Task模型对象
  14. * @var \app\admin\model\Task
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Task;
  21. }
  22. /**
  23. * 生产管理 作业票列表查看
  24. */
  25. public function show($ids)
  26. {
  27. $row = $this->model->get($ids);
  28. $this->view->assign("row", $row);
  29. $formula_detail['data'] = Db::name('task')->where('id',$ids)->find();
  30. $formula_detail['detail'] = Db::name('formula_detail')->where('pid',$formula_detail['data']['fid'])->select();
  31. foreach ($formula_detail['detail'] as $key=>$value){
  32. $formula_detail['detail'][$key]['percentage'] = decode($value['percentage']);
  33. }
  34. $this->view->assign("formula_detail", $formula_detail);
  35. return $this->view->fetch();
  36. }
  37. /**
  38. * 生成领料单
  39. * @return string
  40. * @throws \think\Exception
  41. */
  42. public function taskadd(){
  43. $ids = input('ids');//获取作业票id
  44. if(empty($ids)){$this->error('异常');}
  45. $ids = substr($ids,0,strlen($ids)-1);
  46. $id = explode(',',$ids);//逗号分割开
  47. $list = [];
  48. foreach ($id as $k=>$v){
  49. $task[$k]= Db::name('task')->where('id',$v)->find();
  50. $list[$k] = Db::name('formula_detail')->where('pid',$task[$k]['fid'])->field('material,percentage')->select();
  51. }
  52. /*
  53. * 根据查到的配方fid去查配方详情表 fid = pid
  54. * 解密百分比字段
  55. * 根据task['number'] 算出应该领出的数量 应领重量 = 材料百分比/(所有材料百分比相加)*task['number']
  56. * 值相加去重
  57. */
  58. $res = array();
  59. foreach ($list as $key=>$value){
  60. //查出百分比 相加得出总计百分比
  61. $total = Db::name('formula_detail')->where('pid',$task[$key]['fid'])->column('percentage');
  62. foreach ($total as $k=>$v){
  63. $total[$k] = decode($v);
  64. }
  65. $num = array_sum($total);//算出所有百分比总数
  66. for ($i=0;$i<count($value);$i++){
  67. if (!empty($value[$i]['percentage'])) {//判断有没有材料和百分比
  68. $list[$key][$i]['percentage'] = decode($value[$i]['percentage']);
  69. //计算应加入的重量
  70. $number = ceil($list[$key][$i]['percentage'] / $num * $task[$key]['number'] * 1000);
  71. $list[$key][$i]['num'] = number_format($number/1000,3);
  72. $list[$key][$i]['machine'] = $task[$key]['machine'];
  73. $list[$key][$i]['str'] = '';//为汇总数组中机台做准备字段
  74. array_push($res,$list[$key][$i]);//将数组push到新的需要整合的数组中
  75. }else{
  76. unset($list[$key][$i]);
  77. }
  78. }
  79. $list[$key] = array_values($list[$key]);
  80. }
  81. $result = $this->sumWeight($res,'material','num');
  82. foreach ($list as $k=>$v){
  83. $sum_list = $this->sumWeight($v,'material','num');
  84. for ($i=0;$i<count($result);$i++){//循环汇总表
  85. for ($j=0;$j<count($sum_list);$j++){
  86. if ($result[$i]['material'] == $sum_list[$j]['material']){
  87. if ( $result[$i]['str'] != $sum_list[$j]['machine']){
  88. $result[$i]['str'] .= "<div>".$sum_list[$j]['machine'].":".$sum_list[$j]['num']."</div>";
  89. }
  90. }
  91. }
  92. }
  93. }
  94. // echo "<pre>";
  95. // print_r($task);//一维
  96. // echo "</pre>";
  97. //
  98. // echo "<pre>";
  99. // print_r($sum_list);//原材料信息
  100. // echo "</pre>";
  101. // echo "<pre>";
  102. // print_r($result);//汇总领料单
  103. // echo "</pre>";
  104. $this->view->assign("task", $task);//生成领料单配方信息
  105. $this->view->assign("list", $list);//生成领料单
  106. $this->view->assign("result", $result);//汇总领料单
  107. return $this->view->fetch('taskadd');
  108. }
  109. /**
  110. * 计算数量,值去重&相加
  111. * $arr 数组
  112. * $str1 要合并的值
  113. * $str2 要计算的值
  114. */
  115. function sumWeight($arr,$str1,$str2) {
  116. $item = array();
  117. foreach ($arr as $k => $v) {
  118. if (!isset($item[$v[$str1]])) {
  119. $item[$v[$str1]] = $v;
  120. } else {
  121. $item[$v[$str1]][$str2] += $v[$str2];
  122. }
  123. // if ($k == 13) {
  124. // echo "<pre>";
  125. // print_r($item);die;
  126. // echo "</pre>";
  127. // }
  128. }
  129. return array_values($item);
  130. }
  131. /**
  132. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  133. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  134. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  135. */
  136. }