<?php 

@session_start();
set_time_limit(0);

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

	}else{
		echo "Go And Fuck Your Self";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['todate']?></p>	
        
        <table border="1px" cellpadding="1px" cellspacing="1" width="98%" style="font-size:20px;">
        <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['id'].'"  AND userType="2" AND status="1" AND isActive="1"';
			$D = $DB->dbRows($sql);
			
			
			$dateClause = '(lotdate between "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'")';
			
			
			
			$totT = 0;
			$win70T = 0;
			$win80T = 0;
			$win90T = 0;
			$CR10T = 0;
			$CR20T = 0;
			$commT = 0;
			$netAmtT = 0;
			$bonusT = 0;
			$netPayT = 0;
			
			foreach($D as $k=>$D){
				$tot = 0;
				$win = '';
				$comm = '';
				$win90 = 0;
				$win80 = 0;
				$win70 = 0;
				$bonus = '';
				$retailer = '';
				$agent = '';
				
				getAllRetailerAgent($D['id']);
				$agent = substr($GLOBALS['retailer'], 0, -1);
				$R = explode(',',$agent);
				?>
                <tr>
        	<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['userCode']?></a></td>
            <td>
            	<?php
                	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 += getSale($L);
							
							if($D['winrate'] == '90'){
								 $win90 += winClaimeAdmin($R,'90');
								
							}else if($D['winrate'] == '80'){
								$win80 += winClaimeAdmin($R,'80');
								
							}else if($D['winrate'] == '70'){
								$win70 += winClaimeAdmin($R,'70');
								
							}
							
							
						}
						//echo $win90;die;
					echo $tot.'/-';
					 $GLOBALS['totT'] = ($GLOBALS['totT']+$tot); 
				?>
            
            </td>
            <td><?php	echo $win90; $GLOBALS['win90T'] += $win90; ?></td>
            <td><?php	echo $win80;  $GLOBALS['win80T'] += $win80; ?></td>
            <td><?php echo $win70; $GLOBALS['win70T'] += $win70; ?></td>
            <td>0 Cr</td>
            <td>0 Cr</td>
            <td><?php echo $comm = ((($tot)/100)*$D['commission']); $GLOBALS['commT'] += $comm;?></td>
            <td><?php echo ($tot-($comm+$win90+$win80+$win70));
					$netPay = ($tot-($comm+$win90+$win80+$win70));
					$GLOBALS['netAmtT'] += $netPay;
				?></td>
            <td><?php echo $bonus = (($netPay/100)*$D['bonus']); $GLOBALS['bonusT'] +=$bonus;?></td>
            <td><?php 
			//$sum = ($tot-(($comm+$bouns)));
			echo ($netPay-$bonus);
				$GLOBALS['netPayT'] += ($netPay-$bonus);
			?></td>
        </tr>
                <?php
				
			}
			
		?>
        <tr style=" background-color:#FC911B">
        	<td>Total</td>
            <td><?php echo $GLOBALS['totT'];?></td>
            <td><?php echo $GLOBALS['win90T']?></td>
            <td><?php echo $GLOBALS['win80T']?></td>
            <td><?php echo $GLOBALS['win70T']?></td>
            <td>0</td>
            <td>0</td>
            <td><?php echo $GLOBALS['commT'];?></td>
            <td><?php echo $GLOBALS['netAmtT'];?></td>
            <td><?php echo $GLOBALS['bonusT'];?></td>
            <td><?php echo $GLOBALS['netPayT'];?></td>
        </tr>
        <?php
		
		
		
		function getAllRetailerAgent($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'].',';
						}
						getAllRetailerAgent($D['id']);
					}
				}
			}
		}
		
		
			
		
		function getSale($L){
			$tot = 0;
			foreach($L as $L){
				$tot +=$L['T'];
			}
			return $tot;
		}
		
		function winClaimeAdmin($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);
	
			}
		?>
        </table>
			

	</div>

</body>
</html>
