Task.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 = 'id,name,bach,machine,fid';
  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. $row = $this->model->get($ids);
  121. $this->view->assign("row", $row);
  122. $this->view->assign("ids", $ids);
  123. $formula_detail['data'] = Db::name('task')->where('id',$ids)->find();
  124. $formula_detail['detail'] = Db::name('formula_detail')->where('pid',$formula_detail['data']['fid'])->select();
  125. foreach ($formula_detail['detail'] as $key=>$value){
  126. $formula_detail['detail'][$key]['percentage'] = decode($value['percentage']);
  127. }
  128. $this->view->assign("formula_detail", $formula_detail);
  129. $task = Db::name('task')->where('id',$ids)->find();
  130. $gyinfo= Db::name('formula_detail')->where('pid',$row['fid'])->select();
  131. if (empty($gyinfo)){return array('status'=>0,'msg'=>'数据错误');}
  132. $total = Db::name('formula_detail')->where('pid',$row['fid'])->column('percentage');
  133. if (empty($total)){return array('status'=>0,'msg'=>'数据错误');}
  134. foreach ($total as $k=>$v){
  135. $total[$k] = decode($v);
  136. }
  137. $num = array_sum($total);
  138. foreach ($gyinfo as $key=>$value){
  139. if($gyinfo[$key]['gy_name'] == null){
  140. $gyinfo[$key]['gy_name'] = '';
  141. }
  142. $gyinfo[$key]['num'] = '';
  143. $gyinfo[$key]['percentage'] = decode($value['percentage']);
  144. if($task['kuodan']){
  145. if (!empty($value['percentage'])){
  146. $kd_sum = $task['number']+$task['kuodan'];
  147. $number = ceil($gyinfo[$key]['percentage'] / $num * $kd_sum *1000);
  148. }
  149. }else{
  150. if (!empty($value['percentage'])){
  151. $number = ceil($gyinfo[$key]['percentage'] / $num * $task['number'] *1000);
  152. }
  153. }
  154. $gyinfo[$key]['num'] = number_format($number / 1000,3);
  155. $gyinfo[$key]['numm'] = $num;
  156. }
  157. $date = date('Y/m/d');
  158. $this->view->assign("task", $task);
  159. $this->view->assign("gyinfo", $gyinfo);
  160. $this->view->assign("date", $date);
  161. return $this->view->fetch();
  162. }
  163. /**
  164. * 生成作业票 查看 打印页面
  165. */
  166. // public function task_print(){
  167. // $params = input('');
  168. // if ($params['ids'] == '' || $params['number'] == '' || !is_numeric($params['number'])){
  169. // return array('status'=>0,'msg'=>'请求参数错误');
  170. // }
  171. // $task = Db::name('task')->where('id',$params['ids'])->find();
  172. // $formula = Db::name('formula')->where('name',$task['name'])->find();
  173. // $formula_detail= Db::name('formula_detail')->where('pid',$formula['id'])->where('version',$formula['version'])->field('id,material,percentage,gy_name,gy_num')->select();
  174. // if (empty($formula_detail)){return array('status'=>0,'msg'=>'数据错误');}else{return $formula_detail;}
  175. // $this->view->assign("formula_detail", $formula_detail);
  176. // }
  177. /**
  178. * 生成领料单
  179. * @return string
  180. * @throws \think\Exception
  181. */
  182. public function taskadd(){
  183. $ids = input('ids');//获取作业票id
  184. if(empty($ids)){$this->error('异常');}
  185. $ids = substr($ids,0,strlen($ids)-1);
  186. $id = explode(',',$ids);//逗号分割开
  187. $list = [];
  188. foreach ($id as $k=>$v){
  189. $task[$k]= Db::name('task')->where('id',$v)->find();
  190. $list[$k] = Db::name('formula_detail')->where('pid',$task[$k]['fid'])->field('material,percentage')->select();
  191. }
  192. /*
  193. * 根据查到的配方fid去查配方详情表 fid = pid
  194. * 解密百分比字段
  195. * 根据task['number'] 算出应该领出的数量 应领重量 = 材料百分比/(所有材料百分比相加)*task['number']
  196. * 值相加去重
  197. */
  198. $res = array();
  199. foreach ($list as $key=>$value){
  200. //查出百分比 相加得出总计百分比
  201. $total = Db::name('formula_detail')->where('pid',$task[$key]['fid'])->column('percentage');
  202. foreach ($total as $k=>$v){
  203. $total[$k] = decode($v);
  204. }
  205. $num = array_sum($total);//算出所有百分比总数
  206. for ($i=0;$i<count($value);$i++){
  207. if (!empty($value[$i]['percentage'])) {//判断有没有材料和百分比
  208. $list[$key][$i]['percentage'] = decode($value[$i]['percentage']);
  209. //计算应加入的重量
  210. $number = ceil($list[$key][$i]['percentage'] / $num * $task[$key]['number'] * 1000);
  211. $list[$key][$i]['num'] = number_format($number/1000,3);
  212. $list[$key][$i]['machine'] = $task[$key]['machine'];
  213. $list[$key][$i]['str'] = '';//为汇总数组中机台做准备字段
  214. array_push($res,$list[$key][$i]);//将数组push到新的需要整合的数组中
  215. }else{
  216. unset($list[$key][$i]);
  217. }
  218. }
  219. $list[$key] = array_values($list[$key]);
  220. }
  221. $result = $this->sumWeight($res,'material','num');
  222. foreach ($list as $k=>$v){
  223. $sum_list = $this->sumWeight($v,'material','num');
  224. for ($i=0;$i<count($result);$i++){//循环汇总表
  225. for ($j=0;$j<count($sum_list);$j++){
  226. if ($result[$i]['material'] == $sum_list[$j]['material']){
  227. if ( $result[$i]['str'] != $sum_list[$j]['machine']){
  228. $result[$i]['str'] .= "<div>".$sum_list[$j]['machine'].":".$sum_list[$j]['num']."</div>";
  229. }
  230. }
  231. }
  232. }
  233. }
  234. // echo "<pre>";
  235. // print_r($task);//一维
  236. // echo "</pre>";
  237. //
  238. // echo "<pre>";
  239. // print_r($sum_list);//原材料信息
  240. // echo "</pre>";
  241. // echo "<pre>";
  242. // print_r($result);//汇总领料单
  243. // echo "</pre>";
  244. $this->view->assign("task", $task);//生成领料单配方信息
  245. $this->view->assign("list", $list);//生成领料单
  246. $this->view->assign("result", $result);//汇总领料单
  247. return $this->view->fetch('taskadd');
  248. }
  249. /**
  250. * 计算数量,值去重&相加
  251. * $arr 数组
  252. * $str1 要合并的值
  253. * $str2 要计算的值
  254. */
  255. function sumWeight($arr,$str1,$str2) {
  256. $item = array();
  257. foreach ($arr as $k => $v) {
  258. if (!isset($item[$v[$str1]])) {
  259. $item[$v[$str1]] = $v;
  260. } else {
  261. $item[$v[$str1]][$str2] += $v[$str2];
  262. }
  263. // if ($k == 13) {
  264. // echo "<pre>";
  265. // print_r($item);die;
  266. // echo "</pre>";
  267. // }
  268. }
  269. return array_values($item);
  270. }
  271. /**
  272. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  273. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  274. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  275. */
  276. }