<?php 

@session_start();


	if($_REQUEST['id'] !=''){

	}else{
		die;
	}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=11" >
<!--[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/ie7.css">
</head>

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



		<p><?php echo $_REQUEST['frmdate']?> To <?php echo $_REQUEST['frmdate']?></p>	
        
        <table border="1px" cellpadding="1px" cellspacing="1" width="98%" style="font-size:20px;" id="tblData">
        <tr>
        	<td>Agent Name</td>
            <td>Sales</td>
            <td>Win 90</td>
            <td>Win 80</td>
            <td>Win 70</td>
            <td>10 Cr</td>
            <td>20 Cr</td>
            <td>Comm</td>
            <td>Net Amt</td>
            <td>Bonus</td>
            <td>Net To Pay</td>
        </tr>
        <?php
        
			$sql = 'SELECT * FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['agentID'].'" AND status="1" AND isActive="1"';
			$D = $DB->dbRows($sql);
			//$_REQUEST['frmdate'] = '2021-06-10';
			//$_REQUEST['todate'] = '2021-06-11';
			$dateClause = '(lotdate between "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'")';
			foreach($D as $D){
				$tot = 0;
				$win90 = 0;
				$win80 = 0;
				$win70 = 0;
				$retailer = '';
				?>
                <tr>
        	<!-- td><a href="<?php echo SITEURL?>/ticket-list/?retailerID=<?php echo $D['id']?>&id=<?php echo $_REQUEST['id']?>&u=<?php echo $_REQUEST['u']?>"><?php echo $D['name']?></a></td -->
            <td><a href="<?php echo SITEURL?>/retailer-list/?agentID=<?php echo $D['id']?>&id=<?php echo $_REQUEST['id']?>&u=<?php echo $_REQUEST['u']?>"><?php echo $D['name']?></a></td>
            <td>
            	 <a href="<?php echo SITEURL?>/ticket-list/?retailerID=<?php echo $D['id']?>&id=<?php echo $_REQUEST['id']?>&u=<?php echo $_REQUEST['u']?>">
				<?php
					if($D['userType']==3){
                	 	$sqlS = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE  uID="'.$D['id'].'" AND status="1" AND '.$dateClause.' GROUP BY tranID';
						$S = $DB->dbRows($sqlS);
						$tot += getSale1($S);
					}else{
							
							getAllRetailerAgent71($D['id']);
							$agent = substr($GLOBALS['retailer'], 0, -1);
							$R = explode(',',$agent);
							
							foreach($R as $R){ 

								$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$R.'" AND status="1" AND '.$dateClause.' GROUP BY tranID';
								$L = $DB->dbRows($sqlL);
								//print_r($L);
								$tot += getSale1($L);
								if($D['winrate'] == '90'){
									 $win90 += winClaimeAdmin1($R,'90');
								}else if($D['winrate'] == '80'){
									$win80 += winClaimeAdmin1($R,'80');
								}else if($D['winrate'] == '70'){
									$win70 += winClaimeAdmin1($R,'70');
								}
								
								
							}
					
					}
					echo $tot;
				?>
            </a>
            </td>
            <td>
           
			<?php
            	if($D['winrate'] == '90'){
					$win90 += winClaimeAdmin1($D['id'],'90');
				}
				echo $win90;
			?></td>
            <td><?php
            	if($D['winrate'] == '80'){
					$win80 += winClaimeAdmin1($D['id'],'80');
				}
				echo $win80;
			?></td>
            <td><?php
            	if($D['winrate'] == '70'){
					$win70 += winClaimeAdmin1($D['id'],'70');
				}
				echo $win70;
			?></td>
            <td>0 Cr</td>
            <td>0 Cr</td>
            <td><?php echo $comm = ((($tot)/100)*$D['commission']);?></td>
            <td><?php 
					$netPay = ($tot-($comm+$win90+$win80+$win70));
					echo ($tot-($comm+$win90+$win80+$win70))?></td>
            <td><?php echo $bonus = (($netPay/100)*$D['bonus']);?></td>
            <td><?php 
				//$sum = ($tot-(($comm-$bouns)));echo ($sum-$win90);
				echo ($netPay-$bonus);
				
				?></td>
        </tr>
                <?php
			}
			
			function getSale1($L){
			$tot = 0;
			foreach($L as $L){
				$tot +=$L['T'];
			}
			return $tot;
			}
			function winClaimeAdmin1($userID,$win){
				global $DB;		
				$dateClause = '(date(dateAdded) between "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'")';			 
				$sql = 'SELECT * FROM '.$DB->pre.'result_win WHERE uID="'.$userID.'" AND '.$dateClause;
				$S = $DB->dbRows($sql);
				//print_r($S);
				$point = 0;
				$cv = 0;
				foreach($S as $S){
				//	echo $S['point'];
					$point += ($S['point']*$win)*$S['cv'];
					//$cv += $S['cv'];
					
				}
				$str = $point;
				return ($str);
	
			}
			function getAllRetailerAgent71($ID){
			global $DB;
			$sql = 'SELECT id,userType FROM '.$DB->pre.'users WHERE underHead="'.$ID.'" ';
			$D = $DB->dbRows($sql);
			if(count($D) > 0){
				foreach($D as $D){
					if($D['id'] !=''){
						if($D['userType'] == 3){
							$GLOBALS['retailer'] .= $D['id'].',';
						}
						getAllRetailerAgent71($D['id']);
					}
				}
			}
		}
		
		?>
        </table>
			

	</div>

</body>
</html>

<script>
  document.getElementById('tblData').focus();
document.onkeydown = getBackBtn;
function getBackBtn(){

	var key = event.which || event.keyCode || event.charCode; 
	//alert(key+"-----"+'<?php echo SITEURL?>/live-betting/?u=<?php echo $_REQUEST['u']?>&flag=1&id=<?php echo $_REQUEST['id']?>');
	
	if(key == 8){
      window.history.go(-1);
	}
}
</script>
