Task.php 12 KB

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