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

//获取上个月日期
$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));
$title = "单日审核报表";

/*
//job
$now_day = date('d');
if($now_day == 1){
	$flag_arr = file_get_contents('');
	//执行如下脚本
}

//获取上个月日期
$ym = date("Y-m");
$last_date = getMonthRange($ym);
$begin_date = $last_date[0];
$end_date = $last_date[1];
 
//获取单月头像审核数量
$sql = "select indate,account,count(*) as  head_num from admin_broker_check_log  where type = 'p_1' and indate >= '".$begin_date."' and indate <= '".$end_date."' group by indate ,account";
$head_info = $dbback->GetAll($sql);

 // print_r($head_info); 

//echo $sql;die;
//获取单月经纪人证书审核数量
$sql = "select indate,account,count(*) as  cert_num from admin_broker_check_log  where type = 'p_4_1' and indate >= '".$begin_date."' and indate <= '".$end_date."' group by indate,account ";
$cert_info = $dbback->GetAll($sql);
//print_r($cert_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_total = $dbback->GetAll($sql);

//获取单月经纪人证书待审核总数
$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_total = $dbback->GetAll($sql);
 

//获取上个月总天数 
$day_num = substr($end_date,8,2);
$data_arr = array();

//暂时不处理real房和account账号数据
for($i=1;$i<=$day_num;$i++){
	$day = $i < 10 ? '0'.$i : $i ;
	$date = $lastym.'-'.$day;
	if(is_array($head_info)){
		foreach($head_info as $val){
			if($val['indate'] == $date){
				$data_arr[$date][$val['account']]['head_num'] = $val['head_num'];
				$data_arr[$date][$val['account']]['cert_num'] = $val['cert_num'];
			}
		}
	}
	if(is_array($cert_info)){
		foreach($cert_info as $val){
			if($val['indate'] == $date){
				$data_arr[$date][$val['account']]['head_num'] = $val['head_num'];
				$data_arr[$date][$val['account']]['cert_num'] = $val['cert_num'];
			}
		}
	}
}
$sql = "insert into admin_broker_check_opernum_log (dateline,admin,head_num,cert_num) values";
if(is_array($data_arr)){
	foreach($data_arr as $key=>$value){
		if(is_array($value)){
			foreach($value as $k=>$val){
				$flag = 1;
				$val['head_num'] = $val['head_num'] ? $val['head_num'] : 0;
				$val['cert_num'] = $val['cert_num'] ? $val['cert_num'] : 0;
				$sql .= "('".$key."','".$k."','".$val['head_num']."','".$val['cert_num']."'),";
			}
		}
	}

}
if($flag == 1){
	$sql = rtrim($sql,',');
	$tmp = $db->Execute($sql);
}


//删除二个月前的数据
$del_time =  date("Y-m", strtotime("-2 month"));
$del_date = getMonthRange($del_time);
$delsql = "DELETE FROM admin_broker_check_opernum_log WHERE dateline <= '".$del_date."'";
*/


//获取单月审核记录数
$sql = "select dateline,SUM(head_num) as head_num,SUM(cert_num) as cert_num, SUM(real_num) as real_num,SUM(account_num) as account_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;

if(is_array($data_info) && !empty($data_info)){
	foreach($data_info as $key=>$val){
		if(is_array($head_info)){
			foreach($head_info as $val1){
				if($val['dateline'] == $val1['indate']){
					$data_info[$key]['head_total'] = $val1['head_total'];
				}
		    }
		}
		if(is_array($cert_info)){
			foreach($cert_info as $val2){
				if($val['dateline'] == $val2['indate']){
					$data_info[$key]['cert_total'] = $val2['cert_total'];
				}
		    }
		}
	}
}

//下载报表
if($actiontype == 'exportcsv'){
	$data = "日期,头像已审核,头像总量,经纪人证书已审核,经纪人证书总量,real房产证已审核,real房产证总量,账号已审核,账号总量
	";

	if(is_array($data_info) && !empty($data_info)){
		foreach($data_info as $key=>$val){
			$data .= $val['dateline'].','.$val['head_num'].','.$val['head_total'].','.$val['cert_num'].','.$val['cert_total'].",--,--,--,--
			";
		}
	}

	$mime_type = 'text/x-csv';
	header('Content-Type: ' . $mime_type);
    header("Content-Disposition: attachment; filename=经纪人审核单日报表".$lastym.".csv");
    $actiontype = '';
    echo $data;
    exit();
}

//重新整理数据
if(is_array($head_num2)){
	foreach($head_num2 as $key=>$val){
		$head_num2[$val['indate']] = $val['num'];
	}
}
if(is_array($certificate_num1)){
	foreach($certificate_num1 as $key=>$val){
		$certificate_num1[$val['indate']] = $val['num'];
	}
}
if(is_array($certificate_num2)){
	foreach($certificate_num2 as $key=>$val){
		$certificate_num2[$val['indate']] = $val['num'];
	}
}


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