<?php
	@session_start();
	if($_SESSION['id'] != ''){
		
	}else{
		die;
	}
	set_time_limit(0);
?>
<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" >

<link rel="stylesheet" type="text/css" href="<?php echo SITEURL?>/css/style.css">

</head>

<body style="background-color: rgb(204, 238, 150);">
	<div class="container">
		<div class="row inline">
			<div class="navstyle">
					

						
<!--
						<a href="login.html" id="login-btn">login</a>
-->				</li>

				
            </div>
        </div>
        <div class="row">
        <form id="frmDate" name="frmDate" method="POST" action="<?php echo SITEURL?>/lottery-agent">
			<table>
            <tr>
            	<td>From Date</td>
                <td><input id="fromDate" name="fromDate" type="date" required></td>
                <td>To</td>
                <td><input id="toDate" name="toDate" type="date" required></td>
                <td><input id="btnSubmit" name="btnSubmi" type="submit" value="Submit"></td>
            </tr>
            </table>
            </form>
        </div>
        <div class="row inline" style="float: left; width: 72%; background-color: white; min-height: 500px;">
        <?php
			$sql = 'SELECT userCode FROM '.$DB->pre.'users WHERE id="'.intval($_SESSION['id']).'"';
			$D = $DB->dbrow($sql);
        	
			$sqlB = 'SELECT balance FROM '.$DB->pre.'balance WHERE userCode="'.strtoupper($D['userCode']).'" ORDER BY balanceID DESC limit 0,1';
			$B = $DB->dbRow($sqlB);
			
		?>
            <h4 style="text-align: center;">Agent Balance : <?php echo $B['balance']?> </h4>
			<div class="menus">
                <ul>
                    <?php echo innerAdminMenu();?>
                </ul>
		    </div>

		
        
		<div class="row">
			<div id="table02" style="width: 939*px;">
            	<table>
                	<tr>
                        <th>Agent Name &emsp; </th>
                        <th>Sales </th>
                        <th>Win 90 </th>
                        <th>Win 80 </th>
                        <th>Win 70 </th>
                        <th>10 Cr </th>
                        <th>20 Cr </th>
                        <th>Comm </th>
                        <th>Net Amount</th>
                        <th>Bonus </th>
                        <th>Net Pay</th>
                    </tr>
                    <?php
					$i = 0;
					
				 	$sql = 'select * from mx_users where userType="3" and underHead in(select id from mx_users where underHead="'.$_SESSION['id'].'" AND userType="2") OR underHead="'.$_SESSION['id'].'"';
					$D = $DB->dbRows($sql);
				//	echo "<pre>";
				//	print_r($D);die;
					
					$sqlAW  =  'SELECT winrate,commission,bonus FROM '.$DB->pre.'users WHERE id="'.$_SESSION['id'].'"';
					$AW = $DB->dbRow($sqlAW);
					
					$agentSale = 0;
					$agentWin = 0;
					
					$tempComm = 0;
					$tempBonus = 0;
					$tempNetAmt = 0;
					$tempNetPay = 0;
					
					
					//Variabl For Show Below Total
					$saleT = 0;
					$win90T = 0;
					$win80T = 0;
					$win70T = 0;
					$cr10T = 0;
					$cr20T = 0;
					$commissionT = 0;
					$netAmtT = 0;
					$bonusT = 0;
					$netPayT = 0 ;
					
					
					
					$retailer = array();
					$dateClause = 'lotDate="'.date('Y-m-d').'"';
					$_REQUEST['fromDate'] = ($_REQUEST['fromDate']==''?date('Y-m-d'):$_REQUEST['fromDate']);
					$_REQUEST['toDate'] = ($_REQUEST['toDate']==''?date('Y-m-d'):$_REQUEST['toDate']);
					if($_REQUEST['fromDate'] !='' && $_REQUEST['toDate'] != ''){
						$dateClause = ' (lotDate BETWEEN "'.$_REQUEST['fromDate'].'" AND "'.$_REQUEST['toDate'].'")';
					}
					foreach($D as $k=>$D){
						
						
						$win90 = 0;
						$win80 = 0;
						$win70 = 0;
						$cr20 = 0;
						$cr10 = 0;					
						//if($D['userType'] == 3){
							if($D['winrate'] == 90){
								//echo $AW['winrate'].'--';
								 $win90 = winClaimeAdminNew($D['id'],$D['winrate']);
								 $GLOBALS['agentWin'] += $win90;
							}else if($D['winrate'] == 80){
								$win80 = winClaimeAdminNew($D['id'],$D['winrate']);
								$GLOBALS['agentWin'] += $win80;
							}else if($D['winrate'] == 70){
								$win70 = winClaimeAdminNew($D['id'],$D['winrate']);
								$GLOBALS['agentWin'] += $win70;
							}
						//}
						
						
						?>
                        <tr>

                        <td><a href="<?php echo SITEURL?>/lottery-agent/create-agent/?userID=<?php echo $D['id']?>&u=<?php echo $_REQUEST['u']?>&flag=<?php echo $_REQUEST['flag']?>&id=<?php echo $_REQUEST['id']?>"><?php echo $D['userCode']?></a></td>
                        <td>
                        <?php
							$tot = 0;
							$commRet = '';
								
								  $sqlL = 'select sum(T) AS SS FROM (SELECT sum(distinct(ticketTot)) as T FROM mx_lottary WHERE  status="1" AND (lotdate between "'.$_REQUEST['fromDate'].'" AND "'.$_REQUEST['toDate'].'") AND uID ="'.$D['id'].'" GROUP BY tranID) as S';	
								
								$L = $DB->dbRow($sqlL);
								//$commRet = $L['commission'];	
								  $tot = $L['SS'];
								
								$GLOBALS['agentSale'] += $tot;
								
							
							//echo $GLOBALS['agentSale'];
							$GLOBALS['saleT'] += $tot;
							echo $tot.'/-';
						?>
                        
                        
                        </td>                    
                        <td><?php $GLOBALS['win90T'] += $win90; echo $win90?></td>                    
                        <td><?php $GLOBALS['win80T'] += $win80;echo $win80?></td>                    
                        <td><?php $GLOBALS['win70T'] += $win70;echo $win70?></td>                    
                        <td>
                        <?php
                        	if($D['userType'] == 3 && $D['winrate'] == 80){
								//$cr10 =  (winClaimeAdminNew($D['id'],90)- winClaimeAdminNew($D['id'],$D['winrate']));
							}
							$GLOBALS['cr10T'] += $cr10;
							echo $cr10;
						?>
                        </td>                    
                        <td>
                        <?php
                        	if($D['userType'] == 3 && $D['winrate'] == 70){
								//$cr20 =  (winClaimeAdminNew($D['id'],90)- winClaimeAdminNew($D['id'],$D['winrate']));
							}
							$GLOBALS['cr20T'] += $cr20;
							echo $cr20
						?>
                         </td>                   
                        <td><?php

							$commission =  (($tot/100)*$D['commission']);
							$GLOBALS['tempComm'] += (($tot/100)*$AW['commission']);
							$GLOBALS['commissionT'] += $commission;
							echo $commission;
						?></td>
                        <td><?php 
								$GLOBALS['netAmtT'] += ($tot-($commission+$win90+$win80+$win70));
								echo ($tot-($commission+$win90+$win80+$win70));
								$tempNetAmt1 = ($tot-($commission+$win90+$win80+$win70));
								$GLOBALS['tempNetAmt'] +=($tot-((($tot/100)*$AW['commission'])+$win90+$win80+$win70));
								$tempNetAmt2 = ($tot-((($tot/100)*$AW['commission'])+$win90+$win80+$win70));
								
						 ?></td>
                        <td><?php
                        	$bonus =  (($tempNetAmt1/100)*$D['bonus']);
							$GLOBALS['tempBonus'] += (($tempNetAmt2/100)*$AW['bonus']);
							$agBonus = (($tempNetAmt2/100)*$AW['bonus']);
							$GLOBALS['bonusT'] += $bonus;
							echo $bonus;
						?></td>
                        <td><?php 
								$GLOBALS['netPayT'] += ($tempNetAmt1-($bonus)); 
								echo ($tempNetAmt1-($bonus));
								$GLOBALS['tempNetPay'] += ($tempNetAmt2-($agBonus));
							
						?></td>
                        
                      </tr>
                        <?php
						//echo getAgentUser1($D['id'],$AW['winrate']);

					}
                    	
					?>
                    <tr style="background-color: #F7B63E">
                    	<td>Total</td>
                        <td><?php echo $GLOBALS['saleT'];?></td>
                        <td><?php echo $GLOBALS['win90T'];?></td>
                        <td><?php echo $GLOBALS['win80T'];?></td>
                        <td><?php echo $GLOBALS['win70T'];?></td>
                        <td><?php echo $GLOBALS['cr10T'];?></td>
                        <td><?php echo $GLOBALS['cr20T'];?></td>
                        <td><?php echo $GLOBALS['commissionT'];?></td>
                        <td><?php echo $GLOBALS['netAmtT'];?></td>
                        <td><?php echo $GLOBALS['bonusT'];?></td>
                        <td><?php echo $GLOBALS['netPayT'];?></td>
                        
                        
                    </tr>
                    
                </table>
			</div>
				
			
		</div>
        </div>

        <div class="right_display" style="float: right; width: 27%; background-color: rgb(255, 255, 255);  min-height: 500px;">
        <div class="right_display" style="margin-left: 20px; width: 80%;">
            <table class="right_display_table">
                <tr>
                    <th>Agent Code :</th>
                    <th><?php echo strtoupper($_SESSION['u'])?></th>
                </tr>
                <tr>
                    <th>Date Form :</th>
                    <th><?php echo date('d-m-Y')?> </th>
                </tr>
                <tr>
                    <th>Date to :</th>
                    <th><?php echo date('d-m-Y')?> </th>
                </tr>
            </table>

            <hr>
			<?php
            		 $sqlN = 'SELECT * FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['id'].'" ';
					$N = $DB->dbRow($sqlN);
					echo $N['bonus'];
					
			
			?>
            <table width="90%" style="margin-left: 10px; margin-bottom: 10px;" >
                <tr>
                    <td style="text-align: left;">Sale </td>
                    <td style="text-align: right;"><?php echo $GLOBALS['agentSale'];?></td>
                </tr>
                <tr>
                    <td style="text-align: left;">Commission</td>
                    <td style="text-align: right;"><?php 
					echo $GLOBALS['tempComm'];
					?></td>
                </tr>
                <tr>
                    <td style="text-align: left;">Wining </td>
                    <td style="text-align: right;"><?php echo $GLOBALS['agentWin'];?> </td>
                </tr>
            </table>

            <hr>

            <table width="90%" style="margin-left: 10px; margin-bottom: 10px;" >
                <tr>
                    <td style="text-align: left;">Net Amount  </td>
                    <td style="text-align: right;"><?php 
					//$netAmt =  ($GLOBALS['agentSale']-($GLOBALS['tempComm']+$GLOBALS['agentWin']));
					echo  $GLOBALS['tempNetAmt'];
					?></td>
                </tr>
                <tr>
                    <td style="text-align: left;">Bonus </td>
                    <td style="text-align: right;"><?php  
						$agentBonus = (($netAmt/100)*$N['bonus']);
						echo $GLOBALS['tempBonus'];
						?> </td>
                </tr>
            </table>

            <hr>
            <table width="90%" style="margin-left: 10px; margin-bottom: 10px;" >
                <tr>
                    <td style="text-align: left;">Net to Pay </td>
                    <td style="text-align: right;"><?php echo $GLOBALS['tempNetPay'];?> </td>
                </tr>
            </table>

            <hr>
        </div>
        </div>
	</div>
<?php
$str = '';


function winClaimeAdminNew($userID,$win){
			global $DB;				
			$dateClause = '(date(dateAdded) between "'.$_REQUEST['fromDate'].'" AND "'.$_REQUEST['toDate'].'")';	 
			
			
			
			$sql = 'SELECT sum(point*cv) as S FROM mx_result_win WHERE   (date(dateAdded) between "'.$_REQUEST['fromDate'].'" AND "'.$_REQUEST['toDate'].'") AND uID ="'.$userID.'"';
			$S = $DB->dbRow($sql);
			//print_r($S);
			
			$str = ($S['S']*$win);
			return ($str);
	
}



?>
</body>
</html>
