<?php 


@session_start();


	if($_SESSION['SECEXP'] !=''){
		
	}else{
		//echo "Go And Fuck Your self";die;
	}
	if(isset($_REQUEST['bntSubmit'])){
		saveResultNew();
		header('Location:'.SITEURL.'/live-betting/?u='.$_REQUEST['u'].'&flag=1&id='.$_REQUEST['id'],true,307);
		//echo "in";
	}
//	header( 'Content-type: text/html; charset=utf-8');
	
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
 <meta http-equiv="refresh" content="20">
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/ie7.css">
<![endif]-->
<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/ie11.css">

<style>
.three_in_one_b{
	display:inline;
}
a.cls-tkt:link{
  color:blue;
  background-color: transparent;
  text-decoration:underline;
}
a.cls-tkt:visited {
  color: pink;
  background-color: transparent;
  text-decoration: none;
}
</style>

</head>

<body style="background:#fff;">
    <div class="container">
        <div class="row inline">
            <div class="menus">
     			 <ul style="">
          			<?php echo innerAdminMenu()?>
      			</ul>
    		</div>
            

        </div>
        <div class="row" align="center">
            <p>
                Date: &nbsp; <?php echo date('Y-m-d')?> &emsp; Draw Time:&nbsp; <?php echo drawTime()?> &nbsp;::&nbsp; Point :: 
                <b style="color:red; font-size:27px;">
                <?php
                   $sqlB1 = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.$_REQUEST['u'].'" ORDER BY balanceID DESC LIMIT 0,1';
                  $B1 = $DB->dbRow($sqlB1);
                    echo ($B1['balance']==''?'0000.00':$B1['balance']);
                ?>
                 &emsp;&emsp;&emsp;&emsp;&emsp; This  Round 0 </b>
                </p> 
        </div>
		<?php
                    	//$drawTime = drawTime();
                        $drawTime = '11:45';//5:45
						$start = microtime(true);
						/*$sqlP = 'SELECT distinct L.lotnum,L.cellID,L.uID,L.sernum,L.lotdate,L.lottime, R.lotID as lotID,R.lotnum as LT,count(L.cellID) as tc, L.isadvance,L.status, (sum(L.cv)*L.point) as sumcv,GROUP_CONCAT(distinct U.userCode,"") as userCode FROM mx_lottary as L 
									Inner JOIN '.$DB->pre.'users as U on U.id = L.uID 
									left join mx_result_tran  R on L.lotID = R.lID 
									WHERE  L.lotTime="'.$drawTime.'"  AND U.userType="3" AND U.adminID="'.$_REQUEST['id'].'" AND L.lotDate="2021-11-16" group by cellID ORDER BY cellID ';//*/
        /*   $sqlP = '
						
						select lotnum,
				cellID,
				sernum,
				lotDate,
				lotTime,
				GROUP_CONCAT(lotID) as lotID,
				count(cellID) as tc,
				isadvance,
				status,
				sum(sumcv) as sumcv,
				GROUP_CONCAT(userCode) as userCode,
				GROUP_CONCAT(LT) as LT,
				lotteryID
From
(

SELECT distinct L.lotnum,L.cellID,L.uID,L.sernum,L.lotdate,L.lottime,L.lotID as lotteryID,R.lotID as lotID,R.lotnum as LT, L.isadvance,L.status, (sum(L.cv)*L.point) as sumcv ,concat(U.userCode,"[", (sum(L.cv)*L.point),"]") as userCode

FROM mx_lottary as L Inner JOIN mx_users as U on U.id = L.uID left join mx_result_tran R on L.lotID = R.lID 

WHERE  L.lotTime="'.$drawTime.'"  AND U.userType="3" AND U.adminID="'.$_REQUEST['id'].'"  AND L.lotDate="2021-11-16" group by L.uID, L.cellID ORDER BY R.lotID DESC,L.cellID 
) P
group by P.cellID order by LT desc, cellID
						
						
						
						';		*/
						$sqlP = '
								select	lotnum, 
		cellID, 
		sernum, 
		lotDate, 
		lotTime, 
		GROUP_CONCAT(lotID) as lotID, 
		(
		Select 	count(distinct ml.tranID) 
		FROM 	mx_lottary as ml 
				Inner JOIN mx_users as U1 on U1.id = ml.uID 
				WHERE 	ml.lotTime="'.$drawTime.'" 
						AND U1.userType="3" 
						AND U1.adminID="'.$_REQUEST['id'].'" 
						AND ml.lotDate="2021-11-16"
						and ml.cellID = P.cellID
		) as tc, 
		isadvance, 
		status, sum(sumcv) as sumcv, 
		GROUP_CONCAT(userCode) as userCode, 
		GROUP_CONCAT(LT) as LT, lotteryID 
From 
( 
	SELECT 	distinct L.lotnum,
			L.cellID,L.uID,
			L.sernum,
			L.lotdate,
			L.lottime,
			L.lotID as lotteryID,
			R.lotID as lotID,
			R.lotnum as LT, 
			L.isadvance,
			L.status, 
			(sum(L.cv)*L.point) as sumcv ,
			concat(U.userCode,"[", (sum(L.cv)*L.point),"]") as userCode ,
			L.tranID
	FROM 	mx_lottary as L Inner JOIN mx_users as U on U.id = L.uID 
			left join mx_result_tran R on L.lotID = R.lID 
	WHERE 	L.lotTime="'.$drawTime.'" 
			AND U.userType="3" 
			AND U.adminID="'.$_REQUEST['id'].'" 
			AND L.lotDate="2021-11-16" 
			group by L.uID, L.cellID ORDER BY R.lotID DESC,L.cellID 
) P group by P.cellID order by LT desc, cellID
						
						';	
						$P = $DB->dbRows($sqlP);
						
						?>

            <div class="bz" style="width: 100%; background-color: rgb(255, 255, 255);border:0px solid red;">
                
                    <div class="first-row" id="first_menu" style="border:1px solid orange;width:65%; float:left"> 
                        <table border="1" width="100%"   cellspacing="0" cellpadding="17" style="background-color: #6F0">
                            <tr class="bett_menu0">
                                <th>User </th>
                                <th>W-Play </th>
                                <th>User </th>
                                <th>W-Play </th>
                                <th>User </th>
                                <th>W-Play </th>
                                <th>User </th>
                                <th>W-Play </th>
                                <th>User </th>
                                <th>W-Play </th>
                            </tr>
                      </table>

                      <table border="1" width="100%"  cellspacing="0" cellpadding="10" >
                          
					<?php
				
						$trLeft = '';
						$flag = 0;
						$actCV = 0;
						$msg = '';
						$resultLot = array();
						$sqlLT = 'SELECT lotnum FROM '.$DB->pre.'result_tran WHERE uID="'.$_REQUEST['id'].'" AND lotTime="'.$drawTime.'" AND lotdate="2021-11-16" AND lID IS NOT NULL';
						$LT = $DB->dbRows($sqlLT);
						$end = microtime(true);
						echo ( $end-$start);
						foreach($LT as $k=>$LT){
							$resultLot[$k] = $LT['lotnum'];
						}
						
						  
						foreach($P as $k=>$P){
							if($flag < 1){
								$trLeft .= '<tr>';
							}
							
							$css = '';
							
							if(in_array($P['lotnum'],$resultLot)){
								$css = ' style="background-color:purple;color:#fff;"';
							}
							if($P['lotID'] != ''){
								$css = ' style="background-color:yellow"';
							}
							if($P['isadvance'] == '1'){
								$msg = 'ADV';

							}
							if($P['status'] == '0'){
								$msg = 'Cancel';

							}
							if($flag < 4){
								 $cell = '';
								 $trCell = '';
								$trCell = $P['cellID'];
							  
							  	$cellCV = 0;
								$actCV = 0;
								
							  $trLeft .= '<td width="10%" height="15%"  align="center"'.$css.'>'.$msg.'&nbsp;'.($P['userCode']).'</td>';
							  $trLeft .= '<td width="10%" height="15%"  align="center" '.$css.' id="t'.($k+1).'">'.($P['sumcv']*90).'*'.$P['tc'].'</br>
							  
							  <form id="" name="" action="" method="GET">
							  	<input id="id" name="id" value="'.$_REQUEST['id'].'" type="hidden">
								<input id="u" name="u" value="'.$_REQUEST['u'].'" type="hidden">
								<input id="flag" name="flag" type="hidden" value="1">
								<input id="rLotID" name="rLotID" type="hidden" value="'.$P['lotteryID'].'">
								<input id="attr" type="hidden" name="attr" value="'.$P['cellID'].'~'.$P['sernum'].'~'.$P['lotnum'].'~'.$P['lotDate'].'~'.$P['lotTime'].'~'.$_REQUEST['id'].'">
							  	<button id="bntSubmit" name="bntSubmit" value="1" type="submit">'.$trCell.'</button>
							  </form>
							  
							  
							  </td>';
							  $flag++;
							 
							 
							 }else{

								 $cell = '';
								 $trCell = '';
								$trCell = $P['cellID'];
							  
							  	$cellCV = 0;
								$actCV = 0;
								
								
								
								
							  $trLeft .= '<td width="10%" height="15%" align="center"'.$css.'>'.$msg.'&nbsp;'.$P['userCode'].'</td>';
							  $trLeft .= '<td width="10%" height="15%"  align="center" '.$css.' id="t'.($k+1).'">'.($P['sumcv']*90).'*'.$P['tc'].'</br>
							  
							  <form id="" name="" action="" method="GET">
							  	<input id="id" name="id" value="'.$_REQUEST['id'].'" type="hidden">
								<input id="u" name="u" value="'.$_REQUEST['u'].'" type="hidden">
								<input id="flag" name="flag" type="hidden" value="1">
								<input id="rLotID" name="rLotID" type="hidden" value="'.$P['lotteryID'].'">
								<input id="attr" type="hidden" name="attr" value="'.$P['cellID'].'~'.$P['sernum'].'~'.$P['lotnum'].'~'.$P['lotDate'].'~'.$P['lotTime'].'~'.$_REQUEST['id'].'">
							  	<button id="bntSubmit" name="bntSubmit" value="1" type="submit">'.$trCell.'</button>
							  </form>
							  
							  
							  </td>';
								 
								 
								 
								 $trLeft .= '</tr>';
								 $flag = 0;
							}
						}//$p foreach loop end
						echo $trLeft;
					 // $Z foreach loop end
					?>                        
					
                   	</table>     
                    </div>
                
				<?php
              
              $sqlUS = '
              		 SELECT distinct U.userCode

FROM mx_lottary as L LEFT JOIN mx_users as U on L.uID = U.id 
left join mx_result_tran R on L.lotID = R.lID
WHERE L.lotTime="'.$drawTime.'" AND U.userType="3" AND L.lotDate="2021-11-16" AND U.adminID="'.$_REQUEST['id'].'"
and R.lID is not null
              ';
              $US = $DB->dbRows($sqlUS);
              
              $winUser = array();
              foreach($US as $k => $US){
                $winUser[$k] = $US['userCode'] ;
              }
             // print_r($winUser);
             
			   	 $sqlP2 = '
					select sum(S) as S, uID, balance, userCode,commission,bonus FROM ( SELECT distinct L.tranID,L.ticketTot as S,L.uID, B.balance,U.userCode,U.winrate,U.bonus,U.commission FROM mx_lottary as L LEFT JOIN mx_users as U on L.uID = U.id left join mx_balance B on U.userCode = B.userCode and B.balanceID = (select max(t1.balanceID) as balanceID from mx_balance t1 where t1.userCode = U.userCode group by t1.userCode) 
					WHERE L.lotTime="'.$drawTime.'" AND U.userType="3" AND L.lotDate="2021-11-16" AND U.adminID="'.$_REQUEST['id'].'" )P group by P.uID 
				';	
              
              
				$P2 = $DB->dbRows($sqlP2);
				
				 $sum = 0;
				 $tr = '';
				 $td = '';
			     $sum1 = 0;
				
				if(count($P2) > 0){
				
				
				foreach($P2 as $P2){	
					$css = '';
							
					if(in_array($P2['userCode'],$winUser)){
						$css = ' style="padding:3%;background-color:yellow;"';
					}				
					
                  	$sum +=$P2['S'];
					$tr .= '<tr><td>'.$P2['userCode'].'-'.$P2['balance'].'</td><td '.$css.'>'.$P2['S'].'</td><td>'.showNetToPay($P2['uID'],$P2['commission'],$P2['bonus']).'</td><td colspan="4">'.showTickets($drawTime,$P2['uID']).'</td></tr>';
					
				 
				 
				}
				
				 
				$tr .= '</tr>';
				}
				
				
				?>
                <div class="bz_02" style="width:34%; float: right;margin-left:5px;">
                   <!-- <div id="table02" style="width:280px; float:right;"> -->   <!--  <div id="table02" style="width:360px; float:right;">  -->
                        <table border="1" cellspacing="1">
                            <tr>
                                <th>Total </th>
                                <th>
                                    <?php
                                   

                                    echo $sum;
                                    ?>


                                </th>
                                
                                <th><?php echo drawTime();?> </th>
                            </tr>
                            <tr>
                                <th>User Id </th>
                                <th>T.Amt </th>
                                <th>Net Pay</th>
                                <th>T.Amt </th>
                                <th>T.Amt </th>
                                <th>T.Amt </th>
                                <th>T.Amt </th>
                            </tr>
                            <?php echo $tr;?>
                        </table>
                  <!--  </div> -->
                </div>
            </div>
        </div>

  
</body>
</html>
<?php

	function showTickets($lotTime,$uID){
		global $DB;
		$sql = 'SELECT sum(distinct(L.ticketTot)) as S,tranID,status FROM mx_lottary as L WHERE L.lotTime="'.$lotTime.'" AND L.lotDate="2021-11-16" AND uID="'.$uID.'" GROUP BY L.tranID ORDER BY lotID DESC';
		$D = $DB->dbRows($sql);
		$flag = 0;
		$html = '';
		//print_r($D);
		foreach($D as $D){
			if($flag < 4){
				if($D['status'] == '0'){
					$html .= '<b style="background:red"><a href="'.SITEURL.'/ticket/?tran='.$D['tranID'].'&userID='.$uID.'&adminID='.$_REQUEST['id'].'&u='.$_REQUEST['u'].'&flag=1&id='.$_REQUEST['id'].'">'.$D['S'].'</a></b>&nbsp;&nbsp;&nbsp;';
				}else{
					$html .= '<a class="cls-tkt" href="'.SITEURL.'/ticket/?tran='.$D['tranID'].'&userID='.$uID.'&adminID='.$_REQUEST['id'].'&u='.$_REQUEST['u'].'&flag=1&id='.$_REQUEST['id'].'">'.$D['S'].'</a>&nbsp;&nbsp;&nbsp;';
				}
				$flag++;	 
			}else{
				if($D['status'] == '0'){
					$html .= '<br><b style="background:red;"><a href="'.SITEURL.'/ticket/?tran='.$D['tranID'].'&userID='.$uID.'&adminID='.$_REQUEST['id'].'&u='.$_REQUEST['u'].'&flag=1&id='.$_REQUEST['id'].'">'.$D['S'].'</a></b>&nbsp;&nbsp;&nbsp;';
				}else{
					$html .= '<br><a class="cls-tkt" href="'.SITEURL.'/ticket/?tran='.$D['tranID'].'&userID='.$uID.'&adminID='.$_REQUEST['id'].'&u='.$_REQUEST['u'].'&flag=1&id='.$_REQUEST['id'].'">'.$D['S'].'</a>&nbsp;&nbsp;&nbsp;';
				}
				$flag = 0;
			}

			
		}
		return $html;
	}
	




function showNetToPay($retailerID,$rComm,$rBonus){
	global $DB;
	$sale = getSumByDateAD($retailerID);
//	$cancel = getCancelByDateAD($retailerID);
	$winning = getWinByDateAD($retailerID);
	
	$comm = ( (($sale)/100)*$rComm);
	$netAmtPay = ($sale-($winning+$comm));
	$bonus = (($netAmtPay/100)*$rBonus);
	$tempNetPay += ($netAmtPay-$bonus);
	return $tempNetPay;
}

function getSumByDateAD($retailerID){
	global $DB;
	$sql = 'SELECT ticketTot as S FROM '.$DB->pre.'lottary WHERE lotdate="2021-11-16" AND uID="'.$retailerID.'" AND status="1" group by tranID';
	$D = $DB->dbRows($sql);
	$tot = 0;
	foreach($D as $D){
		$tot += $D['S'];
	}

	return $tot;
}
function getCancelByDateAD($retailerID){
	global $DB;
	$sql = 'SELECT ticketTot as S FROM '.$DB->pre.'lottary WHERE lotdate="2021-11-16" AND uID="'.$retailerID.'" AND status="0" group by tranID';
	$D = $DB->dbRows($sql);
	$tot = 0;
	foreach($D as $D){
		$tot += $D['S'];
	}
	
	return $tot;
	
}
function getWinByDateAD($retailerID){
	global $DB;
	$sql = 'SELECT claimAmount as S FROM '.$DB->pre.'result_win WHERE date(dateAdded)="2021-11-16" AND uID="'.$retailerID.'"  group by barcode';
	$D = $DB->dbRows($sql);
	$tot = 0;
	foreach($D as $D){
		$tot += $D['S'];
	}
	
	return $tot;
}



	
//flush();               // - Make sure all buffers are flushed
//ob_flush();

?>

