<?php

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

function get_game_wol($start_date,$end_date,$role_id,$agent_id=0,$c_id=''){
	$sql = " SELECT g.games_id,g.games_name,g.commission,IFNULL(SUM(b.bet_amount), 0) AS bet_amount, IFNULL(SUM(b.win_or_loss), 0) AS win_or_loss,IFNULL(SUM(b.valid_amount), 0) AS valid_amount,IFNULL(SUM(b.comm_amount), 0) AS comm_amount FROM " .
	        $GLOBALS['ecs']->table('bet_log') . " as b,".$GLOBALS['ecs']->table('games')." as g ,".$GLOBALS['ecs']->table('users')." as u WHERE b.game_id = g.games_id and b.user_id=u.user_id ";
	
	$sql.= " and (b.bet_time >= '$start_date' and b.bet_time < '$end_date') "; 

	if($role_id=="1"){
			   $sql.=" and u.parent_id in ".get_agent_tree($agent_id);
	}elseif($role_id=="2"){
	        if ($agent_id>0){
				$sql.=" and u.parent_id in ".get_agent_tree($agent_id);
			}else{
	    	    $sql.=" and  (u.flag='a') ";
			}
	}elseif($role_id=="5"){
	        if ($agent_id>0){
				$sql.=" and u.parent_id in ".get_agent_tree($agent_id);
			}else{
    	       $sql.=" and  (u.flag='b') ";
			}
	}elseif($role_id=="3"){
	        if ($agent_id>0){
				$sql.=" and u.parent_id in ".get_agent_tree($agent_id);
			}else{
    	       $sql.=" and  u.parent_id in ".$c_id;
			}
	}elseif($role_id=="4"){
			$sql.=" and u.parent_id in ".get_agent_tree($agent_id);
	}elseif($role_id=="6"){
			$sql.=" and u.parent_id in ".get_agent_tree($agent_id);
	}else{
			$sql.=" and u.parent_id in ".get_agent_tree($agent_id);
	}
	$sql.= "GROUP BY b.game_id";

    $rs = $GLOBALS['db']->query($sql);
		
	$i=0;
	$sum['bet_amount']=0;
	$sum['valid_amount']=0;
	$sum['win_or_loss']=0;
	$sum['real_amount']=0;
	$sum['comm_amount']=0;
	$sum['agent_comm_amount']=0;
	
	while($row=$GLOBALS['db']->fetchRow($rs)){
		$rp_all[$i]['games_id']  =$row['games_id'];
		$rp_all[$i]['games_name']  =$row['games_name'];
		$rp_all[$i]['win_or_loss'] =$row['win_or_loss'];
		$rp_all[$i]['bet_amount']  =$row['bet_amount'];
		$rp_all[$i]['valid_amount'] =$row['valid_amount'];
		$rp_all[$i]['comm_amount'] =$row['comm_amount'];
		$rp_all[$i]['commission']  =$row['commission'];
		$rp_all[$i]['commission']  =$row['commission'];
		$rp_all[$i]['real_amount'] =($row['win_or_loss']*$row['commission']/100)+$row['comm_amount'];	//實際金額

		$rp_all[$i]['up_amount'] =($row['win_or_loss']*(100-$row['commission'])/100)-$row['comm_amount'];	//上繳金額
		$rp_all[$i]['agent_comm_amount'] = ($rp_all[$i]['real_amount']*($agent_comm)/100);	//代理金額	

		$sum['bet_amount']+=$row['bet_amount'];
		$sum['valid_amount']+=$row['valid_amount'];
		$sum['win_or_loss']+=$row['win_or_loss'];
		$sum['comm_amount']+=$row['comm_amount'];

		$sum['real_amount']+=($row['win_or_loss']*$row['commission']/100)+$row['comm_amount'];
		$sum['up_amount']+=($row['win_or_loss']*(100-$row['commission'])/100)-$row['comm_amount'];
		$sum['agent_comm_amount']+=$rp_all[$i]['agent_comm_amount'];

		$rp_all[$i]['real_amount'] =$rp_all[$i]['real_amount'];	//實際金額
		$rp_all[$i]['up_amount'] =$rp_all[$i]['up_amount'];	//上繳金額
		$rp_all[$i]['agent_comm_amount'] = $rp_all[$i]['agent_comm_amount'];	//代理金額	



		$i++;
	}

	return $rp_all;
}

function get_user_wol($start_date,$end_date,$c_id=''){

		$sql =	"SELECT r.user_id, u.user_name ,
			sum(case game_id when 63  then win_or_loss else 0 end) as a_all_wol,
			sum(case game_id when 63  then comm_amount else 0 end) as a1_wol,
			sum(case game_id when 67  then comm_amount else 0 end) as a2_wol,
			sum(case game_id when 56  then win_or_loss else 0 end) as b_wol,
			sum(case game_id when 70  then win_or_loss else 0 end) as b_wol,
			sum(case game_id when 69  then win_or_loss else 0 end) as c_wol,
			sum(case game_id when 34  then win_or_loss else 0 end) as d1_wol,
			sum(case game_id when 65  then win_or_loss else 0 end) as d2_wol,
			sum(case game_id when 68  then win_or_loss else 0 end) as e_wol,
			sum(case game_id when 68  then comm_amount else 0 end) as e_com,
			sum(case game_id when 58  then win_or_loss else 0 end) as f_wol,
			sum(case game_id when 58  then comm_amount else 0 end) as f_com,
			sum(case game_id when 66  then win_or_loss else 0 end) as g_wol,
			sum(case game_id when 64  then comm_amount else 0 end) as h_wol, 
			sum(case game_id when 35  then win_or_loss else 0 end) as i_wol,
			sum(case game_id when 57  then win_or_loss else 0 end) as j_wol,
			sum(case game_id when 59  then win_or_loss else 0 end) as k_wol,
			sum(win_or_loss) as sum_wol
			FROM  ".$GLOBALS['ecs']->table('bet_log')." AS r, ".$GLOBALS['ecs']->table('users')." AS u
			WHERE r.user_id = u.user_id and (bet_time>='".$start_date."' and bet_time<'".$end_date."') 
			GROUP BY user_id Having user_id in ".$c_id;
//echo $sql;
	    $result = $GLOBALS['db']->query($sql);
		  while($rs=$GLOBALS['db']->fetchRow($result))
	    {
					$user_wol[$rs['user_id']]['a_all_wol']=isset($rs['a_all_wol']) ? intval($rs['a_all_wol']) : 0;
					$user_wol[$rs['user_id']]['a1_wol']=isset($rs['a1_wol']) ? intval($rs['a1_wol']) : 0;
					$user_wol[$rs['user_id']]['a2_wol']=isset($rs['a2_wol']) ? intval($rs['a2_wol']) : 0;
					$user_wol[$rs['user_id']]['a_wol']=$user_wol[$rs['user_id']]['a1_wol']+$user_wol[$rs['user_id']]['a2_wol'];
					$user_wol[$rs['user_id']]['b_wol']=isset($rs['b_wol']) ? intval($rs['b_wol'])*0.9 : 0;
					$user_wol[$rs['user_id']]['c_wol']=isset($rs['c_wol']) ? intval($rs['c_wol'])*0.9 : 0;
					$user_wol[$rs['user_id']]['d1_wol']=isset($rs['d1_wol']) ? intval($rs['d1_wol'])*0.9 : 0;
					$user_wol[$rs['user_id']]['d2_wol']=isset($rs['d2_wol']) ? intval($rs['d2_wol'])*0.9 : 0;
					$user_wol[$rs['user_id']]['d_wol']=$user_wol[$rs['user_id']]['d1_wol']+$user_wol[$rs['user_id']]['d2_wol'];
					$user_wol[$rs['user_id']]['e_wol']=isset($rs['e_wol']) ? intval($rs['e_wol'])*0.92 : 0;
					$user_wol[$rs['user_id']]['j_wol']=isset($rs['j_wol']) ? intval($rs['j_wol'])*0.88 : 0;
					$user_wol[$rs['user_id']]['e_wol']+=$user_wol[$rs['user_id']]['j_wol'];
					$user_wol[$rs['user_id']]['e_wol']+=$user_wol[$rs['user_id']]['e_com'];
					$user_wol[$rs['user_id']]['f_wol']=isset($rs['f_wol']) ? intval($rs['f_wol'])*0.92 : 0;
					$user_wol[$rs['user_id']]['f_wol']+=$user_wol[$rs['user_id']]['f_com'];
					$user_wol[$rs['user_id']]['g_wol']=isset($rs['g_wol']) ? intval($rs['g_wol'])*0.91 : 0;
					$user_wol[$rs['user_id']]['h_wol']=isset($rs['h_wol']) ? intval($rs['h_wol']) : 0;
					$user_wol[$rs['user_id']]['i_wol']=isset($rs['i_wol']) ? intval($rs['i_wol'])*0.88 : 0;
					$user_wol[$rs['user_id']]['k_wol']=isset($rs['k_wol']) ? intval($rs['k_wol'])*0.88 : 0;
					$user_wol[$rs['user_id']]['g_wol']+=$user_wol[$rs['user_id']]['k_wol'];
					$user_wol[$rs['user_id']]['sum_wol']=$user_wol[$rs['user_id']]['a_wol']+$user_wol[$rs['user_id']]['b_wol']+$user_wol[$rs['user_id']]['c_wol']+$user_wol[$rs['user_id']]['d_wol']+$user_wol[$rs['user_id']]['e_wol']+$user_wol[$rs['user_id']]['f_wol']+$user_wol[$rs['user_id']]['g_wol']+$user_wol[$rs['user_id']]['i_wol'];
			}
			return $user_wol;
}

?>