| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use think\Db;
- /**
- * 作业票管理
- *
- * @icon fa fa-circle-o
- */
- class Task extends Backend
- {
- /**
- * Task模型对象
- * @var \app\admin\model\Task
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->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 "<pre>";
- // print_r($params);
- // echo "</pre>";
- // 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;$i<count($value);$i++){
- if (!empty($value[$i]['percentage'])) {//判断有没有材料和百分比
- $list[$key][$i]['percentage'] = decode($value[$i]['percentage']);
- //计算应加入的重量
- $number = ceil($list[$key][$i]['percentage'] / $num * $task[$key]['number'] * 1000);
- $list[$key][$i]['num'] = number_format($number/1000,3);
- $list[$key][$i]['machine'] = $task[$key]['machine'];
- $list[$key][$i]['str'] = '';//为汇总数组中机台做准备字段
- array_push($res,$list[$key][$i]);//将数组push到新的需要整合的数组中
- }else{
- unset($list[$key][$i]);
- }
- }
- $list[$key] = array_values($list[$key]);
- }
- $result = $this->sumWeight($res,'material','num');
- foreach ($list as $k=>$v){
- $sum_list = $this->sumWeight($v,'material','num');
- for ($i=0;$i<count($result);$i++){//循环汇总表
- for ($j=0;$j<count($sum_list);$j++){
- if ($result[$i]['material'] == $sum_list[$j]['material']){
- if ( $result[$i]['str'] != $sum_list[$j]['machine']){
- $result[$i]['str'] .= "<div>".$sum_list[$j]['machine'].":".$sum_list[$j]['num']."</div>";
- }
- }
- }
- }
- }
- // echo "<pre>";
- // print_r($task);//一维
- // echo "</pre>";
- //
- // echo "<pre>";
- // print_r($sum_list);//原材料信息
- // echo "</pre>";
- // echo "<pre>";
- // print_r($result);//汇总领料单
- // echo "</pre>";
- $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 "<pre>";
- // print_r($item);die;
- // echo "</pre>";
- // }
- }
- return array_values($item);
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- }
|