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

//获取管理员

$sql = "select username,purview from  admin_user where role = 'base' and status = '1'";
$key = "get_esfadmin_new_user_".$esfadmin['now_city'];
$user_info = $mc->get($key);
if(!$user_info){
	$user_info = $dbback_sys->GetAll($sql);
	$mc->set($key,$user_info,0,86400);
}

// print_r(unserialize($user_info['purview']))  ;
$admin_user = array();
if(is_array($user_info)){
	foreach($user_info as $val){
		$user_city = unserialize($val['purview']);
		if(array_key_exists($esfadmin['now_city'],$user_city)){
			$admin_user[$val['username']] = $val['username'];
		}
	}
}

//前台查询参数
$search_display = getSelect($admin_user,'search_admin',$search_admin);
// print_r($search_display);die;

//获取上个月日期
$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));

$adminName = $search_admin ? $search_admin : $_SESSION[$web_auth]["username"]; 

$title = "月度质检统计报表(按人员)";
 
//获取个人经纪人审核质检结果 
$sql = "select id,dateline,admin,head_pass,head_no,cert_pass,cert_no,real_pass,real_no  from admin_broker_qa_stat where dateline >='".$begin_date."' and dateline <='".$end_date."' and  admin='".$adminName."'";
$data_info = $dbback->GetAll($sql);
 

 //对数据进行重新整理
if(is_array($data_info) && !empty($data_info)){
	foreach($data_info as $key=>$val){
		$data_info[$key]['head_total'] = $val['head_pass'] + $val['head_no'];
		$data_info[$key]['head_pass_perc'] = $data_info[$key]['head_total'] ? ceil(($val['head_pass']/$data_info[$key]['head_total'])*100) : 0;

		$data_info[$key]['cert_total'] = $val['cert_pass'] + $val['cert_no'];
		$data_info[$key]['cert_pass_perc'] = $data_info[$key]['cert_total'] ? ceil(($val['cert_pass']/$data_info[$key]['cert_total'])*100) : 0;

		$data_info[$key]['real_total'] = $val['real_pass'] + $val['real_no'];
		$data_info[$key]['real_pass_perc'] = $data_info[$key]['real_total'] ? ceil(($val['real_pass']/$data_info[$key]['real_total'])*100) : 0;
	}
}

//下载报表
if($actiontype == 'exportcsv'){
	$data = "日期,头像通过率,经纪人证书通过率,real房证书通过率
	";
	if(is_array($data_info) && !empty($data_info)){
		foreach($data_info as $key=>$val){
			$head_pass_perc = $data_info[$key]['head_pass_perc'] ?  $data_info[$key]['head_pass_perc'].'%' : '--';
			$cert_pass_perc = $data_info[$key]['cert_pass_perc'] ?  $data_info[$key]['cert_pass_perc'].'%' : '--';
			$real_pass_perc = $data_info[$key]['real_pass_perc'] ?  $data_info[$key]['real_pass_perc'].'%' : '--';
			$data .= $val['dateline'].','.$head_pass_perc.','.$cert_pass_perc.','.$real_pass_perc.'
			';
		}
	}

	$mime_type = 'text/x-csv';
	header('Content-Type: ' . $mime_type);
    header("Content-Disposition: attachment; filename=月度质检报表(".$adminName.")".$lastym.".csv");
    $actiontype = '';
    echo $data;
    exit();
}

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