<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
include_once($conf_basedir."base.inc.php");
checkAuth('46');	//判断登录和权限
$title = "月度审核报表";

//获取上个月日期
$ym = date("Y-m");
$last_date = getMonthRange($ym);
$begin_date = $last_date[0];
$end_date = $last_date[1];
$lastym = date('Y-m',strtotime($begin_date));
//获取上个月总天数，生成日期
$day_num = substr($end_date,8,2);
$day_arr = array();
for($i=1;$i<=$day_num;$i++){
	$day = $i < 10 ? '0'.$i : $i ;
	$day_arr[] = $lastym.'-'.$day;
} 
 

//获取单月审核记录数
$sql = "select dateline,SUM(head_num) as head_num,SUM(cert_num) as cert_num  from admin_broker_check_opernum_log  where dateline >= '".$begin_date."' and dateline <= '".$end_date."' group by dateline limit 0,31";
$data_info = $dbback->GetAll($sql);
 
// print_r($data_info);die;
//获取单月头像待审核总数
$sql = "select indate, count(*) as head_total from broker_certificate  where indate >= '".$begin_date."' and indate <= '".$end_date."' and kind = 'p_1' group by indate limit 0,31";
$head_info = $dbback->GetAll($sql);
 //print_r($head_info);die;

//获取单月经纪人证书待审核总数
$sql = "select indate, count(*) as cert_total from broker_certificate  where indate >= '".$begin_date."' and indate <= '".$end_date."' and kind = 'p_4_1' group by indate limit 0,31";
$cert_info = $dbback->GetAll($sql);
//print_r($cert_info);die;

//暂只处理头像和经纪人证书
$data_arr = array();
if(is_array($data_info) && !empty($data_info)){
	foreach($data_info as $key=>$val){
		$data_arr['head'][$val['dateline']] = $val['head_num'];
		$data_arr['cert'][$val['dateline']] = $val['cert_num'];

		if(is_array($head_info)){
			foreach($head_info as $val1){
				if($val['dateline'] == $val1['indate']){
					$data_arr['head'][$val['dateline'].'_perc'] = $val1['head_total'] ? ceil(($data_arr['head'][$val['dateline']]/$val1['head_total'])*100) : 0;
					break;
				}
		    }
		}
		if(is_array($cert_info)){
			foreach($cert_info as $val2){
				if($val['dateline'] == $val2['indate']){
					$data_arr['cert'][$val['dateline'].'_perc'] = $val2['cert_total'] ? ceil(($data_arr['cert'][$val['dateline']]/$val2['cert_total'])*100) : 0;
					break;
				}
		    }
		}
	}
	$data_arr['head']['type_name'] = '头像';
	$data_arr['cert']['type_name'] = '经纪人证书';
}
 // print_r($data_arr);die;

 //下载报表
if($actiontype == 'exportcsv'){
	$data = $lastym."月,";
	foreach($day_arr as $key=>$val){ 
		$key = $key + 1;
		$data .= $key.'日,' ;
	} 
    $data = rtrim($data,',');
    $data .= '
	';
	if(is_array($data_arr) && !empty($data_info)){
		foreach($data_arr as $key=>$value){
			$data .= $value['admin'].',';
			foreach($day_arr as $key=>$val){ 
				$data .= $value[$val.'_perc'] ? $value[$val.'_perc'].'%,' : '--,';
			}
			$data = rtrim($data,',');
			$data .= '
			';
		}
	}
	// echo $data;die;
	$mime_type = 'text/x-csv';
	header('Content-Type: ' . $mime_type);
    header("Content-Disposition: attachment; filename=经纪人审核月度质检统计报表".$lastym.".csv");
    $actiontype = '';
    echo $data;
    exit();
}

 //引用模版文件
include_once(Template("header"));
include_once(Template("broker_statistic/monthly_audit"));
include_once(Template("footer"));