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

//获取上个月日期
$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));
 
//前台查询参数
$search_arr = array('surface'=>'封面图','sort2'=>'室内图','sort0'=>'户型图','sort1'=>'外景图','remark'=>'描述');
$search_display = getSelect($search_arr,'search_type',$search_type);

//获取上个月总天数，生成日期
$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;
}

switch($search_type){
	case 'surface' :
		$type_mane = '封面图';
		break;
	case 'sort2' :
		$type_mane = '室内图';
		break;
	case 'sort0' :
		$type_mane = '户型图';
		break;
	case 'sort1' :
		$type_mane = '外景图';
		break;
	default :
		$search_type = 'remark';
	    $type_mane = '描述';
		break;
}

$fields = $search_type.'_pass,'.$search_type.'_no ';

$title = "月度质检统计报表(按类型)";
//获取个人经纪人审核质检结果 
$sql = "select id,dateline,admin, ".$fields." from admin_house_qa_stat where dateline >='".$begin_date."' and dateline <='".$end_date."' ";
$data_info = $dbback->GetAll($sql);
 //对数据进行重新整理
if(is_array($data_info) && !empty($data_info)){
	foreach($data_info as $key=>$val){
		$total = $val[$search_type.'_pass'] + $val[$search_type.'_no'];
		$pass_perc = $total ? ceil(($val[$search_type.'_pass']/$total)*100) : 0;
		$data_info[$val['admin']][$val['dateline']] = $pass_perc;
		$data_info[$val['admin']]['admin'] = $val['admin'];
		unset($data_info[$key]);
	}
}

 //下载报表
if($actiontype == 'exportcsv'){
	$data = $lastym."月,";
	foreach($day_arr as $key=>$val){ 
		$key = $key + 1;
		$data .= $key.'日,' ;
	} 
    $data = rtrim($data,',');
    $data .= '
	';
	if(is_array($data_info) && !empty($data_info)){
		foreach($data_info as $key=>$value){
			$data .= $value['admin'].',';
			foreach($day_arr as $key=>$val){ 
				$data .= $value[$val] ? $value[$val].'%,' : '--,';
			}
			$data = rtrim($data,',');
			$data .= '
			';
		}
	}
	$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_qa_type"));
include_once(Template("footer"));