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