123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013 |
- <?php
- namespace app\admin\controller;
- use think\App;
- use think\Db;
- class WasteStatistics extends Auth {
- public function work() {
- $cur = date('Y-m-d');
- $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
- $end = input('end', date('Y-m-d'));
- $start1 = $start . ' 00:00:00';
- $end1 = $end . ' 23:59:59';
- $type = input('type');
- $typeList = Db::name('waste_type')
- ->where('enable', 1)
- ->where('del', 0)
- //->where('org_id', $this->orgId)
- ->select();
- $list = $this->workData($start1, $end1, $type);
- $this->assign('list', $list);
- $this->assign('type', $type);
- $this->assign('typeList', $typeList);
- $this->assign('start', $start);
- $this->assign('end', $end);
- return $this->fetch();
- }
- public function workData($start1, $end1, $type) {
- $map1[] = ['create_time', '>=', $start1];
- $map1[] = ['create_time', '<=', $end1];
- $map1[] = ['org_id', '=', $this->orgId];
- if (!empty($type)) {
- $map1[] = ['cateid', '=', $type];
- }
- $list = Db::name('waste_record')
- ->where($map1)
- ->where('del', 0)
- ->group('user_id')
- ->select();
- foreach ($list as $k => $v) {
- $list[$k]['title'] = Db::name('user')
- ->where('id', $v['user_id'])
- ->value('real_name');
- $c = Db::name('waste_record')
- ->where($map1)
- ->where('del', 0)
- ->where('user_id', $v['user_id'])
- ->count();
- $list[$k]['count'] = $c ? $c : 0;
- $weight = Db::name('waste_record')
- ->where($map1)
- ->where('del', 0)
- ->where('user_id', $v['user_id'])
- ->sum('weight');
- $list[$k]['weight'] = $weight ? round($weight / 1000, 2) : 0;
- }
- return $list;
- }
- public function workExport() {
- $cur = date('Y-m-d');
- $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
- $end = input('end', date('Y-m-d'));
- $start1 = $start . ' 00:00:00';
- $end1 = $end . ' 23:59:59';
- $type = input('type');
- $ret = $this->workData($start1, $end1, $type);
- include_once env('root_path') . '/extend/phpexcel/Classes/PHPExcel.php';
- //实例化PHPExcel类
- $objPHPExcel = new \PHPExcel();
- //激活当前的sheet表
- $objPHPExcel->setActiveSheetIndex(0);
- //设置表格头(即excel表格的第一行)
- $objPHPExcel->setActiveSheetIndex(0)
- ->setCellValue('A1', '编号')
- ->setCellValue('B1', '姓名')
- ->setCellValue('C1', '单数')
- ->setCellValue('D1', '重量(kg)');
- // 设置表格头水平居中
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //设置列水平居中
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
- ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //设置单元格宽度
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(20);
- $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(20);
- //循环刚取出来的数组,将数据逐一添加到excel表格。
- for ($i = 0; $i < count($ret); $i++) {
- $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $ret[$i]['user_id']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $ret[$i]['title']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $ret[$i]['count']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $ret[$i]['weight']);
- }
- //设置保存的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 waste_year() {
- $org_id = $this->orgId;
- $year = date('Y');
- $qyear = $year - 1;
- $arr1 = [$year . '01', $year . '02', $year . '03', $year . '04', $year . '05', $year . '06', $year . '07', $year . '08', $year . '09', $year . '10', $year . '11', $year . '12',];
- $arr2 = [$qyear . '01', $qyear . '02', $qyear . '03', $qyear . '04', $qyear . '05', $qyear . '06', $qyear . '07', $qyear . '08', $qyear . '09', $qyear . '10', $qyear . '11', $qyear . '12',];
- $arrs = array_merge($arr2, $arr1);
- $lists = [];
- foreach ($arrs as $k => $v) {
- $weight = Db::name('waste_record')
- ->where('org_id', $org_id)
- ->where('del', 0)
- ->where('create_yyyymm', $v)
- ->sum('weight');
- $lists[] = [
- 'month' => $v,
- 'month_text' => intval(date('m', strtotime($v . '01'))) . '月',
- 'weight' => $weight && !empty($weight) ? round($weight / 1000, 2) : 0
- ];
- }
- $warr = $warr1 = $warr2 = [];
- $tmonths = [];
- $hb = [];
- // 计算环比
- foreach ($lists as $k => $v) {
- if ($k == 0) {
- $lists[$k]['hb'] = 0;
- }
- else {
- if ($lists[$k - 1]['weight'] > 0) {
- $lists[$k]['hb'] = round(($v['weight'] - $lists[$k - 1]['weight']) / $lists[$k - 1]['weight'], 3) * 100;
- }
- else {
- $lists[$k]['hb'] = 0;
- }
- }
- if ($k >= 12) {
- $warr1[] = $v['weight'];
- }
- else {
- $warr2[] = $v['weight'];
- }
- $warr[] = $v['weight'];
- $tmonths[] = date('Y-m', strtotime($v['month'] . '01'));
- $hb[] = $lists[$k]['hb'];
- }
- //计算同比
- $tbarr = [];
- foreach ($arr1 as $k => $v) {
- $v1 = $lists[$k]['weight'];
- $v2 = $lists[$k + 12]['weight'];
- if ($v1 > 0) {
- $tbarr[] = round(($v2 - $v1) / $v1, 3) * 100;
- }
- else {
- $tbarr[] = 0;
- }
- }
- $data = [
- 'lists' => $lists,
- 'tb' => $tbarr,
- 'months' => ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
- 'year' => $year,
- 'qyear' => $qyear,
- 'warr' => $warr,
- 'warr1' => $warr1,
- 'warr2' => $warr2,
- 'tmonths' => $tmonths,
- 'hb' => $hb
- ];
- $this->assign('data', $data);
- return $this->fetch();
- }
- //医疗报表统计
- public function view(){
- $month = input('month',date('Y-m'));
- $dep_id = input('dep_id','');
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $address =(new \app\common\model\Address())->getListByType(7,$this->orgId);
- $dep = (new \app\common\model\Dep())->getList($this->orgId);
- $typeList = Db::name('waste_type')
- ->where('enable', 1)
- ->where('del', 0)
- // ->where('org_id', $this->orgId)
- ->select();
- $return = $this->viewData($month,$dep_id,$addr_id,$type_id);
- $this->assign('data',$return);
- $this->assign('address',$address);
- $this->assign('dep',$dep);
- $this->assign('typeList',$typeList);
- $this->assign('month',$month);
- $this->assign('dep_id',$dep_id);
- $this->assign('addr_id',$addr_id);
- $this->assign('type_id',$type_id);
- return $this->fetch();
- }
- public function viewData($month,$dep_id,$addr_id,$type_id){
- $map[] = ['org_id','=',$this->orgId];
- $typeMap = [];
- if($type_id !=''){
- $typeMap[] = ['id','=',$type_id];
- }
- $typeList = Db::name('waste_type')
- ->where('enable', 1)
- ->where('del', 0)
- ->where($typeMap)
- // ->where('org_id', $this->orgId)
- ->select();
- $recordMap = [];
- $depWhere = [];
- if($dep_id !=''){
- $depWhere[] = ['id','=',$dep_id];
- $map[] = ['del','=',0];
- $map[] = ['enable','=',1];
- $map[] = ['dep_id','=',$dep_id];
- $map[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
- $address = Db::name('address')
- ->where($map)
- ->order('id desc')
- ->field('id,title,remark,sn,x,y')
- ->select();
- if($address){
- $recordMap[] = ['waste_device_id','in',array_column($address,'id')];
- }else{
- $recordMap[] = ['waste_device_id','=',0];
- }
- }
- if($addr_id!=''){
- $recordMap[] = ['waste_device_id','=',$addr_id];
- }
- $name1 = [];
- $name2 = [];
- $name3 = [];
- $data1 = [];
- $data2 = [];
- $data3 = [];
- $data4 = [];
- $day = getDay(strtotime($month));
- foreach ($day as $k1=>$v){
- $name2[] = date('d',strtotime($v));
- }
- $dep = Db::name('dep')
- ->where('del',0)
- ->where('org_id',$this->orgId)
- ->where('enable',1)
- ->where($depWhere)
- ->select();
- $depList = [];
- foreach ($dep as $k=>$v){
- $dep_map[] = ['del','=',0];
- $dep_map[] = ['enable','=',1];
- $dep_map[] = ['dep_id','=',$v['id']];
- $dep_map[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
- $address = Db::name('address')
- ->where($dep_map)
- ->order('id desc')
- ->field('id,title,remark,sn,x,y')
- ->select();
- if($address){
- $depList[] = [
- 'title'=>$v['title'],
- 'ids'=>array_column($address,'id')
- ];
- }
- }
- foreach ($depList as $k2=>$v2){
- $map = [];
- if($month){
- $map[] =['create_yyyymm','=',date('Ym',strtotime($month))];
- }
- $nums1 = Db::name('waste_record')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->where($recordMap)
- ->where($map)
- ->where('waste_device_id','in',$v2['ids'])
- ->sum('weight');
- $depList[$k2]['total'] = $nums1?round($nums1/1000,2):0;
- }
- $depList = $depList?list_sort_by($depList,'total','desc'):[];
- $name3[] = $depList?array_column($depList,'title'):[];
- foreach ($typeList as $k=>$v){
- $map = [];
- if($month){
- $map[] =['create_yyyymm','=',date('Ym',strtotime($month))];
- }
- $num = Db::name('waste_record')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->where('cateid',$v['id'])
- ->where($recordMap)
- ->where($map)
- ->sum('weight');
- $total = $num?round($num/1000,2):0;
- $name1[] = $v['title'];
- $data1[] = $total;
- $data2[] =['value'=>$total,'name'=>$v['title']];
- $numList = [];
- $numList1 = [];
- foreach ($day as $k1=>$v1){
- $nums = Db::name('waste_record')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->where('cateid',$v['id'])
- ->where($recordMap)
- ->where($map)
- ->where('create_yyyymmdd',$v1)
- ->sum('weight');
- $totals = $nums?round($nums/1000,2):0;
- $numList[] = $totals;
- }
- foreach ($depList as $k2=>$v2){
- if($k2<=9){
- $nums1 = Db::name('waste_record')
- ->where('org_id',$this->orgId)
- ->where('del',0)
- ->where('cateid',$v['id'])
- ->where($map)
- ->where('waste_device_id','in',$v2['ids'])
- ->sum('weight');
- $totals1 = $nums1?round($nums1/1000,2):0;
- $numList1[] = $totals1;
- }
- }
- $data3[] = [
- 'name'=>$v['title'],
- 'type'=>'line',
- 'stack'=>'stack',
- 'data'=>$numList
- ];
- $data4[] = [
- 'name'=>$v['title'],
- 'type'=>'line',
- 'stack'=>'stack',
- 'data'=>$numList1
- ];
- }
- $return = [
- 'name1'=>$name1,
- 'name2'=>$name2,
- 'name3'=>$name3,
- 'data1'=>$data1,
- 'data2'=>$data2,
- 'data3'=>$data3,
- 'data4'=>$data4,
- ];
- return $return;
- }
- public function viewExportData($month,$dep_id,$addr_id,$type_id){
- $tMap = [];
- $tMap[] = ['enable','=',1];
- $tMap[] = ['del','=',0];
- //$tMap[] = ['org_id','=',$this->orgId];
- if($type_id!=''){
- $tMap[] = ['id','=',$type_id];
- }
- $lists = [];
- $type = Db::name('waste_type')->where($tMap)->select();
- $type = $type?$type:[];
- $first = [
- 'name' => '类型',
- ];
- $second = [
- 'name' => '重量',
- ];
- $third = [
- 'name' => '比例',
- ];
- $fourth = [
- 'name' => '',
- ];
- $five = [
- 'name' => '按天统计',
- ];
- $total = 0;
- $count = 1;
- foreach ($type as $k=>$v){
- $count++;
- $map = [];
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$this->orgId];
- $map[] = ['status','in',[0,1,2]];
- if($type_id){
- $map[] = ['cateid','=',$type_id];
- }
- if($addr_id){
- $map[] = ['waste_device_id','=',$addr_id];
- }
- if($dep_id){
- $addrs = Db::name('address')->where('dep_id',$dep_id)->where('enable',1)->where('del',0)->column('id');
- if($addrs){
- $map[] = ['waste_device_id','in',$addr_id];
- }else{
- $map[] = ['waste_device_id','=',-1];
- }
- }
- if($month){
- $map[] = ['create_yyyymm','=',str_ireplace('-','',$month)];
- }
- $weight = Db::name('waste_record')->where($map)->sum('weight');
- $total += $weight;
- $first['type'.$v['id']] = $v['title'];
- $second['type'.$v['id']] = round($weight/1000,2);
- $third['type'.$v['id']] = $weight;
- $fourth['type'.$v['id']] = '';
- $five['type'.$v['id']] = '';
- }
- foreach ($type as $k=>$v){
- $third['type'.$v['id']] = $total>0?round($third['type'.$v['id']]/$total,2):0;
- }
- $lists[] = $first;
- $lists[] = $second;
- $lists[] = $third;
- $lists[] = $fourth;
- $lists[] = $five;
- $days = getDay(strtotime($month.'-01'));
- foreach ($days as $kk=>$vv){
- $arr = [
- 'name' => date('Y-m-d',strtotime($vv))
- ];
- foreach ($type as $k=>$v){
- $map = [];
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$this->orgId];
- $map[] = ['status','in',[0,1,2]];
- if($type_id){
- $map[] = ['cateid','=',$type_id];
- }
- if($addr_id){
- $map[] = ['waste_device_id','=',$addr_id];
- }
- if($dep_id){
- $addrs = Db::name('address')->where('dep_id',$dep_id)->where('enable',1)->where('del',0)->column('id');
- if($addrs){
- $map[] = ['waste_device_id','in',$addr_id];
- }else{
- $map[] = ['waste_device_id','=',-1];
- }
- }
- $map[] = ['create_yyyymmdd','=',$vv];
- $weight = Db::name('waste_record')->where($map)->sum('weight');
- $arr['type'.$v['id']] = round($weight/1000,2);
- }
- $lists[] = $arr;
- }
- $arr2 = [
- 'name' => '',
- ];
- $arr3 = [
- 'name' => '按科室统计',
- ];
- foreach ($type as $k=>$v){
- $arr2['type'.$v['id']] = '';
- $arr3['type'.$v['id']] = '';
- }
- $lists[] = $arr2;
- $lists[] = $arr3;
- $m[] = ['org_id','=',$this->orgId];
- $m[] = ['enable','=',1];
- $m[] = ['del','=',0];
- if($dep_id > 0){
- $m[] = ['id','=',$dep_id];
- }
- $deps = Db::name('dep')->where($m)->select();
- $deps = $deps?$deps:[];
- foreach ($deps as $kk=>$vv){
- $arr = [
- 'name' => $vv['title'],
- ];
- foreach ($type as $k=>$v){
- $map = [];
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$this->orgId];
- $map[] = ['status','in',[0,1,2]];
- if($type_id){
- $map[] = ['cateid','=',$type_id];
- }
- if($addr_id){
- $map[] = ['waste_device_id','=',$addr_id];
- }
- if($vv['id']){
- $addrs = Db::name('address')->where('dep_id',$dep_id)->where('enable',1)->where('del',0)->column('id');
- if($addrs){
- $map[] = ['waste_device_id','in',$addr_id];
- }else{
- $map[] = ['waste_device_id','=',-1];
- }
- }
- $map[] = ['create_yyyymm','=',$month];
- $weight = Db::name('waste_record')->where($map)->sum('weight');
- $arr['type'.$v['id']] = round($weight/1000,2);
- }
- $lists[] = $arr;
- }
- $address = '地点:全部';
- if($addr_id){
- $address = '地点:'.Db::name('address')->where('id',$addr_id)->value('title');
- }
- $dep = '科室:全部';
- if($dep_id){
- $dep = '科室:'.Db::name('dep')->where('id',$dep_id)->value('title');
- }
- $typestr = '医废类型:全部';
- if($type_id){
- $typestr = '医废类型:'.Db::name('waste_type')->where('id',$type_id)->value('title');
- }
- $monthstr = "月份:".$month;
- $data = [
- 'count' => $count,
- 'address' => $address,
- 'dep' => $dep,
- 'type' => $typestr,
- 'types' => $type,
- 'monthstr' => $monthstr,
- 'lists' => $lists
- ];
- return $data;
- }
- public function viewExport(){
- $month = input('month',date('Y-m'));
- $dep_id = input('dep_id','');
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $data = $this->viewExportData($month,$dep_id,$addr_id,$type_id);
- $lists = $data['lists'];
- $types = $data['types'];
- $char = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
- $lins = [];
- // 暂时支持2层
- for( $i=0;$i<$data['count'];$i++ ){
- $r1 = $i%26;
- $r2 = floor($i/26);
- if($r2 == 0){
- $lins[] = $char[$r1];
- }else if($r2 == 1){
- $lins[] = $char[$r2-1].$char[$r1];
- }else if($r2 == 2){
- $lins[] = $char[$r2-1].$char[$r1];
- }else if($r2 == 3){
- $lins[] = $char[$r2-1].$char[$r1];
- }
- }
- $spreadsheet = new Spreadsheet();
- $worksheet = $spreadsheet->getActiveSheet();
- //设置工作表标题名称
- $worksheet->setTitle('医疗废弃物报表');
- foreach ($lins as $k=>$v){
- $spreadsheet->setActiveSheetIndex(0)->getStyle($v)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
- $spreadsheet->setActiveSheetIndex(0)->getColumnDimension($v)->setAutoSize(true);
- }
- //表头
- //设置单元格内容
- $worksheet->setCellValueByColumnAndRow(1, 1, $data['address']);
- $worksheet->setCellValueByColumnAndRow(2, 1, $data['dep']);
- $worksheet->setCellValueByColumnAndRow(3, 1, $data['type']);
- $worksheet->setCellValueByColumnAndRow(4, 1, $data['monthstr']);
- foreach ($lists as $k=>$v){
- $worksheet->setCellValueByColumnAndRow(1, $k+2, $v['name']);
- foreach ($types as $kk=>$vv){
- $worksheet->setCellValueByColumnAndRow($kk+2, $k+2, $v['type'.$vv['id']]);
- }
- }
- $filename = '医疗废弃物报表'.$month.'.xlsx';
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- header('Content-Disposition: attachment;filename="'.$filename.'"');
- header('Cache-Control: max-age=0');
- $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
- $writer->save('php://output');
- }
- // 医废日统计
- public function wasteDay() {
- $month = input('start',date('Y-m-d'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- //$tMap[] = ['org_id','=',$this->orgId];
- $typeList1 = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title')
- ->select();
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteDataDay($month,$addr_id,$type_id);
- $this->assign('data',$data['list']);
- $this->assign('typeList',$typeList);
- $this->assign('typeList1',$typeList1);
- $this->assign('addressList',$addressList);
- $this->assign('start',$month);
- $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
- $this->assign('type_id',$type_id?explode(',',$type_id):[]);
- return $this->fetch();
- }
- public function wasteDataDay($month,$addr_id,$type_id){
- $org_id = $this->orgId;
- $aMap = [];
- if($addr_id!=''){
- $aMap[] = ['id','in',explode(',',$addr_id)];
- }
- $aMap[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
- $dep = Db::name('address')
- ->where('org_id',$org_id)
- ->where('enable',1)
- ->where('del',0)
- ->where($aMap)
- ->select();
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- $type = Db::name('waste_type')
- ->where($tMap)
- ->where('enable',1)
- ->where('del',0)
- ->select();
- $date = date('Ymd',strtotime($month));
- $tnum = $tkg = 0;
- $records = Db::name('waste_record')
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where('create_yyyymmdd',$date)
- ->field('waste_device_id,cateid,weight,num')
- ->select();
- foreach ($dep as $k=>$v){
- $ttkg = $ttnums = 0;
- foreach ($type as $k1=>$v1){
- $datas = 0;
- $totals = 0;
- $weight = 0;
- $weight2 = 0;
- $ttnums = 0;
- $totalkg = 0;
- $num = '';
- $totalNum = 0;
- $num2 = '';
- $totalNum2 = 0;
- foreach ($records as $rk=>$rv){
- if($rv['waste_device_id'] == $v['id'] && $rv['cateid'] == $v1['id']){
- $datas++;
- $weight += $rv['weight'];
- // $weight2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum += $rv['num'];
- }
- }
- if($rv['cateid'] == $v1['id']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $totals++;
- $weight2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }else{
- $totals++;
- $weight2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }
- if($rv['waste_device_id'] == $v['id']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $ttnums++;
- $totalkg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }else{
- $ttnums++;
- $totalkg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }
- }
- $dep[$k]['data'][] = $datas;
- $dep[$k]['total'][] = $totals;
- // $dep[$k]['data'][] = $weight?round($weight/1000,2):0;
- // $dep[$k]['total'][] = $weight2?round($weight2/1000,2):0;
- $dep[$k]['nums'] = $ttnums;
- $ttkg = $totalkg?round($totalkg/1000,3):0;
- $dep[$k]['totalkg'] = $ttkg;
- $we = $weight?round($weight/1000,3):0;
- if($totalNum > 0){
- $num = '('.$totalNum.$v1['unit'].')';
- }
- $dep[$k]['data'][] = $we.$num;
- $we2 = $weight2?round($weight2/1000,3):0;
- if($totalNum2 > 0){
- $num2 = '('.$totalNum2.$v1['unit'].')';
- }
- $dep[$k]['total'][] =$we2.$num2;
- // $dep[$k]['total'][] = $we2 > 0 ? $we2 : $totalNum2.$v1['unit'];
- }
- $tnum += $ttnums;
- $tkg += $ttkg;
- }
- $data = [
- 'list'=>$dep,
- 'tnum'=>$tnum,
- 'tkg'=>$tkg,
- ];
- return $data;
- }
- public function wasteDataDayOld($month,$addr_id,$type_id){
- $org_id = $this->orgId;
- $aMap = [];
- if($addr_id!=''){
- $aMap[] = ['id','in',explode(',',$addr_id)];
- }
- $aMap[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
- $dep = Db::name('address')
- ->where('org_id',$org_id)
- ->where('enable',1)
- ->where('del',0)
- ->where($aMap)
- ->select();
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- $type = Db::name('waste_type')
- ->where('enable',1)
- ->where('del',0)
- //->where('org_id',$this->orgId)
- ->where($tMap)
- ->select();
- $date = date('Ymd',strtotime($month));
- foreach ($dep as $k=>$v){
- foreach ($type as $k1=>$v1){
- $map = [];
- $map[] = ['CREATE_YYYYMMDD','=',$date];
- $row = Db::name('waste_record')
- ->where('waste_device_id',$v['id'])
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->count();
- $dep[$k]['data'][] = $row;
- $map1 = [];
- if($addr_id!=''){
- $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
- }
- $dep[$k]['total'][] = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->where($map1)
- ->count();
- $kg = Db::name('waste_record')
- ->where('waste_device_id',$v['id'])
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->sum('weight');
- $we = $kg?round($kg/1000,2):0;
- $dep[$k]['data'][] = $we;
- $kg = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->where($map1)
- ->sum('weight');
- $we = $kg?round($kg/1000,2):0;
- $dep[$k]['total'][] =$we;
- }
- }
- return $dep;
- }
- //医废日统计导出
- public function wasteDayExport()
- {
- set_time_limit(0);
- ini_set("memory_limit", "1024M");
- $month = input('start',date('Y-m-d'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $data = $this->wasteDataDay($month,$addr_id,$type_id);
- $data = $data['list'];
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- //$tMap[] = ['org_id','=',$this->orgId];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $type = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $type = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $title = '医废日统计';
- if(!empty($month)){
- $title = $month.'医废日统计';
- }
- header("Content-type: application/vnd.ms-excel");
- header("Content-Type: application/force-download");
- header("Content-Disposition: attachment; filename=".$title.".xls");
- header('Expires:0');
- header('Pragma:public');
- $res = '';
- $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
- $res.='<tr style="background: #ffffff;">
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">地点</th>';
- foreach ($type as $k=>$v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
- }
- $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
- $res.='<tr style="background: #ffffff;">';
- foreach ($type as $v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
- }
- foreach ($data as $k=>$v){
- $res.='<tr style="background: #ffffff;">';
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['title'].'</td>';
- foreach ($v['data'] as $k1=>$v1){
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
- $res.='</tr>';
- }
- $res.='<tr style="background: #ffffff;">
- <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
- if(!empty($data)){
- foreach ($data[0]['total'] as $k1=>$v1){
- $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- }
- $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
- </tr>';
- echo $res;
- }
- // 医废月统计
- public function wasteMonth() {
- $month = input('start',date('Y-m'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- // $tMap[] = ['org_id','=',$this->orgId];
- $typeList1 = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title')
- ->select();
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteMonthData($month,$addr_id,$type_id);
-
- $this->assign('data',$data['res']);
- $this->assign('totalData',$data['total']);
- $this->assign('typeList',$typeList);
- $this->assign('typeList1',$typeList1);
- $this->assign('addressList',$addressList);
- $this->assign('start',$month);
- $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
- $this->assign('type_id',$type_id?explode(',',$type_id):[]);
- return $this->fetch();
- }
- public function wasteMonthData($month,$addr_id,$type_id){
- $org_id = $this->orgId;
- $dep = getDay(strtotime($month));
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- // $type = Db::name('waste_type')
- // ->where('enable',1)
- // ->where('del',0)
- // ->where($tMap)
- // ->select();
- $type = Db::name('waste_type')
- ->where($tMap)
- ->where('enable',1)
- ->where('del',0)
- ->select();
- $newTypeIds = array_column($type,'id');
- $res = [];
- $total = [];
- $records = Db::name('waste_record')
- ->alias('wr')
- ->join('address a','a.id=wr.waste_device_id')
- ->join('waste_type wt','wt.id=wr.cateid')
- ->where('wr.org_id',$org_id)
- ->where('wr.del',0)
- ->where('wr.cateid','in',$newTypeIds)
- ->where('wr.create_yyyymm',date('Ym',strtotime($month)))
- ->field('wr.waste_device_id,wr.cateid,wr.weight,wr.create_yyyymmdd,wr.num')
- ->select();
- $tnum = $tkg = 0;
- foreach ($dep as $k=>$v){
- $ttkg = $ttnums = 0;
- foreach ($type as $k1=>$v1){
- $row = 0;
- $row2 = 0;
- $kg = 0;
- $kg2 = 0;
- $num = '';
- $totalNum = 0;
- $num2 = '';
- $totalNum2 = 0;
- $ttnums = 0;
- $totalkg = 0;
- foreach ($records as $rk=>$rv){
- if($rv['cateid'] == $v1['id'] && $rv['create_yyyymmdd'] == $v){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $row++;
- $kg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum += $rv['num'];
- }
- }
- }else{
- $row++;
- $kg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum += $rv['num'];
- }
- }
- }
- if($rv['create_yyyymmdd'] == $v){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $ttnums +=1;
- $totalkg += $rv['weight'];
- }
- }else{
- $ttnums +=1;
- $totalkg += $rv['weight'];
- }
- }
- if($rv['cateid'] == $v1['id']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $row2++;
- $kg2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }else{
- $row2++;
- $kg2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }
- }
- $res[$k]['list'][] = $row;
- $total[$k][] = $row2;
- $res[$k]['nums'] = $ttnums;
- $ttkg = $totalkg?round($totalkg/1000,3):0;
- $res[$k]['totalkg'] = $ttkg;
- $we = $kg?round($kg/1000,2):0;
- // $res[$k]['list'][] = $we > 0 ? $we : $totalNum.$v1['unit'];
- $we2 = $kg2?round($kg2/1000,2):0;
- if($totalNum > 0){
- $num = '('.$totalNum.$v1['unit'].')';
- }
- $res[$k]['list'][] = $we.$num;
- if($totalNum2 > 0){
- $num2 = '('.$totalNum2.$v1['unit'].')';
- }
- $total[$k][] =$we2.$num2;
- // $total[$k][] = $we2 > 0 ? $we2 : $totalNum2.$v1['unit'];
- // $we = $kg?round($kg/1000,2):0;
- // $res[$k][] = $we;
- //
- // $we = $kg2?round($kg2/1000,2):0;
- // $total[$k][] =$we;
- }
- $tnum += $ttnums;
- $tkg += $ttkg;
- }
- return ['res'=>$res,'total'=>$total,'tnum'=>$tnum,'tkg'=>$tkg];
- }
- public function wasteMonthDataOld($month,$addr_id,$type_id){
- $org_id = $this->orgId;
- $dep = getDay(strtotime($month));
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- $type = Db::name('waste_type')
- ->where('enable',1)
- ->where('del',0)
- // ->where('org_id',$this->orgId)
- ->where($tMap)
- ->select();
- $res = [];
- $total = [];
- foreach ($dep as $k=>$v){
- foreach ($type as $k1=>$v1){
- $map = [];
- $map[] = ['CREATE_YYYYMMDD','=',$v];
- $map1 = [];
- if($addr_id!=''){
- $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
- }
- $row = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->where($map1)
- ->count();
- $res[$k][] = $row;
- $map2 = [];
- $map2[] = ['CREATE_YYYYMM','=',date('Ym',strtotime($v))];
- $row = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map1)
- ->where($map2)
- ->count();
- $total[$k][] = $row;
- $kg = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->where($map1)
- ->sum('weight');
- $we = $kg?round($kg/1000,2):0;
- $res[$k][] = $we;
- $kg = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map1)
- ->where($map2)
- ->sum('weight');
- $we = $kg?round($kg/1000,2):0;
- $total[$k][] =$we;
- }
- }
- return ['res'=>$res,'total'=>$total];
- }
- public function wasteMonthExport(){
- $month = input('start',date('Y-m'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- set_time_limit(0);
- ini_set("memory_limit", "1024M");
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- // $tMap[] = ['org_id','=',$this->orgId];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteMonthData($month,$addr_id,$type_id);
- $lists = $data['res'];
- $title = '医废月统计';
- if(!empty($month)){
- $title = $month.'医废月统计';
- }
- header("Content-type: application/vnd.ms-excel");
- header("Content-Type: application/force-download");
- header("Content-Disposition: attachment; filename=".$title.".xls");
- header('Expires:0');
- header('Pragma:public');
- $res = '';
- $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
- $res.='<tr style="background: #ffffff;">
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">日期</th>';
- foreach ($typeList as $k=>$v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
- }
- $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
- $res.='<tr style="background: #ffffff;">';
- foreach ($typeList as $v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
- }
- if(isset($lists[0]['list'])){
- foreach ($lists as $k=>$v){
- $res.='<tr style="background: #ffffff;">';
- $kk = $k+1;
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$kk.'</td>';
- if(isset($v['list'])){
- foreach ($v['list'] as $k1=>$v1){
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- }
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
- $res.='</tr>';
- }
- }
- $res.='<tr style="background: #ffffff;">
- <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
- if(!empty($data['total'])){
- foreach ($data['total'][0] as $k1=>$v1){
- $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- }
- $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
- </tr>';
- echo $res;
- }
- //科室月统计
- public function wasteDeviceMonth() {
- $month = input('start',date('Y-m'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- //$tMap[] = ['org_id','=',$this->orgId];
- $typeList1 = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title')
- ->select();
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteDeviceMonthData($month,$addr_id,$type_id);
- $this->assign('data',$data['list']);
- $this->assign('typeList',$typeList);
- $this->assign('typeList1',$typeList1);
- $this->assign('addressList',$addressList);
- $this->assign('start',$month);
- $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
- $this->assign('type_id',$type_id?explode(',',$type_id):[]);
- return $this->fetch();
- }
- public function wasteDeviceMonthData($month,$addr_id,$type_id){
- $org_id = $this->orgId;
- $aMap = [];
- if($addr_id!=''){
- $aMap[] = ['id','in',explode(',',$addr_id)];
- }
- $aMap[]=['','exp',Db::raw("FIND_IN_SET(7,types)")];
- $dep = Db::name('address')
- ->where('org_id',$org_id)
- ->where('enable',1)
- ->where('del',0)
- ->where($aMap)
- ->select();
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- $type = Db::name('waste_type')
- ->where($tMap)
- ->where('enable',1)
- ->where('del',0)
- ->select();
- $date = date('Ym',strtotime($month));
- $tnum = $tkg = 0;
- $records = Db::name('waste_record')
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where('create_yyyymm',$date)
- ->field('waste_device_id,cateid,weight,num')
- ->select();
- foreach ($dep as $k=>$v){
- $ttkg = $ttnums = 0;
- foreach ($type as $k1=>$v1){
- $datas = 0;
- $totals = 0;
- $weight = 0;
- $weight2 = 0;
- $ttnums = 0;
- $totalkg = 0;
- $num = '';
- $totalNum = 0;
- $num2 = '';
- $totalNum2 = 0;
- foreach ($records as $rk=>$rv){
- if($rv['waste_device_id'] == $v['id'] && $rv['cateid'] == $v1['id']){
- $datas++;
- $weight += $rv['weight'];
- // $weight2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum += $rv['num'];
- }
- }
- if($rv['cateid'] == $v1['id']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $totals++;
- $weight2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }else{
- $totals++;
- $weight2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }
- if($rv['waste_device_id'] == $v['id']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $ttnums++;
- $totalkg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }else{
- $ttnums++;
- $totalkg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }
- }
- $dep[$k]['data'][] = $datas;
- $dep[$k]['total'][] = $totals;
- // $dep[$k]['data'][] = $weight?round($weight/1000,2):0;
- // $dep[$k]['total'][] = $weight2?round($weight2/1000,2):0;
- $dep[$k]['nums'] = $ttnums;
- $ttkg = $totalkg?round($totalkg/1000,3):0;
- $dep[$k]['totalkg'] = $ttkg;
- $we = $weight?round($weight/1000,3):0;
- if($totalNum > 0){
- $num = '('.$totalNum.$v1['unit'].')';
- }
- $dep[$k]['data'][] = $we.$num;
- $we2 = $weight2?round($weight2/1000,3):0;
- if($totalNum2 > 0){
- $num2 = '('.$totalNum2.$v1['unit'].')';
- }
- $dep[$k]['total'][] =$we2.$num2;
- // $dep[$k]['total'][] = $we2 > 0 ? $we2 : $totalNum2.$v1['unit'];
- }
- $tnum += $ttnums;
- $tkg += $ttkg;
- }
- $data = [
- 'list'=>$dep,
- 'tnum'=>$tnum,
- 'tkg'=>$tkg,
- ];
- return $data;
- }
-
- public function wasteDeviceMonthExport()
- {
- set_time_limit(0);
- ini_set("memory_limit", "1024M");
- $month = input('start',date('Y-m-d'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $data = $this->wasteDeviceMonthData($month,$addr_id,$type_id);
- $data = $data['list'];
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $type = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $type = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $title = '科室月统计';
- if(!empty($month)){
- $title = $month.'科室月统计';
- }
- header("Content-type: application/vnd.ms-excel");
- header("Content-Type: application/force-download");
- header("Content-Disposition: attachment; filename=".$title.".xls");
- header('Expires:0');
- header('Pragma:public');
- $res = '';
- $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
- $res.='<tr style="background: #ffffff;">
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">地点</th>';
- foreach ($type as $k=>$v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
- }
- $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
- $res.='<tr style="background: #ffffff;">';
- foreach ($type as $v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
- }
- foreach ($data as $k=>$v){
- $res.='<tr style="background: #ffffff;">';
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['title'].'</td>';
- foreach ($v['data'] as $k1=>$v1){
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
- $res.='</tr>';
- }
- $res.='<tr style="background: #ffffff;">
- <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
- if(!empty($data)){
- foreach ($data[0]['total'] as $k1=>$v1){
- $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- }
- $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
- </tr>';
- echo $res;
- }
-
- // 医废年统计
- public function wasteYears() {
- $month = input('start',date('Y'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- //$tMap[] = ['org_id','=',$this->orgId];
- $typeList1 = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title')
- ->select();
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteYearsData($month,$addr_id,$type_id);
- $this->assign('data',$data['list']);
- $this->assign('typeList',$typeList);
- $this->assign('typeList1',$typeList1);
- $this->assign('addressList',$addressList);
- $this->assign('start',$month);
- $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
- $this->assign('type_id',$type_id?explode(',',$type_id):[]);
- return $this->fetch();
- }
- public function wasteYearsData($month,$addr_id,$type_id){
- $org_id = $this->orgId;
- $dep = get_month1($month);
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- $type = Db::name('waste_type')
- ->where($tMap)
- ->where('enable',1)
- ->where('del',0)
- ->select();
- $records = Db::name('waste_record')
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where('create_yyyy',$month)
- ->field('waste_device_id,cateid,weight,create_yyyymm,num')
- ->select();
- $tnum = $tkg = 0;
- foreach ($dep as $k=>$v){
- $ttkg = $ttnums = 0;
- foreach ($type as $k1=>$v1){
- $row = 0;
- $row2 = 0;
- $kg = 0;
- $kg2 = 0;
- $num = '';
- $totalNum = 0;
- $num2 = '';
- $totalNum2 = 0;
- $ttnums = 0;
- $totalkg = 0;
- foreach ($records as $rk=>$rv){
- if($rv['cateid'] == $v1['id'] && $rv['create_yyyymm'] == $v['date']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $row++;
- $kg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum += $rv['num'];
- }
- }
- }else{
- $row++;
- $kg += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum += $rv['num'];
- }
- }
- }
- if($rv['create_yyyymm'] == $v['date']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $ttnums++;
- $totalkg += $rv['weight'];
- }
- }else{
- $ttnums++;
- $totalkg += $rv['weight'];
- }
- }
- if($rv['cateid'] == $v1['id']){
- if($addr_id!=''){
- $addr_ids = explode(',',$addr_id);
- if(in_array($rv['waste_device_id'],$addr_ids)){
- $row2++;
- $kg2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }else{
- $row2++;
- $kg2 += $rv['weight'];
- if($v1['type'] == 1){
- $totalNum2 += $rv['num'];
- }
- }
- }
- }
- $dep[$k]['list'][] = $row;
- $dep[$k]['total'][] = $row2;
- $dep[$k]['nums'] = $ttnums;
- $ttkg = $totalkg?round($totalkg/1000,3):0;
- $dep[$k]['totalkg'] = $ttkg;
- $we = $kg?round($kg/1000,2):0;
- $we2 = $kg2?round($kg2/1000,2):0;
- if($totalNum > 0){
- $num = '('.$totalNum.$v1['unit'].')';
- }
- $dep[$k]['list'][] = $we.$num;
- if($totalNum2 > 0){
- $num2 = '('.$totalNum2.$v1['unit'].')';
- }
- $dep[$k]['total'][] =$we2.$num2;
- }
- $tnum += $ttnums;
- $tkg += $ttkg;
- }
- $data = [
- 'list'=>$dep,
- 'tnum'=>$tnum,
- 'tkg'=>$tkg,
- ];
- return $data;
- }
- public function wasteYearsDataOld($month,$addr_id,$type_id){
- $org_id = $this->orgId;
- $dep = get_month($month);
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- $type = Db::name('waste_type')
- ->where('enable',1)
- ->where('del',0)
- //->where('org_id',$this->orgId)
- ->where($tMap)
- ->select();
- foreach ($dep as $k=>$v){
- foreach ($type as $k1=>$v1){
- $map = [];
- $map[] = ['create_yyyymm','=',$v['date']];
- $map1 = [];
- if($addr_id!=''){
- $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
- }
- $row = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->where($map1)
- ->count();
- $dep[$k]['list'][] = $row;
- $map2 = [];
- $map2[] = ['create_yyyy','=',$month];
- $row = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map1)
- ->where($map2)
- ->count();
- $dep[$k]['total'][] = $row;
- $kg = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map)
- ->where($map1)
- ->sum('weight');
- $we = $kg?round($kg/1000,2):0;
- $dep[$k]['list'][] = $we;
- $kg = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map1)
- ->where($map2)
- ->sum('weight');
- $we = $kg?round($kg/1000,2):0;
- $dep[$k]['total'][] = $we;
- }
- }
- return $dep;
- }
- public function wasteYearsExport() {
- $month = input('start',date('Y'));
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- set_time_limit(0);
- ini_set("memory_limit", "1024M");
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- //$tMap[] = ['org_id','=',$this->orgId];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteYearsData($month,$addr_id,$type_id);
- $list = $data['list'];
- $title = '医废年统计';
- if(!empty($month)){
- $title = $month.'医废年统计';
- }
- header("Content-type: application/vnd.ms-excel");
- header("Content-Type: application/force-download");
- header("Content-Disposition: attachment; filename=".$title.".xls");
- header('Expires:0');
- header('Pragma:public');
- $res = '';
- $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
- $res.='<tr style="background: #ffffff;">
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">日期</th>';
- foreach ($typeList as $k=>$v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
- }
- $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
- $res.='</tr>';
- $res.='<tr style="background: #ffffff;">';
- foreach ($typeList as $v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
- }
- $res.='</tr>';
- if(isset($list[0]['list'])){
- foreach ($list as $k=>$v){
- $res.='<tr style="background: #ffffff;">';
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v['key'].'</td>';
- if(isset($v['list'])){
- foreach ($v['list'] as $k1=>$v1){
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- }
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
- $res.='</tr>';
- }
- }
- $res.='<tr style="background: #ffffff;">
- <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">共计:</td>';
- if(isset($list[1]['total'])){
- foreach ($list[1]['total'] as $k1=>$v1){
- $res.=' <td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v1.'</td>';
- }
- }
- $res.='<td style="border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>
- </tr>';
- echo $res;
- }
- // 医废暂存统计
- public function wasteZc() {
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- $addressList = (new \app\common\model\Address())->getListByType(7,$this->orgId);
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- // $tMap[] = ['org_id','=',$this->orgId];
- $typeList1 = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title')
- ->select();
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteZcData($addr_id,$type_id);
- $this->assign('data',$data);
- $this->assign('typeList',$typeList);
- $this->assign('typeList1',$typeList1);
- $this->assign('addressList',$addressList);
- $this->assign('addr_id',$addr_id?explode(',',$addr_id):[]);
- $this->assign('type_id',$type_id?explode(',',$type_id):[]);
- return $this->fetch();
- }
- public function wasteZcData($addr_id,$type_id){
- $org_id = $this->orgId;
- $tMap = [];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- }
- $type = Db::name('waste_type')
- ->where('enable',1)
- ->where('del',0)
- //->where('org_id',$this->orgId)
- ->where($tMap)
- ->select();
- $res = [];
- foreach ($type as $k1=>$v1){
- $map1 = [];
- if($addr_id!=''){
- $map1[] = ['waste_device_id','in',explode(',',$addr_id)];
- }
- $row = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map1)
- ->where(function ($query)
- {
- $query->where('status', 1)->whereor('status', 2);
- })
- ->count();
- $res[]= $row;
- $kg = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map1)
- ->where(function ($query)
- {
- $query->where('status', 1)->whereor('status', 2);
- })
- ->sum('weight');
- $we = $kg?round($kg/1000,2):0;
- $num = Db::name('waste_record')
- ->where('cateid',$v1['id'])
- ->where('org_id',$org_id)
- ->where('del',0)
- ->where($map1)
- ->where(function ($query)
- {
- $query->where('status', 1)->whereor('status', 2);
- })
- ->sum('num');
- if($num > 0){
- $we = $we.'kg'.'('.$num.$v1['unit'].')';
- }
- $res[]= $we;
- }
- return $res;
- }
- // 医废暂存统计导出
- public function wasteZcExport() {
- $addr_id = input('addr_id','');
- $type_id = input('type_id','');
- set_time_limit(0);
- ini_set("memory_limit", "1024M");
- $tMap[] = ['del','=',0];
- $tMap[] = ['enable','=',1];
- //$tMap[] = ['org_id','=',$this->orgId];
- if($type_id!=''){
- $tMap[] = ['id','in',explode(',',$type_id)];
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }else{
- $typeList = Db::name('waste_type')
- ->where($tMap)
- ->field('id,title,type')
- ->select();
- }
- $data = $this->wasteZcData($addr_id,$type_id);
- $title = '医废暂存统计';
- header("Content-type: application/vnd.ms-excel");
- header("Content-Type: application/force-download");
- header("Content-Disposition: attachment; filename=".$title.".xls");
- header('Expires:0');
- header('Pragma:public');
- $res = '';
- $res.='<table style="border-top: 1px solid #ddd;border-left: 1px solid #ddd;border-spacing: 0;">';
- $res.='<tr style="background: #ffffff;">';
- foreach ($typeList as $k=>$v){
- $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" colspan="2">'.$v['title'].'</th>';
- }
- $res.='<th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd" rowspan="2">备注</th>';
- $res.='</tr>';
- $res.='<tr style="background: #ffffff;">';
- foreach ($typeList as $v){
- $res.=' <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">件数/包</th>
- <th style="text-align: center;background: #ffffff;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">重量kg</th>';
- }
- $res.='</tr>';
- $res.='<tr style="background: #ffffff;">';
- foreach ($data as $k=>$v){
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd">'.$v.'</td>';
- }
- $res.='<td style="text-align: center;border-bottom: 1px solid #ddd;border-right: 1px solid #ddd"></td>';
- $res.='</tr>';
- echo $res;
- }
- }
|