<?php 

@session_start();


	if($_SESSION['SECEXP'] !=''){

	}else{
		header('Location:'.SITEURL);
	}
?>

<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>
		    
		    
		    <form id="frmRetailer" name="frmRetailer" method="GET" action="<?php echo SITEURL?>/retailer-list/">
			<input id="frmdate" name="frmdate" type="date" required> To <input id="todate" name="todate" type="date" required>
			<input id="agentID" name="agentID" type="hidden" value="<?php echo $_REQUEST['agentID']?>" >
			<select id="userID" name="userID">
				<option value="">-Select User-</option>
				<?php
					$sqlSelU = 'SELECT name,id FROM '.$DB->pre.'users WHERE status="1" AND isActive="1" AND  userType=3';
					$SU = $DB->dbRows($sqlSelU);
					foreach($SU as $SU){
						?>
						<option value="<?php echo $SU['id']?>"><?php echo $SU['name']?></option>
						<?php
					}
				?>
			</select>
			<input id="btnSubmit" name="btnSubmit" type="submit" value="Submit">
			</form>
		</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>Distributor</td>
            <td>Net To Pay</td>
        </tr>
        <?php
        
		$userClause = ($_REQUEST['userID']==''?'':' AND id="'.$_REQUEST['userID'].'"');
			$sql = 'SELECT * FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['agentID'].'" AND status="1" AND isActive="1"'.$userClause;
			$D = $DB->dbRows($sql);
			$_REQUEST['frmdate'] = ($_REQUEST['frmdate']==''?date('Y-m-d'):$_REQUEST['frmdate']);
			$_REQUEST['todate'] = ($_REQUEST['todate']==''?date('Y-m-d'):$_REQUEST['todate']);
			$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(C.T) as T FROM (SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE  uID="'.$D['id'].'" AND status="1" AND '.$dateClause.' GROUP BY tranID) as C';
						$S = $DB->dbRow($sqlS);
						$tot += $S['T'];
					}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 = (($S['T']/100)*1);?></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;	
				$_REQUEST['frmdate'] = ($_REQUEST['frmdate']==''?date('Y-m-d'):$_REQUEST['frmdate']);
			$_REQUEST['todate'] = ($_REQUEST['todate']==''?date('Y-m-d'):$_REQUEST['todate']);
			
				$dateClause = '(date(dateAdded) between "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'")';			 
			$sql = 'SELECT sum(S) as S from (SELECT claimAmount as  S FROM mx_result_win WHERE uID="'.$userID.'" AND '.$dateClause.' group by barcode) as C';
				$S = $DB->dbRow($sql);
				//print_r($S);
				$point = 0;
				$cv = 0;
				if($S['S'] > 0){
				//	echo $S['point'];
					$point += ($S['S']);
					//$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>
