<?php
	
	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" >

<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 inline" style="float: left; width: 72%; background-color: white; min-height: 500px;">
        <?php
			$sql = 'SELECT userCode FROM '.$DB->pre.'users WHERE id="'.intval($_REQUEST['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" style="margin-bottom:-10px; width: 70%; margin-left: 5px;">
        	<h4 style="margin-bottom:-17px; margin-top:5px;"><?php echo date('d-m-Y')?> &emsp; <?php echo date('h:i:s')?> &emsp; Start</h4>
        	<h4>Date: <?php echo (date('d-m-Y',strtotime($_REQUEST['frmdate']))==''?date('d-m-Y'):date('d-m-Y',strtotime($_REQUEST['frmdate'])));?> &emsp; to &emsp; <?php echo (date('d-m-Y',strtotime($_REQUEST['todate']))==''?date('d-m-Y'):date('d-m-Y',strtotime($_REQUEST['todate'])));?> </h4>
        </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 '.$DB->pre.'users WHERE underHead="'.$_REQUEST['id'].'"    ';
					$D = $DB->dbRows($sql);
				//	echo "<pre>";
					//print_r($D);die;
					
					$sqlAW  =  'SELECT winrate,commission,bonus FROM '.$DB->pre.'users WHERE id="'.$_REQUEST['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').'"';
					if($_REQUEST['frmdate'] !='' && $_REQUEST['todate'] != ''){
						$dateClause = ' (lotDate BETWEEN "'.$_REQUEST['frmdate'].'" 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 = winClaimeAdminCommon($D['id'],$D['winrate']);
								 $GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AW['winrate']);
							}else if($D['winrate'] == 80){
								$win80 = winClaimeAdminCommon($D['id'],$D['winrate']);
								$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AW['winrate']);
							}else if($D['winrate'] == 70){
								$win70 = winClaimeAdminCommon($D['id'],$D['winrate']);
								$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AW['winrate']);
							}
						}else{
							$GLOBALS['retailer'] = array();
							$allRet = getAgentRetailerCommon($D['id']);

							$totS = 0;
							$sqlU = 'SELECT * FROM '.$DB->pre.'users WHERE id="'.$D['id'].'"';
							$U = $DB->dbRow($sqlU);
							foreach($allRet as $RS){
								$totS += winClaimeAdminCommon($RS,$U['winrate']);
								$GLOBALS['agentWin'] += winClaimeAdminCommon($RS,$AW['winrate']);
							}
							if($D['winrate'] == 90){
								$win = $totS;
							}else if($D['winrate'] == 80){
								$win = $totS;
							}else if($D['winrate'] == 80){
								$win = $totS;
							}

						}
						
						
						?>
                        <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 = '';
							$sqlRW  =  'SELECT winrate,commission,bonus FROM '.$DB->pre.'users WHERE id="'.$D['id'].'"';
							$RW = $DB->dbRow($sqlRW);
							if($D['userType'] == 3){
								$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$D['id'].'" AND status="1" AND '.$dateClause.' GROUP BY tranID';
								$L = $DB->dbRows($sqlL);
								$commRet = $L['commission'];	
								 $tot = getSaleCommon($L);
								//$GLOBALS['agentSale'] += winClaimeAdminCommon($D['id'],$AW['winrate']);
								$GLOBALS['agentSale'] += $tot;
								
							}else{
								$GLOBALS['retailer'] = array();
								$allRet = getAgentRetailerCommon($D['id']);
								foreach($allRet as $RS){
									$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$RS.'" AND status="1" AND '.$dateClause.' GROUP BY tranID';
									$L = $DB->dbRows($sqlL);
									 $tot = getSaleCommon($L);
									//$GLOBALS['agentSale'] += winClaimeAdminCommon($D['id'],$AW['winrate']);
									$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 =  (winClaimeAdminCommon($D['id'],90)- winClaimeAdminCommon($D['id'],$D['winrate']));
							}
							$GLOBALS['cr10T'] += $cr10;
							echo $cr10;
						?>
                        </td>                    
                        <td>
                        <?php
                        	if($D['userType'] == 3 && $D['winrate'] == 70){
								$cr20 =  (winClaimeAdminCommon($D['id'],90)- winClaimeAdminCommon($D['id'],$D['winrate']));
							}
							$GLOBALS['cr20T'] += $cr20;
							echo $cr20
						?>
                         </td>                   
                        <td><?php

							$commission =  (($tot/100)*$RW['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)*$RW['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($_REQUEST['u'])?></th>
                </tr>
                <tr>
                    <th>Date Form :</th>
                    <th><?php echo $_REQUEST['frmdate']?> </th>
                </tr>
                <tr>
                    <th>Date to :</th>
                    <th><?php echo $_REQUEST['todate']?> </th>
                </tr>
            </table>

            <hr>
			<?php
            		 $sqlN = 'SELECT * FROM '.$DB->pre.'users WHERE underHead="'.$_REQUEST['id'].'" ';
					$N = $DB->dbRow($sqlN);
					
			
			?>
            <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 getAgentUser1($ID,$AGW){
	global $DB;
	 $sql = 'SELECT * FROM '.$DB->pre.'users WHERE underHead="'.$ID.'" ';
	
	$D = $DB->dbRows($sql);
	$tot = 0;
	
	$dateClause = 'lotdate="'.date('Y-m-d').'"';
		if($_REQUEST['frmdate'] !='' && $_REQUEST['todate'] != ''){
			$dateClause = ' (date(dateAdded) BETWEEN "'.$_REQUEST['frmdate'].'" AND "'.$_REQUEST['todate'].'")';
		}
	
	foreach($D as $D){
	
		if($D['id'] !=''){
			
			
			if($D['userType'] == 3){
				$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$D['id'].'" AND '.$dateClause.' GROUP BY tranID';
				$L = $DB->dbRow($sqlL);
				
				$tot += getSaleCommon($L);
				$GLOBALS['agentSale'] += $tot;
				
			}else{
				$GLOBALS['retailer'] = array();
				$allRet = getAgentRetailerCommon($D['id']);
				foreach($allRet as $RS){
					$sqlL = 'SELECT sum(distinct(ticketTot)) as T FROM '.$DB->pre.'lottary WHERE uID="'.$RS.'" AND '.$dateClause.' GROUP BY tranID';
					$L = $DB->dbRow($sqlL);
					$tot += getSaleCommon($RS);
					$GLOBALS['agentSale'] += $tot;
				}
				

			}
			
			
			
			
			
			$win = 0;  
			if($D['userType'] == 3){
				if($D['winrate'] == 90){
					$win = winClaimeAdminCommon($D['id'],$D['winrate']);
					$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AGW);
				}else if($D['winrate'] == 80){
					$win = winClaimeAdminCommon($D['id'],$D['winrate']);
					$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AGW);
				}else if($D['winrate'] == 80){
					$win = winClaimeAdminCommon($D['id'],$D['winrate']);
					$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AGW);
				}
			}else{
				$GLOBALS['retailer'] = array();
				$allRet = getAgentRetailerCommon($D['id']);

				$totS = 0;
				$sqlU = 'SELECT * FROM '.$DB->pre.'users WHERE id="'.$D['id'].'"';
				$U = $DB->dbRow($sqlU);
				foreach($allRet as $RS){
					$totS += winClaimeAdminCommon($RS,$U['winrate']);;
				}
				if($D['winrate'] == 90){
					$win = $totS;
					$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AGW);
				}else if($D['winrate'] == 80){
					$win = $totS;
					$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AGW);
				}else if($D['winrate'] == 70){
					$win = $totS;
					$GLOBALS['agentWin'] += winClaimeAdminCommon($D['id'],$AGW);
				}

			}
			
			
			$cr20 = 0;
			$cr10 = 0;
			if($D['userType'] == 3 && $D['winrate'] == 70){
					$cr20 = (winClaimeAdminCommon($D['id'],90)- winClaimeAdminCommon($D['id'],$D['winrate']));
			}
			if($D['userType'] == 3 && $D['winrate'] == 80){
					$cr10 = (winClaimeAdminCommon($D['id'],90)- winClaimeAdminCommon($D['id'],$D['winrate']));
			}
			
			
		$str .= '
			<tr>

				<td><a href="'.SITEURL.'/lottery-agent/create-agent/?userID='.$D['id'].'&u='.$_REQUEST['u'].'&flag='.$_REQUEST['flag'].'&id='.$_REQUEST['id'].'">'.$D['userCode'].'</a></td>
				<td>'.$tot.' </td>                    
				<td>'.$win.'</td>                    
				<td>'.$win.'</td>                    
				<td>'.$win.'</td>                    
				<td>'.$cr10.'</td>                    
				<td>'.$cr20.'</td>                   
				<td>'.(($tot/100)*$D['commission']).'</td>                    
				<td>'.($tot - (($tot/100)*$D['commission'])).'</td>                    
				<td>'.((($tot-($commission+$win))/100)*$D['bonus']).'</td>
				<td>'.($tot - ((($tot/100)*$D['commission'])+(($tot/100)*$D['bonus']))).'</td>                   
			  </tr>
		
		';
			
		}
		getAgentUser1($D['id']);
	}
	return $str;
}
?>
</body>
</html>
