<?php
/**
 * 系统报表
 * $Id: report_list.php
 * @author zhoz
 * @package manager.public_html
 */
// 底层包含
require_once("manager_include.inc");

// 登录检查
require_once("check_login.inc");

// 权限检查
if (!checkAuthority("SYSTEM")) {
	// エラー表示
	$layout_pages = array();
	$layout_pages["footer"] = "footer.inc";
	$layout_pages["top"] = "menu.inc";
	$layout_pages["middle"] = "error.inc";
	$message = "权限不足，请联系系统管理员。";
	require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
	exit;
}

// 参数取得
$date_min = ParamUtil::getRequestString("date_min", date("Y-m-01"));
$date_max = ParamUtil::getRequestString("date_max", date("Y-m-d"));
if (!DateUtil::checkValidDate($date_min)) {
	$date_min = date("Y-m-01");
}
if (!DateUtil::checkValidDate($date_max)) {
	$date_max = date("Y-m-d");
}
$param = array();
$param["date_min"] = $date_min;
$param["date_max"] = $date_max;

// 一览取得:
$data_list = getReportList($param);

/**
 * 充值总金额，充值总代币数，消耗总代币数，兑换总代币数，平台剩余代币数
 * 充值金额，充值代币数，转盘消耗代币数，兑换代币数，代币数增减（充值-消耗-兑换）
 */
function getReportList($param = array()) {
	$diff_date_array = array();
	$result_array = array();
	$total_array = array(
			"pay_count" => 0,
			"point_count" => 0,
			"cost_point_count" => 0,
			"ex_point_count" => 0,
			"remain_point" => 0
		);
	$date_min = $param["date_min"];
	$date_max = $param["date_max"];
	$d = floor((strtotime($date_max)-strtotime($date_min))/(24*60*60));
	for ($i=0; $i<=$d; $i++) {
		$diff_date_array[] = date("Y-m-d", strtotime("$date_max-$i day"));
	}
	
	$db = &FishowDBManager::getInstance();
	// 充值金额，充值代币数user_course_dat
	$sql1 = "select date_trunc('day', target_date) as target_date,sum(amount) as price, sum(point) as point from user_course_dat where delete_flg=false and status='SUCCESS'" .
			" and date_trunc('day', target_date)>='" . $date_min . "'" .
			" and date_trunc('day', target_date)<='" . $date_max .  "' group by date_trunc('day', target_date)";
	$result1 = $db->executeQuery($sql1);
	
	// 兑换代币数user_order_dat !NEW
	$sql2 = "select date_trunc('day', target_date) as target_date,sum(point) as ex_point from user_order_dat where delete_flg=false and status != 'NEW'" .
			" and date_trunc('day', target_date)>='" . $date_min . "'" .
			" and date_trunc('day', target_date)<='" . $date_max .  "' group by date_trunc('day', target_date)";
	$result2 = $db->executeQuery($sql2);
	
	// 其它消耗：这里是负数要小心
	$sql4 = "select date_trunc('day', target_date) as target_date,sum(point) as cost_point2 from user_point_log_dat where delete_flg=false and point<0" .
			" and date_trunc('day', target_date)>='" . $date_min . "'" .
			" and date_trunc('day', target_date)<='" . $date_max .  "' group by date_trunc('day', target_date)";
	$result4 = $db->executeQuery($sql4);
	
	foreach ($diff_date_array as $diff_date) {
		$result_array[$diff_date] = array(
			"date" => $diff_date,
			"price" => 0,
			"point" => 0,
			"cost_point" => 0,
			"ex_point" => 0
		);
		
		foreach ($result1 as $tmp) {
			if (date("Y-m-d", strtotime($tmp["target_date"])) == $diff_date) {
				$result_array[$diff_date]["price"] += $tmp["price"];
				$result_array[$diff_date]["point"] += $tmp["point"];
			}
		}
		foreach ($result2 as $tmp) {
			if (date("Y-m-d", strtotime($tmp["target_date"])) == $diff_date) {
				$result_array[$diff_date]["ex_point"] += $tmp["ex_point"];
			}
		}
		foreach ($result4 as $tmp) {
			if (date("Y-m-d", strtotime($tmp["target_date"])) == $diff_date) {
				$result_array[$diff_date]["cost_point"] -= $tmp["cost_point2"];
			}
		}
	}
	// 充值总金额，充值总代币数，消耗总代币数，兑换总代币数，平台剩余代币数
	$sql_total1 = "select sum(amount) as pay_count, sum(point) as point_count from user_course_dat where delete_flg=false and status='SUCCESS'";
	$result_total1 = $db->executeQuery($sql_total1);
	if (!empty($result_total1[0]["pay_count"])) {
		$total_array["pay_count"] = $result_total1[0]["pay_count"];
	}
	if (!empty($result_total1[0]["point_count"])) {
		$total_array["point_count"] = $result_total1[0]["point_count"];
	}
	
	// 其它总消耗：这里是负数要小心
	$sql_total3 = "select sum(point) as cost_point_count2 from user_point_log_dat where delete_flg=false and point<0";
	$result_total3 = $db->executeQuery($sql_total3);
	if (!empty($result_total3[0]["cost_point_count2"])) {
		$total_array["cost_point_count"] -= $result_total3[0]["cost_point_count2"];
	}
	// 平台剩余代币数
	$sql_total4 = "select sum(point) as remain_point from user_point_dat where delete_flg=false";
	$result_total4 = $db->executeQuery($sql_total4);
	if (!empty($result_total4[0]["remain_point"])) {
		$total_array["remain_point"] = $result_total4[0]["remain_point"];
	}
	
	// 兑换代币数user_order_dat !NEW
	$sql_total5 = "select sum(point) as ex_point_count from user_order_dat where delete_flg=false and status != 'NEW'";
	$result_total5 = $db->executeQuery($sql_total5);
	if (!empty($result_total5[0]["ex_point_count"])) {
		$total_array["ex_point_count"] = $result_total5[0]["ex_point_count"];
	}
	
	$result_array["total_count"] = $total_array;
	return $result_array;
}


// ページ
$layout_pages = array();
$layout_pages["top"] = "menu.inc";
$layout_pages["menu_clicked"] = "li_system";
$layout_pages["middle"] = "report_list.inc";
require_once(MANAGER_TEMPLATE_DIR_PATH . "/layout/topmenutopsub_layout.inc");
exit;