<?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 adminID="'.$_REQUEST['id'].'"  AND userType="2" AND status="1" AND isActive="1"';
			$D = $DB->dbRows($sql);;
			
			
			
			
			//print_r($R);die;
			$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 = '';
				
				
				?>
                <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
                	
							
							$sqlL = 'select sum(T) AS SS FROM (SELECT sum(distinct(ticketTot)) as T FROM mx_lottary WHERE  status="1" AND (lotdate between "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'") AND uID in(SELECT id FROM mx_users WHERE underHead="'.$D['id'].'") GROUP BY tranID) as S';
							
							
							$L = $DB->dbRow($sqlL);
							echo $L['SS'].'/-';
							$tot = $L['SS'];
							
							if($D['winrate'] == '90'){
								 $win90 += winClaimeAdmin($D['id'],'90');
								
							}else if($D['winrate'] == '80'){
								$win80 += winClaimeAdmin($D['id'],'80');
								
							}else if($D['winrate'] == '70'){
								$win70 += winClaimeAdmin($D['id'],'70');
								
							}
							
							
						
						//echo $win90;die;
					 $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 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;
			$sql = 'SELECT sum(point*cv) as S FROM mx_result_win WHERE   (date(dateAdded) between "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'") AND uID in(SELECT id FROM mx_users WHERE underHead="'.$userID.'")';
			$S = $DB->dbRow($sql);
			//print_r($S);
			
			$str = ($S['S']*$win);
			return ($str);
	
			}
		?>
        </table>
			

	</div>

</body>
</html>
