<?php
$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
//$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");
checkAuth('37');	//判断登录和权限


$title = "月度审核报表";
$search_flag = 0;

$ym = date("Y-m");
//获取日期
$last_date = getMonthRange($ym);
$month_date_start = $last_date[0];
$month_date_end = $last_date[1];
$search_ym = date('Y-m',strtotime($month_date_start));

/*设置前台select信息
$lastym = date("Y-m");
$search_ym_arr = array($lastym=>$lastym);
//获取前台select信息
$ym_display = getSelect($search_ym_arr,'search_ym',$search_ym);
*/
//获得数据当前页面数据
$ordersql = $actiontype == 'exportcsv' ? "" : " order by id desc ";
$sql = "SELECT dateline, admin,  SUM(sort0_num) AS sort0_num, SUM(sort1_num) AS sort1_num, SUM(sort2_num) AS sort2_num, SUM(remark_num) AS remark_num FROM admin_house_check_opernum_log WHERE (dateline >= '$month_date_start' AND dateline <= '$month_date_end') GROUP BY dateline ".$ordersql." limit 31";
$data_arr = $dbback->GetAll($sql);

//获取图片入库量和描述入库量
$month_date_start = str_replace("-", "", $month_date_start);
$month_date_end = str_replace("-", "", $month_date_end);
$sql = "SELECT surface_num,in_num FROM admin_pic_total_stat WHERE (dateline >= '$month_date_start' AND dateline <= '$month_date_end') ".$ordersql." limit 31";
$pic_total = $dbback->GetAll($sql);
$sql = "SELECT in_num FROM admin_remark_total_stat WHERE (dateline >= '$month_date_start' AND dateline <= '$month_date_end') ".$ordersql." limit 31";
$remark_total = $dbback->GetAll($sql);

$data_info = array();
if(count($data_arr))
{
	foreach($data_arr as $key=>$data)
	{
		$value = array();
		
		$value['dateline'] = $data['dateline'];
		$value['sort0_num'] = $data['sort0_num'];
		$value['sort1_num'] = $data['sort1_num'];
		$value['sort2_num'] = $data['sort2_num'];
		$value['remark_num'] = number_format($data['remark_num'] / $remark_total[$key]['in_num'] * 100, 2, '.', '')."%";
		$value['total_num'] = number_format(($data['sort0_num']+$data['sort1_num']+$data['sort2_num']+$data['remark_num'])/($remark_total[$key]['in_num']+$pic_total[$key]['in_num']+$pic_total[$key]['surface_num']) * 100, 2, '.', '')."%";
		$data_info[] = $value;

		unset($value);
	}
}


//下载报表
if($actiontype == 'exportcsv'){
	$data = $search_month."日期,户型图,外景图,室内图,描述,总量
	";

	if(is_array($data_info) && !empty($data_info)){
		foreach($data_info as $key=>$val){
			$data .= $val['dateline'].','.$val['sort0_num'].','.$val['sort1_num'].','.$val['sort2_num'].','.$val['remark_num'].','.$val['total_num'].'
			';
		}
	}

	$mime_type = 'text/x-csv';
	header('Content-Type: ' . $mime_type);
    header("Content-Disposition: attachment; filename=房源审核月度报表".$search_ym.".csv");
    $actiontype = '';
    echo $data;
    exit();
}

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