model = new \app\admin\model\Task; } /** * 编辑 * * @param $ids * @return string * @throws DbException * @throws \think\Exception */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { //新增关联订单 $order = Db::name('order')->where('status','neq',3)->field('id,customer,product,number,completed')->select(); $uncompleted_order = array(); if (!empty($order)){ foreach($order as $k=>$v){ $uncompleted_order[$k]['id'] = $v['id']; if (empty($v['completed'])){ $completed = 0; }else{ $completed = $v['completed']; } $uncompleted_order[$k]['str'] = $v['id'].'、'.$v['customer'].'-'.$v['product'].'-'.'总数量('.$v['number'].'kg)'.'-' .'已完成('.$completed.'kg)'; } } $formula_detail['data'] = Db::name('task')->where('id',$ids)->find(); $formula_detail['detail'] = Db::name('formula_detail')->where('pid',$formula_detail['data']['fid'])->select(); foreach ($formula_detail['detail'] as $key=>$value){ $formula_detail['detail'][$key]['percentage'] = decode($value['percentage']); } $this->view->assign("formula_detail", $formula_detail); $this->view->assign('row', $row); $this->view->assign('order',$uncompleted_order); $this->view->assign('machineList',\app\admin\model\Machine::select()); return $this->view->fetch(); } /** * 如果修改关联订单,1、将之前oid的已完成订单数量减掉 2、加上新选择的oid的已完成订单数量 * 3、修改作业票信息 */ $params = $this->request->post('row/a'); // echo "
";
//        print_r($params);
//        echo "
"; // die; if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { if ($row['oid'] != $params['oid']){ //第一步 $res = Db::name('order')->where('id',$row['oid'])->find(); $num = $res['completed'] - $row['number']; Db::name('order')->where('id',$row['oid'])->setField('completed',$num); //第二步 $order = Db::name('order')->where('id',$params['oid'])->find(); $o_num = $order['completed'] + $row['number']; Db::name('order')->where('id',$params['oid'])->setField('completed',$o_num); } //第三步 $result = Db::name('task')->where('id',$ids)->update($params); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } /** * 生产管理 作业票列表查看 */ public function show($ids) { $row = $this->model->get($ids); $this->view->assign("row", $row); $formula_detail['data'] = Db::name('task')->where('id',$ids)->find(); $formula_detail['detail'] = Db::name('formula_detail')->where('pid',$formula_detail['data']['fid'])->select(); foreach ($formula_detail['detail'] as $key=>$value){ $formula_detail['detail'][$key]['percentage'] = decode($value['percentage']); } $this->view->assign("formula_detail", $formula_detail); return $this->view->fetch(); } /** * 生成领料单 * @return string * @throws \think\Exception */ public function taskadd(){ $ids = input('ids');//获取作业票id if(empty($ids)){$this->error('异常');} $ids = substr($ids,0,strlen($ids)-1); $id = explode(',',$ids);//逗号分割开 $list = []; foreach ($id as $k=>$v){ $task[$k]= Db::name('task')->where('id',$v)->find(); $list[$k] = Db::name('formula_detail')->where('pid',$task[$k]['fid'])->field('material,percentage')->select(); } /* * 根据查到的配方fid去查配方详情表 fid = pid * 解密百分比字段 * 根据task['number'] 算出应该领出的数量 应领重量 = 材料百分比/(所有材料百分比相加)*task['number'] * 值相加去重 */ $res = array(); foreach ($list as $key=>$value){ //查出百分比 相加得出总计百分比 $total = Db::name('formula_detail')->where('pid',$task[$key]['fid'])->column('percentage'); foreach ($total as $k=>$v){ $total[$k] = decode($v); } $num = array_sum($total);//算出所有百分比总数 for ($i=0;$isumWeight($res,'material','num'); foreach ($list as $k=>$v){ $sum_list = $this->sumWeight($v,'material','num'); for ($i=0;$i"; } } } } } // echo "
";
//        print_r($task);//一维
//        echo "
"; // // echo "
";
//        print_r($sum_list);//原材料信息
//        echo "
"; // echo "
";
//        print_r($result);//汇总领料单
//        echo "
"; $this->view->assign("task", $task);//生成领料单配方信息 $this->view->assign("list", $list);//生成领料单 $this->view->assign("result", $result);//汇总领料单 return $this->view->fetch('taskadd'); } /** * 计算数量,值去重&相加 * $arr 数组 * $str1 要合并的值 * $str2 要计算的值 */ function sumWeight($arr,$str1,$str2) { $item = array(); foreach ($arr as $k => $v) { if (!isset($item[$v[$str1]])) { $item[$v[$str1]] = $v; } else { $item[$v[$str1]][$str2] += $v[$str2]; } // if ($k == 13) { // echo "
";
//                print_r($item);die;
//                echo "
"; // } } return array_values($item); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ }