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

//获取管理员

$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);
}
$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,surface_pass,surface_no,sort0_pass,sort0_no,sort1_pass,sort1_no,sort2_pass,sort2_no,remark_pass,remark_no  from admin_house_qa_stat where dateline >='".$begin_date."' and dateline <='".$end_date."' and  admin='".$adminName."'";
 
$data_info = $dbback->GetAll($sql);
 
// print_r($data_info);die;

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

		$data_info[$key]['sort0_total'] = $val['sort0_pass'] + $val['sort0_no'];
		$data_info[$key]['sort0_perc'] = $data_info[$key]['sort0_total'] ? ceil(($val['sort0_pass']/$data_info[$key]['sort0_total'])*100) : 0;

		$data_info[$key]['sort1_total'] = $val['sort1_pass'] + $val['sort1_no'];
		$data_info[$key]['sort1_perc'] = $data_info[$key]['sort1_total'] ? ceil(($val['sort1_pass']/$data_info[$key]['sort1_total'])*100) : 0;

		$data_info[$key]['sort2_total'] = $val['sort2_pass'] + $val['sort2_no'];
		$data_info[$key]['sort2_perc'] = $data_info[$key]['sort2_total'] ? ceil(($val['sort2_pass']/$data_info[$key]['sort2_total'])*100) : 0;

		$data_info[$key]['remark_total'] = $val['remark_pass'] + $val['remark_no'];
		$data_info[$key]['remark_perc'] = $data_info[$key]['remark_total'] ? ceil(($val['remark_pass']/$data_info[$key]['remark_total'])*100) : 0;
	}
}

//下载报表
if($actiontype == 'exportcsv'){
	$data = "日期,封面图通过率,室内图通过率,户型图通过率,外景图通过率,描述通过率
	";
	if(is_array($data_info) && !empty($data_info)){
		foreach($data_info as $key=>$val){
			$surface_perc = $val['surface_perc'] ?  $val['surface_perc'].'%' : '--';
			$sort0_perc = $val['sort0_perc'] ?  $val['sort0_perc'].'%' : '--';
			$sort1_perc = $val['sort1_perc'] ?  $val['sort1_perc'].'%' : '--';
			$sort2_perc = $val['sort2_perc'] ?  $val['sort2_perc'].'%' : '--';
			$remark_perc = $val['remark_perc'] ?  $val['remark_perc'].'%' : '--';
			$data .= $val['dateline'].','.$surface_perc.','.$sort2_perc.','.$sort0_perc.','.$sort1_perc.','.$remark_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("house_statistic/monthly_qa_person"));
include_once(Template("footer"));