| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?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;
- }
- /**
- * 生产管理 作业票列表查看
- */
- 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']);
- //计算应加入的重量
- $list[$key][$i]['num'] = number_format($list[$key][$i]['percentage'] / $num * $task[$key]['number'],3);
- array_push($res,$list[$key][$i]);//将数组push到新的需要整合的数组中
- }else{
- unset($list[$key][$i]);
- }
- }
- $list[$key] = array_values($list[$key]);
- }
- $result = $this->sumWeight($res,'material','num');
- // echo "<pre>";
- // print_r($task);//一维
- // echo "</pre>";
- //
- // echo "<pre>";
- // print_r($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中对应的方法复制到当前控制器,然后进行修改
- */
- }
|