| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080 | <?phpnamespace app\admin\controller;use think\App;use think\Db;class RepairStatistics extends Auth{    public function addrType(){        $start = input('start',date('Y-m'));        $end = input('end',date('Y-m'));        if($start > $end){            $this->error('开始时间不能大于结束时间');        }        $map[] = ['o.create_yyyymm','>=',date('Ym',strtotime($start))];        $map[] = ['o.create_yyyymm','<=',date('Ym',strtotime($end))];        $typeIds = input('typeIds');        if($typeIds !=''){            $where[] = ['id','in',$typeIds];        }        $mode = [            ['type'=>1,'title'=>'需求数','value'=>[1]],            ['type'=>2,'title'=>'已派工','value'=>[4]],            ['type'=>3,'title'=>'已完成','value'=>[5,6]],            ['type'=>4,'title'=>'已取消','value'=>[2,3]],        ];        $addr = Db::name('address')            ->field('id,title')            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->where('','exp',Db::raw("FIND_IN_SET(1,types)"))            ->select();        $where[] = ['org_id','=',$this->orgId];        $type = Db::name('order_type')            ->where($where)            ->where('parent_id',0)            ->where('del',0)            ->where('enable',1)            ->select();        $type = $type?$type:[];        $lists = [];        foreach ($addr as $k=>$v){            $a = [                'title' => $v['title'],                'total' => '',                'status' => '',            ];            foreach ($type as $key=>$val){                $a['type'.$val['id']] = '';            }            $lists[] = $a;            foreach ($mode as $kk=>$vv){                $b = [                    'title' => '',                    'status' => $vv['title'],                ];                $total = 0;                foreach ($type as $key=>$val){                    $subTypeId = Db::name('order_type')                        ->where('parent_id',$val['id'])                        ->where('del',0)                        ->where('enable',1)                        ->where('org_id',$this->orgId)                        ->column('id');                    $count  = Db::name('orders')                        ->alias('o')                        ->join('order_repair or','or.order_id=o.id')                        ->where('o.work_type_mode',1)                        ->where('o.del',0)                        ->where($map)                        ->whereIn('o.order_mode',$vv['value'])                        ->where('or.address_id',$v['id'])                        ->whereIn('or.type_id',$subTypeId)                        ->count();                    $total += $count;                    $b['type'.$val['id']] = $count;                }                $b['total'] = $total;                $lists[] = $b;            }        }        $this->assign('lists',$lists);        $this->assign('addr',$addr);        $this->assign('type',$type);        $typeList = Db::name('order_type')            ->where('org_id',$this->orgId)            ->where('parent_id',0)            ->where('del',0)            ->where('enable',1)            ->select();        $this->assign('typeList',$typeList);        $this->assign('start',$start);        $this->assign('end',$end);        $this->assign('typeIds',$typeIds);        $this->assign('typeIds2',!empty($typeIds)?explode(',',$typeIds):[]);        return  $this->fetch();    }    public function addrTypeExport(){        $start = input('start',date('Y-m'));        $end = input('end',date('Y-m'));        if($start > $end){            $this->error('开始时间不能大于结束时间');        }        $map[] = ['o.create_yyyymm','>=',date('Ym',strtotime($start))];        $map[] = ['o.create_yyyymm','<=',date('Ym',strtotime($end))];        $typeIds = input('typeIds','','trim');        if($typeIds !=''){            $where[] = ['id','in',$typeIds];        }        $mode = [            ['type'=>1,'title'=>'需求数','value'=>[1]],            ['type'=>2,'title'=>'已派工','value'=>[4]],            ['type'=>3,'title'=>'已完成','value'=>[5,6]],            ['type'=>4,'title'=>'已取消','value'=>[2,3]],        ];        $addr = Db::name('address')            ->field('id,title')            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->where('','exp',Db::raw("FIND_IN_SET(1,types)"))            ->select();        $where[] = ['org_id','=',$this->orgId];        $type = Db::name('order_type')            ->where($where)            ->where('parent_id',0)            ->where('del',0)            ->where('enable',1)            ->select();        $type = $type?$type:[];        $lists = [];        foreach ($addr as $k=>$v){            $a = [                'title' => $v['title'],                'total' => '',            ];            foreach ($type as $key=>$val){                $a['type'.$val['id']] = '';            }            $lists[] = $a;            foreach ($mode as $kk=>$vv){                $b = [                    'title' => $vv['title'],                ];                $total = 0;                foreach ($type as $key=>$val){                    $subTypeId = Db::name('order_type')                        ->where('parent_id',$val['id'])                        ->where('del',0)                        ->where('enable',1)                        ->where('org_id',$this->orgId)                        ->column('id');                    $count  = Db::name('orders')                        ->alias('o')                        ->join('order_repair or','or.order_id=o.id')                        ->where('o.work_type_mode',1)                        ->where('o.del',0)                        ->where($map)                        ->whereIn('o.order_mode',$vv['value'])                        ->where('or.address_id',$v['id'])                        ->whereIn('or.type_id',$subTypeId)                        ->count();                    $total += $count;                    $b['type'.$val['id']] = $count;                }                $b['total'] = $total;                $lists[] = $b;            }        }        include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';        //实例化PHPExcel类        $objPHPExcel =new \PHPExcel();        //激活当前的sheet表        $objPHPExcel->setActiveSheetIndex(0);        //设置表格头(即excel表格的第一行)        $sheet = $objPHPExcel->setActiveSheetIndex(0);        $arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T');        foreach ($arr as $k=>$v) {            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(false);//换行            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth(18);        }        $sheet->setCellValueByColumnAndRow(0, 1, '地点');        if($type){            foreach ($type as $k=>$v){                $sheet->setCellValueByColumnAndRow($k+1, 1, $v['title']);            }        }        $sheet->setCellValueByColumnAndRow(count($type)+1, 1, '总数');        foreach ($lists as $k=>$v) {            $sheet->setCellValueByColumnAndRow(0, $k + 2, $v['title']);            foreach ($type as $kk=>$vv){                $sheet->setCellValueByColumnAndRow($kk+1, $k + 2, $v['type'.$vv['id']]);            }            $sheet->setCellValueByColumnAndRow(count($type)+1, $k + 2, $v['total']);        }        //设置保存的Excel表格名称        $filename = '报修地点报表_' . date('YmdHis', time()) . '.xls';        //设置当前激活的sheet表格名称        $objPHPExcel->getActiveSheet()->setTitle('报修地点报表');        //设置浏览器窗口下载表格        ob_end_clean();        header("Content-Type: application/force-download");        header("Content-Type: application/octet-stream");        header("Content-Type: application/download");        header('Content-Disposition:inline;filename="' . $filename);        //生成excel文件        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');        //下载文件在浏览器窗口        return $objWriter->save('php://output');    }    public function todoXyTime(){        $list = Db::name('todo')            ->where('work_type_mode',1)            ->where('del',0)            ->where('todo_mode','in',[2,3])            ->where('org_id',$this->orgId)            ->order('id desc')            ->select();        $count = Db::name('todo')            ->where('work_type_mode',1)            ->where('del',0)            ->where('todo_mode','in',[2,3])            ->where('org_id',$this->orgId)            ->order('id desc')            ->sum('xy_time');        foreach ($list as $k=>$v){            $subType  = Db::name('order_type')                ->alias('ot')                ->join('order_repair or','or.type_id=ot.id')                ->where('or.order_id',$v['order_id'])                ->value('type_id');            $parentId = Db::name('order_type')->where('id',$subType)->value('parent_id');            $list[$k]['type_title'] = Db::name('order_type')->where('id',$parentId)->value('title');            $list[$k]['content'] = Db::name('orders')->where('id',$v['order_id'])->value('content');            $list[$k]['dep'] = Db::name('orders')->alias('o')->join('dep d','d.id=o.dep_id')->where('o.id',$v['order_id'])->value('d.title');            $list[$k]['user_name'] = Db::name('user')->where('id',$v['to_user_id'])->value('real_name');            $list[$k]['xy_time'] = round($v['xy_time']/60,1);        }        $this->assign('count',round($count/60,1));        $this->assign('list',$list);        return  $this->fetch();    }    public function xyTimeEcharts(){        $curTime = date('Y-m-d H:i:s');        $time = [            ['title'=>'24小时内','sTime'=>date('Y-m-d H:i:s',strtotime($curTime)-3600*24),'eTime'=>$curTime,'type'=>1],            ['title'=>'24~48小时','sTime'=>date('Y-m-d H:i:s',strtotime($curTime)-3600*48),'eTime'=>date('Y-m-d H:i:s',strtotime($curTime)-3600*24),'type'=>1],            ['title'=>'超过72小时','sTime'=>'','eTime'=>date('Y-m-d H:i:s',strtotime($curTime)-3600*72),'type'=>2],            ['title'=>'未响应','sTime'=>'','eTime'=>'','type'=>3],            ['title'=>'总数','sTime'=>'','eTime'=>'','type'=>4],        ];        $list = [];        foreach ($time as $k=>$v){            if($v['type'] == 1){                $count = Db::name('todo')                    ->where('org_id',$this->orgId)                    ->where('work_type_mode',1)                    ->where('del',0)                    ->where('create_time','>',$v['sTime'])                    ->where('create_time','<=',$v['eTime'])                    ->where('todo_mode','in',[2,3])                    ->count();            }elseif ($v['type'] == 2){                $count = Db::name('todo')                    ->where('org_id',$this->orgId)                    ->where('work_type_mode',1)                    ->where('del',0)                    ->where('create_time','<',$v['eTime'])                    ->where('todo_mode','in',[2,3])                    ->count();            }elseif ($v['type'] == 3){                $count = Db::name('todo')                    ->where('org_id',$this->orgId)                    ->where('work_type_mode',1)                    ->where('del',0)                    ->where('todo_mode',1)                    ->count();            }elseif($v['type'] == 4){                $count = Db::name('todo')                    ->where('org_id',$this->orgId)                    ->where('work_type_mode',1)                    ->where('del',0)                    ->count();            }            $list[$k]['title'] = $v['title'];            $list[$k]['value'] = $count;        }        $data = [            'title'=>array_column($time,'title'),            'list'=>$list,        ];        $this->success('','',$data);    }    public function xyTimeExport(){        $list = Db::name('todo')            ->where('work_type_mode',1)            ->where('del',0)            ->where('todo_mode','in',[2,3])            ->where('org_id',$this->orgId)            ->order('id desc')            ->select();        $count = Db::name('todo')            ->where('work_type_mode',1)            ->where('del',0)            ->where('todo_mode','in',[2,3])            ->where('org_id',$this->orgId)            ->order('id desc')            ->sum('xy_time');        foreach ($list as $k=>$v){            $subType  = Db::name('order_type')                ->alias('ot')                ->join('order_repair or','or.type_id=ot.id')                ->where('or.order_id',$v['order_id'])                ->value('type_id');            $parentId = Db::name('order_type')->where('id',$subType)->value('parent_id');            $list[$k]['type_title'] = Db::name('order_type')->where('id',$parentId)->value('title');            $list[$k]['content'] = Db::name('orders')->where('id',$v['order_id'])->value('content');            $list[$k]['dep'] = Db::name('orders')->alias('o')->join('dep d','d.id=o.dep_id')->where('o.id',$v['order_id'])->value('d.title');            $list[$k]['user_name'] = Db::name('user')->where('id',$v['to_user_id'])->value('real_name');            $list[$k]['xy_time'] = round($v['xy_time']/60,1);        }        include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';        //实例化PHPExcel类        $objPHPExcel =new \PHPExcel();        //激活当前的sheet表        $objPHPExcel->setActiveSheetIndex(0);        //设置表格头(即excel表格的第一行)        $sheet = $objPHPExcel->setActiveSheetIndex(0);        $arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N');        foreach ($arr as $k=>$v) {            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(false);//换行            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth(18);        }        $sheet->setCellValueByColumnAndRow(0, 1, '类型');        $sheet->setCellValueByColumnAndRow(1, 1, '部门');        $sheet->setCellValueByColumnAndRow(2, 1, '员工');        $sheet->setCellValueByColumnAndRow(3, 1, '需求描述');        $sheet->setCellValueByColumnAndRow(4, 1, '状态');        $sheet->setCellValueByColumnAndRow(5, 1, '响应时间');        $sheet->setCellValueByColumnAndRow(6, 1, '合计');        foreach ($list as $k=>$v) {            $sheet->setCellValueByColumnAndRow(0, $k + 2, $v['type_title']);            $sheet->setCellValueByColumnAndRow(1, $k + 2, $v['dep']);            $sheet->setCellValueByColumnAndRow(2, $k + 2, $v['user_name']);            $sheet->setCellValueByColumnAndRow(3, $k + 2, $v['content']);            if($v['todo_mode'] == 2){                $sheet->setCellValueByColumnAndRow(4, $k + 2, '进行中');            }            $sheet->setCellValueByColumnAndRow(5, $k + 2, $v['xy_time']);        }        $sheet->setCellValueByColumnAndRow(6, $k + 2, round($count/60,1));        //设置保存的Excel表格名称        $filename = '报修工单响应时间_' . date('YmdHis', time()) . '.xls';        //设置当前激活的sheet表格名称        $objPHPExcel->getActiveSheet()->setTitle('报修工单响应时间');        //设置浏览器窗口下载表格        ob_end_clean();        header("Content-Type: application/force-download");        header("Content-Type: application/octet-stream");        header("Content-Type: application/download");        header('Content-Disposition:inline;filename="' . $filename);        //生成excel文件        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');        //下载文件在浏览器窗口        return $objWriter->save('php://output');    }    public function addrWorkHour(){        $start = input('start',date('Y-m'));        $end = input('end',date('Y-m'));        if($start > $end){            $this->error('开始时间不能大于结束时间');        }        $map[] = ['t.create_yyyymm','>=',date('Ym',strtotime($start))];        $map[] = ['t.create_yyyymm','<=',date('Ym',strtotime($end))];        $addrIds= input('addrIds','','trim');        if($addrIds !=''){            $where[] = ['id','in',$addrIds];        }        $where[] = ['org_id','=',$this->orgId];        $addr = Db::name('address')            ->field('id,title')            ->where($where)            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->where('','exp',Db::raw("FIND_IN_SET(1,types)"))            ->select();        $addr = $addr?$addr:[];        $list = [];        $total = $total2 = 0;        foreach ($addr as $k=>$v){            $count = Db::name('todo')                ->alias('t')                ->join('order_repair or','or.order_id=t.order_id')                ->where('or.address_id',$v['id'])                ->where($map)                ->where('t.del',0)                ->where('t.work_type_mode',1)                ->where('t.org_id',$this->orgId)                ->where('t.todo_mode',3)                ->count();            $total +=$count;            $count2 = Db::name('todo')                ->alias('t')                ->join('order_repair or','or.order_id=t.order_id')                ->where('or.address_id',$v['id'])                ->where($map)                ->where('t.del',0)                ->where('t.work_type_mode',1)                ->where('t.org_id',$this->orgId)                ->where('t.todo_mode',3)                ->sum('wc_time');            $total2 +=$count2;            $list[$k]['title'] = $v['title'];            $list[$k]['count'] = $count;            $list[$k]['count2'] = round($count2/60,1);        }        $this->assign('list',$list);        $this->assign('total',$total);        $this->assign('total2',round($total2/60,1));        $addrList = Db::name('address')            ->field('id,title')            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->where('','exp',Db::raw("FIND_IN_SET(1,types)"))            ->select();        $this->assign('addrList',$addrList);        $this->assign('start',$start);        $this->assign('end',$end);        $this->assign('addrIds',$addrIds);        $this->assign('addrIds2',!empty($addrIds)?explode(',',$addrIds):[]);        return  $this->fetch();    }    public function addrWorkHourExport(){        $start = input('start',date('Y-m'));        $end = input('end',date('Y-m'));        if($start > $end){            $this->error('开始时间不能大于结束时间');        }        $map[] = ['t.create_yyyymm','>=',date('Ym',strtotime($start))];        $map[] = ['t.create_yyyymm','<=',date('Ym',strtotime($end))];        $addrIds= input('addrIds','','trim');        if($addrIds !=''){            $where[] = ['id','in',$addrIds];        }        $where[] = ['org_id','=',$this->orgId];        $addr = Db::name('address')            ->field('id,title')            ->where($where)            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->where('','exp',Db::raw("FIND_IN_SET(1,types)"))            ->select();        $addr = $addr?$addr:[];        $list = [];        $total = $total2 = 0;        foreach ($addr as $k=>$v){            $count = Db::name('todo')                ->alias('t')                ->join('order_repair or','or.order_id=t.order_id')                ->where('or.address_id',$v['id'])                ->where($map)                ->where('t.del',0)                ->where('t.work_type_mode',1)                ->where('t.org_id',$this->orgId)                ->where('t.todo_mode',3)                ->count();            $total +=$count;            $count2 = Db::name('todo')                ->alias('t')                ->join('order_repair or','or.order_id=t.order_id')                ->where('or.address_id',$v['id'])                ->where($map)                ->where('t.del',0)                ->where('t.work_type_mode',1)                ->where('t.org_id',$this->orgId)                ->where('t.todo_mode',3)                ->sum('wc_time');            $total2 +=$count2;            $list[$k]['title'] = $v['title'];            $list[$k]['count'] = $count;            $list[$k]['count2'] = round($count2/60,1);        }        include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';        //实例化PHPExcel类        $objPHPExcel =new \PHPExcel();        //激活当前的sheet表        $objPHPExcel->setActiveSheetIndex(0);        //设置表格头(即excel表格的第一行)        $sheet = $objPHPExcel->setActiveSheetIndex(0);        $arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N');        foreach ($arr as $k=>$v) {            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(false);//换行            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth(18);        }        $sheet->setCellValueByColumnAndRow(0, 1, '地点');        $sheet->setCellValueByColumnAndRow(1, 1, '工单数');        $sheet->setCellValueByColumnAndRow(2, 1, '所需工时');        foreach ($list as $k=>$v) {            $sheet->setCellValueByColumnAndRow(0, $k + 2, $v['title']);            $sheet->setCellValueByColumnAndRow(1, $k + 2, $v['count']);            $sheet->setCellValueByColumnAndRow(2, $k + 2, $v['count2']);        }        $sheet->setCellValueByColumnAndRow(0, count($list)+3, '合计');        $sheet->setCellValueByColumnAndRow(1, count($list)+3, '总工单数');        $sheet->setCellValueByColumnAndRow(2, count($list)+3, $total);        $sheet->setCellValueByColumnAndRow(1, count($list)+4, '总工时');        $sheet->setCellValueByColumnAndRow(2, count($list)+4, round($total2/60,1));        $sheet->setCellValueByColumnAndRow(1, count($list)+5, '平均工时');        if($total > 0){            $sheet->setCellValueByColumnAndRow(2, count($list)+5, round($total2/60/$total,1));        }else{            $sheet->setCellValueByColumnAndRow(2, count($list)+5, 0);        }        //设置保存的Excel表格名称        $filename = '报修地点工时报表_' . date('YmdHis', time()) . '.xls';        //设置当前激活的sheet表格名称        $objPHPExcel->getActiveSheet()->setTitle('报修地点工时报表');        //设置浏览器窗口下载表格        ob_end_clean();        header("Content-Type: application/force-download");        header("Content-Type: application/octet-stream");        header("Content-Type: application/download");        header('Content-Disposition:inline;filename="' . $filename);        //生成excel文件        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');        //下载文件在浏览器窗口        return $objWriter->save('php://output');    }    public function typeWorkHour(){        $start = input('start',date('Y-m'));        $end = input('end',date('Y-m'));        if($start > $end){            $this->error('开始时间不能大于结束时间');        }        $map[] = ['t.create_yyyymm','>=',date('Ym',strtotime($start))];        $map[] = ['t.create_yyyymm','<=',date('Ym',strtotime($end))];        $typeIds = input('typeIds');        if($typeIds !=''){            $where[] = ['id','in',$typeIds];        }        $addr = Db::name('address')            ->field('id,title')            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->where('','exp',Db::raw("FIND_IN_SET(1,types)"))            ->select();        $where[] = ['org_id','=',$this->orgId];        $type = Db::name('order_type')            ->where($where)            ->where('parent_id',0)            ->where('del',0)            ->where('enable',1)            ->select();        $type = $type?$type:[];        $newType = [];        foreach ($type as $k=>$v){            $a = [                'title'=>$v['title'],                'id'=>$v['id'],                'type'=>1            ];            $newType[]= $a;            $b= [                'title'=>$v['title'].'工时',                'id'=>$v['id'],                'type'=>2            ];            $newType[]= $b;        }        $this->assign('newType',$newType);        $lists = [];        $total = $total2 = 0 ;        foreach ($addr as $k=>$v){            $a = [                'title' => $v['title'],            ];            foreach ($newType as $key=>$val){                $subTypeId = Db::name('order_type')                    ->where('parent_id',$val['id'])                    ->where('del',0)                    ->where('enable',1)                    ->where('org_id',$this->orgId)                    ->column('id');                if($val['type']==1){                    $count  = Db::name('todo')                        ->alias('t')                        ->join('order_repair or','or.order_id=t.order_id')                        ->where('t.work_type_mode',1)                        ->where('t.del',0)                        ->where($map)                        ->where('t.todo_mode',3)                        ->where('or.address_id',$v['id'])                        ->whereIn('or.type_id',$subTypeId)                        ->count();                    $total +=$count;                }elseif ($val['type']==2){                    $wx_time  = Db::name('todo')                        ->alias('t')                        ->join('order_repair or','or.order_id=t.order_id')                        ->where('t.work_type_mode',1)                        ->where('t.del',0)                        ->where($map)                        ->where('t.todo_mode',3)                        ->where('or.address_id',$v['id'])                        ->whereIn('or.type_id',$subTypeId)                        ->sum('wc_time');                    $count = round($wx_time/60,1);                    $total2 +=$count;                }                $a['type'.$key] = $count;            }            $lists[] = $a;        }        $this->assign('lists',$lists);        $this->assign('total',$total);        $this->assign('total2',$total2);        $this->assign('addr',$addr);        $this->assign('type',$type);        $typeList = Db::name('order_type')            ->where('org_id',$this->orgId)            ->where('parent_id',0)            ->where('del',0)            ->where('enable',1)            ->select();        $this->assign('typeList',$typeList);        $this->assign('start',$start);        $this->assign('end',$end);        $this->assign('typeIds',$typeIds);        $this->assign('typeIds2',!empty($typeIds)?explode(',',$typeIds):[]);        return  $this->fetch();    }    public function typeWorkHourExport(){        $start = input('start',date('Y-m'));        $end = input('end',date('Y-m'));        if($start > $end){            $this->error('开始时间不能大于结束时间');        }        $map[] = ['t.create_yyyymm','>=',date('Ym',strtotime($start))];        $map[] = ['t.create_yyyymm','<=',date('Ym',strtotime($end))];        $typeIds = input('typeIds');        if($typeIds !=''){            $where[] = ['id','in',$typeIds];        }        $addr = Db::name('address')            ->field('id,title')            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->where('','exp',Db::raw("FIND_IN_SET(1,types)"))            ->select();        $where[] = ['org_id','=',$this->orgId];        $type = Db::name('order_type')            ->where($where)            ->where('parent_id',0)            ->where('del',0)            ->where('enable',1)            ->select();        $type = $type?$type:[];        $newType = [];        foreach ($type as $k=>$v){            $a = [                'title'=>$v['title'],                'id'=>$v['id'],                'type'=>1            ];            $newType[]= $a;            $b= [                'title'=>$v['title'].'工时',                'id'=>$v['id'],                'type'=>2            ];            $newType[]= $b;        }        $this->assign('newType',$newType);        $lists = [];        $total = $total2 = 0 ;        foreach ($addr as $k=>$v){            $a = [                'title' => $v['title'],            ];            foreach ($newType as $key=>$val){                $subTypeId = Db::name('order_type')                    ->where('parent_id',$val['id'])                    ->where('del',0)                    ->where('enable',1)                    ->where('org_id',$this->orgId)                    ->column('id');                if($val['type']==1){                    $count  = Db::name('todo')                        ->alias('t')                        ->join('order_repair or','or.order_id=t.order_id')                        ->where('t.work_type_mode',1)                        ->where('t.del',0)                        ->where($map)                        ->where('t.todo_mode',3)                        ->where('or.address_id',$v['id'])                        ->whereIn('or.type_id',$subTypeId)                        ->count();                    $total +=$count;                }elseif ($val['type']==2){                    $wx_time  = Db::name('todo')                        ->alias('t')                        ->join('order_repair or','or.order_id=t.order_id')                        ->where('t.work_type_mode',1)                        ->where('t.del',0)                        ->where($map)                        ->where('t.todo_mode',3)                        ->where('or.address_id',$v['id'])                        ->whereIn('or.type_id',$subTypeId)                        ->sum('wc_time');                    $count = round($wx_time/60,1);                    $total2 +=$count;                }                $a['type'.$key] = $count;            }            $lists[] = $a;        }        include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';        //实例化PHPExcel类        $objPHPExcel =new \PHPExcel();        //激活当前的sheet表        $objPHPExcel->setActiveSheetIndex(0);        //设置表格头(即excel表格的第一行)        $sheet = $objPHPExcel->setActiveSheetIndex(0);        $arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T');        foreach ($arr as $k=>$v) {            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(false);//换行            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth(18);        }        $sheet->setCellValueByColumnAndRow(0, 1, '地点');        foreach ($newType as $k=>$v){            $sheet->setCellValueByColumnAndRow($k+1, 1, $v['title']);        }        foreach ($lists as $k=>$v) {            $sheet->setCellValueByColumnAndRow(0, $k + 2, $v['title']);            foreach ($newType as $kk=>$vv){                $sheet->setCellValueByColumnAndRow($kk+1, $k + 2, $v['type'.$kk]);            }        }        $sheet->setCellValueByColumnAndRow(0, count($lists)+3, '合计');        $sheet->setCellValueByColumnAndRow(1, count($lists)+3, '总工单数');        $sheet->setCellValueByColumnAndRow(2, count($lists)+3, $total);        $sheet->setCellValueByColumnAndRow(1, count($lists)+4, '总工时');        $sheet->setCellValueByColumnAndRow(2, count($lists)+4, $total2);        $sheet->setCellValueByColumnAndRow(1, count($lists)+5, '平均工时');        if($total > 0){            $sheet->setCellValueByColumnAndRow(2, count($lists)+5, $total2/$total);        }else{            $sheet->setCellValueByColumnAndRow(2, count($lists)+5, 0);        }        //设置保存的Excel表格名称        $filename = '报修类型工时报表_' . date('YmdHis', time()) . '.xls';        //设置当前激活的sheet表格名称        $objPHPExcel->getActiveSheet()->setTitle('报修类型工时报表');        //设置浏览器窗口下载表格        ob_end_clean();        header("Content-Type: application/force-download");        header("Content-Type: application/octet-stream");        header("Content-Type: application/download");        header('Content-Disposition:inline;filename="' . $filename);        //生成excel文件        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');        //下载文件在浏览器窗口        return $objWriter->save('php://output');    }    public function consumables(){        $month = input('month',date('Y-m'));//        $curYear = date('Y',strtotime($month));//        $curMonth = date('m',strtotime($month));        $map[] = ['create_yyyymm','=',date('Ym',strtotime($month))];        $goodsIds = input('goodsIds');        if($goodsIds !=''){            $where[] = ['id','in',$goodsIds];        }        $where[] = ['org_id','=',$this->orgId];        $where[] = ['del','=',0];        $where[] = ['enable','=',1];        $mate = Db::name('mate_goods')            ->field('id,title')            ->where($where)            ->select();        $this->assign('mate',$mate);        //获取某年某个月的总天数//        $t =  cal_days_in_month(CAL_GREGORIAN,$curMonth,$curYear);        $t = date('t',strtotime($month));        $sDay = strtotime($month.'-01');        $arr = [];        for ($i = 0;$i<$t;$i++){            $arr[$i]['date'] = date('m-d',$sDay+$i*86400);            $arr[$i]['ymd'] = date('Y-m-d',$sDay+$i*86400);        }        $lists = [];        foreach ($arr as $k=>$v){            $a = [                'title' => $v['date'],            ];            foreach ($mate as $key=>$val){                $count = Db::name('todo_mate_item')                    ->where('items_id',$val['id'])                    ->where('create_time','>=',$v['ymd'].' 00:00:00')                    ->where('create_time','<=',$v['ymd'].' 23:59:59')                    ->sum('total');                $a['type'.$val['id']] = $count;            }            $lists[] = $a;        }        $subtotal = []; $total = 0 ;        foreach ($mate as $k=>$v){            $c = Db::name('todo_mate_item')                ->where('items_id',$v['id'])                ->where('create_time','>=',$month.'-01 00:00:00')                ->where('create_time','<=',$month.'-31 23:59:59')                ->sum('total');            $total +=$c;            $subtotal[$k] = $c;        }        $this->assign('lists',$lists);        $this->assign('subtotal',$subtotal);        $this->assign('total',$total);        $mateGoods = Db::name('mate_goods')            ->field('id,title')            ->where('org_id',$this->orgId)            ->where('del',0)            ->where('enable',1)            ->select();        $this->assign('mateGoods',$mateGoods);        $this->assign('month',$month);        $this->assign('goodsIds',$goodsIds);        $this->assign('goodsIds2',!empty($goodsIds)?explode(',',$goodsIds):[]);        return  $this->fetch();    }    public function consumablesExport(){        $month = input('month',date('Y-m'));//        $curYear = date('Y',strtotime($month));//        $curMonth = date('m',strtotime($month));        $map[] = ['create_yyyymm','=',date('Ym',strtotime($month))];        $goodsIds = input('goodsIds');        if($goodsIds !=''){            $where[] = ['id','in',$goodsIds];        }        $where[] = ['org_id','=',$this->orgId];        $where[] = ['del','=',0];        $where[] = ['enable','=',1];        $mate = Db::name('mate_goods')            ->field('id,title')            ->where($where)            ->select();        $this->assign('mate',$mate);        //获取某年某个月的总天数//        $t =  cal_days_in_month(CAL_GREGORIAN,$curMonth,$curYear);        $t = date('t',strtotime($month));        $sDay = strtotime($month.'-01');        $arr = [];        for ($i = 0;$i<$t;$i++){            $arr[$i]['date'] = date('m-d',$sDay+$i*86400);            $arr[$i]['ymd'] = date('Y-m-d',$sDay+$i*86400);        }        $lists = [];        foreach ($arr as $k=>$v){            $a = [                'title' => $v['date'],            ];            foreach ($mate as $key=>$val){                $count = Db::name('todo_mate_item')                    ->where('items_id',$val['id'])                    ->where('create_time','>=',$v['ymd'].' 00:00:00')                    ->where('create_time','<=',$v['ymd'].' 23:59:59')                    ->sum('total');                $a['type'.$val['id']] = $count;            }            $lists[] = $a;        }        $subtotal = []; $total = 0 ;        foreach ($mate as $k=>$v){            $c = Db::name('todo_mate_item')                ->where('items_id',$v['id'])                ->where('create_time','>=',$month.'-01 00:00:00')                ->where('create_time','<=',$month.'-31 23:59:59')                ->sum('total');            $total +=$c;            $subtotal[$k] = $c;        }        include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';        //实例化PHPExcel类        $objPHPExcel =new \PHPExcel();        //激活当前的sheet表        $objPHPExcel->setActiveSheetIndex(0);        //设置表格头(即excel表格的第一行)        $sheet = $objPHPExcel->setActiveSheetIndex(0);        $arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T');        foreach ($arr as $k=>$v) {            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(false);//换行            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v.'1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);            $objPHPExcel->getActiveSheet()->getStyle($v)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);            $objPHPExcel->getActiveSheet()->getColumnDimension($v)->setWidth(18);        }        $sheet->setCellValueByColumnAndRow(0, 1, '日期');        foreach ($mate as $k=>$v){            $sheet->setCellValueByColumnAndRow($k+1, 1, $v['title']);        }        foreach ($lists as $k=>$v) {            $sheet->setCellValueByColumnAndRow(0, $k + 2, $v['title']);            foreach ($mate as $kk=>$vv){                $sheet->setCellValueByColumnAndRow($kk+1, $k + 2, $v['type'.$vv['id']]);            }        }        $sheet->setCellValueByColumnAndRow(0, count($lists)+3, '小计');        foreach ($subtotal as $k=>$v){            $sheet->setCellValueByColumnAndRow($k+1, count($lists)+3, $v);        }        $sheet->setCellValueByColumnAndRow(0, count($lists)+4, '总计');        $sheet->setCellValueByColumnAndRow(1, count($lists)+4, $total);        //设置保存的Excel表格名称        $filename = '报修耗材报表_' . date('YmdHis', time()) . '.xls';        //设置当前激活的sheet表格名称        $objPHPExcel->getActiveSheet()->setTitle('报修耗材报表');        //设置浏览器窗口下载表格        ob_end_clean();        header("Content-Type: application/force-download");        header("Content-Type: application/octet-stream");        header("Content-Type: application/download");        header('Content-Disposition:inline;filename="' . $filename);        //生成excel文件        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');        //下载文件在浏览器窗口        return $objWriter->save('php://output');    }}
 |