Task.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. protected $searchFields = 'name,bach,machine';
  13. /**
  14. * Task模型对象
  15. * @var \app\admin\model\Task
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Task;
  22. }
  23. /**
  24. * 编辑
  25. *
  26. * @param $ids
  27. * @return string
  28. * @throws DbException
  29. * @throws \think\Exception
  30. */
  31. public function edit($ids = null)
  32. {
  33. $row = $this->model->get($ids);
  34. if (!$row) {
  35. $this->error(__('No Results were found'));
  36. }
  37. $adminIds = $this->getDataLimitAdminIds();
  38. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  39. $this->error(__('You have no permission'));
  40. }
  41. if (false === $this->request->isPost()) {
  42. //新增关联订单
  43. $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select();
  44. $uncompleted_order = array();
  45. if (!empty($order)){
  46. foreach($order as $k=>$v){
  47. $uncompleted_order[$k]['id'] = $v['id'];
  48. if (empty($v['completed'])){
  49. $completed = 0;
  50. }else{
  51. $completed = $v['completed'];
  52. }
  53. $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)';
  54. }
  55. }
  56. $formula_detail['data'] = Db::name('task')->where('id',$ids)->find();
  57. $formula_detail['detail'] = Db::name('formula_detail')->where('pid',$formula_detail['data']['fid'])->select();
  58. foreach ($formula_detail['detail'] as $key=>$value){
  59. $formula_detail['detail'][$key]['percentage'] = decode($value['percentage']);
  60. }
  61. $this->view->assign("formula_detail", $formula_detail);
  62. $this->view->assign('row', $row);
  63. $this->view->assign('order',$uncompleted_order);
  64. $this->view->assign('machineList',\app\admin\model\Machine::select());
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 如果修改关联订单,1、将之前oid的已完成订单数量减掉 2、加上新选择的oid的已完成订单数量
  69. * 3、修改作业票信息
  70. */
  71. $params = $this->request->post('row/a');
  72. // echo "<pre>";
  73. // print_r($params);
  74. // echo "</pre>";
  75. // die;
  76. if (empty($params)) {
  77. $this->error(__('Parameter %s can not be empty', ''));
  78. }
  79. $params = $this->preExcludeFields($params);
  80. $result = false;
  81. Db::startTrans();
  82. try {
  83. if ($row['oid'] != $params['oid']){
  84. //第一步
  85. $res = Db::name('order')->where('id',$row['oid'])->find();
  86. $num = $res['completed'] - $row['number'];
  87. Db::name('order')->where('id',$row['oid'])->setField('completed',$num);
  88. //第二步
  89. $order = Db::name('order')->where('id',$params['oid'])->find();
  90. $o_num = $order['completed'] + $row['number'];
  91. Db::name('order')->where('id',$params['oid'])->setField('completed',$o_num);
  92. }
  93. //第三步
  94. $result = Db::name('task')->where('id',$ids)->update($params);
  95. Db::commit();
  96. } catch (ValidateException|PDOException|Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. if (false === $result) {
  101. $this->error(__('No rows were updated'));
  102. }
  103. $this->success();
  104. }
  105. /**
  106. * 生产管理 作业票列表查看
  107. */
  108. public function show($ids)
  109. {
  110. $row = $this->model->get($ids);
  111. $this->view->assign("row", $row);
  112. $formula_detail['data'] = Db::name('task')->where('id',$ids)->find();
  113. $formula_detail['detail'] = Db::name('formula_detail')->where('pid',$formula_detail['data']['fid'])->select();
  114. foreach ($formula_detail['detail'] as $key=>$value){
  115. $formula_detail['detail'][$key]['percentage'] = decode($value['percentage']);
  116. }
  117. $this->view->assign("formula_detail", $formula_detail);
  118. return $this->view->fetch();
  119. }
  120. /**
  121. * 生成领料单
  122. * @return string
  123. * @throws \think\Exception
  124. */
  125. public function taskadd(){
  126. $ids = input('ids');//获取作业票id
  127. if(empty($ids)){$this->error('异常');}
  128. $ids = substr($ids,0,strlen($ids)-1);
  129. $id = explode(',',$ids);//逗号分割开
  130. $list = [];
  131. foreach ($id as $k=>$v){
  132. $task[$k]= Db::name('task')->where('id',$v)->find();
  133. $list[$k] = Db::name('formula_detail')->where('pid',$task[$k]['fid'])->field('material,percentage')->select();
  134. }
  135. /*
  136. * 根据查到的配方fid去查配方详情表 fid = pid
  137. * 解密百分比字段
  138. * 根据task['number'] 算出应该领出的数量 应领重量 = 材料百分比/(所有材料百分比相加)*task['number']
  139. * 值相加去重
  140. */
  141. $res = array();
  142. foreach ($list as $key=>$value){
  143. //查出百分比 相加得出总计百分比
  144. $total = Db::name('formula_detail')->where('pid',$task[$key]['fid'])->column('percentage');
  145. foreach ($total as $k=>$v){
  146. $total[$k] = decode($v);
  147. }
  148. $num = array_sum($total);//算出所有百分比总数
  149. for ($i=0;$i<count($value);$i++){
  150. if (!empty($value[$i]['percentage'])) {//判断有没有材料和百分比
  151. $list[$key][$i]['percentage'] = decode($value[$i]['percentage']);
  152. //计算应加入的重量
  153. $number = ceil($list[$key][$i]['percentage'] / $num * $task[$key]['number'] * 1000);
  154. $list[$key][$i]['num'] = number_format($number/1000,3);
  155. $list[$key][$i]['machine'] = $task[$key]['machine'];
  156. $list[$key][$i]['str'] = '';//为汇总数组中机台做准备字段
  157. array_push($res,$list[$key][$i]);//将数组push到新的需要整合的数组中
  158. }else{
  159. unset($list[$key][$i]);
  160. }
  161. }
  162. $list[$key] = array_values($list[$key]);
  163. }
  164. $result = $this->sumWeight($res,'material','num');
  165. foreach ($list as $k=>$v){
  166. $sum_list = $this->sumWeight($v,'material','num');
  167. for ($i=0;$i<count($result);$i++){//循环汇总表
  168. for ($j=0;$j<count($sum_list);$j++){
  169. if ($result[$i]['material'] == $sum_list[$j]['material']){
  170. if ( $result[$i]['str'] != $sum_list[$j]['machine']){
  171. $result[$i]['str'] .= "<div>".$sum_list[$j]['machine'].":".$sum_list[$j]['num']."</div>";
  172. }
  173. }
  174. }
  175. }
  176. }
  177. // echo "<pre>";
  178. // print_r($task);//一维
  179. // echo "</pre>";
  180. //
  181. // echo "<pre>";
  182. // print_r($sum_list);//原材料信息
  183. // echo "</pre>";
  184. // echo "<pre>";
  185. // print_r($result);//汇总领料单
  186. // echo "</pre>";
  187. $this->view->assign("task", $task);//生成领料单配方信息
  188. $this->view->assign("list", $list);//生成领料单
  189. $this->view->assign("result", $result);//汇总领料单
  190. return $this->view->fetch('taskadd');
  191. }
  192. /**
  193. * 计算数量,值去重&相加
  194. * $arr 数组
  195. * $str1 要合并的值
  196. * $str2 要计算的值
  197. */
  198. function sumWeight($arr,$str1,$str2) {
  199. $item = array();
  200. foreach ($arr as $k => $v) {
  201. if (!isset($item[$v[$str1]])) {
  202. $item[$v[$str1]] = $v;
  203. } else {
  204. $item[$v[$str1]][$str2] += $v[$str2];
  205. }
  206. // if ($k == 13) {
  207. // echo "<pre>";
  208. // print_r($item);die;
  209. // echo "</pre>";
  210. // }
  211. }
  212. return array_values($item);
  213. }
  214. /**
  215. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  216. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  217. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  218. */
  219. }